Sztuczna_Inteligencja-projekt/basic_grid.py

105 lines
2.3 KiB
Python
Raw Normal View History

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,
QUIT
2021-03-14 21:50:50 +01: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)
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
RUNNING = True
ticker = 0
2021-03-14 21:50:50 +01:00
clock = pygame.time.Clock()
# Main loop
while RUNNING:
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:
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:
RUNNING = False
2021-03-14 21:50:50 +01:00
# Get all keys pressed at a time
2021-03-14 21:50:50 +01:00
pressed_keys = pygame.key.get_pressed()
# Update the state of tractor
2021-03-14 21:50:50 +01:00
tractor.update(pressed_keys)
tractor.hydrate(field, pressed_keys)
2021-03-14 21:50:50 +01:00
# Set the screen background
2021-03-14 21:50:50 +01:00
screen.fill(DBROWN)
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)
# 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()
# Ensure program maintains a rate of 15 frames per second
clock.tick(4)