From 3d7e3d1eba26cc57b31337b0708fce3747f8a988 Mon Sep 17 00:00:00 2001 From: Moisis Date: Wed, 17 May 2023 14:04:38 +0300 Subject: Improved GUI Settings added (Debug , Sound) SoundSystem added (Become sometimes laggy) --- src/game.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'src/game.py') diff --git a/src/game.py b/src/game.py index a7efb67..928b665 100644 --- a/src/game.py +++ b/src/game.py @@ -1,3 +1,6 @@ +import os +import sys + from direction import DIRECTION from game_state import GameState from mode import MODE @@ -7,8 +10,9 @@ import pygame class Game(): - def __init__(self): - self.settings = settings + def __init__(self, lolsettings): + self.settings = lolsettings + def show_gameover_screen(self, screen, game_state, sprites): font = pygame.font.SysFont(None, 64) @@ -21,9 +25,9 @@ class Game(): # Blit the "Game Over" text onto the screen text_rect_1 = game_over_text_1.get_rect( - center=(WIDTH/2, HEIGHT/2 - 75)) + center=(WIDTH / 2, HEIGHT / 2 - 75)) text_rect_2 = game_over_text_2.get_rect( - center=(WIDTH/2, HEIGHT/2)) + center=(WIDTH / 2, HEIGHT / 2)) screen.blit(game_over_text_1, text_rect_1) screen.blit(game_over_text_2, text_rect_2) @@ -44,7 +48,9 @@ class Game(): elif event.type == pygame.KEYDOWN and event.key == pygame.K_q: game_state.game_over = True quit_game = True - break + pygame.quit() + os.system("python MacPan.py 1") + exit() elif event.type == pygame.QUIT: game_state.game_over = True quit_game = True # Set the flag to True to break out of both loops @@ -59,16 +65,16 @@ class Game(): wining_text_2 = font.render( "Press R to play again or Q to quit", True, (255, 255, 255)) - text_rect_1 = wining_text_1.get_rect( - center=(WIDTH/2, HEIGHT/2 - 75)) - text_rect_2 = wining_text_2.get_rect( - center=(WIDTH/2, HEIGHT/2)) + # text_rect_1 = wining_text_1.get_rect( + # center=(WIDTH / 2, HEIGHT / 2 - 75)) + # text_rect_2 = wining_text_2.get_rect( + # center=(WIDTH / 2, HEIGHT / 2)) # Blit the "Game Over" text onto the screen text_rect_1 = wining_text_1.get_rect( - center=(WIDTH/2, HEIGHT/2)) + center=(WIDTH / 2, HEIGHT / 2)) text_rect_2 = wining_text_2.get_rect( - center=(WIDTH/2, HEIGHT/2 + 100)) + center=(WIDTH / 2, HEIGHT / 2 + 100)) screen.blit(wining_text_1, text_rect_1) screen.blit(wining_text_2, text_rect_2) @@ -95,7 +101,7 @@ class Game(): break def reset_game(self, game_state, sprites): - game_state.reset(sprites) + game_state.reset(sprites,self.settings) def run(self): # Initialize Pygame @@ -122,7 +128,7 @@ class Game(): timer_event = pygame.USEREVENT + 1 pygame.time.set_timer(timer_event, 1000 * 10, 1) - game_state = GameState(sprites) + game_state = GameState(sprites, self.settings) # Set the pacman velocity dx = 0 @@ -132,11 +138,12 @@ class Game(): counter = 0 clock = pygame.time.Clock() - + pygame.mixer.set_num_channels(5) pygame.mixer.music.load('../assets/sfx/game_start.wav') siren_sound = pygame.mixer.Sound('../assets/sfx/siren_1.wav') - if settings.sound: + + if self.settings.sound: pygame.mixer.music.play() siren_sound.play(-1) @@ -246,11 +253,14 @@ class Game(): if game_state.food == 246: self.show_wining_screen(screen, game_state, sprites) - if not game_state.is_pacman_alive: + siren_sound.stop() + pygame.mixer.music.load('../assets/sfx/death_1.wav') + pygame.mixer.music.play() self.show_gameover_screen( screen, game_state, sprites) game_state.is_pacman_alive = True + else: # Update the screen pygame.display.flip() -- cgit v1.2.3 From 38bf9644941393a553e9383176d7ec12f64992f7 Mon Sep 17 00:00:00 2001 From: Moisis Date: Wed, 17 May 2023 17:34:23 +0300 Subject: Added SCORE --- src/game.py | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'src/game.py') diff --git a/src/game.py b/src/game.py index 928b665..192c356 100644 --- a/src/game.py +++ b/src/game.py @@ -1,5 +1,6 @@ import os import sys +import time from direction import DIRECTION from game_state import GameState @@ -16,7 +17,7 @@ class Game(): def show_gameover_screen(self, screen, game_state, sprites): font = pygame.font.SysFont(None, 64) - + self.score(screen, game_state.score , 200 , 800) # Render the "Game Over" text to a surface game_over_text_1 = font.render( "Game Over", True, (255, 255, 255)) @@ -56,19 +57,41 @@ class Game(): quit_game = True # Set the flag to True to break out of both loops break + + def score(self, screen, text , POS1, POS2 ): + font = pygame.font.SysFont(None, 21) + + wining_text_123 = font.render( + "Score", True, (255, 255, 255)) + + # Render the "Game Over" text to a surface + wining_text_12 = font.render( + str(text), True, (255, 255, 255)) + + # Blit the "Game Over" text onto the screen + text_rect_12 = wining_text_12.get_rect( + center=(POS1 / 2, POS2 / 2)) + + text_rect_123 = wining_text_123.get_rect( + center=(POS1 / 2,( (POS2 / 2)-50) )) + + + screen.blit(wining_text_12, text_rect_12) + screen.blit(wining_text_123, text_rect_123) + + # Update the display + pygame.display.flip() + def show_wining_screen(self, screen, game_state, sprites): font = pygame.font.SysFont(None, 64) + self.score(screen, game_state.score, 200, 800) # Render the "Game Over" text to a surface wining_text_1 = font.render( "Congratulation You Won!!", True, (255, 255, 255)) wining_text_2 = font.render( "Press R to play again or Q to quit", True, (255, 255, 255)) - # text_rect_1 = wining_text_1.get_rect( - # center=(WIDTH / 2, HEIGHT / 2 - 75)) - # text_rect_2 = wining_text_2.get_rect( - # center=(WIDTH / 2, HEIGHT / 2)) # Blit the "Game Over" text onto the screen text_rect_1 = wining_text_1.get_rect( @@ -94,7 +117,9 @@ class Game(): elif event.type == pygame.KEYDOWN and event.key == pygame.K_q: game_state.game_over = True quit_game = True - break + pygame.quit() + os.system("python MacPan.py 1") + exit() elif event.type == pygame.QUIT: game_state.game_over = True quit_game = True # Set the flag to True to break out of both loops @@ -152,12 +177,15 @@ class Game(): # setting game fps clock.tick(settings.fps) + self.score(screen, game_state.score,WIDTH,HEIGHT) # counter logic for cycling between pacman different sprites if counter < 19: counter += 1 + else: counter = 0 + screen.fill((0, 0, 0)) # Clear the screen # Temporary values for delta_x and delta_y in the position of pacman @@ -256,15 +284,16 @@ class Game(): if not game_state.is_pacman_alive: siren_sound.stop() pygame.mixer.music.load('../assets/sfx/death_1.wav') - pygame.mixer.music.play() + if self.settings.sound: + pygame.mixer.music.play() self.show_gameover_screen( screen, game_state, sprites) game_state.is_pacman_alive = True - + self.score( screen, game_state) else: # Update the screen pygame.display.flip() # Quit Pygame - print(game_state.score) + pygame.quit() -- cgit v1.2.3 From 0889aaf69cc0d4ca32ce66e000e827bddf6bd162 Mon Sep 17 00:00:00 2001 From: Moisis Date: Wed, 17 May 2023 17:39:05 +0300 Subject: Final Touches --- src/game.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/game.py') diff --git a/src/game.py b/src/game.py index 192c356..e211da7 100644 --- a/src/game.py +++ b/src/game.py @@ -154,6 +154,7 @@ class Game(): pygame.time.set_timer(timer_event, 1000 * 10, 1) game_state = GameState(sprites, self.settings) + self.score(screen,game_state.score,WIDTH,HEIGHT) # Set the pacman velocity dx = 0 @@ -289,7 +290,7 @@ class Game(): self.show_gameover_screen( screen, game_state, sprites) game_state.is_pacman_alive = True - self.score( screen, game_state) + else: # Update the screen pygame.display.flip() -- cgit v1.2.3 From 6a351ed2d6a554ed6d338abba234527705d1040f Mon Sep 17 00:00:00 2001 From: Moisis Date: Fri, 19 May 2023 23:40:23 +0300 Subject: Final Touches #2 --- src/game.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'src/game.py') diff --git a/src/game.py b/src/game.py index e211da7..8872c0c 100644 --- a/src/game.py +++ b/src/game.py @@ -14,10 +14,9 @@ class Game(): def __init__(self, lolsettings): self.settings = lolsettings - def show_gameover_screen(self, screen, game_state, sprites): font = pygame.font.SysFont(None, 64) - self.score(screen, game_state.score , 200 , 800) + self.score(screen, game_state.score, 200, 800) # Render the "Game Over" text to a surface game_over_text_1 = font.render( "Game Over", True, (255, 255, 255)) @@ -57,12 +56,11 @@ class Game(): quit_game = True # Set the flag to True to break out of both loops break - - def score(self, screen, text , POS1, POS2 ): - font = pygame.font.SysFont(None, 21) + def score(self, screen, text, POS1, POS2): + font = pygame.font.Font('../assets/PressStart2P-Regular.ttf', 21) wining_text_123 = font.render( - "Score", True, (255, 255, 255)) + "Score", True, (255, 255, 255)) # Render the "Game Over" text to a surface wining_text_12 = font.render( @@ -73,8 +71,7 @@ class Game(): center=(POS1 / 2, POS2 / 2)) text_rect_123 = wining_text_123.get_rect( - center=(POS1 / 2,( (POS2 / 2)-50) )) - + center=(POS1 / 2, ((POS2 / 2) - 50))) screen.blit(wining_text_12, text_rect_12) screen.blit(wining_text_123, text_rect_123) @@ -92,7 +89,6 @@ class Game(): wining_text_2 = font.render( "Press R to play again or Q to quit", True, (255, 255, 255)) - # Blit the "Game Over" text onto the screen text_rect_1 = wining_text_1.get_rect( center=(WIDTH / 2, HEIGHT / 2)) @@ -126,7 +122,7 @@ class Game(): break def reset_game(self, game_state, sprites): - game_state.reset(sprites,self.settings) + game_state.reset(sprites, self.settings) def run(self): # Initialize Pygame @@ -154,7 +150,7 @@ class Game(): pygame.time.set_timer(timer_event, 1000 * 10, 1) game_state = GameState(sprites, self.settings) - self.score(screen,game_state.score,WIDTH,HEIGHT) + # self.score(screen,game_state.score,WIDTH,HEIGHT) # Set the pacman velocity dx = 0 @@ -168,7 +164,6 @@ class Game(): pygame.mixer.music.load('../assets/sfx/game_start.wav') siren_sound = pygame.mixer.Sound('../assets/sfx/siren_1.wav') - if self.settings.sound: pygame.mixer.music.play() siren_sound.play(-1) @@ -178,7 +173,8 @@ class Game(): # setting game fps clock.tick(settings.fps) - self.score(screen, game_state.score,WIDTH,HEIGHT) + # self.score(screen, game_state.score,WIDTH,HEIGHT) + # counter logic for cycling between pacman different sprites if counter < 19: counter += 1 @@ -186,7 +182,6 @@ class Game(): else: counter = 0 - screen.fill((0, 0, 0)) # Clear the screen # Temporary values for delta_x and delta_y in the position of pacman @@ -292,6 +287,7 @@ class Game(): game_state.is_pacman_alive = True else: + self.score(screen, game_state.score, WIDTH, HEIGHT) # Update the screen pygame.display.flip() -- cgit v1.2.3