Add so tractor can turn around

This commit is contained in:
Adam Szpilkowski 2022-06-05 13:28:17 +02:00
parent 921aeec597
commit 08d5775068
3 changed files with 31 additions and 0 deletions

View File

@ -8,3 +8,4 @@ class Constants:
LEFT = [-1, 0]
ROTATE_RIGHT = "rotate_right"
ROTATE_LEFT = "rotate_left"
TURN_AROUND = 'turn_around'

View File

@ -70,6 +70,30 @@ class Tractor(Sprite):
pygame.time.wait(self.settings.freeze_time) # bez tego sie kreci jak hot-wheels
def turn_around(self):
if self.curr_direction == C.UP:
self.curr_direction = C.DOWN
self.image = pygame.transform.scale(
pygame.image.load('assets/images/tractor/tractor-transparent-down.png'),
(self.settings.tile_size, self.settings.tile_size))
elif self.curr_direction == C.LEFT:
self.curr_direction = C.RIGHT
self.image = pygame.transform.scale(
pygame.image.load('assets/images/tractor/tractor-transparent-right.png'),
(self.settings.tile_size, self.settings.tile_size))
elif self.curr_direction == C.DOWN:
self.curr_direction = C.UP
self.image = pygame.transform.scale(
pygame.image.load('assets/images/tractor/tractor-transparent-up.png'),
(self.settings.tile_size, self.settings.tile_size))
elif self.curr_direction == C.RIGHT:
self.curr_direction = C.LEFT
self.image = pygame.transform.scale(
pygame.image.load('assets/images/tractor/tractor-transparent-left.png'),
(self.settings.tile_size, self.settings.tile_size))
pygame.time.wait(self.settings.freeze_time) # bez tego sie kreci jak hot-wheels
def move(self):
self.rect.x += self.curr_direction[0] * self.settings.tractor_speed
self.rect.y -= self.curr_direction[1] * self.settings.tractor_speed
@ -139,6 +163,8 @@ class Tractor(Sprite):
self.turn_right()
elif action == C.ROTATE_LEFT:
self.turn_left()
elif action == C.TURN_AROUND:
self.turn_around()
elif action == C.MOVE:
self.move()

View File

@ -48,6 +48,10 @@ class Node:
temp_direction = [temp_direction[1], -temp_direction[0]]
action_list.append((C.ROTATE_RIGHT, ((x, y), temp_direction)))
temp_direction = self.agent_direction
temp_direction = [-temp_direction[0], -temp_direction[1]]
action_list.append((C.TURN_AROUND, ((x, y), temp_direction)))
if self.agent_direction == C.RIGHT and x < 9:
action_list.append((C.MOVE, ((x + 1, y), self.agent_direction)))
elif self.agent_direction == C.LEFT and x > 0: