Sztuczna_Inteligencja-projekt/basic_grid.py
Cezary Adamczak 3e11cd8831 Główny plik uruchomieniowy
basic_grid zaktualizowany na zajęcia nr 4
2021-03-28 23:49:44 +02:00

105 lines
2.3 KiB
Python

# 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,
QUIT
)
from field import *
from tractor import *
from plant import *
from colors import *
from dimensions import *
# 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)
tractor = Tractor(field[0][0])
plant = Plant(field[1][1], "wheat")
print(tractor.rect)
print(field[0][0].rect)
# Variable to keep the main loop running
RUNNING = True
ticker = 0
clock = pygame.time.Clock()
# Main loop
while RUNNING:
# 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:
RUNNING = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
RUNNING = False
# Get all keys pressed at a time
pressed_keys = pygame.key.get_pressed()
# Update the state of tractor
tractor.update(pressed_keys)
tractor.hydrate(field, pressed_keys)
# Set the screen background
screen.fill(DBROWN)
# 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)
# 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
pygame.display.flip()
# Ensure program maintains a rate of 15 frames per second
clock.tick(4)