Zaktualizuj 'basic_grid.py'

This commit is contained in:
Cezary Adamczak 2021-06-03 16:02:44 +02:00
parent 227e83bf74
commit 9f4d3ea648

View File

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