From 6c43e3b3ab77ec0c3c57763178723a50e4dcac52 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Wed, 19 Apr 2023 02:09:53 +0200 Subject: Added the ghost sprites to the game. --- src/Game.py | 47 ++++++++++++++++++++++++++++++++++------------- src/Ghost.py | 10 +++++----- src/Player.py | 26 +------------------------- src/blinky.py | 4 ++-- src/clyde.py | 9 +++++++++ src/inky.py | 9 +++++++++ src/pinky.png | Bin 0 -> 2911 bytes src/pinky.py | 5 ++--- src/util.py | 28 ++++++++++++++++++++++++++++ 9 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 src/clyde.py create mode 100644 src/inky.py create mode 100644 src/pinky.png create mode 100644 src/util.py (limited to 'src') diff --git a/src/Game.py b/src/Game.py index 0ba8c79..0d517a1 100644 --- a/src/Game.py +++ b/src/Game.py @@ -1,13 +1,12 @@ -from pygame.mouse import get_pressed import Player -import Ghost from pinky import Pinky from blinky import Blinky +from inky import Inky +from clyde import Clyde from Direction import DIRECTION import settings as Settings import map as Map import pygame -import math class Game(): @@ -25,12 +24,20 @@ class Game(): # Sprite sheet for pacman sprite_sheet = pygame.image.load( '../assets/pacman_left_sprite.png').convert_alpha() + blinky_sprite = pygame.image.load( + '../assets/blinky.png').convert_alpha() + pinky_sprite = pygame.image.load( + '../assets/pinky.png').convert_alpha() + clyde_sprite = pygame.image.load( + '../assets/clyde.png').convert_alpha() + inky_sprite = pygame.image.load( + '../assets/inky.png').convert_alpha() player = Player.Player(sprite_sheet) - blinky = Blinky(75, 75) - pinky = Pinky(27 * 30, 30 * 30 + 15) - inky = Ghost.Ghost("orange", 75, 30 * 30 + 15) - clyde = Ghost.Ghost("cyan", 27 * 30 + 15, 75) + blinky = Blinky(blinky_sprite,75, 75) + pinky = Pinky(pinky_sprite, 27 * 30, 30 * 30 + 15) + inky = Inky(inky_sprite, 75, 30 * 30 + 15) + clyde = Clyde(clyde_sprite, 27 * 30 + 15, 75) # Set the pacman velocity dx = 0 @@ -48,7 +55,6 @@ class Game(): grid_x = Settings.settings.width // len(maze.maze[0]) grid_y = Settings.settings.height // len(maze.maze) - print(grid_x, grid_y) # Checks collision with walls @@ -59,6 +65,8 @@ class Game(): is_free = maze.maze[y][x] == 0 if is_dot or is_big_dot: maze.maze[y][x] = 0 + # munch_sound.play(1, fade_ms=1) + return (is_dot or is_free or is_big_dot) @@ -77,13 +85,21 @@ class Game(): return True - # Main game loop + pygame.mixer.music.load('../assets/sfx/game_start.wav') + siren_sound = pygame.mixer.Sound('../assets/sfx/siren_1.wav') + munch_sound = pygame.mixer.Sound('../assets/sfx/munch_1.wav') + + pygame.mixer.music.play() + siren_sound.play(-1) running = True + # Main game loop while running: # setting game fps clock.tick(Settings.settings.fps) + + # counter logic for cycling between pacman different sprites if counter < 19: counter += 1 @@ -102,6 +118,7 @@ class Game(): running = False elif event.type == pygame.KEYDOWN: # Move the circle based on the pressed key + # if not pygame.mixer.get_busy(): if event.key == pygame.K_w: player.direction = DIRECTION.UP ty = -player.speed @@ -120,6 +137,7 @@ class Game(): ty = 0 # Necssarry to move only horizontal or vertical keys = pygame.key.get_pressed() + # if not pygame.mixer.get_busy(): if keys[pygame.K_w]: ty = -player.speed tx = 0 @@ -156,14 +174,17 @@ class Game(): blinky.move(maze.maze, player) pinky.move(maze.maze, player) - # inky.move(maze.maze, player) - # clyde.move(maze.maze, player) + inky.move(maze.maze, player) + clyde.move(maze.maze, player) + maze.draw_map(screen) + player.draw(screen, counter) blinky.draw(screen) pinky.draw(screen) - # inky.draw(screen) - # clyde.draw(screen) + inky.draw(screen) + clyde.draw(screen) + # Update the screen pygame.display.flip() diff --git a/src/Ghost.py b/src/Ghost.py index 17f5ed0..a26de62 100644 --- a/src/Ghost.py +++ b/src/Ghost.py @@ -1,12 +1,14 @@ import pygame import math +from util import get_sprites from Direction import DIRECTION import map as Map class Ghost(): - def __init__(self, color, x, y): + def __init__(self,sprite_sheet, color, x, y): self.x = x self.y = y + self.sprite = get_sprites(sprite_sheet) self.color = color self.last_move = 3 self.speed = 3 @@ -82,7 +84,5 @@ class Ghost(): def draw(self, screen): radius = 30 // 2 - pos = (self.x , self.y) - pygame.draw.circle(screen, self.color, pos, radius) - - + pos = (self.x - radius , self.y - radius) + screen.blit(self.sprite[0], pos) diff --git a/src/Player.py b/src/Player.py index 430a974..21c1f7e 100644 --- a/src/Player.py +++ b/src/Player.py @@ -1,33 +1,9 @@ from typing import List from Direction import DIRECTION +from util import get_sprites import pygame -def get_sprites(sprite_sheet) -> List: - sheet_width, sheet_height = sprite_sheet.get_size() - sprite_width, sprite_height = 32, 32 - rows = sheet_height // sprite_height - columns = sheet_width // sprite_width - sprites = [] - - for row in range(rows): - for col in range(columns): - x = col * sprite_width - y = row * sprite_height - - # Create a new surface for the current sprite and blit it from the - # sprite sheet onto this new surface - new_sprite_surface = pygame.Surface( - (sprite_width, sprite_height), pygame.SRCALPHA) - new_sprite_surface.blit( - sprite_sheet, (0, 0), (x, y, x + sprite_width, y + sprite_height)) - - # Add this new surface to our list of sprites - sprites.append(new_sprite_surface) - - return sprites - - class Player(): def __init__(self, sprite_sheet): self.x = 30 * 17 - 15 diff --git a/src/blinky.py b/src/blinky.py index d30bc52..ca647e3 100644 --- a/src/blinky.py +++ b/src/blinky.py @@ -1,8 +1,8 @@ from Ghost import Ghost class Blinky(Ghost): - def __init__(self, x, y): - super().__init__("red", x, y) + def __init__(self, sprite_sheet, x, y): + super().__init__(sprite_sheet, "red", x, y) diff --git a/src/clyde.py b/src/clyde.py new file mode 100644 index 0000000..ae96bfb --- /dev/null +++ b/src/clyde.py @@ -0,0 +1,9 @@ +from Ghost import Ghost + +class Clyde(Ghost): + def __init__(self, sprite_sheet, x, y): + super().__init__(sprite_sheet, "red", x, y) + + + + diff --git a/src/inky.py b/src/inky.py new file mode 100644 index 0000000..926a8d1 --- /dev/null +++ b/src/inky.py @@ -0,0 +1,9 @@ +from Ghost import Ghost + +class Inky(Ghost): + def __init__(self, sprite_sheet, x, y): + super().__init__(sprite_sheet, "red", x, y) + + + + diff --git a/src/pinky.png b/src/pinky.png new file mode 100644 index 0000000..59661ee Binary files /dev/null and b/src/pinky.png differ diff --git a/src/pinky.py b/src/pinky.py index b512bb9..f89865d 100644 --- a/src/pinky.py +++ b/src/pinky.py @@ -5,8 +5,8 @@ import math from Ghost import Ghost class Pinky(Ghost): - def __init__(self, x, y): - super().__init__("pink", x, y) + def __init__(self, sprite_sheet, x, y): + super().__init__(sprite_sheet,"pink", x, y) @@ -59,7 +59,6 @@ class Pinky(Ghost): forbidden = 1 new_target = self.get_four_tiles_ahead_of_pacman(target) - print((target.x, target.y), new_target) for i in range(len(dx)): if i != forbidden: diff --git a/src/util.py b/src/util.py new file mode 100644 index 0000000..66fecb4 --- /dev/null +++ b/src/util.py @@ -0,0 +1,28 @@ +from typing import List +import pygame + + +def get_sprites(sprite_sheet) -> List: + sheet_width, sheet_height = sprite_sheet.get_size() + sprite_width, sprite_height = 32, 32 + rows = sheet_height // sprite_height + columns = sheet_width // sprite_width + sprites = [] + + for row in range(rows): + for col in range(columns): + x = col * sprite_width + y = row * sprite_height + + # Create a new surface for the current sprite and blit it from the + # sprite sheet onto this new surface + new_sprite_surface = pygame.Surface( + (sprite_width, sprite_height), pygame.SRCALPHA) + new_sprite_surface.blit( + sprite_sheet, (0, 0), (x, y, x + sprite_width, y + sprite_height)) + + # Add this new surface to our list of sprites + sprites.append(new_sprite_surface) + + return sprites + -- cgit v1.2.3