71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
import pygame
|
|
import os
|
|
from kb import ile_podlac
|
|
from tile import Tile
|
|
from config import TILE_SIZE
|
|
|
|
class Tractor(pygame.sprite.Sprite):
|
|
def __init__(self, field):
|
|
super().__init__
|
|
self.field = field
|
|
|
|
self.image = pygame.image.load('images/tractor.png').convert_alpha()
|
|
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
|
|
self.rect = self.image.get_rect()
|
|
|
|
x, y = 0, 0
|
|
self.rect.topleft = (x, y)
|
|
|
|
self.water = 200
|
|
|
|
def draw(self, surface):
|
|
surface.blit(self.image, self.rect)
|
|
|
|
|
|
def move(self, direction):
|
|
if direction == "up" and self.rect.y > 0:
|
|
self.rect.y -= TILE_SIZE
|
|
self.print_tile_type()
|
|
elif direction == "down" and self.rect.y < 15 * TILE_SIZE:
|
|
self.rect.y += TILE_SIZE
|
|
self.print_tile_type()
|
|
elif direction == "left" and self.rect.x > 0:
|
|
self.rect.x -= TILE_SIZE
|
|
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()
|
|
if keys[pygame.K_LEFT]:
|
|
self.move('left')
|
|
if keys[pygame.K_RIGHT]:
|
|
self.move('right')
|
|
if keys[pygame.K_UP]:
|
|
self.move('up')
|
|
if keys[pygame.K_DOWN]:
|
|
self.move('down')
|
|
|
|
|
|
def print_tile_type(self):
|
|
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):
|
|
x = self.rect.x // TILE_SIZE
|
|
y = self.rect.y // TILE_SIZE
|
|
current_tile = self.field.tiles.sprites()[y * 16 + x]
|
|
return current_tile
|