240 lines
10 KiB
Python
240 lines
10 KiB
Python
|
|
import pygame
|
|
from config import *
|
|
from agent import *
|
|
from map_add_ons import *
|
|
from mobs import *
|
|
from bfs import *
|
|
from nn import *
|
|
from astar import *
|
|
from genetic import *
|
|
|
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
|
pygame.init()
|
|
self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
|
|
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
self.running = True
|
|
self.clock = pygame.time.Clock()
|
|
|
|
self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg")
|
|
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
|
|
|
|
self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png")
|
|
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
|
|
|
|
pygame.display.set_caption('Gra-SI')
|
|
|
|
self.bfs = Bfs(self)
|
|
self.nn = NeuralN()
|
|
self.astar = Astar(self)
|
|
self.genetic = Genetic()
|
|
|
|
self.cell_costs = [[1 for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
|
self.obstacles = [[False for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
|
|
|
|
|
|
|
|
|
|
def new(self): # tworzy się nowa sesja grania
|
|
self.all_sprites = pygame.sprite.LayeredUpdates()
|
|
|
|
self.rock_sprites = pygame.sprite.LayeredUpdates()
|
|
self.grass_sprites = pygame.sprite.LayeredUpdates()
|
|
self.archer_orks = pygame.sprite.LayeredUpdates()
|
|
self.infantry_orks = pygame.sprite.LayeredUpdates()
|
|
|
|
self.sauronL = pygame.sprite.LayeredUpdates()
|
|
self.flowers = pygame.sprite.LayeredUpdates()
|
|
self.little_rock_sprites = pygame.sprite.LayeredUpdates()
|
|
|
|
self.agent = Agent(self,1,1)
|
|
self.sauron = Sauron(self, 1, 10)
|
|
self.obstacles[1][10] = True
|
|
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
|
|
|
|
|
|
self.list_object = list()
|
|
self.list_object = Genetic.generate_population()
|
|
l = len(self.list_object)
|
|
while l > 0:
|
|
obj = self.list_object.pop()
|
|
x,y = self.bfs.get_coordinates(obj[0])
|
|
cell_x, cell_y = Genetic.get_cell_x_y_cord(x,y)
|
|
l=l-1
|
|
if obj[1] == 'A':
|
|
self.archer_ork = Archer_ork(self,cell_x,cell_y)
|
|
self.obstacles[cell_x][cell_y] = True
|
|
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
|
if obj[1] == 'I':
|
|
self.infantry_ork = Infantry_ork(self,cell_x,cell_y)
|
|
self.obstacles[cell_x][cell_y] = True
|
|
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
|
if obj[1] == 'F':
|
|
self.flower = Health_flower(self,cell_x,cell_y)
|
|
if obj[1] == 'R':
|
|
self.rock = Rocks(self,cell_x,cell_y)
|
|
self.obstacles[cell_x][cell_y] = True
|
|
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
|
|
if obj[1] == 'G':
|
|
self.grass = Grass(self,cell_x,cell_y)
|
|
self.cell_costs[cell_x][cell_y] = 5
|
|
if obj[1] == 'B':
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
def update(self):
|
|
self.all_sprites.update()
|
|
|
|
|
|
def events(self):
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
self.running = False
|
|
pygame.quit()
|
|
if event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_SPACE:
|
|
self.goal_pos = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
|
self.move_agent(self.astar.a_star(self.goal_pos))
|
|
|
|
|
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
mouse_presses = pygame.mouse.get_pressed()
|
|
if mouse_presses[0]:
|
|
|
|
gx = self.archer_ork.x
|
|
gy = self.archer_ork.y
|
|
a_cell_x, a_cell_y = Genetic.get_cell_x_y_cord(gx,gy)
|
|
gx2 = self.infantry_ork.x
|
|
gy2 = self.infantry_ork.y
|
|
i_cell_x, i_cell_y = Genetic.get_cell_x_y_cord(gx2,gy2)
|
|
|
|
|
|
x = self.sauron.x
|
|
y = self.sauron.y
|
|
goal = x//TILE_SIZE,y//TILE_SIZE
|
|
mob_image = self.sauron.image_path
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
prediction = "SAURON"
|
|
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze, program się będzie zawieszać
|
|
if prediction == "SAURON" and self.agent.level < 3:
|
|
x = self.archer_ork.x,
|
|
y = self.archer_ork.y,
|
|
goal = x//TILE_SIZE,y//TILE_SIZE
|
|
mob_image = self.archer_ork.image_path
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
prediction = "ORK_ARCHER"
|
|
elif prediction == "SAURON" and self.agent.level >= 3:
|
|
self.obstacles[1][10] = False
|
|
self.move_agent(self.astar.a_star(goal))
|
|
|
|
elif prediction == "ORK_INFANTRY":
|
|
self.obstacles[i_cell_x][i_cell_y] = False
|
|
self.move_agent(self.astar.a_star(goal))
|
|
if self.agent.current_health < self.agent.max_health:
|
|
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
|
self.move_agent(self.astar.a_star(goal))
|
|
x = self.sauron.x
|
|
y = self.sauron.y
|
|
goal = x//TILE_SIZE,y//TILE_SIZE
|
|
mob_image = self.sauron.image_path
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
prediction = "SAURON"
|
|
elif prediction == "ORK_ARCHER":
|
|
self.obstacles[a_cell_x][a_cell_y] = False
|
|
self.move_agent(self.astar.a_star(goal))
|
|
if self.agent.current_health < self.agent.max_health:
|
|
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
|
self.move_agent(self.astar.a_star(goal))
|
|
x = self.infantry_ork.x,
|
|
y = self.infantry_ork.y,
|
|
goal = x//TILE_SIZE,y//TILE_SIZE
|
|
mob_image = self.infantry_ork.image_path
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
prediction = "ORK_INFANTRY"
|
|
|
|
|
|
|
|
|
|
def prediction_road(self,x,y,mob_image):
|
|
mob_goal_cell = (self.bfs.get_cell_number(x,y))
|
|
if self.bfs.get_up_cell(mob_goal_cell) == None:
|
|
goal_cell = self.bfs.get_down_cell(mob_goal_cell)
|
|
x,y = self.bfs.get_coordinates(goal_cell)
|
|
goal = x//TILE_SIZE,y//TILE_SIZE
|
|
self.move_agent(self.astar.a_star(goal))
|
|
prediction = self.nn.predict(mob_image)
|
|
else:
|
|
goal_cell = self.bfs.get_up_cell(mob_goal_cell)
|
|
x,y = self.bfs.get_coordinates(goal_cell)
|
|
goal = x//TILE_SIZE,y//TILE_SIZE
|
|
self.move_agent(self.astar.a_star(goal))
|
|
prediction = self.nn.predict(mob_image)
|
|
return prediction
|
|
|
|
def move_agent(self,path):
|
|
print("PATH:::::",path)
|
|
for cell_to_move in path:
|
|
x, y = self.bfs.get_coordinates(cell_to_move)
|
|
print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
|
if(self.bfs.get_cell_number(self.agent.rect.x,self.agent.rect.y)!=cell_to_move):
|
|
if x > self.agent.rect.x:
|
|
self.agent.direction = 0
|
|
elif y > self.agent.rect.y:
|
|
self.agent.direction = 1
|
|
elif x < self.agent.rect.x:
|
|
self.agent.direction = 2
|
|
elif y < self.agent.rect.y:
|
|
self.agent.direction = 3
|
|
if self.agent.direction==0:
|
|
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
|
self.agent.x_change += TILE_SIZE
|
|
elif self.agent.direction==1:
|
|
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
|
self.agent.y_change += TILE_SIZE
|
|
elif self.agent.direction==2:
|
|
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
|
self.agent.x_change -= TILE_SIZE
|
|
elif self.agent.direction==3:
|
|
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction])
|
|
self.agent.y_change -= TILE_SIZE
|
|
|
|
self.agent.rotate()
|
|
self.update()
|
|
self.map()
|
|
|
|
print("Polozenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y)
|
|
self.clock.tick(2)
|
|
|
|
def map(self): # tworzenie mapy
|
|
self.clock.tick(FRAMERATE)
|
|
for x in range(0, WIDTH, TILE_SIZE):
|
|
for y in range(0, 768, TILE_SIZE):
|
|
self.SCREEN.blit(self.BACKGROUND,(x,y))
|
|
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
|
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
|
|
self.flowers.draw(self.SCREEN)
|
|
self.all_sprites.draw(self.SCREEN)
|
|
self.rock_sprites.draw(self.SCREEN)
|
|
self.grass_sprites.draw(self.SCREEN)
|
|
self.SCREEN.blit(self.LVL_ICON, (340 ,780))
|
|
pygame.display.update()
|
|
|
|
def main(self):
|
|
self.events()
|
|
self.update()
|
|
self.map()
|
|
|
|
|
|
g = Game()
|
|
g.new()
|
|
|
|
while g.running:
|
|
g.main()
|