diff --git a/app/__init__.py b/app/__init__.py index 989694d..a20dcb8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -34,16 +34,16 @@ class App: keys = pygame.key.get_pressed() if keys[pygame.K_UP]: - self.__tractor.move_up() - print(self.__tractor) + self.__tractor.direction_up() if keys[pygame.K_DOWN]: - self.__tractor.move_down() - print(self.__tractor) + self.__tractor.direction_down() if keys[pygame.K_LEFT]: - self.__tractor.move_left() - print(self.__tractor) + self.__tractor.direction_left() if keys[pygame.K_RIGHT]: - self.__tractor.move_right() + self.__tractor.direction_right() + + if keys[pygame.K_m]: + self.__tractor.move() print(self.__tractor) if keys[pygame.K_h]: diff --git a/app/base_field.py b/app/base_field.py index 21e9a3c..306874b 100644 --- a/app/base_field.py +++ b/app/base_field.py @@ -7,8 +7,12 @@ class BaseField: def __init__(self, img_path: str): self._img_path = img_path - def draw_field(self, screen: pygame.Surface, pos_x: int, pos_y: int, is_centered: bool = False, size: tuple = None): - img = pygame.image.load(self._img_path) + def draw_field(self, screen: pygame.Surface, pos_x: int, pos_y: int, is_centered: bool = False, size: tuple = None, + angle: int = 0): + pre_img = pygame.image.load(self._img_path) + if angle == 90: + pre_img = pygame.transform.flip(pre_img, True, False) + img = pygame.transform.rotate(pre_img, angle) scale = pygame.transform.scale(img, (FIELD_SIZE, FIELD_SIZE)) rect = img.get_rect() if is_centered: diff --git a/app/tractor.py b/app/tractor.py index 40cc022..2d91398 100644 --- a/app/tractor.py +++ b/app/tractor.py @@ -24,27 +24,49 @@ class Tractor(BaseField): self.__move = FIELD_SIZE self.__board = board self.__harvested_corps = [] + self.__direction = 270 + self.__fuel = 10 def draw(self, screen: pygame.Surface): self.draw_field(screen, self.__pos_x + FIELD_SIZE / 2, self.__pos_y + FIELD_SIZE / 2, - is_centered=True, size=(FIELD_SIZE, FIELD_SIZE)) + is_centered=True, size=(FIELD_SIZE, FIELD_SIZE), angle=self.__direction) # Key methods handlers - def move_up(self): - if self.__pos_y - self.__move >= 0: - self.__pos_y -= self.__move - def move_down(self): - if self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT: - self.__pos_y += self.__move + def direction_up(self): + self.__direction = 0 - def move_left(self): - if self.__pos_x - self.__move >= 0: - self.__pos_x -= self.__move + def direction_right(self): + self.__direction = 270 - def move_right(self): - if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH: - self.__pos_x += self.__move + def direction_down(self): + self.__direction = 180 + + def direction_left(self): + self.__direction = 90 + + def move(self): + if self.__fuel > 0: + self.__fuel -= 1 + + if self.__direction == 0: + if self.__pos_y - self.__move >= 0: + self.__pos_y -= self.__move + + elif self.__direction == 270: + if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH: + self.__pos_x += self.__move + + elif self.__direction == 180: + if self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT: + self.__pos_y += self.__move + + elif self.__direction == 90: + if self.__pos_x - self.__move >= 0: + self.__pos_x -= self.__move + + else: + print("Run out of fuel!") def hydrate(self): if self.check_field(Sand): @@ -135,4 +157,4 @@ class Tractor(BaseField): def __str__(self): x, y = self.get_position() - return f"Position: {x}:{y} - {type(self.__board.get_fields()[x][y]).__name__}" + return f"Position: {x}:{y} - {type(self.__board.get_fields()[x][y]).__name__}\nFuel: {self.__fuel}\n" diff --git a/resources/tractor.png b/resources/tractor.png index 81b11e3..627ef1a 100644 Binary files a/resources/tractor.png and b/resources/tractor.png differ