2021-03-14 13:42:33 +01:00
|
|
|
import pygame as pg
|
|
|
|
import sys
|
2021-04-10 13:48:55 +02:00
|
|
|
import ctypes
|
2021-04-10 12:44:54 +02:00
|
|
|
from os import path
|
2021-05-15 15:28:45 +02:00
|
|
|
|
|
|
|
from matplotlib import pyplot
|
|
|
|
from sklearn import datasets
|
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
|
|
from sklearn import tree
|
|
|
|
|
2021-04-10 22:23:34 +02:00
|
|
|
from maze import *
|
2021-04-10 12:44:54 +02:00
|
|
|
from grid import *
|
2021-03-14 13:42:33 +01:00
|
|
|
from settings import *
|
|
|
|
from sprites import *
|
2021-04-14 00:22:27 +02:00
|
|
|
from graphsearch import *
|
2021-04-24 01:44:57 +02:00
|
|
|
from astar2 import *
|
2021-05-15 21:04:51 +02:00
|
|
|
from pizza import *
|
|
|
|
from learning import *
|
2021-05-31 02:28:13 +02:00
|
|
|
from nn import *
|
2021-06-19 23:55:47 +02:00
|
|
|
from genetic import *
|
|
|
|
#from genetic3 import *
|
2021-05-31 02:28:13 +02:00
|
|
|
#from csv_writer import *
|
2021-04-10 23:30:03 +02:00
|
|
|
|
|
|
|
|
2021-05-18 14:32:53 +02:00
|
|
|
|
2021-03-14 13:42:33 +01:00
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
|
|
|
pg.init()
|
|
|
|
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
pg.display.set_caption(TITLE)
|
|
|
|
self.clock = pg.time.Clock()
|
|
|
|
pg.key.set_repeat(500, 100)
|
|
|
|
self.load_data()
|
2021-04-10 23:30:03 +02:00
|
|
|
self.wentyl_bezpieczenstwa = 0
|
2021-05-15 21:04:51 +02:00
|
|
|
#ExampleGenerator.generate(ExampleGenerator)
|
2021-03-14 13:42:33 +01:00
|
|
|
|
|
|
|
def load_data(self):
|
2021-04-10 12:44:54 +02:00
|
|
|
game_folder = path.dirname(__file__)
|
|
|
|
self.map_data = []
|
2021-05-15 21:04:51 +02:00
|
|
|
with open(path.join(game_folder, 'main_map.txt'), 'rt') as f:
|
2021-04-10 12:44:54 +02:00
|
|
|
for line in f:
|
|
|
|
self.map_data.append(line)
|
2021-03-14 13:42:33 +01:00
|
|
|
|
|
|
|
def new(self):
|
|
|
|
# initialize all variables and do all the setup for a new game
|
2021-05-15 21:04:51 +02:00
|
|
|
#glebokosc rozmiar masa moc szkodliwosc zabawka stan teledysk
|
2021-05-19 16:36:06 +02:00
|
|
|
# x0 = glebokosc x1 = rozmiar x2 = masa x3 = moc x4 = szkodliwosc x5 = zabawka x6 = stan x7 = teledysk
|
|
|
|
# oflagowac, zdetonowac, sprzedac na allegro, sprzedac na czarnym rynku, obejrzec
|
2021-03-14 13:42:33 +01:00
|
|
|
self.all_sprites = pg.sprite.Group()
|
|
|
|
self.walls = pg.sprite.Group()
|
2021-04-10 13:48:55 +02:00
|
|
|
self.mines = pg.sprite.Group()
|
2021-04-24 01:44:57 +02:00
|
|
|
self.puddles = pg.sprite.Group()
|
2021-04-10 12:44:54 +02:00
|
|
|
for row, tiles in enumerate(self.map_data):
|
|
|
|
for col, tile in enumerate(tiles):
|
|
|
|
if tile == '2':
|
|
|
|
Mine(self, col, row)
|
2021-05-19 16:36:06 +02:00
|
|
|
Mine.set_parameters(Mine,18,32,6,7,0,1,0,0)
|
2021-04-10 12:44:54 +02:00
|
|
|
if tile == '3':
|
|
|
|
Bomb(self, col, row)
|
|
|
|
if tile == '4':
|
|
|
|
Grenade(self, col, row)
|
2021-04-11 12:02:52 +02:00
|
|
|
if tile == "#":
|
|
|
|
Wall(self, col, row)
|
2021-04-24 01:44:57 +02:00
|
|
|
if tile == 'p':
|
|
|
|
Puddle(self, col, row)
|
2021-04-14 03:55:35 +02:00
|
|
|
if tile == '>':
|
2021-04-14 15:30:36 +02:00
|
|
|
self.player = Player(self, col, row, Direction.Right.name)
|
2021-04-14 03:55:35 +02:00
|
|
|
if tile == '^':
|
2021-04-14 15:30:36 +02:00
|
|
|
self.player = Player(self, col, row, Direction.Up.name)
|
2021-04-14 03:55:35 +02:00
|
|
|
if tile == '<':
|
2021-04-14 15:30:36 +02:00
|
|
|
self.player = Player(self, col, row, Direction.Left.name)
|
2021-04-14 03:55:35 +02:00
|
|
|
if tile == 'v':
|
2021-04-24 01:44:57 +02:00
|
|
|
self.player = Player(self, col, row, Direction.Down.name)
|
2021-03-14 13:42:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# game loop - set self.playing = False to end the game
|
|
|
|
self.playing = True
|
|
|
|
while self.playing:
|
|
|
|
self.events()
|
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
pg.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
# update portion of the game loop
|
|
|
|
self.all_sprites.update()
|
|
|
|
|
|
|
|
def draw_grid(self):
|
|
|
|
for x in range(0, WIDTH, TILESIZE):
|
|
|
|
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
|
|
|
|
for y in range(0, HEIGHT, TILESIZE):
|
|
|
|
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
|
|
|
|
|
|
|
|
def draw(self):
|
|
|
|
self.screen.fill(BGCOLOR)
|
|
|
|
self.draw_grid()
|
|
|
|
self.all_sprites.draw(self.screen)
|
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
def events(self):
|
|
|
|
# catch all events here
|
2021-04-10 23:30:03 +02:00
|
|
|
|
2021-03-14 13:42:33 +01:00
|
|
|
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_LEFT:
|
2021-04-14 15:30:36 +02:00
|
|
|
self.player.move(dx=0, move='Left')
|
2021-03-14 13:42:33 +01:00
|
|
|
if event.key == pg.K_RIGHT:
|
2021-04-14 15:30:36 +02:00
|
|
|
self.player.move(dx=0, move='Right')
|
2021-03-14 13:42:33 +01:00
|
|
|
if event.key == pg.K_UP:
|
2021-04-14 15:30:36 +02:00
|
|
|
self.player.move(dy=0, move='Forward')
|
2021-04-10 23:30:03 +02:00
|
|
|
if event.key == pg.K_F2 and self.wentyl_bezpieczenstwa == 0:
|
|
|
|
|
2021-04-10 22:23:34 +02:00
|
|
|
self.player.maze.run()
|
|
|
|
self.player.parse_maze_moves()
|
|
|
|
self.i_like_to_move_it()
|
2021-04-10 23:30:03 +02:00
|
|
|
self.wentyl_bezpieczenstwa = 1
|
2021-05-31 13:37:15 +02:00
|
|
|
if event.key == pg.K_F3 and self.wentyl_bezpieczenstwa == 0:
|
2021-05-15 21:04:51 +02:00
|
|
|
pg.event.clear()
|
2021-04-14 15:30:36 +02:00
|
|
|
player_moves = BFS.run()
|
|
|
|
self.graph_move(player_moves)
|
2021-05-31 13:37:15 +02:00
|
|
|
self.wentyl_bezpieczenstwa = 1
|
2021-04-24 01:44:57 +02:00
|
|
|
if event.key == pg.K_F4 and self.wentyl_bezpieczenstwa == 0:
|
2021-05-15 21:04:51 +02:00
|
|
|
pg.event.clear()
|
2021-04-25 23:32:06 +02:00
|
|
|
#print("test1")
|
2021-04-24 01:44:57 +02:00
|
|
|
agent = SweeperAgent()
|
2021-04-25 23:32:06 +02:00
|
|
|
player_moves = SweeperAgent.run(agent)
|
|
|
|
self.graph_move(player_moves)
|
|
|
|
self.wentyl_bezpieczenstwa = 1
|
2021-05-15 21:04:51 +02:00
|
|
|
|
2021-04-24 01:44:57 +02:00
|
|
|
# Test.run()
|
2021-05-15 15:28:45 +02:00
|
|
|
if event.key == pg.K_F5:
|
2021-05-18 14:32:53 +02:00
|
|
|
self.player.decision_tree_learning()
|
|
|
|
#print("lol xD")
|
2021-05-15 21:04:51 +02:00
|
|
|
pg.event.clear()
|
2021-05-29 18:51:32 +02:00
|
|
|
if event.key == pg.K_F6 and self.wentyl_bezpieczenstwa == 0:
|
2021-05-31 02:28:13 +02:00
|
|
|
#LabelSetter.set_labels()
|
|
|
|
self.player.ai_mode = "tree"
|
|
|
|
agent = SweeperAgent()
|
|
|
|
player_moves = SweeperAgent.run(agent)
|
|
|
|
self.graph_move(player_moves)
|
2021-05-29 18:51:32 +02:00
|
|
|
self.wentyl_bezpieczenstwa = 1
|
2021-05-31 02:28:13 +02:00
|
|
|
if event.key == pg.K_F7 and self.wentyl_bezpieczenstwa == 0:
|
|
|
|
#net_runner = NetRunner()
|
|
|
|
#net_runner.prepare_data()
|
|
|
|
self.player.ai_mode = "neural"
|
|
|
|
agent = SweeperAgent()
|
|
|
|
player_moves = SweeperAgent.run(agent)
|
|
|
|
self.graph_move(player_moves)
|
|
|
|
self.wentyl_bezpieczenstwa = 1
|
2021-06-19 23:55:47 +02:00
|
|
|
if event.key == pg.K_F8 and self.wentyl_bezpieczenstwa == 0:
|
|
|
|
#print("pressed")
|
|
|
|
genetic_runner = Genetic()
|
|
|
|
genetic_runner.run()
|
|
|
|
self.wentyl_bezpieczenstwa = 1
|
|
|
|
if event.key == pg.K_F9 and self.wentyl_bezpieczenstwa == 0:
|
|
|
|
#genetic_lib_runner = GeneticalWithLib()
|
|
|
|
#genetic_lib_runner.run()
|
|
|
|
self.wentyl_bezpieczenstwa = 1
|
|
|
|
|
2021-05-31 02:28:13 +02:00
|
|
|
|
|
|
|
|
2021-05-29 18:51:32 +02:00
|
|
|
|
|
|
|
|
2021-05-15 21:04:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-15 15:28:45 +02:00
|
|
|
|
2021-04-24 01:44:57 +02:00
|
|
|
|
2021-04-14 15:30:36 +02:00
|
|
|
|
|
|
|
def graph_move(self, moves):
|
|
|
|
for i in moves:
|
|
|
|
if i == 'Right':
|
|
|
|
self.player.move(dx=0, move='Right')
|
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
|
|
|
if i == 'Left':
|
|
|
|
self.player.move(dx=0, move='Left')
|
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
|
|
|
if i == 'Forward':
|
|
|
|
self.player.move(dx=0, move='Forward')
|
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-14 00:22:27 +02:00
|
|
|
|
2021-04-10 22:23:34 +02:00
|
|
|
|
|
|
|
def i_like_to_move_it(self):
|
|
|
|
for i in self.player.moves:
|
|
|
|
if i == 'Right':
|
|
|
|
self.player.move(dx=1, direction='Right')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
if i == 'Turn Right':
|
|
|
|
self.player.move(0, 0, direction='Right')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
if i == 'Left':
|
|
|
|
self.player.move(dx=-1, direction='Left')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
if i == 'Turn Left':
|
|
|
|
self.player.move(0, 0, direction='Left')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
if i == 'Down':
|
|
|
|
self.player.move(dy=1, direction='Down')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
if i == 'Turn Down':
|
|
|
|
self.player.move(0, 0, direction='Down')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
if i == 'Up':
|
|
|
|
self.player.move(dy=-1, direction='Up')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
if i == 'Turn Up':
|
|
|
|
self.player.move(0, 0, direction='Up')
|
2021-04-10 23:30:03 +02:00
|
|
|
self.update()
|
|
|
|
self.draw()
|
|
|
|
pg.time.delay(250)
|
2021-04-10 22:23:34 +02:00
|
|
|
|
2021-03-14 13:42:33 +01:00
|
|
|
|
|
|
|
def show_start_screen(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def show_go_screen(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# create the game object
|
|
|
|
g = Game()
|
|
|
|
g.show_start_screen()
|
2021-04-10 22:23:34 +02:00
|
|
|
|
|
|
|
#m = Maze()
|
|
|
|
#m.run()
|
|
|
|
|
2021-03-14 13:42:33 +01:00
|
|
|
while True:
|
|
|
|
g.new()
|
|
|
|
g.run()
|
2021-04-10 23:30:03 +02:00
|
|
|
g.show_go_screen()
|