2021-03-14 21:50:50 +01:00
|
|
|
# Import the pygame module
|
|
|
|
import pygame
|
|
|
|
|
|
|
|
# Import pygame.locals for easier access to key coordinates
|
|
|
|
# Updated to conform to flake8 and black standards
|
|
|
|
from pygame.locals import (
|
|
|
|
K_UP,
|
|
|
|
K_DOWN,
|
|
|
|
K_LEFT,
|
|
|
|
K_RIGHT,
|
|
|
|
K_ESCAPE,
|
|
|
|
K_SPACE,
|
|
|
|
KEYDOWN,
|
2021-03-28 23:49:44 +02:00
|
|
|
QUIT
|
2021-03-14 21:50:50 +01:00
|
|
|
)
|
|
|
|
|
2021-03-28 23:49:44 +02:00
|
|
|
from field import *
|
|
|
|
from tractor import *
|
|
|
|
from plant import *
|
|
|
|
from colors import *
|
|
|
|
from dimensions import *
|
|
|
|
|
2021-03-14 21:50:50 +01:00
|
|
|
# Initialize pygame
|
|
|
|
pygame.init()
|
|
|
|
|
|
|
|
# Name the window
|
|
|
|
pygame.display.set_caption("Inteligentny Traktor")
|
|
|
|
|
|
|
|
# Create the screen object
|
|
|
|
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
|
|
|
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
|
|
|
|
|
|
# Create field array
|
|
|
|
field = []
|
|
|
|
|
|
|
|
for row in range(GSIZE):
|
|
|
|
field.append([])
|
|
|
|
for column in range(GSIZE):
|
|
|
|
fieldbit = Field(row, column)
|
|
|
|
field[row].append(fieldbit)
|
|
|
|
|
2021-03-28 23:49:44 +02:00
|
|
|
tractor = Tractor(field[0][0])
|
|
|
|
|
|
|
|
plant = Plant(field[1][1], "wheat")
|
|
|
|
|
|
|
|
|
|
|
|
print(tractor.rect)
|
|
|
|
print(field[0][0].rect)
|
|
|
|
|
2021-03-14 21:50:50 +01:00
|
|
|
# Variable to keep the main loop running
|
2021-03-28 23:49:44 +02:00
|
|
|
RUNNING = True
|
|
|
|
|
|
|
|
ticker = 0
|
2021-03-14 21:50:50 +01:00
|
|
|
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
|
|
|
# Main loop
|
2021-03-28 23:49:44 +02:00
|
|
|
while RUNNING:
|
2021-03-15 08:48:34 +01:00
|
|
|
|
2021-03-14 21:50:50 +01:00
|
|
|
# Look at every event in the queue
|
|
|
|
for event in pygame.event.get():
|
|
|
|
# Did the user hit a key?
|
|
|
|
if event.type == KEYDOWN:
|
|
|
|
# Was it the Escape key? If so, stop the loop.
|
|
|
|
if event.key == K_ESCAPE:
|
2021-03-28 23:49:44 +02:00
|
|
|
RUNNING = False
|
2021-03-14 21:50:50 +01:00
|
|
|
# Did the user click the window close button? If so, stop the loop.
|
|
|
|
elif event.type == QUIT:
|
2021-03-28 23:49:44 +02:00
|
|
|
RUNNING = False
|
2021-03-14 21:50:50 +01:00
|
|
|
|
2021-03-15 08:48:34 +01:00
|
|
|
# Get all keys pressed at a time
|
2021-03-14 21:50:50 +01:00
|
|
|
pressed_keys = pygame.key.get_pressed()
|
|
|
|
|
2021-03-15 08:48:34 +01:00
|
|
|
# Update the state of tractor
|
2021-03-14 21:50:50 +01:00
|
|
|
tractor.update(pressed_keys)
|
2021-03-15 08:48:34 +01:00
|
|
|
tractor.hydrate(field, pressed_keys)
|
2021-03-14 21:50:50 +01:00
|
|
|
|
2021-03-15 08:48:34 +01:00
|
|
|
# Set the screen background
|
2021-03-14 21:50:50 +01:00
|
|
|
screen.fill(DBROWN)
|
2021-03-28 23:49:44 +02:00
|
|
|
|
2021-03-14 21:50:50 +01:00
|
|
|
# Draw the field
|
|
|
|
for row in range(GSIZE):
|
|
|
|
for column in range(GSIZE):
|
|
|
|
screen.blit(field[row][column].surf, field[row][column].rect)
|
|
|
|
|
|
|
|
screen.blit(tractor.surf, tractor.rect)
|
|
|
|
|
2021-03-28 23:49:44 +02:00
|
|
|
# Draw the player on the screen
|
|
|
|
screen.blit(plant.surf, plant.rect)
|
|
|
|
|
|
|
|
if ticker == 0:
|
|
|
|
for row in range(GSIZE):
|
|
|
|
for column in range(GSIZE):
|
|
|
|
field[row][column].dehydrate()
|
|
|
|
|
|
|
|
plant.grow()
|
|
|
|
|
|
|
|
ticker = (ticker + 1)%4
|
|
|
|
|
|
|
|
# Update the screen
|
2021-03-14 21:50:50 +01:00
|
|
|
pygame.display.flip()
|
|
|
|
|
2021-03-15 08:48:34 +01:00
|
|
|
# Ensure program maintains a rate of 15 frames per second
|
2021-03-28 23:49:44 +02:00
|
|
|
clock.tick(4)
|