2022-05-13 13:12:56 +02:00
|
|
|
from asyncio import sleep
|
2022-04-07 20:06:09 +02:00
|
|
|
from calendar import c
|
2022-05-13 13:12:56 +02:00
|
|
|
import time
|
2022-04-23 19:14:23 +02:00
|
|
|
from game_objects.player import Player
|
2022-04-04 11:40:18 +02:00
|
|
|
import pygame as pg
|
|
|
|
import sys
|
|
|
|
from os import path
|
2022-04-24 19:05:48 +02:00
|
|
|
import math
|
2022-04-03 15:47:41 +02:00
|
|
|
from map import *
|
2022-04-04 11:40:18 +02:00
|
|
|
from settings import *
|
2022-04-23 19:14:23 +02:00
|
|
|
from map import map
|
|
|
|
from map import map_utils
|
2022-04-24 19:05:48 +02:00
|
|
|
from path_search_algorthms import bfs
|
2022-04-28 20:12:46 +02:00
|
|
|
from path_search_algorthms import a_star, a_star_utils
|
2022-05-09 19:18:24 +02:00
|
|
|
from decision_tree import decisionTree
|
2022-03-10 15:53:41 +01:00
|
|
|
|
2022-04-27 21:49:17 +02:00
|
|
|
from game_objects import aiPlayer
|
2022-05-13 08:31:40 +02:00
|
|
|
import itertools
|
2022-05-09 19:18:24 +02:00
|
|
|
|
|
|
|
|
2022-05-13 08:31:40 +02:00
|
|
|
def getTree():
|
2022-05-09 19:18:24 +02:00
|
|
|
tree = decisionTree.tree()
|
|
|
|
decisionTree.tree_as_txt(tree)
|
2022-05-13 08:31:40 +02:00
|
|
|
# decisionTree.tree_to_png(tree)
|
2022-05-09 19:18:24 +02:00
|
|
|
decisionTree.tree_to_structure(tree)
|
2022-05-12 09:39:25 +02:00
|
|
|
drzewo = decisionTree.tree_from_structure('./decision_tree/tree_model')
|
2022-05-13 08:31:40 +02:00
|
|
|
# print("Dla losowych danych predykcja czy wziąć kosz to: ")
|
|
|
|
# dec = decisionTree.decision(drzewo, *(4,1,1,1))
|
|
|
|
# print('---')
|
|
|
|
# print(f"decision is{dec}")
|
|
|
|
# print('---')
|
|
|
|
|
|
|
|
return drzewo
|
2022-05-09 19:18:24 +02:00
|
|
|
|
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
class Game():
|
2022-04-07 21:37:51 +02:00
|
|
|
|
2022-03-10 16:16:59 +01:00
|
|
|
def __init__(self):
|
2022-04-04 11:40:18 +02:00
|
|
|
pg.init()
|
2022-04-23 19:14:23 +02:00
|
|
|
self.clock = pg.time.Clock()
|
2022-05-13 08:31:40 +02:00
|
|
|
self.dt = self.clock.tick(FPS) / 333.0
|
2022-04-04 11:40:18 +02:00
|
|
|
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
pg.display.set_caption("Trashmaster")
|
|
|
|
self.load_data()
|
2022-04-23 19:14:23 +02:00
|
|
|
self.init_game()
|
2022-04-27 21:49:17 +02:00
|
|
|
# because dont work without data.txt
|
2022-04-24 19:05:48 +02:00
|
|
|
# self.init_bfs()
|
2022-04-28 18:43:15 +02:00
|
|
|
# self.init_a_star()
|
2022-05-13 08:31:40 +02:00
|
|
|
self.t = aiPlayer.aiPlayer(self.player, game=self)
|
|
|
|
|
|
|
|
|
|
|
|
def get_actions_by_coords(self,x,y):
|
|
|
|
pos = (x,y)
|
|
|
|
offset_x, offset_y = self.camera.offset()
|
|
|
|
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
|
|
|
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE),
|
|
|
|
math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(),
|
|
|
|
clicked_coords[0], clicked_coords[1], self.mapArray)
|
|
|
|
return actions
|
2022-04-04 11:40:18 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
def init_game(self):
|
2022-04-04 11:40:18 +02:00
|
|
|
# initialize all variables and do all the setup for a new game
|
|
|
|
|
2022-04-23 19:30:06 +02:00
|
|
|
# sprite groups and map array for calculations
|
2022-05-10 01:05:34 +02:00
|
|
|
(self.roadTiles, self.wallTiles, self.trashbinTiles), self.mapArray = map.get_tiles()
|
2022-04-23 19:14:23 +02:00
|
|
|
self.agentSprites = pg.sprite.Group()
|
|
|
|
# player obj
|
2022-04-28 17:21:35 +02:00
|
|
|
self.player = Player(self, 32, 32)
|
2022-04-23 19:14:23 +02:00
|
|
|
# camera obj
|
|
|
|
self.camera = map_utils.Camera(MAP_WIDTH_PX, MAP_HEIGHT_PX)
|
2022-04-03 15:47:41 +02:00
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
# other
|
2022-04-24 19:05:48 +02:00
|
|
|
self.debug_mode = False
|
2022-04-04 11:40:18 +02:00
|
|
|
|
2022-05-13 08:31:40 +02:00
|
|
|
|
2022-04-23 19:30:06 +02:00
|
|
|
def init_bfs(self):
|
|
|
|
start_node = (0, 0)
|
|
|
|
target_node = (18, 18)
|
2022-04-24 19:05:48 +02:00
|
|
|
find_path = bfs.BreadthSearchAlgorithm(start_node, target_node, self.mapArray)
|
2022-04-23 19:30:06 +02:00
|
|
|
path = find_path.bfs()
|
|
|
|
# print(path)
|
|
|
|
realPath = []
|
|
|
|
nextNode = target_node
|
2022-05-09 19:18:24 +02:00
|
|
|
for i in range(len(path) - 1, 0, -1):
|
2022-04-23 19:30:06 +02:00
|
|
|
node = path[i]
|
|
|
|
if node[0] == nextNode:
|
|
|
|
realPath.insert(0, node[0])
|
|
|
|
nextNode = node[1]
|
|
|
|
print(realPath)
|
|
|
|
|
2022-04-24 19:05:48 +02:00
|
|
|
def init_a_star(self):
|
|
|
|
# szukanie sciezki na sztywno i wyprintowanie wyniku (tablica stringow)
|
|
|
|
start_x = 0
|
|
|
|
start_y = 0
|
|
|
|
target_x = 6
|
|
|
|
target_y = 2
|
|
|
|
path = a_star.search_path(start_x, start_y, target_x, target_y, self.mapArray)
|
|
|
|
print(path)
|
|
|
|
|
2022-05-13 08:31:40 +02:00
|
|
|
def init_decision_tree(self):
|
|
|
|
# logika pracy z drzewem
|
|
|
|
self.positive_decision = []
|
|
|
|
self.negative_decision = []
|
|
|
|
|
|
|
|
for i in self.trashbinTiles:
|
|
|
|
atrrs_container = i.get_attributes()
|
|
|
|
x, y = i.get_coords()
|
|
|
|
dec = decisionTree.decision(getTree(), *atrrs_container)
|
|
|
|
if dec[0] == 1:
|
|
|
|
self.positive_decision.append(i)
|
|
|
|
else:
|
|
|
|
self.negative_decision.append(i)
|
|
|
|
|
2022-05-13 13:12:56 +02:00
|
|
|
# print('positive actions')
|
2022-05-13 08:31:40 +02:00
|
|
|
# for i in self.positive_actions:
|
2022-05-13 13:12:56 +02:00
|
|
|
# print('----')
|
|
|
|
# print(i)
|
|
|
|
# print('----')
|
|
|
|
|
|
|
|
print('positive actions')
|
2022-05-22 13:40:59 +02:00
|
|
|
print(len(self.positive_decision))
|
2022-05-13 13:12:56 +02:00
|
|
|
for i in self.positive_decision:
|
|
|
|
# print(i.get_coords())
|
|
|
|
trash_x, trash_y = i.get_coords()
|
|
|
|
action = self.get_actions_by_coords(trash_x, trash_y)
|
|
|
|
self.t.startAiController(action)
|
|
|
|
# print(self.positive_actions[0])
|
|
|
|
|
|
|
|
# self.t.startAiController(self.positive_actions[0])
|
2022-05-13 08:31:40 +02:00
|
|
|
|
|
|
|
|
2022-04-23 19:14:23 +02:00
|
|
|
def load_data(self):
|
|
|
|
game_folder = path.dirname(__file__)
|
|
|
|
img_folder = path.join(game_folder, 'resources/textures')
|
|
|
|
|
2022-05-09 19:18:24 +02:00
|
|
|
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
|
|
|
|
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
|
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
def run(self):
|
|
|
|
# game loop - set self.playing = False to end the game
|
|
|
|
self.playing = True
|
2022-05-13 08:31:40 +02:00
|
|
|
self.init_decision_tree()
|
2022-04-04 11:40:18 +02:00
|
|
|
while self.playing:
|
2022-05-09 19:18:24 +02:00
|
|
|
self.dt = self.clock.tick(FPS) / 1000.0
|
2022-04-04 11:40:18 +02:00
|
|
|
self.events()
|
|
|
|
self.update()
|
|
|
|
self.draw()
|
2022-04-07 21:37:51 +02:00
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
def quit(self):
|
|
|
|
pg.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
# update portion of the game loop
|
2022-04-23 19:14:23 +02:00
|
|
|
self.agentSprites.update()
|
2022-04-04 11:40:18 +02:00
|
|
|
self.camera.update(self.player)
|
|
|
|
# pygame.display.update()
|
2022-04-03 15:47:41 +02:00
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
def draw(self):
|
2022-05-09 19:18:24 +02:00
|
|
|
# display fps as window title
|
2022-04-04 11:40:18 +02:00
|
|
|
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
2022-04-08 02:17:02 +02:00
|
|
|
|
2022-05-09 19:18:24 +02:00
|
|
|
# rerender map
|
2022-04-23 19:14:23 +02:00
|
|
|
map.render_tiles(self.roadTiles, self.screen, self.camera)
|
2022-04-24 19:05:48 +02:00
|
|
|
map.render_tiles(self.wallTiles, self.screen, self.camera, self.debug_mode)
|
2022-05-10 01:05:34 +02:00
|
|
|
map.render_tiles(self.trashbinTiles, self.screen, self.camera)
|
2022-04-23 19:14:23 +02:00
|
|
|
|
2022-05-09 19:18:24 +02:00
|
|
|
# rerender additional sprites
|
2022-04-23 19:14:23 +02:00
|
|
|
for sprite in self.agentSprites:
|
2022-04-04 11:40:18 +02:00
|
|
|
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
2022-04-24 19:05:48 +02:00
|
|
|
if self.debug_mode:
|
2022-04-04 11:40:18 +02:00
|
|
|
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
2022-05-09 19:18:24 +02:00
|
|
|
|
|
|
|
# finally update screen
|
2022-04-04 11:40:18 +02:00
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
def events(self):
|
|
|
|
for event in pg.event.get():
|
|
|
|
if event.type == pg.QUIT:
|
|
|
|
self.quit()
|
|
|
|
if event.type == pg.KEYDOWN:
|
|
|
|
if event.key == pg.K_ESCAPE:
|
|
|
|
self.quit()
|
|
|
|
if event.key == pg.K_h:
|
2022-04-24 19:05:48 +02:00
|
|
|
self.debug_mode = not self.debug_mode
|
2022-04-23 19:14:23 +02:00
|
|
|
if event.type == pg.MOUSEBUTTONUP:
|
|
|
|
pos = pg.mouse.get_pos()
|
2022-04-28 21:28:03 +02:00
|
|
|
offset_x, offset_y = self.camera.offset()
|
|
|
|
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
2022-05-09 19:18:24 +02:00
|
|
|
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE),
|
|
|
|
math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(),
|
|
|
|
clicked_coords[0], clicked_coords[1], self.mapArray)
|
2022-05-13 08:31:40 +02:00
|
|
|
# print(actions)
|
|
|
|
|
2022-04-29 11:01:16 +02:00
|
|
|
if (actions != None):
|
2022-05-13 08:31:40 +02:00
|
|
|
self.t.startAiController(actions)
|
2022-03-09 21:27:57 +01:00
|
|
|
|
2022-05-13 08:31:40 +02:00
|
|
|
|
2022-05-09 19:18:24 +02:00
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
# create the game object
|
2022-04-27 21:49:17 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
g = Game()
|
2022-05-13 08:31:40 +02:00
|
|
|
|
2022-04-04 11:40:18 +02:00
|
|
|
g.run()
|
2022-04-27 21:49:17 +02:00
|
|
|
g.show_go_screen()
|