2023-06-12 13:03:27 +02:00
|
|
|
|
2023-03-29 16:09:57 +02:00
|
|
|
import pygame
|
2023-03-29 16:58:24 +02:00
|
|
|
from config import *
|
2023-03-29 18:11:34 +02:00
|
|
|
from agent import *
|
2023-03-30 15:51:20 +02:00
|
|
|
from map_add_ons import *
|
2023-05-04 14:06:31 +02:00
|
|
|
from mobs import *
|
|
|
|
from bfs import *
|
2023-06-01 16:09:01 +02:00
|
|
|
from nn import *
|
2023-06-12 13:03:27 +02:00
|
|
|
from astar import *
|
|
|
|
|
2023-03-29 16:09:57 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
class Game:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pygame.init()
|
2023-04-21 13:11:47 +02:00
|
|
|
self.state =[-1,-1,-1,-1,-1,-1,-1,-1]
|
2023-03-29 18:11:34 +02:00
|
|
|
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
self.running = True
|
|
|
|
self.clock = pygame.time.Clock()
|
2023-06-12 13:03:27 +02:00
|
|
|
|
2023-06-01 16:09:01 +02:00
|
|
|
self.BACKGROUND_IMG= pygame.image.load("./pozostale_zdjecia/podloze.jpg")
|
2023-03-30 15:51:20 +02:00
|
|
|
self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
|
2023-03-29 18:11:34 +02:00
|
|
|
|
2023-06-01 16:09:01 +02:00
|
|
|
self.LVL_ICON_PNG = pygame.image.load("./pozostale_zdjecia/lvl_icon.png")
|
2023-04-18 22:15:23 +02:00
|
|
|
self.LVL_ICON = pygame.transform.scale(self.LVL_ICON_PNG,(24,24))
|
|
|
|
|
2023-03-31 15:10:37 +02:00
|
|
|
pygame.display.set_caption('Gra-SI')
|
2023-03-29 18:11:34 +02:00
|
|
|
|
2023-05-04 14:06:31 +02:00
|
|
|
self.bfs = Bfs(self)
|
2023-06-01 17:23:53 +02:00
|
|
|
self.nn = NeuralN()
|
2023-06-12 13:03:27 +02:00
|
|
|
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)]
|
|
|
|
|
|
|
|
|
2023-05-04 14:06:31 +02:00
|
|
|
|
2023-06-11 17:52:19 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
def new(self): # tworzy się nowa sesja grania
|
2023-03-30 15:51:20 +02:00
|
|
|
self.all_sprites = pygame.sprite.LayeredUpdates()
|
2023-05-04 14:06:31 +02:00
|
|
|
|
2023-03-30 20:10:29 +02:00
|
|
|
self.rock_sprites = pygame.sprite.LayeredUpdates()
|
2023-05-04 14:06:31 +02:00
|
|
|
self.grass_sprites = pygame.sprite.LayeredUpdates()
|
2023-03-30 22:09:13 +02:00
|
|
|
self.archer_orks = pygame.sprite.LayeredUpdates()
|
|
|
|
self.infantry_orks = pygame.sprite.LayeredUpdates()
|
2023-06-11 17:52:19 +02:00
|
|
|
|
2023-03-30 22:09:13 +02:00
|
|
|
self.sauronL = pygame.sprite.LayeredUpdates()
|
2023-04-29 12:00:36 +02:00
|
|
|
self.flowers = pygame.sprite.LayeredUpdates()
|
2023-05-04 14:18:50 +02:00
|
|
|
self.little_rock_sprites = pygame.sprite.LayeredUpdates()
|
2023-05-04 14:06:31 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
self.agent = Agent(self,1,1)
|
2023-03-30 19:54:48 +02:00
|
|
|
self.archer_ork = Archer_ork(self,10,10)
|
2023-06-12 13:03:27 +02:00
|
|
|
self.obstacles[10][10] = True
|
2023-05-04 14:06:31 +02:00
|
|
|
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.archer_ork.x,self.archer_ork.y))
|
2023-03-31 12:01:43 +02:00
|
|
|
self.infantry_ork = Infantry_ork(self,10,4)
|
2023-06-12 13:03:27 +02:00
|
|
|
self.obstacles[10][4] = True
|
2023-05-04 14:06:31 +02:00
|
|
|
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.infantry_ork.x,self.infantry_ork.y))
|
2023-06-11 17:52:19 +02:00
|
|
|
|
2023-03-30 19:54:48 +02:00
|
|
|
self.sauron = Sauron(self, 1, 10)
|
2023-06-12 13:03:27 +02:00
|
|
|
self.obstacles[1][10] = True
|
2023-05-04 14:06:31 +02:00
|
|
|
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
|
2023-04-29 12:00:36 +02:00
|
|
|
self.flower = Health_flower(self, 8,2)
|
2023-06-12 13:03:27 +02:00
|
|
|
|
|
|
|
for y in range (2,5):
|
|
|
|
for x in range (2):
|
|
|
|
self.grass = Grass(self,x,y)
|
|
|
|
self.cell_costs[x][y] = 5
|
|
|
|
|
2023-03-30 15:51:20 +02:00
|
|
|
for y in range(5):
|
|
|
|
self.rock = Rocks(self,3,y)
|
2023-06-12 13:03:27 +02:00
|
|
|
self.obstacles[3][y] = True
|
2023-05-04 14:06:31 +02:00
|
|
|
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
|
2023-06-11 17:52:19 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
|
2023-03-31 11:03:11 +02:00
|
|
|
def update(self):
|
2023-03-29 18:11:34 +02:00
|
|
|
self.all_sprites.update()
|
2023-03-30 20:10:29 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
|
|
|
|
def events(self):
|
2023-03-29 16:09:57 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
2023-03-29 18:11:34 +02:00
|
|
|
self.running = False
|
2023-03-29 16:09:57 +02:00
|
|
|
pygame.quit()
|
2023-06-12 13:03:27 +02:00
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_SPACE:
|
|
|
|
self.start_pos = (self.agent.x//TILE_SIZE, self.agent.y//TILE_SIZE)
|
|
|
|
self.goal_pos = (self.flower.x//TILE_SIZE, self.flower.y//TILE_SIZE)
|
|
|
|
self.path = []
|
|
|
|
self.move_agent(self.astar.a_star(self.start_pos, self.goal_pos,self.path))
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-05-02 20:36:45 +02:00
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
|
mouse_presses = pygame.mouse.get_pressed()
|
|
|
|
if mouse_presses[0]:
|
2023-06-11 17:52:19 +02:00
|
|
|
|
2023-06-01 17:23:53 +02:00
|
|
|
x = self.sauron.x
|
|
|
|
y = self.sauron.y
|
|
|
|
mob_image = self.sauron.SAURON_IMG
|
2023-06-12 13:03:27 +02:00
|
|
|
while True: #do poprawienia poprawne rozpoznawanie póki co nie będzie działać dobrze, program się będzie zawieszać
|
2023-06-01 17:23:53 +02:00
|
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
|
|
if prediction == "SAURON":
|
|
|
|
x = self.infantry_ork.x
|
|
|
|
y = self.infantry_ork.y
|
|
|
|
mob_image = self.infantry_ork.INFANTRY_ORK_IMG
|
|
|
|
prediction = self.prediction_road(x,y,mob_image)
|
2023-06-02 15:46:56 +02:00
|
|
|
if prediction == "ORK_INFANTRY":
|
2023-06-11 17:52:19 +02:00
|
|
|
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y)))
|
2023-06-02 15:46:56 +02:00
|
|
|
x = self.archer_ork.x
|
|
|
|
y = self.archer_ork.y
|
|
|
|
mob_image = self.archer_ork.ARCHER_ORK_IMG
|
|
|
|
prediction = self.prediction_road(x,y,mob_image)
|
|
|
|
if prediction == "ORK_ARCHER":
|
2023-06-11 17:52:19 +02:00
|
|
|
self.move_agent(self.bfs.bfs(self.bfs.get_cell_number(x,y)))
|
2023-06-02 15:46:56 +02:00
|
|
|
x = self.sauron.x
|
|
|
|
y = self.sauron.y
|
|
|
|
mob_image = self.sauron.SAURON_IMG
|
|
|
|
|
2023-06-01 17:23:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def prediction_road(self,x,y,mob_image):
|
|
|
|
mob_goal = (self.bfs.get_cell_number(x,y))
|
|
|
|
if self.bfs.get_up_cell(mob_goal) == None:
|
|
|
|
goal = self.bfs.get_down_cell(mob_goal)
|
|
|
|
else:
|
|
|
|
goal = self.bfs.get_up_cell(mob_goal)
|
2023-06-11 17:52:19 +02:00
|
|
|
self.move_agent(self.bfs.bfs(goal))
|
2023-06-01 17:23:53 +02:00
|
|
|
prediction = self.nn.predict(mob_image)
|
|
|
|
return prediction
|
|
|
|
|
2023-06-11 17:52:19 +02:00
|
|
|
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.x,self.agent.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)
|
2023-03-29 16:09:57 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
def map(self): # tworzenie mapy
|
|
|
|
self.clock.tick(FRAMERATE)
|
|
|
|
for x in range(0, WIDTH, TILE_SIZE):
|
2023-04-18 21:38:22 +02:00
|
|
|
for y in range(0, 768, TILE_SIZE):
|
2023-03-30 15:51:20 +02:00
|
|
|
self.SCREEN.blit(self.BACKGROUND,(x,y))
|
2023-03-29 18:11:34 +02:00
|
|
|
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
|
2023-05-03 16:10:03 +02:00
|
|
|
pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
|
|
|
|
self.flowers.draw(self.SCREEN)
|
2023-03-30 15:51:20 +02:00
|
|
|
self.all_sprites.draw(self.SCREEN)
|
|
|
|
self.rock_sprites.draw(self.SCREEN)
|
2023-05-04 14:06:31 +02:00
|
|
|
self.grass_sprites.draw(self.SCREEN)
|
2023-04-18 22:15:23 +02:00
|
|
|
self.SCREEN.blit(self.LVL_ICON, (340 ,780))
|
2023-03-29 18:11:34 +02:00
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
def main(self):
|
|
|
|
self.events()
|
|
|
|
self.update()
|
|
|
|
self.map()
|
2023-05-02 20:36:45 +02:00
|
|
|
|
2023-05-05 14:56:42 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
g = Game()
|
|
|
|
g.new()
|
2023-06-12 13:03:27 +02:00
|
|
|
|
2023-03-29 18:11:34 +02:00
|
|
|
while g.running:
|
2023-03-31 11:03:11 +02:00
|
|
|
g.main()
|