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 rose_f = tile.f self.water_lvl.append((rose_w, (0, 3))) if tile.pos == (7, 1): orchid_w = tile.w orchid_f = tile.f self.water_lvl.append((orchid_w, (5, 0))) if tile.pos == (1, 7): tulip_w = tile.w tulip_f = tile.f self.water_lvl.append((tulip_w, (3, 8))) if tile.pos == (7, 7): sunflower_w = tile.w sunflower_f = tile.f 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) + "% " + str(rose_f), 1, (255, 255, 255)) text2 = font.render("orchids: " + str(orchid_w) + "% " + str(orchid_f), 1, (255, 255, 255)) text3 = font.render("tulips: " + str(tulip_w) + "% " + str(tulip_f), 1, (255, 255, 255)) text4 = font.render("sunflowers: " + str(sunflower_w) + "%" + str(sunflower_f), 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 fertilizing(self, soil): if soil == (0, 3): for tile in self.tiles: if tile.object == rose: if tile.w < 50: tile.w = 100 tile.f = "OK" if soil == (5, 0): for tile in self.tiles: if tile.object == orchid: if tile.w < 50: tile.w = 100 tile.f = "OK" if soil == (3, 8): for tile in self.tiles: if tile.object == tulip: if tile.w < 50: tile.w = 100 tile.f = "OK" if soil == (8, 5): for tile in self.tiles: if tile.object == sunflower: if tile.w < 50: tile.w = 100 tile.f = "OK" def fertilization_target(self): f = [] if self.day % 2 == 0: fert_sunflowers = (8, 5) f.append(fert_sunflowers) for tile in self.tiles: if tile.object == sunflower: tile.f = "NO" if self.day % 5 == 0: fert_orchids = (5, 0) f.append(fert_orchids) for tile in self.tiles: if tile.object == orchid: tile.f = "NO" if self.day % 10 == 0: fert_roses = (0, 3) f.append(fert_roses) for tile in self.tiles: if tile.object == rose: tile.f = "NO" if self.day % 20 == 0: fert_tulip = (3, 8) f.append(fert_tulip) for tile in self.tiles: if tile.object == tulip: tile.f = "NO" return f def add_day(self): pg.time.wait(100) self.day = self.day + 1 for tile in self.tiles: tile.w = tile.w - tile.wu if tile.w < 0: tile.w = 0 display_farm() def watering_paths(): path = astar(background, a.pos, g.water_target()) while len(path) > 1: display_farm() a.display(path[1]) del(path[1]) else: g.watering(path[0]) def fertilization_ways(): targets = g.fertilization_target() for one in targets: way = astar(background, a.pos, one) while len(way) > 1: display_farm() a.display(way[1]) del(way[1]) else: g.fertilizing(one) g.add_day() def display_farm(): g.display_background() g.display_objects() g.display_legend() def game_loop(): display_farm() if g.water_target() is None: if g.fertilization_target(): fertilization_ways() else: g.add_day() else: watering_paths() 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()