Zaktualizuj 'basic_grid.py'

This commit is contained in:
Cezary Adamczak 2021-06-03 16:02:44 +02:00
parent 227e83bf74
commit 9f4d3ea648
1 changed files with 24 additions and 13 deletions

View File

@ -1,10 +1,7 @@
### TO DO: PLANT SPRITES, PLANT GROWTH ###
# 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_LEFT,
@ -14,6 +11,7 @@ from pygame.locals import (
QUIT
)
# Import other files from project
import field as F
import tractor as T
import plant as P
@ -32,26 +30,29 @@ pygame.display.set_caption("Inteligentny Traktor")
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT))
#define map
# Define the map of the field
mapschema = maps.createField()
# Create field array
field = []
# Populate the field array
for row in range(D.GSIZE):
field.append([])
for column in range(D.GSIZE):
fieldbit = F.Field(row, column, mapschema[column][row])
field[row].append(fieldbit)
# Create Tractor object
tractor = T.Tractor(field, [0,0])
tractor = T.Tractor(field[0][0])
# Define the map of plants
mapschema = maps.createPlants()
# Createt plants array
plants = []
# Populate the plants array
for row in range(D.GSIZE):
plants.append([])
for column in range(D.GSIZE):
@ -59,14 +60,16 @@ for row in range(D.GSIZE):
plantbit = P.Plant(field[row][column], mapschema[column][row])
plants[row].append(plantbit)
# Create list for tractor instructions
path = []
# Variable to keep the main loop running
RUNNING = True
# Variable conroling timed eventes
TICKER = 0
# Initialize clock
clock = pygame.time.Clock()
@ -84,11 +87,14 @@ while RUNNING:
elif event.type == QUIT:
RUNNING = False
# Create key Node that will be used to calculate tractor instructions
processor = N.Node(field, tractor.position, tractor.direction)
# If path is empty or nonexistent, create new one
if path is None or len(path) == 0:
path = processor.findPathToPlant()
# control tractor by poping instructions from path list
if path is not None:
if path[0] == "move":
tractor.move()
@ -105,9 +111,10 @@ while RUNNING:
else:
path.pop(0)
# Get all keys pressed at a time
# Get all keys pressed at a time CURRENTLY UNUSED
pressed_keys = pygame.key.get_pressed()
# control tractor with pressed keys CURRENTLY UNUSED
if pressed_keys[K_UP]:
tractor.move()
elif pressed_keys[K_LEFT]:
@ -123,23 +130,27 @@ while RUNNING:
for column in range(D.GSIZE):
screen.blit(field[row][column].surf, field[row][column].rect)
# Draw the tactor
screen.blit(tractor.surf, tractor.rect)
# Draw the player on the screen
# Plants grow with every 10th tick, then they are drawn
for row in plants:
for plant in row:
if TICKER % 10 == 0:
plant.grow()
plant.tick()
plant.grow()
screen.blit(plant.surf, plant.rect)
# Field are drying with every 100th tick
if TICKER == 0:
for row in range(D.GSIZE):
for column in range(D.GSIZE):
field[row][column].dehydrate()
# Increment ticker
TICKER = (TICKER + 1)%100
# Update the screen
pygame.display.flip()
# Ensure program maintains a stable framerate
clock.tick(8)