Compare commits

...

8 Commits

Author SHA1 Message Date
Wojciech Kubicki
7c30177bf1 feat(tractor): add basic watering functionality 2024-03-25 02:16:14 +01:00
Wojciech Kubicki
09b65e9fd1 feat(tractor): print how much water current tile needs 2024-03-25 02:01:09 +01:00
Wojciech Kubicki
ff7e5c4b99 feat(tractor): add method to get current tile 2024-03-25 02:00:28 +01:00
Wojciech Kubicki
7de410c59a refactor(tile_init): set type based on random vegetable 2024-03-25 01:52:34 +01:00
Wojciech Kubicki
8ce604df46 feat(tile_type): randomly assign each tile a vegetable at init 2024-03-25 01:27:20 +01:00
Wojciech Kubicki
eaaac9f277 feat(tractor): print out on what tile type the tractor is currently on 2024-03-25 00:38:40 +01:00
Wojciech Kubicki
af87ff2475 fix(tractor): prevent tractor from moving off board 2024-03-25 00:11:09 +01:00
Wojciech Kubicki
8dceac3ebe chore(readme): add pythlog to required dependencies 2024-03-25 00:01:46 +01:00
3 changed files with 56 additions and 10 deletions

View File

@ -7,6 +7,7 @@ Wymagane biblioteki do pobrania:
```
pip install pygame
pip install python-dotenv
pip install pytholog
```
Stwórz plik `.env` w głównym folderze projektu o poniższej treści:

View File

@ -1,20 +1,29 @@
import os
import pygame
from dotenv import find_dotenv, load_dotenv
from kb import tractor_kb
import pytholog as pl
import random
class Tile(pygame.sprite.Sprite):
def __init__(self, id, type, field):
def __init__(self, id, field, type):
super().__init__()
self.id = id
x = id%16
y = id//16
self.type = type
self.field = field
self.set_type(type)
self.rect = self.image.get_rect()
vegetables = tractor_kb.query(pl.Expr("warzywo(Nazwa_warzywa)"))
random_vegetable = vegetables[random.randint(0, len(vegetables)-1)]['Nazwa_warzywa']
self.set_type(random_vegetable)
self.faza = 'posadzono'
# you can set TILE_SIZE in the .env file to adjust the window size
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
self.rect = self.image.get_rect()
self.rect.topleft = (x * TILE_SIZE, y * TILE_SIZE)
def draw(self, surface):
@ -22,8 +31,10 @@ class Tile(pygame.sprite.Sprite):
def set_type(self, type):
self.type = type
if self.type == 'grass':
self.image = pygame.image.load("images/grass.png").convert()
# if self.type == 'grass':
# self.image = pygame.image.load("images/grass.png").convert()
self.image = pygame.image.load("images/grass.png").convert()
# you can set TILE_SIZE in the .env file to adjust the window size
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))

View File

@ -1,6 +1,8 @@
import pygame
import os
from dotenv import find_dotenv, load_dotenv
from kb import ile_podlac
from tile import Tile
class Tractor(pygame.sprite.Sprite):
def __init__(self, field):
@ -15,6 +17,8 @@ class Tractor(pygame.sprite.Sprite):
x, y = 0, 0
self.rect.topleft = (x, y)
self.water = 200
def draw(self, surface):
surface.blit(self.image, self.rect)
@ -23,14 +27,26 @@ class Tractor(pygame.sprite.Sprite):
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
if direction == "up":
if direction == "up" and self.rect.y > 0:
self.rect.y -= TILE_SIZE
elif direction == "down":
self.print_tile_type()
elif direction == "down" and self.rect.y < 15 * TILE_SIZE:
self.rect.y += TILE_SIZE
elif direction == "left":
self.print_tile_type()
elif direction == "left" and self.rect.x > 0:
self.rect.x -= TILE_SIZE
elif direction == "right":
self.print_tile_type()
elif direction == "right" and self.rect.x < 15 * TILE_SIZE:
self.rect.x += TILE_SIZE
self.print_tile_type()
curent_tile = self.get_current_tile()
water_needed = ile_podlac(curent_tile.type, curent_tile.faza)[0]['Woda']
if self.water >= water_needed:
print(f"watered {curent_tile.type} with {water_needed} liters\n")
self.water -= water_needed
else:
print(f"{water_needed - self.water} more litres of water needed to water {curent_tile.type}\n")
def update(self):
keys = pygame.key.get_pressed()
@ -42,3 +58,21 @@ class Tractor(pygame.sprite.Sprite):
self.move('up')
if keys[pygame.K_DOWN]:
self.move('down')
def print_tile_type(self):
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
x = self.rect.x // TILE_SIZE
y = self.rect.y // TILE_SIZE
tile_type = self.field.tiles.sprites()[y * 16 + x].type
print(f"The tractor is on a {tile_type} tile.")
def get_current_tile(self):
load_dotenv(find_dotenv())
TILE_SIZE = int(os.getenv("TILE_SIZE"))
x = self.rect.x // TILE_SIZE
y = self.rect.y // TILE_SIZE
current_tile = self.field.tiles.sprites()[y * 16 + x]
return current_tile