Dodanie generacji 60% szansa na Kombinacje 10 % na mutacje

This commit is contained in:
Volodymyr Kyiashko 2023-06-15 22:07:01 +02:00
parent e90f5b5d75
commit 3dd6b8a9ee

553
main.py
View File

@ -1,215 +1,338 @@
import pygame import pygame
from config import * from config import *
from agent import * from agent import *
from map_add_ons import * from map_add_ons import *
from mobs import * from mobs import *
from bfs import * from bfs import *
from nn import * from nn import *
from astar import * from astar import *
import math
import random
class Game:
def __init__(self): class Game:
pygame.init()
self.state =[-1,-1,-1,-1,-1,-1,-1,-1] def __init__(self):
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.init()
self.running = True self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
self.clock = pygame.time.Clock() self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
self.running = True
self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg") self.clock = pygame.time.Clock()
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg")
self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png") self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png")
pygame.display.set_caption('Gra-SI') self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
self.bfs = Bfs(self) pygame.display.set_caption('Gra-SI')
self.nn = NeuralN()
self.astar = Astar(self) self.bfs = Bfs(self)
self.nn = NeuralN()
self.cell_costs = [[1 for _ in range(TILE_SIZE)] for _ in range(TILE_SIZE)] self.astar = Astar(self)
self.obstacles = [[False 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)]
def new(self): # tworzy się nowa sesja grania
self.all_sprites = pygame.sprite.LayeredUpdates()
def new(self): # tworzy się nowa sesja grania
self.rock_sprites = pygame.sprite.LayeredUpdates() self.all_sprites = pygame.sprite.LayeredUpdates()
self.grass_sprites = pygame.sprite.LayeredUpdates()
self.archer_orks = pygame.sprite.LayeredUpdates() self.rock_sprites = pygame.sprite.LayeredUpdates()
self.infantry_orks = pygame.sprite.LayeredUpdates() self.grass_sprites = pygame.sprite.LayeredUpdates()
self.archer_orks = pygame.sprite.LayeredUpdates()
self.sauronL = pygame.sprite.LayeredUpdates() self.infantry_orks = pygame.sprite.LayeredUpdates()
self.flowers = pygame.sprite.LayeredUpdates()
self.little_rock_sprites = pygame.sprite.LayeredUpdates() self.sauronL = pygame.sprite.LayeredUpdates()
self.flowers = pygame.sprite.LayeredUpdates()
self.agent = Agent(self,1,1) self.little_rock_sprites = pygame.sprite.LayeredUpdates()
self.archer_ork = Archer_ork(self,10,10)
self.obstacles[10][10] = True #Agent,Archer_ork,infantry_ork,sauron,flower,grass x6, rocks x5 tablica 16 elementowa y=0-11 x=0-12 random.randint(x, y) = od x do y downolny int
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y)) self.allpositions=[]
self.infantry_ork = Infantry_ork(self,10,4) self.allpositionsSet=set()
self.obstacles[10][4] = True while(len(self.allpositionsSet)<100):
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y)) self.positions=[] #.append
self.positionsSet=set() #.add
self.sauron = Sauron(self, 1, 10) for x in range(16):
self.obstacles[1][10] = True while len(self.positionsSet)<16:
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y)) pos1=random.randint(0,12) #x
self.flower = Health_flower(self, 8,2) pos2=random.randint(0,11) #y
pom=(pos1,pos2)
for y in range (2,5): lenSetBefore=len(self.positionsSet)
for x in range (2): self.positionsSet.add(pom)
self.grass = Grass(self,x,y) lenSetAfter=len(self.positionsSet)
self.cell_costs[x][y] = 5 if(lenSetAfter>lenSetBefore):
self.positions.append(pom)
for y in range(5): AllPositionsSetB=len(self.allpositionsSet)
self.rock = Rocks(self,3,y) self.allpositionsSet.add(tuple(self.positions))
self.obstacles[3][y] = True AllPositionsSetA=len(self.allpositionsSet)
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y)) if(AllPositionsSetA>AllPositionsSetB):
self.positions.append([1000])
self.allpositions.append(self.positions)
def update(self): def sprawdz_powtorzenia(tablica):
self.all_sprites.update() wystapienia = set()
for element in tablica[:-1]:
if tuple(element) in wystapienia:
def events(self): return True # Powtórzenie znalezione
for event in pygame.event.get(): wystapienia.add(tuple(element))
if event.type == pygame.QUIT: return False # Brak powtórzeń
self.running = False def ocena_tablicy(dane):
pygame.quit() for x in range(100):
if event.type == pygame.KEYDOWN: if(sprawdz_powtorzenia(dane[x])):
if event.key == pygame.K_SPACE: dane[x][-1]=10000000
self.goal_pos = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE) else:
self.move_agent(self.astar.a_star(self.goal_pos)) x1,y1=dane[x][0]
x2,y2=dane[x][1]
x3,y3=dane[x][2]
x4,y4=dane[x][3]
if event.type == pygame.MOUSEBUTTONDOWN: r1=math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
mouse_presses = pygame.mouse.get_pressed() r2=math.sqrt((x2 - x3)**2 + (y2 - y3)**2)
if mouse_presses[0]: r3=math.sqrt((x3 - x4)**2 + (y3 - y4)**2)
r12=math.sqrt((x1 - x3)**2 + (y1 - y3)**2)
x = self.sauron.x r13=math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
y = self.sauron.y avg=(r1+r2+r3)/3
goal = x//TILE_SIZE,y//TILE_SIZE grade=abs(r1-avg)+abs(r2-avg)+abs(r3-avg)
mob_image = self.sauron.image_path if(r1<4):
prediction = self.prediction_road(x,y,mob_image) grade=grade+5
prediction = "SAURON" if(r12<4):
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze, program się będzie zawieszać grade=grade+5
if prediction == "SAURON" and self.agent.level < 3: if(r13<4):
x = self.archer_ork.x grade=grade+5
y = self.archer_ork.y grade=round(grade,2)*100
goal = x//TILE_SIZE,y//TILE_SIZE dane[x][-1]=grade
mob_image = self.archer_ork.image_path return dane
prediction = self.prediction_road(x,y,mob_image) def sort_tablicy(dane):
prediction = "ORK_ARCHER" posortowana_tablica = sorted(dane, key=lambda x: x[-1])
elif prediction == "SAURON" and self.agent.level >= 3: return posortowana_tablica
self.obstacles[1][10] = False def generuj_pary(n):
self.move_agent(self.astar.a_star(goal)) pary = []
for i in range(1, n+1):
elif prediction == "ORK_INFANTRY": for j in range(i+1, n+1):
self.obstacles[10][4] = False pary.append([i, j])
self.move_agent(self.astar.a_star(goal)) return pary
if self.agent.current_health < self.agent.max_health: self.WszystkiePary=generuj_pary(7)
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE) def polacz_tablice(tablica1, tablica2,n):
self.move_agent(self.astar.a_star(goal)) nowa_tablica = tablica1[:n] + tablica2[n:]
x = self.sauron.x return nowa_tablica
y = self.sauron.y self.positionsAfterGrade=ocena_tablicy(self.allpositions)
goal = x//TILE_SIZE,y//TILE_SIZE self.sortedAfterGrade=sort_tablicy(self.positionsAfterGrade)
mob_image = self.sauron.image_path n=10
prediction = self.prediction_road(x,y,mob_image) self.licznik=0
prediction = "SAURON"
elif prediction == "ORK_ARCHER": while(self.sortedAfterGrade[0][16]!=0 or self.licznik <n):
self.obstacles[10][10] = False self.WynikKombinacji=[]
self.move_agent(self.astar.a_star(goal)) pomWynikInt=0
if self.agent.current_health < self.agent.max_health: pomWszystkieParyInt=0
goal = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE) while(len(self.WynikKombinacji)<20 and pomWszystkieParyInt<21):
self.move_agent(self.astar.a_star(goal)) gen1=self.sortedAfterGrade[self.WszystkiePary[pomWszystkieParyInt][0]-1]
x = self.infantry_ork.x gen2=self.sortedAfterGrade[self.WszystkiePary[pomWszystkieParyInt][1]-1]
y = self.infantry_ork.y rollKombinacja=random.randint(0,100)# chance 60%
goal = x//TILE_SIZE,y//TILE_SIZE rollMutacja=random.randint(0,100)# chance 10%
mob_image = self.infantry_ork.image_path if(rollKombinacja<61):
prediction = self.prediction_road(x,y,mob_image) KombInt=random.randint(1,16)
prediction = "ORK_INFANTRY" self.WynikKombinacji.append(polacz_tablice(gen1,gen2,KombInt))
if(rollMutacja<11):
MutacjaInt=random.randint(0,16)
xPoMutacji=random.randint(0,12) #x
yPoMutacji=random.randint(0,11) #y
def prediction_road(self,x,y,mob_image): self.WynikKombinacji[pomWynikInt][MutacjaInt]=[xPoMutacji,yPoMutacji]
mob_goal_cell = (self.bfs.get_cell_number(x,y)) pomWynikInt=pomWynikInt+1
if self.bfs.get_up_cell(mob_goal_cell) == None: pomWszystkieParyInt=pomWszystkieParyInt+1
goal_cell = self.bfs.get_down_cell(mob_goal_cell) laczenieGeneracji=self.sortedAfterGrade[:(100-len(self.WynikKombinacji))]+self.WynikKombinacji
x,y = self.bfs.get_coordinates(goal_cell) OcenaWszystkich=ocena_tablicy(laczenieGeneracji)
goal = x//TILE_SIZE,y//TILE_SIZE self.sortedAfterGrade=sort_tablicy(OcenaWszystkich)
self.move_agent(self.astar.a_star(goal)) self.licznik=self.licznik+1
prediction = self.nn.predict(mob_image) #self.sortedAfterGrade[0] najlepszy
else: self.najlepszaGeneracja=self.sortedAfterGrade[0] #Agent,Archer_ork,infantry_ork,sauron,flower,grass x6, rocks x5 tablica 16 elementowa y=0-11 x=0-12 random.randint(x, y) = od x do y downolny int
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.agent = Agent(self,self.najlepszaGeneracja[0][0],self.najlepszaGeneracja[0][1])
self.move_agent(self.astar.a_star(goal)) self.archer_ork = Archer_ork(self,self.najlepszaGeneracja[1][0],self.najlepszaGeneracja[1][1])
prediction = self.nn.predict(mob_image) self.obstacles[self.najlepszaGeneracja[1][0]][self.najlepszaGeneracja[1][1]] = True
return prediction self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
self.infantry_ork = Infantry_ork(self,self.najlepszaGeneracja[2][0],self.najlepszaGeneracja[2][1])
def move_agent(self,path): self.obstacles[self.najlepszaGeneracja[2][0]][self.najlepszaGeneracja[2][1]] = True
print("PATH:::::",path) self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
for cell_to_move in path:
x, y = self.bfs.get_coordinates(cell_to_move) self.sauron = Sauron(self, self.najlepszaGeneracja[3][0], self.najlepszaGeneracja[3][1])
print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y) self.obstacles[self.najlepszaGeneracja[3][0]][self.najlepszaGeneracja[3][1]] = True
if(self.bfs.get_cell_number(self.agent.rect.x,self.agent.rect.y)!=cell_to_move): self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
if x > self.agent.rect.x: self.flower = Health_flower(self, self.najlepszaGeneracja[4][0],self.najlepszaGeneracja[4][1])
self.agent.direction = 0
elif y > self.agent.rect.y: for y in range (6):
self.agent.direction = 1 self.grass = Grass(self,self.najlepszaGeneracja[y+5][0],self.najlepszaGeneracja[y+5][1])
elif x < self.agent.rect.x: self.cell_costs[self.najlepszaGeneracja[y+5][0]][self.najlepszaGeneracja[y+5][1]] = 5
self.agent.direction = 2
elif y < self.agent.rect.y: for y in range(5):
self.agent.direction = 3 self.rock = Rocks(self,self.najlepszaGeneracja[y+11][0],self.najlepszaGeneracja[y+11][1])
if self.agent.direction==0: self.obstacles[self.najlepszaGeneracja[y+11][0]][self.najlepszaGeneracja[y+11][1]] = True
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
self.agent.x_change += TILE_SIZE
elif self.agent.direction==1: """ to jest tak jak bylo zmieniam tylko pozycje elementow
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) self.agent = Agent(self,1,1)
self.agent.y_change += TILE_SIZE self.archer_ork = Archer_ork(self,10,10)
elif self.agent.direction==2: self.obstacles[10][10] = True
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
self.agent.x_change -= TILE_SIZE self.infantry_ork = Infantry_ork(self,10,4)
elif self.agent.direction==3: self.obstacles[10][4] = True
#print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
self.agent.y_change -= TILE_SIZE
self.sauron = Sauron(self, 1, 10)
self.agent.rotate() self.obstacles[1][10] = True
self.update() self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
self.map() self.flower = Health_flower(self, 8,2)
print("Polozenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y) for y in range (2,5):
self.clock.tick(2) for x in range (2):
self.grass = Grass(self,x,y)
def map(self): # tworzenie mapy self.cell_costs[x][y] = 5
self.clock.tick(FRAMERATE)
for x in range(0, WIDTH, TILE_SIZE): for y in range(5):
for y in range(0, 768, TILE_SIZE): self.rock = Rocks(self,3,y)
self.SCREEN.blit(self.BACKGROUND,(x,y)) self.obstacles[3][y] = True
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE) self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1) """
self.flowers.draw(self.SCREEN) def update(self):
self.all_sprites.draw(self.SCREEN) self.all_sprites.update()
self.rock_sprites.draw(self.SCREEN)
self.grass_sprites.draw(self.SCREEN)
self.SCREEN.blit(self.LVL_ICON, (340 ,780)) def events(self):
pygame.display.update() for event in pygame.event.get():
if event.type == pygame.QUIT:
def main(self): self.running = False
self.events() pygame.quit()
self.update() if event.type == pygame.KEYDOWN:
self.map() 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))
g = Game()
g.new()
while g.running: if event.type == pygame.MOUSEBUTTONDOWN:
g.main() mouse_presses = pygame.mouse.get_pressed()
if mouse_presses[0]:
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[10][4] = 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[10][10] = 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()