2023-06-17 11:49:38 +02:00
|
|
|
import random
|
2023-03-26 18:19:10 +02:00
|
|
|
import os
|
2023-03-10 22:52:29 +01:00
|
|
|
import pygame
|
2023-03-13 10:36:49 +01:00
|
|
|
from pygame.locals import *
|
2023-06-17 11:49:38 +02:00
|
|
|
from core.chicken import chicken as chick
|
|
|
|
from core.field import field_settings
|
|
|
|
from core.plants import plants_settings
|
|
|
|
|
|
|
|
from agent.neural_network import inference
|
|
|
|
#import agent.neural_network.inference
|
|
|
|
#import neural_network.inference
|
|
|
|
# import core.plants.plant as plant
|
|
|
|
# import core.plants.plants_settings as plants_settings
|
|
|
|
import agent.methods.graph_search as graph_search
|
2023-03-10 22:52:29 +01:00
|
|
|
|
2023-05-22 09:32:17 +02:00
|
|
|
|
2023-06-15 19:33:40 +02:00
|
|
|
#import models.field_block as field_block
|
2023-05-21 18:46:38 +02:00
|
|
|
|
|
|
|
|
2023-03-21 23:55:52 +01:00
|
|
|
class Game:
|
2023-03-13 10:36:49 +01:00
|
|
|
cell_size = 50
|
2023-03-27 11:43:13 +02:00
|
|
|
cell_number = 15 # horizontally
|
2023-05-08 21:15:37 +02:00
|
|
|
blocks_number = 20
|
2023-06-15 19:33:40 +02:00
|
|
|
dry_grass_number = 50
|
|
|
|
wet_grass_number = (cell_number*cell_number) - dry_grass_number
|
2023-03-22 21:09:00 +01:00
|
|
|
|
2023-06-15 19:33:40 +02:00
|
|
|
def __init__(self):
|
2023-03-27 11:43:13 +02:00
|
|
|
# initialize a window
|
2023-03-10 22:52:29 +01:00
|
|
|
pygame.init()
|
2023-03-27 11:43:13 +02:00
|
|
|
self.surface = pygame.display.set_mode((self.cell_size*self.cell_number, self.cell_size*self.cell_number))
|
|
|
|
|
2023-06-17 11:49:38 +02:00
|
|
|
self.grass_list = [] # 1-level
|
|
|
|
self.plant_list = [] # 2-level
|
2023-06-18 01:59:54 +02:00
|
|
|
self.fruits_list = [] # 2-level - second model agent
|
2023-06-17 11:49:38 +02:00
|
|
|
self.stone_list = [] # 3-level
|
|
|
|
self.aim_list = [] # 4-level
|
2023-03-27 11:43:13 +02:00
|
|
|
|
2023-06-15 19:33:40 +02:00
|
|
|
self.Field = field_settings.FieldSettings(self.surface, self.cell_size, self.cell_number)
|
|
|
|
self.Field.locate_field(self.grass_list, 0, self.wet_grass_number) # wet grass
|
|
|
|
self.Field.locate_field(self.grass_list, 1, self.dry_grass_number) # dry grass
|
2023-03-12 20:22:10 +01:00
|
|
|
|
2023-06-15 19:33:40 +02:00
|
|
|
self.Plants = plants_settings.PlantsSettings(self.surface, self.cell_size, self.cell_number)
|
|
|
|
|
2023-06-18 01:59:54 +02:00
|
|
|
#plant_list
|
|
|
|
# self.Plants.locate_plant(self.plant_list, 'wheat', self.blocks_number)
|
|
|
|
# self.Plants.locate_plant(self.plant_list, 'flower', self.blocks_number)
|
|
|
|
# self.Plants.locate_plant(self.plant_list, 'bush', self.blocks_number)
|
|
|
|
|
|
|
|
#fruits_list
|
|
|
|
self.Plants.locate_fruit(self.fruits_list, 'apple', self.blocks_number)
|
|
|
|
self.Plants.locate_fruit(self.fruits_list, 'banana', self.blocks_number)
|
|
|
|
self.Plants.locate_fruit(self.fruits_list, 'strawberry', self.blocks_number)
|
|
|
|
self.Plants.locate_fruit(self.fruits_list, 'grapes', self.blocks_number)
|
|
|
|
self.Plants.locate_fruit(self.plant_list, 'wheat', self.blocks_number)
|
|
|
|
|
|
|
|
|
2023-03-26 18:19:10 +02:00
|
|
|
|
2023-06-15 19:33:40 +02:00
|
|
|
self.Plants.locate_aim(self.aim_list, 0, 0)
|
|
|
|
|
2023-06-18 01:59:54 +02:00
|
|
|
self.Plants.locate_fruit(self.stone_list, 'stone', self.blocks_number)
|
2023-06-15 19:33:40 +02:00
|
|
|
|
|
|
|
#self.image_wheat = self.Plants.wheat_watered()
|
|
|
|
self.chicken = chick.Chicken(self.surface, self.cell_size, self.cell_number)
|
|
|
|
self.chicken.draw()
|
2023-03-13 10:36:49 +01:00
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
def run(self):
|
|
|
|
running = True
|
2023-03-13 10:36:49 +01:00
|
|
|
clock = pygame.time.Clock()
|
2023-06-15 19:33:40 +02:00
|
|
|
move_chicken_event = pygame.USEREVENT + 1
|
|
|
|
pygame.time.set_timer(move_chicken_event, 500) # chicken moves every 1000 ms
|
|
|
|
self.search_object = graph_search.Search(self.cell_size, self.cell_number)
|
|
|
|
chicken_next_moves = []
|
2023-04-17 10:20:05 +02:00
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
|
2023-06-05 05:25:13 +02:00
|
|
|
veggies = dict()
|
|
|
|
veggies_debug = dict()
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
while running:
|
2023-03-13 10:36:49 +01:00
|
|
|
clock.tick(60) # manual fps control not to overwork the computer
|
2023-03-10 22:52:29 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == KEYDOWN:
|
|
|
|
if pygame.key.get_pressed()[K_ESCAPE]:
|
|
|
|
running = False
|
2023-03-13 10:36:49 +01:00
|
|
|
if pygame.key.get_pressed()[K_UP]:
|
2023-06-15 19:33:40 +02:00
|
|
|
# self.chicken.move('up', self.cell_size, self.cell_number)
|
|
|
|
self.chicken.move('move', self.cell_size, self.cell_number)
|
2023-04-16 13:07:46 +02:00
|
|
|
# if pygame.key.get_pressed()[K_DOWN]:
|
2023-06-15 19:33:40 +02:00
|
|
|
# self.chicken.move('down', self.cell_size, self.cell_number)
|
2023-03-13 10:36:49 +01:00
|
|
|
if pygame.key.get_pressed()[K_LEFT]:
|
2023-06-15 19:33:40 +02:00
|
|
|
self.chicken.move('left', self.cell_size, self.cell_number)
|
2023-03-13 10:36:49 +01:00
|
|
|
if pygame.key.get_pressed()[K_RIGHT]:
|
2023-06-15 19:33:40 +02:00
|
|
|
self.chicken.move('right', self.cell_size, self.cell_number)
|
2023-03-22 21:09:00 +01:00
|
|
|
if pygame.key.get_pressed()[K_SPACE]:
|
2023-06-15 19:33:40 +02:00
|
|
|
self.chicken.water(self.dead_leaf_body, self.green_leaf_body, self.cell_size)
|
|
|
|
|
|
|
|
if event.type == move_chicken_event:
|
|
|
|
if len(chicken_next_moves) == 0:
|
|
|
|
|
2023-04-17 16:53:54 +02:00
|
|
|
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
|
2023-06-15 19:33:40 +02:00
|
|
|
closest_wheat = self.search_object.closest_point(self.chicken.x, self.chicken.y, 'wheat', self.plant_list)
|
|
|
|
self.aim_list[0].xy[0] = closest_wheat[0]
|
|
|
|
self.aim_list[0].xy[1] = closest_wheat[1]
|
|
|
|
chicken_next_moves = self.search_object.astarsearch(
|
|
|
|
[self.chicken.x, self.chicken.y, angles[self.chicken.angle]], [closest_wheat[0], closest_wheat[1]], self.stone_list, self.plant_list)
|
|
|
|
|
|
|
|
#neural_network
|
2023-06-17 11:49:38 +02:00
|
|
|
current_veggie = next(os.walk('./agent/neural_network/images/test'))[1][random.randint(0, len(next(os.walk('./agent/neural_network/images/test'))[1])-1)]
|
2023-06-05 05:25:13 +02:00
|
|
|
if(current_veggie in veggies_debug):
|
|
|
|
veggies_debug[current_veggie]+=1
|
|
|
|
else:
|
|
|
|
veggies_debug[current_veggie] = 1
|
|
|
|
|
2023-06-17 11:49:38 +02:00
|
|
|
current_veggie_example = next(os.walk(f'./agent/neural_network/images/test/{current_veggie}'))[2][random.randint(0, len(next(os.walk(f'./agent/neural_network/images/test/{current_veggie}'))[2])-1)]
|
|
|
|
predicted_veggie = inference.main(f"./agent/neural_network/images/test/{current_veggie}/{current_veggie_example}")
|
2023-06-05 05:25:13 +02:00
|
|
|
if predicted_veggie in veggies:
|
|
|
|
veggies[predicted_veggie]+=1
|
|
|
|
else:
|
|
|
|
veggies[predicted_veggie] = 1
|
|
|
|
print("Debug veggies: ", veggies_debug, "Predicted veggies: ", veggies)
|
2023-04-17 10:20:05 +02:00
|
|
|
else:
|
2023-06-15 19:33:40 +02:00
|
|
|
self.chicken.move(chicken_next_moves.pop(0)[0])
|
|
|
|
if len(chicken_next_moves) == 0:
|
|
|
|
self.chicken.water([self.aim_list[0].xy[0], self.aim_list[0].xy[1]], self.plant_list)
|
|
|
|
print(self.chicken.x, self.chicken.y)
|
|
|
|
|
2023-03-10 22:52:29 +01:00
|
|
|
elif event.type == QUIT:
|
|
|
|
running = False
|
|
|
|
|
2023-03-27 15:29:06 +02:00
|
|
|
self.surface.fill((123, 56, 51)) # background color
|
2023-06-15 19:33:40 +02:00
|
|
|
self.Field.draw_grass(self.grass_list)
|
|
|
|
self.Plants.draw_plant(self.plant_list)
|
2023-06-18 01:59:54 +02:00
|
|
|
self.Plants.draw_plant(self.fruits_list)
|
2023-06-15 19:33:40 +02:00
|
|
|
self.Plants.draw_plant(self.stone_list)
|
2023-06-17 11:49:38 +02:00
|
|
|
|
2023-06-15 19:33:40 +02:00
|
|
|
self.Plants.draw_aim(self.aim_list)
|
2023-03-27 15:29:06 +02:00
|
|
|
|
2023-06-15 19:33:40 +02:00
|
|
|
self.chicken.draw()
|
2023-03-13 14:38:22 +01:00
|
|
|
pygame.display.update()
|
2023-03-10 22:52:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
game = Game()
|
|
|
|
game.run()
|