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 * import math import random 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.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() #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.allpositions=[] self.allpositionsSet=set() while(len(self.allpositionsSet)<100): self.positions=[] #.append self.positionsSet=set() #.add for x in range(16): while len(self.positionsSet)<16: pos1=random.randint(0,12) #x pos2=random.randint(0,11) #y pom=(pos1,pos2) lenSetBefore=len(self.positionsSet) self.positionsSet.add(pom) lenSetAfter=len(self.positionsSet) if(lenSetAfter>lenSetBefore): self.positions.append(pom) AllPositionsSetB=len(self.allpositionsSet) self.allpositionsSet.add(tuple(self.positions)) AllPositionsSetA=len(self.allpositionsSet) if(AllPositionsSetA>AllPositionsSetB): self.positions.append([1000]) self.allpositions.append(self.positions) def sprawdz_powtorzenia(tablica): wystapienia = set() for element in tablica[:-1]: if tuple(element) in wystapienia: return True # Powtórzenie znalezione wystapienia.add(tuple(element)) return False # Brak powtórzeń def ocena_tablicy(dane): for x in range(100): if(sprawdz_powtorzenia(dane[x])): dane[x][-1]=10000000 else: x1,y1=dane[x][0] x2,y2=dane[x][1] x3,y3=dane[x][2] x4,y4=dane[x][3] r1=math.sqrt((x1 - x2)**2 + (y1 - y2)**2) r2=math.sqrt((x2 - x3)**2 + (y2 - y3)**2) r3=math.sqrt((x3 - x4)**2 + (y3 - y4)**2) r12=math.sqrt((x1 - x3)**2 + (y1 - y3)**2) r13=math.sqrt((x1 - x2)**2 + (y1 - y2)**2) avg=(r1+r2+r3)/3 grade=abs(r1-avg)+abs(r2-avg)+abs(r3-avg) if(r1<4): grade=grade+5 if(r12<4): grade=grade+5 if(r13<4): grade=grade+5 grade=round(grade,2)*100 dane[x][-1]=grade return dane def sort_tablicy(dane): posortowana_tablica = sorted(dane, key=lambda x: x[-1]) return posortowana_tablica def generuj_pary(n): pary = [] for i in range(1, n+1): for j in range(i+1, n+1): pary.append([i, j]) return pary self.WszystkiePary=generuj_pary(7) def polacz_tablice(tablica1, tablica2,n): nowa_tablica = tablica1[:n] + tablica2[n:] return nowa_tablica self.positionsAfterGrade=ocena_tablicy(self.allpositions) self.sortedAfterGrade=sort_tablicy(self.positionsAfterGrade) n=10 self.licznik=0 while(self.sortedAfterGrade[0][16]!=0 or self.licznik = 3: self.obstacles[self.najlepszaGeneracja[3][0]][self.najlepszaGeneracja[3][1]] = False self.move_agent(self.astar.a_star(goal)) elif prediction == "ORK_INFANTRY": self.obstacles[self.najlepszaGeneracja[2][0]][self.najlepszaGeneracja[2][1]] = 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[self.najlepszaGeneracja[1][0]][self.najlepszaGeneracja[1][1]] = 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()