aboutsummaryrefslogtreecommitdiff
path: root/src/game.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.py')
-rw-r--r--src/game.py204
1 files changed, 122 insertions, 82 deletions
diff --git a/src/game.py b/src/game.py
index a904fad..a7efb67 100644
--- a/src/game.py
+++ b/src/game.py
@@ -1,33 +1,32 @@
-from blinky import Blinky
-from clyde import Clyde
from direction import DIRECTION
+from game_state import GameState
from mode import MODE
-from inky import Inky
-from pinky import Pinky
-from player import Player
from settings import settings
-import map as Map
+from game_state import WIDTH, HEIGHT, TILE_WIDTH, TILE_HEIGHT
import pygame
-WIDTH = settings.width
-HEIGHT = settings.height
-
class Game():
def __init__(self):
self.settings = settings
- def show_gameover_screen(self, screen, game_over):
- font = pygame.font.SysFont(None, 48)
+ def show_gameover_screen(self, screen, game_state, sprites):
+ font = pygame.font.SysFont(None, 64)
# Render the "Game Over" text to a surface
- game_over_text = font.render(
- "Game Over. Press R to try again.", True, (255, 255, 255))
+ game_over_text_1 = font.render(
+ "Game Over", True, (255, 255, 255))
+ game_over_text_2 = font.render(
+ "Press R to try again or Q to quit.", True, (255, 255, 255))
# Blit the "Game Over" text onto the screen
- text_rect = game_over_text.get_rect(
+ text_rect_1 = game_over_text_1.get_rect(
+ center=(WIDTH/2, HEIGHT/2 - 75))
+ text_rect_2 = game_over_text_2.get_rect(
center=(WIDTH/2, HEIGHT/2))
- screen.blit(game_over_text, text_rect)
+
+ screen.blit(game_over_text_1, text_rect_1)
+ screen.blit(game_over_text_2, text_rect_2)
# Update the display
pygame.display.flip()
@@ -38,14 +37,66 @@ class Game():
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
# Reset the game and start again
# Add your own code here to reset the game state
- game_over[0] = False
+ self.reset_game(game_state, sprites)
+ game_state.game_over = False
quit_game = True # Set the flag to True to break out of both loops
break
+ elif event.type == pygame.KEYDOWN and event.key == pygame.K_q:
+ game_state.game_over = True
+ quit_game = True
+ break
elif event.type == pygame.QUIT:
- game_over[0] = True
+ game_state.game_over = True
quit_game = True # Set the flag to True to break out of both loops
break
+ def show_wining_screen(self, screen, game_state, sprites):
+ font = pygame.font.SysFont(None, 64)
+
+ # 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(
+ center=(WIDTH/2, HEIGHT/2))
+ text_rect_2 = wining_text_2.get_rect(
+ center=(WIDTH/2, HEIGHT/2 + 100))
+ screen.blit(wining_text_1, text_rect_1)
+ screen.blit(wining_text_2, text_rect_2)
+
+ # Update the display
+ pygame.display.flip()
+
+ quit_game = False # Initialize the flag variable
+ while not quit_game:
+ for event in pygame.event.get():
+ if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
+ # Reset the game and start again
+ # Add your own code here to reset the game state
+ self.reset_game(game_state, sprites)
+ game_state.game_over = False
+ quit_game = True # Set the flag to True to break out of both loops
+ break
+ elif event.type == pygame.KEYDOWN and event.key == pygame.K_q:
+ game_state.game_over = True
+ quit_game = True
+ break
+ elif event.type == pygame.QUIT:
+ game_state.game_over = True
+ quit_game = True # Set the flag to True to break out of both loops
+ break
+
+ def reset_game(self, game_state, sprites):
+ game_state.reset(sprites)
+
def run(self):
# Initialize Pygame
pygame.init()
@@ -64,27 +115,14 @@ class Game():
clyde_sprite = pygame.image.load('../assets/clyde.png').convert_alpha()
inky_sprite = pygame.image.load('../assets/inky.png').convert_alpha()
+ sprites = [sprite_sheet, blinky_sprite,
+ pinky_sprite, inky_sprite, clyde_sprite]
+
# Set the timer to trigger after 10,000 milliseconds (10 seconds)
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, 1000 * 10, 1)
- # our beautiful maze
- maze = Map.Map()
-
- # length of the map grid size
- TILE_WIDTH = WIDTH // len(maze.maze[0])
- TILE_HEIGHT = HEIGHT // len(maze.maze)
-
- # Initialize the player and the ghosts
- player = Player(sprite_sheet)
- blinky = Blinky(blinky_sprite, 12 * TILE_WIDTH +
- 15, 12 * TILE_HEIGHT + 15)
- pinky = Pinky(pinky_sprite, 11 * TILE_WIDTH +
- 15, 12 * TILE_HEIGHT + 15)
- inky = Inky(inky_sprite, 13 * TILE_WIDTH +
- 15, 12 * TILE_HEIGHT + 15)
- clyde = Clyde(clyde_sprite, 14 * TILE_WIDTH +
- 15, 12 * TILE_HEIGHT + 15)
+ game_state = GameState(sprites)
# Set the pacman velocity
dx = 0
@@ -101,10 +139,9 @@ class Game():
if settings.sound:
pygame.mixer.music.play()
siren_sound.play(-1)
- is_game_over = [False]
# Main game loop
- while not is_game_over[0]:
+ while not game_state.game_over:
# setting game fps
clock.tick(settings.fps)
@@ -120,101 +157,104 @@ class Game():
tx = dx
ty = dy
- is_pacman_alive = [True]
-
# Handling events
for event in pygame.event.get():
if event.type == pygame.QUIT:
- is_game_over[0] = True
+ game_state.game_over = True
elif event.type == pygame.KEYDOWN:
# Move the circle based on the pressed key
if event.key == pygame.K_w:
- player.direction = DIRECTION.UP
- ty = -player.speed
+ game_state.pacman.direction = DIRECTION.UP
+ ty = -game_state.pacman.speed
tx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_s:
- player.direction = DIRECTION.DOWN
- ty = player.speed
+ game_state.pacman.direction = DIRECTION.DOWN
+ ty = game_state.pacman.speed
tx = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_a:
- player.direction = DIRECTION.LEFT
- tx = -player.speed
+ game_state.pacman.direction = DIRECTION.LEFT
+ tx = -game_state.pacman.speed
ty = 0 # Necssarry to move only horizontal or vertical
elif event.key == pygame.K_d:
- player.direction = DIRECTION.RIGHT
- tx = player.speed
+ game_state.pacman.direction = DIRECTION.RIGHT
+ tx = game_state.pacman.speed
ty = 0 # Necssarry to move only horizontal or vertical
# Check for the timer event
if event.type == timer_event:
- pinky.mode = MODE.CHASING
- inky.mode = MODE.CHASING
- blinky.mode = MODE.CHASING
- clyde.mode = MODE.CHASING
+ game_state.pinky.mode = MODE.CHASING
+ game_state.inky.mode = MODE.CHASING
+ game_state.blinky.mode = MODE.CHASING
+ game_state.clyde.mode = MODE.CHASING
keys = pygame.key.get_pressed()
# Simulates holding the key which adds better playability for pacman
if keys[pygame.K_w]:
- player.direction = DIRECTION.UP
- ty = -player.speed
+ game_state.pacman.direction = DIRECTION.UP
+ ty = -game_state.pacman.speed
tx = 0
elif keys[pygame.K_s]:
- player.direction = DIRECTION.DOWN
- ty = player.speed
+ game_state.pacman.direction = DIRECTION.DOWN
+ ty = game_state.pacman.speed
tx = 0
elif keys[pygame.K_a]:
- player.direction = DIRECTION.LEFT
- tx = -player.speed
+ game_state.pacman.direction = DIRECTION.LEFT
+ tx = -game_state.pacman.speed
ty = 0
elif keys[pygame.K_d]:
- player.direction = DIRECTION.RIGHT
- tx = player.speed
+ game_state.pacman.direction = DIRECTION.RIGHT
+ tx = game_state.pacman.speed
ty = 0
# if tx and ty doesn't lead to colliding change the current
# dx and dy to them and other wise
# let pacman move in his previous direction
- if player.check_collision(maze, tx, ty, TILE_WIDTH, TILE_HEIGHT):
+ if game_state.pacman.check_collision(game_state, tx, ty, TILE_WIDTH, TILE_HEIGHT):
dx = tx
dy = ty
if dx < 0:
- player.direction = DIRECTION.LEFT
+ game_state.pacman.direction = DIRECTION.LEFT
elif dx > 0:
- player.direction = DIRECTION.RIGHT
+ game_state.pacman.direction = DIRECTION.RIGHT
elif dy < 0:
- player.direction = DIRECTION.UP
+ game_state.pacman.direction = DIRECTION.UP
elif dy > 0:
- player.direction = DIRECTION.DOWN
+ game_state.pacman.direction = DIRECTION.DOWN
- if player.check_collision(maze, dx, dy, TILE_WIDTH, TILE_HEIGHT):
- player.x += dx
- player.y += dy
- player.x %= 900
+ if game_state.pacman.check_collision(game_state, dx, dy, TILE_WIDTH, TILE_HEIGHT):
+ game_state.pacman.x += dx
+ game_state.pacman.y += dy
+ game_state.pacman.x %= 900 # logic for portal
# Move ghosts
- blinky.move(maze.maze, player, screen, is_pacman_alive, blinky)
- pinky.move(maze.maze, player, screen, is_pacman_alive, blinky)
- inky.move(maze.maze, player, screen, is_pacman_alive, blinky)
- clyde.move(maze.maze, player, screen, is_pacman_alive, blinky)
+ game_state.blinky.move(game_state, screen)
+ game_state.pinky.move(game_state, screen)
+ game_state.inky.move(game_state, screen)
+ game_state.clyde.move(game_state, screen)
# Draw the map on each frame
- maze.draw_map(screen)
+ game_state.map.draw_map(screen)
+
+ # Draw the game_state.pacman and the ghosts
+ game_state.pacman.draw(screen, counter)
+ game_state.blinky.draw(screen, game_state.pacman.powerup, counter)
+ game_state.pinky.draw(screen, game_state.pacman.powerup, counter)
+ game_state.inky.draw(screen, game_state.pacman.powerup, counter)
+ game_state.clyde.draw(screen, game_state.pacman.powerup, counter)
+
+ if game_state.food == 246:
+ self.show_wining_screen(screen, game_state, sprites)
- # Draw the player and the ghosts
- player.draw(screen, counter)
- blinky.draw(screen, player.powerup, counter)
- pinky.draw(screen, player.powerup, counter)
- inky.draw(screen, player.powerup, counter)
- clyde.draw(screen, player.powerup, counter)
- if not is_pacman_alive[0]:
+ if not game_state.is_pacman_alive:
self.show_gameover_screen(
- screen, is_game_over)
- is_pacman_alive[0] = True
+ screen, game_state, sprites)
+ game_state.is_pacman_alive = True
else:
# Update the screen
pygame.display.flip()
# Quit Pygame
+ print(game_state.score)
pygame.quit()