Compare commits

...

1 Commits

Author SHA1 Message Date
d8ad5b7fb0 random map generation 2023-06-14 20:05:06 +02:00
10 changed files with 98 additions and 22 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

52
genetic.py Normal file
View File

@ -0,0 +1,52 @@
# wygenerowanie populacji polozenia mobow na mapie - Archer, Infantry, Flower, Rocks, Grass, Blank
# Fitness
# Crossover
# Mutation
import random
import pygame
TILE_SIZE = 64
TYPES = ['A', 'I', "F", 'R', 'G', 'B']
MAP_TILES_LIST = list()
population = list()
max_population = 168 - 1
population_size = 50
class Genetic():
def generate_population():
if population_size > 168:
print("GENETIC: Podana populacja przekracza limit miejsc na mapie")
pygame.quit()
for i in range(155):
MAP_TILES_LIST.append(i)
random.shuffle(MAP_TILES_LIST)
if 14 in MAP_TILES_LIST:
MAP_TILES_LIST.remove(14)
if 131 in MAP_TILES_LIST:
MAP_TILES_LIST.remove(131)
for i in range(population_size):
tile = MAP_TILES_LIST.pop()
type = random.choice(TYPES)
object = (tile,type)
population.append(object)
if type == 'A':
TYPES.remove('A')
if type == 'I':
TYPES.remove('I')
print(population)
return population
def get_cell_x_y_cord(x,y):
cell_x = x // TILE_SIZE
cell_y = y // TILE_SIZE
return cell_x, cell_y

66
main.py
View File

@ -7,6 +7,7 @@ from mobs import *
from bfs import * from bfs import *
from nn import * from nn import *
from astar import * from astar import *
from genetic import *
class Game: class Game:
@ -29,6 +30,7 @@ class Game:
self.bfs = Bfs(self) self.bfs = Bfs(self)
self.nn = NeuralN() self.nn = NeuralN()
self.astar = Astar(self) self.astar = Astar(self)
self.genetic = Genetic()
self.cell_costs = [[1 for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)] 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)] self.obstacles = [[False for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)]
@ -49,27 +51,41 @@ class Game:
self.little_rock_sprites = pygame.sprite.LayeredUpdates() self.little_rock_sprites = pygame.sprite.LayeredUpdates()
self.agent = Agent(self,1,1) self.agent = Agent(self,1,1)
self.archer_ork = Archer_ork(self,10,10)
self.obstacles[10][10] = True
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
self.infantry_ork = Infantry_ork(self,10,4)
self.obstacles[10][4] = True
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
self.sauron = Sauron(self, 1, 10) self.sauron = Sauron(self, 1, 10)
self.obstacles[1][10] = True self.obstacles[1][10] = True
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y)) self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
self.flower = Health_flower(self, 8,2)
for y in range (2,5):
for x in range (2):
self.grass = Grass(self,x,y)
self.cell_costs[x][y] = 5
for y in range(5): self.list_object = list()
self.rock = Rocks(self,3,y) self.list_object = Genetic.generate_population()
self.obstacles[3][y] = True 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)) 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): def update(self):
@ -92,6 +108,14 @@ class Game:
mouse_presses = pygame.mouse.get_pressed() mouse_presses = pygame.mouse.get_pressed()
if mouse_presses[0]: 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 x = self.sauron.x
y = self.sauron.y y = self.sauron.y
goal = x//TILE_SIZE,y//TILE_SIZE goal = x//TILE_SIZE,y//TILE_SIZE
@ -100,8 +124,8 @@ class Game:
prediction = "SAURON" prediction = "SAURON"
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze, program się będzie zawieszać 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: if prediction == "SAURON" and self.agent.level < 3:
x = self.archer_ork.x x = self.archer_ork.x,
y = self.archer_ork.y y = self.archer_ork.y,
goal = x//TILE_SIZE,y//TILE_SIZE goal = x//TILE_SIZE,y//TILE_SIZE
mob_image = self.archer_ork.image_path mob_image = self.archer_ork.image_path
prediction = self.prediction_road(x,y,mob_image) prediction = self.prediction_road(x,y,mob_image)
@ -111,7 +135,7 @@ class Game:
self.move_agent(self.astar.a_star(goal)) self.move_agent(self.astar.a_star(goal))
elif prediction == "ORK_INFANTRY": elif prediction == "ORK_INFANTRY":
self.obstacles[10][4] = False self.obstacles[i_cell_x][i_cell_y] = False
self.move_agent(self.astar.a_star(goal)) self.move_agent(self.astar.a_star(goal))
if self.agent.current_health < self.agent.max_health: if self.agent.current_health < self.agent.max_health:
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE) goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
@ -123,13 +147,13 @@ class Game:
prediction = self.prediction_road(x,y,mob_image) prediction = self.prediction_road(x,y,mob_image)
prediction = "SAURON" prediction = "SAURON"
elif prediction == "ORK_ARCHER": elif prediction == "ORK_ARCHER":
self.obstacles[10][10] = False self.obstacles[a_cell_x][a_cell_y] = False
self.move_agent(self.astar.a_star(goal)) self.move_agent(self.astar.a_star(goal))
if self.agent.current_health < self.agent.max_health: if self.agent.current_health < self.agent.max_health:
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE) goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
self.move_agent(self.astar.a_star(goal)) self.move_agent(self.astar.a_star(goal))
x = self.infantry_ork.x x = self.infantry_ork.x,
y = self.infantry_ork.y y = self.infantry_ork.y,
goal = x//TILE_SIZE,y//TILE_SIZE goal = x//TILE_SIZE,y//TILE_SIZE
mob_image = self.infantry_ork.image_path mob_image = self.infantry_ork.image_path
prediction = self.prediction_road(x,y,mob_image) prediction = self.prediction_road(x,y,mob_image)