add rotation to tractor

This commit is contained in:
Dominik Cupał 2021-04-11 19:48:44 +02:00
parent d92c015daa
commit 42a04d00c5
3 changed files with 35 additions and 2 deletions

View File

@ -46,6 +46,10 @@ class App:
self.__tractor.move_right()
print(self.__tractor)
if keys[pygame.K_w]:
self.__tractor.move()
print(self.__tractor)
if keys[pygame.K_h]:
self.__tractor.harvest()
@ -58,6 +62,12 @@ class App:
if keys[pygame.K_f]:
self.__tractor.fertilize()
if keys[pygame.K_l]:
self.__tractor.rotate_left()
if keys[pygame.K_r]:
self.__tractor.rotate_right()
def update_screen(self):
pygame.display.flip()

View File

@ -7,14 +7,20 @@ 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):
def draw_field(self, screen: pygame.Surface, pos_x: int, pos_y: int,
is_centered: bool = False, size: tuple = None, angle: float = 0.0):
img = pygame.image.load(self._img_path)
img = pygame.transform.rotate(img, angle)
scale = pygame.transform.scale(img, (FIELD_SIZE, FIELD_SIZE))
rect = img.get_rect()
if is_centered:
rect.center = (pos_x, pos_y)
else:
rect.topleft = (pos_x, pos_y)
if size is not None:
rect.size = (FIELD_SIZE, FIELD_SIZE)
screen.blit(scale, rect)

View File

@ -21,15 +21,26 @@ class Tractor(BaseField):
super().__init__(os.path.join(RESOURCE_DIR, f"{TRACTOR}.{PNG}"))
self.__pos_x = (int(HORIZONTAL_NUM_OF_FIELDS / 2) - 1) * FIELD_SIZE
self.__pos_y = (int(VERTICAL_NUM_OF_FIELDS / 2) - 1) * FIELD_SIZE
self.__angle = 0.0
self.__move = FIELD_SIZE
self.__board = board
self.__harvested_corps = []
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.__angle)
# Key methods handlers
def move(self):
if self.__angle == 0.0:
self.move_right()
elif self.__angle == 90.0:
self.move_up()
elif self.__angle == 180.0:
self.move_left()
else:
self.move_down()
def move_up(self):
if self.__pos_y - self.__move >= 0:
self.__pos_y -= self.__move
@ -46,6 +57,12 @@ class Tractor(BaseField):
if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH:
self.__pos_x += self.__move
def rotate_left(self):
self.__angle = (self.__angle - 90.0) % 360.0
def rotate_right(self):
self.__angle = (self.__angle + 90.0) % 360.0
def hydrate(self):
if self.check_field(Sand):
field = self.get_field_from_board()