74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
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()
|
|
|
|
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"
|
|
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"
|
|
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"
|
|
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"
|
|
elif not board.is_rock(tractor.col + 1, tractor.row):
|
|
tractor.col += 1
|
|
tractor.direction = "right"
|
|
|
|
board.draw_cubes(WIN)
|
|
tractor.draw(WIN)
|
|
pygame.display.update()
|
|
pygame.quit()
|
|
|
|
main() |