Sztuczna_Inteligencja-projekt/basic_grid.py

146 lines
3.3 KiB
Python

### 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,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT
)
import field as F
import tractor as T
import plant as P
import colors as C
import dimensions as D
import node as N
import mapschema as maps
# 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((D.SCREEN_WIDTH, D.SCREEN_HEIGHT))
#define map
mapschema = maps.createField()
# Create field array
field = []
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)
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)
path = []
# 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
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
pressed_keys = pygame.key.get_pressed()
if pressed_keys[K_UP]:
tractor.move()
elif pressed_keys[K_LEFT]:
tractor.rotate_left()
elif pressed_keys[K_RIGHT]:
tractor.rotate_right()
# Set the screen background
screen.fill(C.DBROWN)
# Draw the field
for row in range(D.GSIZE):
for column in range(D.GSIZE):
screen.blit(field[row][column].surf, field[row][column].rect)
screen.blit(tractor.surf, tractor.rect)
# Draw the player on the screen
for row in plants:
for plant in row:
if TICKER % 10 == 0:
plant.grow()
screen.blit(plant.surf, plant.rect)
if TICKER == 0:
for row in range(D.GSIZE):
for column in range(D.GSIZE):
field[row][column].dehydrate()
TICKER = (TICKER + 1)%100
# Update the screen
pygame.display.flip()
clock.tick(8)