2021-03-14 13:42:33 +01:00
|
|
|
import pygame as pg
|
2021-04-08 16:29:37 +02:00
|
|
|
import enum
|
2021-04-10 13:48:55 +02:00
|
|
|
import ctypes
|
2021-04-10 22:23:34 +02:00
|
|
|
import ast
|
2021-05-31 02:28:13 +02:00
|
|
|
import time
|
2021-03-14 13:42:33 +01:00
|
|
|
from settings import *
|
2021-04-10 22:23:34 +02:00
|
|
|
from maze import *
|
2021-05-15 21:04:51 +02:00
|
|
|
from learning import *
|
2021-05-31 02:28:13 +02:00
|
|
|
from nn import *
|
2021-03-14 13:42:33 +01:00
|
|
|
|
2021-05-18 14:32:53 +02:00
|
|
|
|
2021-03-14 13:42:33 +01:00
|
|
|
class Player(pg.sprite.Sprite):
|
2021-04-08 16:29:37 +02:00
|
|
|
def __init__(self, game, x, y, direction = 'Right'):
|
2021-03-14 13:42:33 +01:00
|
|
|
self.groups = game.all_sprites
|
|
|
|
pg.sprite.Sprite.__init__(self, self.groups)
|
|
|
|
self.game = game
|
2021-03-27 19:34:41 +01:00
|
|
|
#self.image = pg.Surface((TILESIZE, TILESIZE))
|
2021-05-15 21:04:51 +02:00
|
|
|
self.image = pg.image.load('images/robot3.bmp')
|
|
|
|
self.baseImage = pg.image.load('images/robot3.bmp')
|
2021-03-27 19:34:41 +01:00
|
|
|
#self.image.fill(YELLOW)
|
|
|
|
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
|
2021-04-08 16:29:37 +02:00
|
|
|
self.baseImage = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
|
2021-03-14 13:42:33 +01:00
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
2021-04-08 16:29:37 +02:00
|
|
|
self.direction = direction
|
2021-04-10 22:23:34 +02:00
|
|
|
self.maze = Maze()
|
|
|
|
self.moves = ''
|
2021-05-15 21:04:51 +02:00
|
|
|
self.my_learning = Learning()
|
2021-05-31 02:28:13 +02:00
|
|
|
self.ai_mode = "none"
|
2021-05-18 14:32:53 +02:00
|
|
|
#self.decision_tree_learning()
|
2021-03-14 13:42:33 +01:00
|
|
|
|
2021-04-14 15:30:36 +02:00
|
|
|
def set_direction(self, direction):
|
|
|
|
self.direction = direction
|
|
|
|
|
|
|
|
def move(self, dx=0, dy=0, move = ''):
|
|
|
|
if move == Moves.Right.name:
|
2021-04-08 16:29:37 +02:00
|
|
|
if self.direction == Direction.Right.name:
|
2021-04-14 15:30:36 +02:00
|
|
|
self.direction = Direction.Down.name
|
|
|
|
elif self.direction == Direction.Down.name:
|
|
|
|
self.direction = Direction.Left.name
|
|
|
|
elif self.direction == Direction.Left.name:
|
|
|
|
self.direction = Direction.Up.name
|
|
|
|
elif self.direction == Direction.Up.name:
|
|
|
|
self.direction = Direction.Right.name
|
|
|
|
elif move == Moves.Left.name:
|
|
|
|
if self.direction == Direction.Right.name:
|
|
|
|
self.direction = Direction.Up.name
|
|
|
|
elif self.direction == Direction.Down.name:
|
|
|
|
self.direction = Direction.Right.name
|
|
|
|
elif self.direction == Direction.Left.name:
|
|
|
|
self.direction = Direction.Down.name
|
|
|
|
elif self.direction == Direction.Up.name:
|
|
|
|
self.direction = Direction.Left.name
|
|
|
|
elif move == Moves.Forward.name:
|
|
|
|
if self.direction == Direction.Right.name:
|
|
|
|
if self.check_border(1):
|
|
|
|
if not self.collide_with_walls(1):
|
|
|
|
if self.collide_with_mines(1):
|
|
|
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
|
|
|
print("Mine Ahead!")
|
|
|
|
self.x += 1
|
|
|
|
else:
|
|
|
|
self.x += 1
|
2021-04-08 16:29:37 +02:00
|
|
|
|
|
|
|
if self.direction == Direction.Up.name:
|
2021-04-14 15:30:36 +02:00
|
|
|
if self.check_border(0, -1):
|
|
|
|
if not self.collide_with_walls(0, -1):
|
|
|
|
if self.collide_with_mines(0, -1):
|
|
|
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
|
|
|
print("Mine Ahead!")
|
|
|
|
self.y += -1
|
|
|
|
else:
|
|
|
|
self.y += -1
|
2021-04-10 13:48:55 +02:00
|
|
|
|
2021-04-08 16:29:37 +02:00
|
|
|
if self.direction == Direction.Down.name:
|
2021-04-14 15:30:36 +02:00
|
|
|
if self.check_border(0, 1):
|
|
|
|
if not self.collide_with_walls(0, 1):
|
|
|
|
if self.collide_with_mines(0, 1):
|
|
|
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
|
|
|
print("Mine Ahead!")
|
|
|
|
self.y += 1
|
|
|
|
else:
|
|
|
|
self.y += 1
|
2021-04-08 16:29:37 +02:00
|
|
|
|
2021-04-14 15:30:36 +02:00
|
|
|
if self.direction == Direction.Left.name:
|
|
|
|
if self.check_border(-1):
|
|
|
|
if not self.collide_with_walls(-1):
|
|
|
|
if self.collide_with_mines(-1):
|
|
|
|
#ctypes.windll.user32.MessageBoxW(0, "Mine Ahead!", "Warning", 1)
|
|
|
|
print("Mine Ahead!")
|
|
|
|
self.x += -1
|
|
|
|
else:
|
2021-05-15 21:04:51 +02:00
|
|
|
self.x += -1
|
2021-04-14 15:30:36 +02:00
|
|
|
print("I move: " + str(self.direction))
|
2021-04-08 16:29:37 +02:00
|
|
|
|
2021-04-14 15:30:36 +02:00
|
|
|
print("My direction is: " + str(self.direction))
|
2021-05-31 02:28:13 +02:00
|
|
|
|
|
|
|
if str(self.ai_mode) == "tree":
|
|
|
|
self.check_bomb()
|
|
|
|
if str(self.ai_mode) == "neural":
|
|
|
|
self.check_bomb_neural()
|
2021-05-18 14:32:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-31 02:28:13 +02:00
|
|
|
def set_ai_mode(self, mode):
|
|
|
|
self.ai_mode = mode
|
2021-04-10 12:44:54 +02:00
|
|
|
|
|
|
|
def check_border(self, dx=0, dy=0):
|
|
|
|
if (self.x + dx) < 0 or (self.y + dy) < 0 or (self.x + dx) >= MAP_SIZE or (self.y + dy) >= MAP_SIZE :
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-04-10 13:48:55 +02:00
|
|
|
def collide_with_mines(self, dx=0, dy=0):
|
|
|
|
for mine in self.game.mines:
|
|
|
|
if mine.x == self.x + dx and mine.y == self.y + dy:
|
|
|
|
return True
|
|
|
|
return False
|
2021-03-14 13:42:33 +01:00
|
|
|
|
2021-04-11 12:02:52 +02:00
|
|
|
def collide_with_walls(self, dx=0, dy=0):
|
|
|
|
for wall in self.game.walls:
|
|
|
|
if wall.x == self.x + dx and wall.y == self.y + dy:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-04-10 22:23:34 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
right = 1
|
|
|
|
left = -1
|
|
|
|
if self.self.moves[n+1] != 'R':
|
|
|
|
if self.moves[n+1] == 'U':
|
|
|
|
module.insert(n, 'Turn Up')
|
|
|
|
if self.moves[n+1] == 'L':
|
|
|
|
module.insert(n, 'Turn Left')
|
|
|
|
if self.moves[n+1] == 'D':
|
|
|
|
module.insert(n, 'Turn Down')
|
|
|
|
"""
|
|
|
|
|
|
|
|
def parse_maze_moves(self):
|
|
|
|
self.moves = [char for char in self.maze.moves]
|
2021-04-11 12:02:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
if self.moves[0] != 'R':
|
|
|
|
if self.moves[0] == 'D':
|
|
|
|
self.moves.insert(0, 'Turn Down')
|
|
|
|
if self.moves[0] == 'U':
|
|
|
|
self.moves.insert(0, 'Turn Up')
|
|
|
|
if self.moves[0] == 'L':
|
|
|
|
self.moves.insert(0, 'Turn Left')
|
|
|
|
|
|
|
|
for n, i in enumerate(self.moves):
|
|
|
|
if i == 'R':
|
|
|
|
self.moves[n] = 'Right'
|
|
|
|
if n != len(self.moves)-1:
|
|
|
|
if i != self.moves[n+1]:
|
|
|
|
if self.moves[n+1] == 'D':
|
|
|
|
self.moves.insert(n+1, 'Turn Down')
|
|
|
|
if self.moves[n+1] == 'U':
|
|
|
|
self.moves.insert(n+1, 'Turn Up')
|
|
|
|
if self.moves[n+1] == 'L':
|
|
|
|
self.moves.insert(n+1, 'Turn Left')
|
|
|
|
else:
|
|
|
|
self.move(dx=-1, direction='Right')
|
|
|
|
if i == 'L':
|
|
|
|
self.moves[n] = 'Left'
|
|
|
|
if n != len(self.moves)-1:
|
|
|
|
if i != self.moves[n+1]:
|
|
|
|
if self.moves[n+1] == 'D':
|
|
|
|
self.moves.insert(n+1, 'Turn Down')
|
|
|
|
if self.moves[n+1] == 'U':
|
|
|
|
self.moves.insert(n+1, 'Turn Up')
|
|
|
|
if self.moves[n+1] == 'R':
|
|
|
|
self.moves.insert(n+1, 'Turn Right')
|
|
|
|
if i == 'D':
|
|
|
|
self.moves[n] = 'Down'
|
|
|
|
if n != len(self.moves)-1:
|
|
|
|
if i != self.moves[n+1]:
|
|
|
|
if self.moves[n+1] == 'R':
|
|
|
|
self.moves.insert(n+1, 'Turn Right')
|
|
|
|
if self.moves[n+1] == 'U':
|
|
|
|
self.moves.insert(n+1, 'Turn Up')
|
|
|
|
if self.moves[n+1] == 'L':
|
|
|
|
self.moves.insert(n+1, 'Turn Left')
|
|
|
|
if i == 'U':
|
|
|
|
self.moves[n] = 'Up'
|
|
|
|
if n != len(self.moves)-1:
|
|
|
|
if i != self.moves[n+1]:
|
|
|
|
if self.moves[n+1] == 'D':
|
|
|
|
self.moves.insert(n+1, 'Turn Down')
|
|
|
|
if self.moves[n+1] == 'R':
|
|
|
|
self.moves.insert(n+1, 'Turn Right')
|
|
|
|
if self.moves[n+1] == 'L':
|
|
|
|
self.moves.insert(n+1, 'Turn Left')
|
|
|
|
|
2021-04-10 22:23:34 +02:00
|
|
|
print(self.moves)
|
|
|
|
|
2021-05-15 21:04:51 +02:00
|
|
|
def decision_tree_learning(self):
|
|
|
|
self.my_learning.load_data()
|
|
|
|
self.my_learning.learn()
|
|
|
|
self.my_learning.draw_tree()
|
2021-05-18 14:32:53 +02:00
|
|
|
print("new decision tree created")
|
|
|
|
print("restart to use saved decision tree")
|
2021-05-15 21:04:51 +02:00
|
|
|
#my_learning.predict()
|
2021-05-18 14:32:53 +02:00
|
|
|
|
|
|
|
|
2021-05-15 21:04:51 +02:00
|
|
|
|
|
|
|
""" sprawdzenie danych miny """
|
|
|
|
def check_bomb(self):
|
|
|
|
if self.check_if_on_mine():
|
|
|
|
current_mine = self.get_my_mine_object()
|
|
|
|
mine_params = current_mine.get_parameters()
|
2021-05-18 14:32:53 +02:00
|
|
|
self.my_learning.predict_on_saved_tree(mine_params)
|
2021-05-15 21:04:51 +02:00
|
|
|
return
|
2021-05-31 02:28:13 +02:00
|
|
|
|
|
|
|
def check_bomb_neural(self):
|
|
|
|
if self.check_if_on_mine():
|
|
|
|
current_mine = self.get_my_mine_object()
|
|
|
|
mine_params = current_mine.get_parameters()
|
|
|
|
self.call_net_runner()
|
|
|
|
return
|
|
|
|
|
|
|
|
def call_net_runner(self):
|
|
|
|
net_runner = NetRunner()
|
|
|
|
net_runner.prepare_data()
|
|
|
|
|
|
|
|
|
2021-05-15 21:04:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_if_on_mine(self):
|
|
|
|
for mine in self.game.mines:
|
|
|
|
if mine.x == self.x and mine.y == self.y:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_my_mine_object(self):
|
|
|
|
if self.check_if_on_mine():
|
|
|
|
for mine in self.game.mines:
|
|
|
|
if mine.x == self.x and mine.y == self.y:
|
|
|
|
return mine
|
2021-04-10 22:23:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-14 13:42:33 +01:00
|
|
|
def update(self):
|
2021-04-10 23:26:13 +02:00
|
|
|
#sleep(0.25)
|
2021-03-14 13:42:33 +01:00
|
|
|
self.rect.x = self.x * TILESIZE
|
|
|
|
self.rect.y = self.y * TILESIZE
|
|
|
|
|
2021-03-27 19:34:41 +01:00
|
|
|
|
2021-04-08 16:29:37 +02:00
|
|
|
class Direction(enum.Enum):
|
|
|
|
Up = 1;
|
|
|
|
Left = 2;
|
|
|
|
Down = 3;
|
|
|
|
Right = 4;
|
2021-03-27 19:34:41 +01:00
|
|
|
|
2021-04-14 15:30:36 +02:00
|
|
|
class Moves(enum.Enum):
|
|
|
|
Left = 1
|
|
|
|
Right = 2
|
|
|
|
Forward = 3
|
|
|
|
|
2021-04-10 12:44:54 +02:00
|
|
|
|
2021-03-27 19:34:41 +01:00
|
|
|
class Mine(pg.sprite.Sprite):
|
|
|
|
def __init__(self, game, x, y):
|
2021-04-10 13:48:55 +02:00
|
|
|
self.groups = game.all_sprites, game.mines
|
2021-03-27 19:34:41 +01:00
|
|
|
pg.sprite.Sprite.__init__(self, self.groups)
|
|
|
|
self.game = game
|
|
|
|
#self.image = pg.Surface((TILESIZE, TILESIZE))
|
|
|
|
self.image = pg.image.load('images/mine.bmp')
|
|
|
|
#self.image.fill(YELLOW)
|
|
|
|
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
2021-05-15 21:04:51 +02:00
|
|
|
|
|
|
|
def set_parameters(self, glebokosc, rozmiar, masa, moc, szkodliwosc, zabawka, teledysk, stan):
|
|
|
|
self.glebokosc = glebokosc
|
|
|
|
self.rozmiar = rozmiar
|
|
|
|
self.masa = masa
|
|
|
|
self.moc = moc
|
|
|
|
self.szkodliwosc = szkodliwosc
|
|
|
|
self.zabawka = zabawka
|
|
|
|
self.teledysk = teledysk
|
|
|
|
self.stan = stan
|
|
|
|
|
|
|
|
def get_parameters(self):
|
|
|
|
param_array = [self.glebokosc, self.rozmiar, self.masa, self.moc, self.szkodliwosc, self.zabawka, self.teledysk, self.stan]
|
|
|
|
return param_array
|
|
|
|
|
2021-03-27 19:34:41 +01:00
|
|
|
def update(self):
|
|
|
|
self.rect.x = self.x * TILESIZE
|
|
|
|
self.rect.y = self.y * TILESIZE
|
|
|
|
|
|
|
|
|
|
|
|
class Bomb(pg.sprite.Sprite):
|
|
|
|
def __init__(self, game, x, y):
|
2021-04-10 13:48:55 +02:00
|
|
|
self.groups = game.all_sprites, game.mines
|
2021-03-27 19:34:41 +01:00
|
|
|
pg.sprite.Sprite.__init__(self, self.groups)
|
|
|
|
self.game = game
|
|
|
|
#self.image = pg.Surface((TILESIZE, TILESIZE))
|
|
|
|
self.image = pg.image.load('images/bomb.bmp')
|
|
|
|
#self.image.fill(YELLOW)
|
|
|
|
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.rect.x = self.x * TILESIZE
|
|
|
|
self.rect.y = self.y * TILESIZE
|
|
|
|
|
|
|
|
|
|
|
|
class Grenade(pg.sprite.Sprite):
|
|
|
|
def __init__(self, game, x, y):
|
2021-04-10 13:48:55 +02:00
|
|
|
self.groups = game.all_sprites, game.mines
|
2021-03-27 19:34:41 +01:00
|
|
|
pg.sprite.Sprite.__init__(self, self.groups)
|
|
|
|
self.game = game
|
|
|
|
#self.image = pg.Surface((TILESIZE, TILESIZE))
|
|
|
|
self.image = pg.image.load('images/grenade.bmp')
|
|
|
|
#self.image.fill(YELLOW)
|
|
|
|
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.rect.x = self.x * TILESIZE
|
|
|
|
self.rect.y = self.y * TILESIZE
|
|
|
|
|
2021-04-11 12:02:52 +02:00
|
|
|
class Wall(pg.sprite.Sprite):
|
|
|
|
def __init__(self, game, x, y):
|
|
|
|
self.groups = game.all_sprites, game.walls
|
|
|
|
pg.sprite.Sprite.__init__(self, self.groups)
|
|
|
|
self.game = game
|
|
|
|
#self.image = pg.Surface((TILESIZE, TILESIZE))
|
|
|
|
self.image = pg.image.load('images/wall.bmp')
|
|
|
|
#self.image.fill(YELLOW)
|
|
|
|
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.rect.x = self.x * TILESIZE
|
|
|
|
self.rect.y = self.y * TILESIZE
|
|
|
|
|
2021-04-24 01:44:57 +02:00
|
|
|
class Puddle(pg.sprite.Sprite):
|
|
|
|
def __init__(self, game, x, y):
|
|
|
|
self.groups = game.all_sprites, game.puddles
|
|
|
|
pg.sprite.Sprite.__init__(self, self.groups)
|
|
|
|
self.game = game
|
|
|
|
self.image = pg.image.load('images/puddle.bmp')
|
|
|
|
self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.rect.x = self.x * TILESIZE
|
|
|
|
self.rect.y = self.y * TILESIZE
|
|
|
|
|
2021-03-27 19:34:41 +01:00
|
|
|
|