Sztuczna_Inteligencja-projekt/basic_grid.py

157 lines
3.9 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
from pygame.locals import (
K_UP,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT
2021-03-14 21:50:50 +01:00
)
2021-06-03 16:02:44 +02:00
# Import other files from project
2021-05-10 14:35:41 +02:00
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
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
2021-05-10 14:35:41 +02:00
screen = pygame.display.set_mode((D.SCREEN_WIDTH, D.SCREEN_HEIGHT))
2021-06-03 16:02:44 +02:00
# Define the map of the field
2021-05-10 14:35:41 +02:00
mapschema = maps.createField()
2021-03-14 21:50:50 +01:00
# Create field array
field = []
2021-06-03 16:02:44 +02:00
# Populate the field array
2021-05-10 14:35:41 +02:00
for row in range(D.GSIZE):
2021-03-14 21:50:50 +01:00
field.append([])
2021-05-10 14:35:41 +02:00
for column in range(D.GSIZE):
fieldbit = F.Field(row, column, mapschema[column][row])
2021-03-14 21:50:50 +01:00
field[row].append(fieldbit)
2021-06-03 16:02:44 +02:00
# Create Tractor object
tractor = T.Tractor(field, [0,0])
2021-06-03 16:02:44 +02:00
# Define the map of plants
2021-05-10 14:35:41 +02:00
mapschema = maps.createPlants()
2021-06-03 16:02:44 +02:00
# Createt plants array
2021-05-10 14:35:41 +02:00
plants = []
2021-06-03 16:02:44 +02:00
# Populate the plants array
2021-05-10 14:35:41 +02:00
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)
2021-06-03 16:02:44 +02:00
# Create list for tractor instructions
2021-05-10 14:35:41 +02:00
path = []
2021-03-14 21:50:50 +01:00
# Variable to keep the main loop running
RUNNING = True
2021-06-03 16:02:44 +02:00
# Variable conroling timed eventes
2021-05-10 14:35:41 +02:00
TICKER = 0
2021-03-14 21:50:50 +01:00
2021-06-03 16:02:44 +02:00
# Initialize clock
2021-03-14 21:50:50 +01:00
clock = pygame.time.Clock()
2021-05-10 14:35:41 +02:00
2021-03-14 21:50:50 +01:00
# 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
2021-06-03 16:02:44 +02:00
# Create key Node that will be used to calculate tractor instructions
2021-05-10 14:35:41 +02:00
processor = N.Node(field, tractor.position, tractor.direction)
2021-06-03 16:02:44 +02:00
# If path is empty or nonexistent, create new one
2021-05-10 14:35:41 +02:00
if path is None or len(path) == 0:
path = processor.findPathToPlant()
2021-06-03 16:02:44 +02:00
# control tractor by poping instructions from path list
2021-05-10 14:35:41 +02:00
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)
2021-06-03 16:02:44 +02:00
# Get all keys pressed at a time CURRENTLY UNUSED
2021-03-14 21:50:50 +01:00
pressed_keys = pygame.key.get_pressed()
2021-05-10 14:35:41 +02:00
2021-06-03 16:02:44 +02:00
# control tractor with pressed keys CURRENTLY UNUSED
2021-05-10 14:35:41 +02:00
if pressed_keys[K_UP]:
tractor.move()
elif pressed_keys[K_LEFT]:
tractor.rotate_left()
elif pressed_keys[K_RIGHT]:
tractor.rotate_right()
2021-03-14 21:50:50 +01:00
# Set the screen background
2021-05-10 14:35:41 +02:00
screen.fill(C.DBROWN)
2021-03-14 21:50:50 +01:00
# Draw the field
2021-05-10 14:35:41 +02:00
for row in range(D.GSIZE):
for column in range(D.GSIZE):
2021-03-14 21:50:50 +01:00
screen.blit(field[row][column].surf, field[row][column].rect)
2021-06-03 16:02:44 +02:00
# Draw the tactor
2021-03-14 21:50:50 +01:00
screen.blit(tractor.surf, tractor.rect)
2021-06-03 16:02:44 +02:00
# Plants grow with every 10th tick, then they are drawn
2021-05-10 14:35:41 +02:00
for row in plants:
for plant in row:
2021-06-03 16:02:44 +02:00
plant.tick()
plant.grow()
2021-05-10 14:35:41 +02:00
screen.blit(plant.surf, plant.rect)
2021-06-03 16:02:44 +02:00
# Field are drying with every 100th tick
2021-05-10 14:35:41 +02:00
if TICKER == 0:
for row in range(D.GSIZE):
for column in range(D.GSIZE):
field[row][column].dehydrate()
2021-06-03 16:02:44 +02:00
# Increment ticker
2021-05-10 14:35:41 +02:00
TICKER = (TICKER + 1)%100
# Update the screen
2021-03-14 21:50:50 +01:00
pygame.display.flip()
2021-06-03 16:02:44 +02:00
# Ensure program maintains a stable framerate
2021-05-10 14:35:41 +02:00
clock.tick(8)