Compare commits

..

2 Commits

Author SHA1 Message Date
Wojciech Kubicki
a7db089ce4 feat(tractor): implement tank controls 2024-04-10 17:41:16 +02:00
Wojciech Kubicki
a1d4895a7a feat(tractor): rotate tractor on left-right arrow keys 2024-04-10 17:38:08 +02:00

View File

@ -25,31 +25,29 @@ class Tractor(pygame.sprite.Sprite):
surface.blit(self.image, self.rect)
def move(self, direction):
if direction == "up" and self.rect.y > 0:
def move(self):
if self.direction == "north" and self.rect.y > 0:
self.rect.y -= TILE_SIZE
self.log_info()
elif direction == "down" and self.rect.y < 15 * TILE_SIZE:
elif self.direction == "south" and self.rect.y < 15 * TILE_SIZE:
self.rect.y += TILE_SIZE
self.log_info()
elif direction == "left" and self.rect.x > 0:
elif self.direction == "west" and self.rect.x > 0:
self.rect.x -= TILE_SIZE
self.log_info()
elif direction == "right" and self.rect.x < 15 * TILE_SIZE:
elif self.direction == "east" and self.rect.x < 15 * TILE_SIZE:
self.rect.x += TILE_SIZE
self.log_info()
self.log_info()
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.move('left')
self.rotate('left')
if keys[pygame.K_RIGHT]:
self.move('right')
self.rotate('right')
if keys[pygame.K_UP]:
self.move('up')
if keys[pygame.K_DOWN]:
self.move('down')
self.move()
if keys[pygame.K_r]:
self.water = 50
print(f"💧 replenished water level: {self.water} litres\n")
@ -103,3 +101,38 @@ class Tractor(pygame.sprite.Sprite):
return neighbors
def rotate(self, rotate):
match (self.direction, rotate):
case ("north", "left"):
self.image = pygame.image.load('images/tractor/west.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'west'
case ("north", "right"):
self.image = pygame.image.load('images/tractor/east.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'east'
case ("east", "right"):
self.image = pygame.image.load('images/tractor/south.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'south'
case ("east", "left"):
self.image = pygame.image.load('images/tractor/north.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'north'
case ("south", "right"):
self.image = pygame.image.load('images/tractor/west.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'west'
case ("south", "left"):
self.image = pygame.image.load('images/tractor/east.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'east'
case ("west", "left"):
self.image = pygame.image.load('images/tractor/south.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'south'
case ("west", "right"):
self.image = pygame.image.load('images/tractor/north.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'north'