rotation ability

This commit is contained in:
Raman Keraz 2021-04-12 22:10:12 +02:00
parent d92c015daa
commit 1c0e50d4f0
4 changed files with 49 additions and 23 deletions

View File

@ -34,16 +34,16 @@ class App:
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_UP]: if keys[pygame.K_UP]:
self.__tractor.move_up() self.__tractor.direction_up()
print(self.__tractor)
if keys[pygame.K_DOWN]: if keys[pygame.K_DOWN]:
self.__tractor.move_down() self.__tractor.direction_down()
print(self.__tractor)
if keys[pygame.K_LEFT]: if keys[pygame.K_LEFT]:
self.__tractor.move_left() self.__tractor.direction_left()
print(self.__tractor)
if keys[pygame.K_RIGHT]: if keys[pygame.K_RIGHT]:
self.__tractor.move_right() self.__tractor.direction_right()
if keys[pygame.K_m]:
self.__tractor.move()
print(self.__tractor) print(self.__tractor)
if keys[pygame.K_h]: if keys[pygame.K_h]:

View File

@ -7,8 +7,12 @@ class BaseField:
def __init__(self, img_path: str): def __init__(self, img_path: str):
self._img_path = img_path 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,
img = pygame.image.load(self._img_path) 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)) scale = pygame.transform.scale(img, (FIELD_SIZE, FIELD_SIZE))
rect = img.get_rect() rect = img.get_rect()
if is_centered: if is_centered:

View File

@ -24,27 +24,49 @@ class Tractor(BaseField):
self.__move = FIELD_SIZE self.__move = FIELD_SIZE
self.__board = board self.__board = board
self.__harvested_corps = [] self.__harvested_corps = []
self.__direction = 270
self.__fuel = 10
def draw(self, screen: pygame.Surface): def draw(self, screen: pygame.Surface):
self.draw_field(screen, self.__pos_x + FIELD_SIZE / 2, self.__pos_y + FIELD_SIZE / 2, 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 # Key methods handlers
def move_up(self):
def direction_up(self):
self.__direction = 0
def direction_right(self):
self.__direction = 270
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: if self.__pos_y - self.__move >= 0:
self.__pos_y -= self.__move self.__pos_y -= self.__move
def move_down(self): 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: if self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT:
self.__pos_y += self.__move self.__pos_y += self.__move
def move_left(self): elif self.__direction == 90:
if self.__pos_x - self.__move >= 0: if self.__pos_x - self.__move >= 0:
self.__pos_x -= self.__move self.__pos_x -= self.__move
def move_right(self): else:
if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH: print("Run out of fuel!")
self.__pos_x += self.__move
def hydrate(self): def hydrate(self):
if self.check_field(Sand): if self.check_field(Sand):
@ -135,4 +157,4 @@ class Tractor(BaseField):
def __str__(self): def __str__(self):
x, y = self.get_position() 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"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 516 B

After

Width:  |  Height:  |  Size: 1.4 KiB