2024-03-10 02:46:14 +01:00
|
|
|
import pygame
|
|
|
|
from board import Board
|
|
|
|
from constant import width, height, rows, cols
|
|
|
|
from tractor import Tractor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fps = 5
|
|
|
|
WIN = pygame.display.set_mode((width, height))
|
|
|
|
|
|
|
|
pygame.display.set_caption('Inteligenty Traktor')
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run = True
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
board = Board()
|
|
|
|
board.load_images()
|
|
|
|
tractor = Tractor(4, 4)
|
|
|
|
while run:
|
|
|
|
clock.tick(fps)
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
run = False
|
|
|
|
|
|
|
|
keys = pygame.key.get_pressed()
|
2024-03-24 19:49:08 +01:00
|
|
|
|
|
|
|
if keys[pygame.K_UP] and tractor.row > 0 :
|
|
|
|
if board.is_weed(tractor.col, tractor.row - 1):
|
|
|
|
board.set_grass(tractor.col, tractor.row - 1)
|
|
|
|
tractor.row -= 1
|
|
|
|
tractor.direction = "up"
|
2024-03-24 20:54:05 +01:00
|
|
|
elif board.is_dirt(tractor.col, tractor.row - 1):
|
|
|
|
board.set_soil(tractor.col, tractor.row - 1)
|
|
|
|
tractor.row -= 1
|
|
|
|
tractor.direction = "up"
|
2024-03-24 19:49:08 +01:00
|
|
|
elif not board.is_rock(tractor.col, tractor.row - 1):
|
|
|
|
tractor.row -= 1
|
|
|
|
tractor.direction = "up"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if keys[pygame.K_DOWN] and tractor.row < rows-1 :
|
|
|
|
if board.is_weed(tractor.col, tractor.row + 1):
|
|
|
|
board.set_grass(tractor.col, tractor.row + 1)
|
|
|
|
tractor.row += 1
|
|
|
|
tractor.direction = "down"
|
2024-03-24 20:54:05 +01:00
|
|
|
elif board.is_dirt(tractor.col, tractor.row + 1):
|
|
|
|
board.set_soil(tractor.col, tractor.row + 1)
|
|
|
|
tractor.row += 1
|
|
|
|
tractor.direction = "down"
|
2024-03-24 19:49:08 +01:00
|
|
|
elif not board.is_rock(tractor.col, tractor.row + 1):
|
|
|
|
tractor.row += 1
|
|
|
|
tractor.direction = "down"
|
|
|
|
|
|
|
|
|
|
|
|
if keys[pygame.K_LEFT] and tractor.col > 0:
|
|
|
|
if board.is_weed(tractor.col - 1, tractor.row):
|
|
|
|
board.set_grass(tractor.col - 1, tractor.row)
|
|
|
|
tractor.col -= 1
|
|
|
|
tractor.direction = "left"
|
2024-03-24 20:54:05 +01:00
|
|
|
elif board.is_dirt(tractor.col - 1, tractor.row):
|
|
|
|
board.set_soil(tractor.col - 1, tractor.row)
|
|
|
|
tractor.col -= 1
|
|
|
|
tractor.direction = "left"
|
2024-03-24 19:49:08 +01:00
|
|
|
elif not board.is_rock(tractor.col - 1, tractor.row):
|
|
|
|
tractor.col -= 1
|
|
|
|
tractor.direction = "left"
|
|
|
|
|
|
|
|
|
|
|
|
if keys[pygame.K_RIGHT] and tractor.col < cols - 1:
|
|
|
|
if board.is_weed(tractor.col + 1, tractor.row):
|
|
|
|
board.set_grass(tractor.col + 1, tractor.row)
|
|
|
|
tractor.col += 1
|
|
|
|
tractor.direction = "right"
|
2024-03-24 20:54:05 +01:00
|
|
|
elif board.is_dirt(tractor.col + 1, tractor.row):
|
|
|
|
board.set_soil(tractor.col + 1, tractor.row)
|
|
|
|
tractor.col += 1
|
|
|
|
tractor.direction = "right"
|
2024-03-24 19:49:08 +01:00
|
|
|
elif not board.is_rock(tractor.col + 1, tractor.row):
|
|
|
|
tractor.col += 1
|
|
|
|
tractor.direction = "right"
|
2024-03-10 02:46:14 +01:00
|
|
|
|
|
|
|
board.draw_cubes(WIN)
|
|
|
|
tractor.draw(WIN)
|
|
|
|
pygame.display.update()
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
main()
|