Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, pos_x, pos_y, player_path, current_sprite, run_speed, state_s
self.target_update_freq = 100 # Update target network every 100 steps

#load train weights
#checkpoint = torch.load('runs/ml-model-test-118/model.pt')
#checkpoint = torch.load('runs/ml-model-test-109/model.pt')
#self.q_network.load_state_dict(checkpoint['model_state_dict'])
#self.target_q_network.load_state_dict(checkpoint['model_state_dict'])

Expand All @@ -58,10 +58,6 @@ def jump(self):
self.is_jumping = False
self.jump_vel = 8.5





def make_decision(self):
if self.rect.y < 400:
return 1 # Pular
Expand Down
71 changes: 35 additions & 36 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,21 @@ def __init__(self, score, hi_score):
self.moving_bomb = pygame.sprite.Group()
self.bomb = Sprites(randint(10000, 30000), 500, 'sprites/bomb', 0.0, 5)
self.moving_bomb.add(self.bomb)





# Pontuação do jogo
self.score = score
self.hi_score = hi_score

self.scroll = 0
self.background = Background(self.SCREEN_WIDTH, self.SCREEN_HEIGHT, self.screen)

self.two_players = True



####################################
# METODOS DO JOGO #
####################################


def run(self):
while True:
self.scroll += 4
self.background.draw_bg(self.scroll)
self.background.draw_ground(self.scroll)
self.draw_game_elements()
self.handle_events()
self.step()
self.render_scores()
pygame.display.update()


def draw_game_elements(self):
self.moving_sprites.draw(self.screen)
self.moving_sprites.update()
Expand All @@ -92,7 +76,7 @@ def draw_game_elements(self):
self.moving_wave.update()
self.moving_shark.draw(self.screen)
self.moving_shark.update()
if self.agent is not None:
if self.two_players:
self.moving_ai.draw(self.screen)
self.moving_ai.update()

Expand Down Expand Up @@ -153,11 +137,18 @@ def step(self):
self.player.jump()



if self.score > 300:
self.shark.run_speed = 12
if self.score > 500:
self.shark.run_speed = 14
if self.score >800:
self.shark.run_speed = 16

self.score += 0.02 # Increment the score over time
action = 0
if self.agent is not None:
action = None


if self.two_players and self.agent is not None:
distancia_max = 1000
distancia_normalizada = (self.agent.rect[0] - self.shark.rect[0]) / distancia_max

Expand All @@ -169,14 +160,16 @@ def step(self):

state = [self.shark.rect[0], (self.agent.rect[0] - self.shark.rect[0]), self.agent.rect[0], is_jumping]
action = self.agent.select_action(state)
self.action = action
print(f'VALOR DA ACAO {action}')

if action == 1:
self.agent.is_jumping = True

if self.agent is not None and self.agent.is_jumping:
self.agent.jump()
action = 0
if self.two_players and self.agent is not None:
if self.agent.is_jumping:
self.agent.jump()
action = 0



Expand All @@ -189,16 +182,10 @@ def step(self):
self.bomb.inc = 0.12
else:
self.bomb.current_sprite = 0.0

## Check if player collides with the bomb
if self.player.rect.colliderect(self.bomb):
self.bomb.rect.x = randint(3000, 4000)
self.bomb.inc = 0




# Check if the player collides with the coin
if self.agent is not None:
if self.two_players and self.agent is not None:
if self.agent.rect.colliderect(self.coin) or self.coin.rect.colliderect(self.agent):
self.score += 10 # Increase the score when the player collects a coin
# Move the coin to a new position
Expand All @@ -216,21 +203,33 @@ def step(self):
self.coin.rect.y = randint(350,500)


if self.two_players and self.agent is not None:
if self.agent.rect.colliderect(self.bomb):
self.two_players = False




# Check for collisions with the shark and update the score accordingly
if self.agent is not None:
if self.two_players and self.agent is not None:
if (
pygame.sprite.spritecollide(self.agent, self.moving_shark, False, pygame.sprite.collide_mask)
or pygame.sprite.spritecollide(self.shark, self.moving_ai, False, pygame.sprite.collide_mask)
):
self.shark.rect.x = randint(1000, 1400) # Move the shark to a new position
self.agent = None
self.two_players = False

if self.shark.rect.x <= -150:
self.shark.rect.x = randint(1000, 1400) # Move the shark to a new position

if self.player.rect.colliderect(self.bomb):
if self.two_players == False:
print("FIM DE JOGO!")
return 0, self.score, self.hi_score


if (pygame.sprite.spritecollide(self.player, self.moving_shark, False, pygame.sprite.collide_mask)):
if self.agent is None:
if self.two_players == False:
print("FIM DE JOGO!")
return 0, self.score, self.hi_score

Expand Down
5 changes: 4 additions & 1 deletion scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ def draw_ground(self, scroll):
)
x += 1

#def draw_platform(self,scroll)
#def draw_platform(self,scroll)



24 changes: 13 additions & 11 deletions tsunami_rush.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
return_to_menu_timer = 0
return_to_menu_duration = 2 # Set the duration in seconds


True
# musica
# Musica do jogo
pygame.mixer.music.load("sounds/8-bit-game-music-122259.mp3")
pygame.mixer.music.set_volume(0.5)
# Start playing the background music (use -1 to loop indefinitely)


# Game loop
run = True
Expand All @@ -51,15 +50,17 @@
pygame.mixer.music.play(-1)
in_menu = False
in_game_one_player = True
in_game_two_player = False
elif menu.selected_option == 1:
pygame.mixer.music.play(-1)
in_menu = False
in_game_one_player = False
in_game_two_player = True
elif menu.selected_option == 2:
run = False

elif in_game_one_player:
game.agent = None
game.two_players = False
done, score, hi_score = game.step()
if done == 0:
pygame.mixer.music.stop()
Expand All @@ -75,21 +76,22 @@
in_game_one_player = False

elif in_game_two_player:
game.agent = AIPlayer(400,520,'sprites/ai_player', 0.05, 0, state_size=7, action_size=2)
game.two_players = True
#game.agent = AIPlayer(400,520,'sprites/ai_player', 0.05, 0, state_size=4, action_size=2)
while True:
done, score, hi_score = game.step()
if done == 0:
pygame.display.flip()
if game.score > game.hi_score:
game.hi_score = game.score
pygame.mixer.music.stop()
game.agent = AIPlayer(400,520,'sprites/ai_player', 0.05, 0, state_size=7, action_size=2)
game.reset()
for i in range(100):
pygame.display.flip()
#atualiza a tela e mostra game over por alguns segundos
game_over.draw_game_over_screen(score,hi_score)
game_over.high_score = game.hi_score
break
for i in range(100):
#atualiza a tela e mostra game over por alguns segundos
pygame.display.flip()
game_over.draw_game_over_screen(score,hi_score)
game_over.high_score = game.hi_score

in_menu = True
in_game_two_player = False
Expand Down