From 1c0e50d4f07c0d9afcbca98eb87e21ee375ad6e3 Mon Sep 17 00:00:00 2001 From: Raman Keraz Date: Mon, 12 Apr 2021 22:10:12 +0200 Subject: [PATCH] rotation ability --- app/__init__.py | 14 ++++++------ app/base_field.py | 8 +++++-- app/tractor.py | 50 ++++++++++++++++++++++++++++++------------ resources/tractor.png | Bin 516 -> 1460 bytes 4 files changed, 49 insertions(+), 23 deletions(-) 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 81b11e3cc8a54b772f5a6b94cbbef9ded6d04d85..627ef1a74151bbf8314bdb33ec9ebb42d5f71442 100644 GIT binary patch literal 1460 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1|;R|J2nC-#^NA%Cx&(BWL^R}t5PF8(|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