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()
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]:

View File

@ -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:

View File

@ -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"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 516 B

After

Width:  |  Height:  |  Size: 1.4 KiB