Intelligent_Tractor/main.py
2020-06-29 13:51:23 +02:00

130 lines
3.7 KiB
Python

import pygame as pg
import sys
import numpy as np
from tile import Tile
from config import *
from agent import Agent
from Astar import *
done = False
clock = pg.time.Clock()
pg.display.set_caption('Intelligent Tractor')
pg.init()
pg.font.init()
class Game:
def __init__(self):
self.tiles = []
self.water_lvl = []
self.day = 1
for y in range(0, 9):
for x in range(0, 9):
pos = (x, y)
t = Tile(background[x][y], objects[x][y], pos)
t.water_usage()
self.tiles.append(t)
def display_background(self):
for tile in self.tiles:
screen.blit(bg_textures[tile.ground], (tile.x * tile_size, tile.y * tile_size))
def display_objects(self):
for tile in self.tiles:
screen.blit(ob_textures[tile.object], (tile.x * tile_size, tile.y * tile_size))
def display_legend(self):
screen.blit(black_surface, (0, 720))
self.water_lvl = []
for tile in self.tiles:
if tile.pos == (1, 1):
rose_w = tile.w
self.water_lvl.append((rose_w, (0, 3)))
if tile.pos == (7, 1):
orchid_w = tile.w
self.water_lvl.append((orchid_w, (5, 0)))
if tile.pos == (1, 7):
tulip_w = tile.w
self.water_lvl.append((tulip_w, (3, 8)))
if tile.pos == (7, 7):
sunflower_w = tile.w
self.water_lvl.append((sunflower_w, (8, 5)))
font = pg.font.SysFont('ubuntumono', 40)
text = font.render("water levels", 1, (255, 255, 255))
text1 = font.render("roses: " + str(rose_w) + "%", 1, (255, 255, 255))
text2 = font.render("orchids: " + str(orchid_w) + "%", 1, (255, 255, 255))
text3 = font.render("tulips: " + str(tulip_w) + "%", 1, (255, 255, 255))
text4 = font.render("sunflowers: " + str(sunflower_w) + "%", 1, (255, 255, 255))
text5 = font.render("day: " + str(self.day), 1, (255, 255, 255))
screen.blit(text, (10, 720))
screen.blit(text1, (10, 760))
screen.blit(text2, (360, 760))
screen.blit(text3, (10, 800))
screen.blit(text4, (360, 800))
screen.blit(text5, (10, 840))
def water_target(self):
o = None
for lvl in self.water_lvl:
if lvl[0] < 10:
o = lvl[1]
break
return o
def watering(self, plants):
if plants == (0, 3):
for tile in self.tiles:
if tile.object == rose:
tile.w = 100
if plants == (5, 0):
for tile in self.tiles:
if tile.object == orchid:
tile.w = 100
if plants == (3, 8):
for tile in self.tiles:
if tile.object == tulip:
tile.w = 100
if plants == (8, 5):
for tile in self.tiles:
if tile.object == sunflower:
tile.w = 100
def add_day(self):
self.day = self.day + 1
for tile in self.tiles:
tile.w = tile.w - tile.wu
if tile.w < 0:
tile.w = 0
def game_loop():
path = None
g.display_background()
g.display_objects()
g.display_legend()
if g.water_target() is None:
g.add_day()
else:
path = astar(background, a.pos, g.water_target())
if path is not None:
if len(path) == 1:
g.watering(path[0])
path = None
else:
a.display(path[1])
a.display()
clock.tick(60)
g = Game()
a = Agent()
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
game_loop()
pg.quit()