Traktor/tractor.py

69 lines
2.7 KiB
Python
Raw Normal View History

2024-03-10 02:46:14 +01:00
import pygame
2024-05-18 17:10:56 +02:00
from constant import size, rows, cols
2024-03-10 02:46:14 +01:00
class Tractor:
def __init__(self, row, col):
self.row = row
self.col = col
self.images = {
2024-03-24 19:28:14 +01:00
"up": pygame.image.load("tractor/up.png"),
"down": pygame.image.load("tractor/down.png"),
"left": pygame.image.load("tractor/left.png"),
2024-05-18 17:10:56 +02:00
"right": pygame.image.load("tractor/right.png")
2024-03-10 02:46:14 +01:00
}
self.direction = "down"
2024-03-24 19:28:14 +01:00
2024-03-10 02:46:14 +01:00
def draw(self, win):
tractor_image = self.images[self.direction]
2024-05-18 17:10:56 +02:00
win.blit(tractor_image, (self.col * size, self.row * size))
def turn_left(self):
if self.direction == "up":
self.direction = "left"
elif self.direction == "left":
self.direction = "down"
elif self.direction == "down":
self.direction = "right"
elif self.direction == "right":
self.direction = "up"
def turn_right(self):
if self.direction == "up":
self.direction = "right"
elif self.direction == "right":
self.direction = "down"
elif self.direction == "down":
self.direction = "left"
elif self.direction == "left":
self.direction = "up"
def move_forward(self, board):
if self.direction == "up" and self.row > 0:
if not board.is_rock(self.row - 1, self.col):
if board.is_weed(self.row - 1, self.col):
board.set_grass(self.row - 1, self.col)
elif board.is_dirt(self.row - 1, self.col):
board.set_soil(self.row - 1, self.col)
self.row -= 1
elif self.direction == "left" and self.col > 0:
if not board.is_rock(self.row, self.col - 1):
if board.is_weed(self.row, self.col - 1):
board.set_grass(self.row, self.col - 1)
elif board.is_dirt(self.row, self.col - 1):
board.set_soil(self.row, self.col - 1)
self.col -= 1
elif self.direction == "down" and self.row < rows - 1:
if not board.is_rock(self.row + 1, self.col):
if board.is_weed(self.row + 1, self.col):
board.set_grass(self.row + 1, self.col)
elif board.is_dirt(self.row + 1, self.col):
board.set_soil(self.row + 1, self.col)
self.row += 1
elif self.direction == "right" and self.col < cols - 1:
if not board.is_rock(self.row, self.col + 1):
if board.is_weed(self.row, self.col + 1):
board.set_grass(self.row, self.col + 1)
elif board.is_dirt(self.row, self.col + 1):
board.set_soil(self.row, self.col + 1)
self.col += 1