Zaktualizuj 'basic_grid.py'

This commit is contained in:
Cezary Adamczak 2021-05-10 14:35:41 +02:00
parent bb05f4b310
commit 2830efa247

View File

@ -1,3 +1,5 @@
### TO DO: PLANT SPRITES, PLANT GROWTH ###
# Import the pygame module # Import the pygame module
import pygame import pygame
@ -5,20 +7,20 @@ import pygame
# Updated to conform to flake8 and black standards # Updated to conform to flake8 and black standards
from pygame.locals import ( from pygame.locals import (
K_UP, K_UP,
K_DOWN,
K_LEFT, K_LEFT,
K_RIGHT, K_RIGHT,
K_ESCAPE, K_ESCAPE,
K_SPACE,
KEYDOWN, KEYDOWN,
QUIT QUIT
) )
from field import * import field as F
from tractor import * import tractor as T
from plant import * import plant as P
from colors import * import colors as C
from dimensions import * import dimensions as D
import node as N
import mapschema as maps
# Initialize pygame # Initialize pygame
pygame.init() pygame.init()
@ -28,32 +30,46 @@ pygame.display.set_caption("Inteligentny Traktor")
# Create the screen object # Create the screen object
# 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((SCREEN_WIDTH, SCREEN_HEIGHT)) screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT))
#define map
mapschema = maps.createField()
# Create field array # Create field array
field = [] field = []
for row in range(GSIZE): for row in range(D.GSIZE):
field.append([]) field.append([])
for column in range(GSIZE): for column in range(D.GSIZE):
fieldbit = Field(row, column) fieldbit = F.Field(row, column, mapschema[column][row])
field[row].append(fieldbit) field[row].append(fieldbit)
tractor = Tractor(field[0][0])
plant = Plant(field[1][1], "wheat") tractor = T.Tractor(field[0][0])
mapschema = maps.createPlants()
plants = []
for row in range(D.GSIZE):
plants.append([])
for column in range(D.GSIZE):
if mapschema[column][row] != 0:
plantbit = P.Plant(field[row][column], mapschema[column][row])
plants[row].append(plantbit)
print(tractor.rect) path = []
print(field[0][0].rect)
# Variable to keep the main loop running # Variable to keep the main loop running
RUNNING = True RUNNING = True
ticker = 0 TICKER = 0
clock = pygame.time.Clock() clock = pygame.time.Clock()
# Main loop # Main loop
while RUNNING: while RUNNING:
@ -68,37 +84,62 @@ while RUNNING:
elif event.type == QUIT: elif event.type == QUIT:
RUNNING = False RUNNING = False
processor = N.Node(field, tractor.position, tractor.direction)
if path is None or len(path) == 0:
path = processor.findPathToPlant()
if path is not None:
if path[0] == "move":
tractor.move()
path.pop(0)
elif path[0] =="left":
tractor.rotate_left()
path.pop(0)
elif path[0] == "right":
tractor.rotate_right()
path.pop(0)
elif path[0] == "hydrate":
tractor.hydrate(field)
path.pop(0)
else:
path.pop(0)
# Get all keys pressed at a time # Get all keys pressed at a time
pressed_keys = pygame.key.get_pressed() pressed_keys = pygame.key.get_pressed()
# Update the state of tractor if pressed_keys[K_UP]:
tractor.update(pressed_keys) tractor.move()
tractor.hydrate(field, pressed_keys) elif pressed_keys[K_LEFT]:
tractor.rotate_left()
elif pressed_keys[K_RIGHT]:
tractor.rotate_right()
# Set the screen background # Set the screen background
screen.fill(DBROWN) screen.fill(C.DBROWN)
# Draw the field # Draw the field
for row in range(GSIZE): for row in range(D.GSIZE):
for column in range(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)
screen.blit(tractor.surf, tractor.rect) screen.blit(tractor.surf, tractor.rect)
# Draw the player on the screen # Draw the player on the screen
screen.blit(plant.surf, plant.rect) for row in plants:
for plant in row:
if TICKER % 10 == 0:
plant.grow()
screen.blit(plant.surf, plant.rect)
if ticker == 0: if TICKER == 0:
for row in range(GSIZE): for row in range(D.GSIZE):
for column in range(GSIZE): for column in range(D.GSIZE):
field[row][column].dehydrate() field[row][column].dehydrate()
plant.grow() TICKER = (TICKER + 1)%100
ticker = (ticker + 1)%4
# Update the screen # Update the screen
pygame.display.flip() pygame.display.flip()
# Ensure program maintains a rate of 15 frames per second clock.tick(8)
clock.tick(4)