diff options
| -rw-r--r-- | src/Direction.py | 7 | ||||
| -rw-r--r-- | src/Player.py | 29 | ||||
| -rw-r--r-- | src/blinky.py | 2 | ||||
| -rw-r--r-- | src/clyde.py | 2 | ||||
| -rw-r--r-- | src/direction.py | 7 | ||||
| -rw-r--r-- | src/game.py (renamed from src/Game.py) | 106 | ||||
| -rw-r--r-- | src/ghost.py (renamed from src/Ghost.py) | 30 | ||||
| -rw-r--r-- | src/inky.py | 2 | ||||
| -rw-r--r-- | src/macpan.py | 6 | ||||
| -rw-r--r-- | src/pinky.py | 6 | ||||
| -rw-r--r-- | src/player.py | 55 |
11 files changed, 119 insertions, 133 deletions
diff --git a/src/Direction.py b/src/Direction.py deleted file mode 100644 index 076a754..0000000 --- a/src/Direction.py +++ /dev/null @@ -1,7 +0,0 @@ -from enum import Enum - -class DIRECTION(Enum): - UP = 1 - DOWN = 2 - RIGHT = 3 - LEFT = 4 diff --git a/src/Player.py b/src/Player.py deleted file mode 100644 index 21c1f7e..0000000 --- a/src/Player.py +++ /dev/null @@ -1,29 +0,0 @@ -from typing import List -from Direction import DIRECTION -from util import get_sprites -import pygame - - -class Player(): - def __init__(self, sprite_sheet): - self.x = 30 * 17 - 15 - self.y = 30 * 25 - 15 - self.sprite = get_sprites(sprite_sheet) - self.speed = 6 - self.direction = DIRECTION.LEFT - - def draw(self, screen, counter): - radius = 30 // 2 - pos = (self.x - radius , self.y - radius) - # pygame.draw.circle(screen, 'green', pos, radius) - if self.direction == DIRECTION.UP: - screen.blit(pygame.transform.rotate( - self.sprite[counter // 5], 270), pos) - elif self.direction == DIRECTION.DOWN: - screen.blit(pygame.transform.rotate( - self.sprite[counter // 5], 90), pos) - elif self.direction == DIRECTION.RIGHT: - screen.blit(pygame.transform.flip( - self.sprite[counter // 5], True, False), pos) - elif self.direction == DIRECTION.LEFT: - screen.blit(self.sprite[counter // 5], pos) diff --git a/src/blinky.py b/src/blinky.py index ca647e3..eee44a8 100644 --- a/src/blinky.py +++ b/src/blinky.py @@ -1,4 +1,4 @@ -from Ghost import Ghost +from ghost import Ghost class Blinky(Ghost): def __init__(self, sprite_sheet, x, y): diff --git a/src/clyde.py b/src/clyde.py index ae96bfb..560c7cb 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -1,4 +1,4 @@ -from Ghost import Ghost +from ghost import Ghost class Clyde(Ghost): def __init__(self, sprite_sheet, x, y): diff --git a/src/direction.py b/src/direction.py new file mode 100644 index 0000000..2ee3ba1 --- /dev/null +++ b/src/direction.py @@ -0,0 +1,7 @@ +from enum import Enum + +class DIRECTION(Enum): + RIGHT = 0 + DOWN = 1 + LEFT = 2 + UP = 3 diff --git a/src/Game.py b/src/game.py index 0d517a1..b2ff3b7 100644 --- a/src/Game.py +++ b/src/game.py @@ -1,39 +1,46 @@ -import Player -from pinky import Pinky from blinky import Blinky -from inky import Inky from clyde import Clyde -from Direction import DIRECTION -import settings as Settings +from direction import DIRECTION +from inky import Inky +from pinky import Pinky +from player import Player +from settings import settings import map as Map import pygame +WIDTH = settings.width +HEIGHT = settings.height + class Game(): def __init__(self): - self.settings = Settings.settings + self.settings = settings - def init(self): + def run(self): # Initialize Pygame pygame.init() # Set the dimensions of the window - screen = pygame.display.set_mode( - (Settings.settings.width, Settings.settings.height)) + screen = pygame.display.set_mode((WIDTH, HEIGHT)) # 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) + sprite_sheet = pygame.image.load( '../assets/pacman_left_sprite.png').convert_alpha() + + # Sprite sheets for the ghosts + 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() + + # 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,75, 75) pinky = Pinky(pinky_sprite, 27 * 30, 30 * 30 + 15) inky = Inky(inky_sprite, 75, 30 * 30 + 15) @@ -48,43 +55,6 @@ class Game(): clock = pygame.time.Clock() - - maze = Map.Map() - - # length of the map grid size - grid_x = Settings.settings.width // len(maze.maze[0]) - grid_y = Settings.settings.height // len(maze.maze) - - - # Checks collision with walls - - # checks if the current position of pacman is either a dot, big dot or free - def is_valid(x, y): - is_dot = maze.maze[y][x] == Map.D - is_big_dot = maze.maze[y][x] == Map.BD - 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) - - - # checks collision with pacman and obstacles returns false if there is a collision and true otherwise - def check_collision(dx, dy): - direct_x = [1, 0, -1, 0, 1, 1, -1, -1] - direct_y = [0, 1, 0, -1, -1, 1, -1, 1] - - for i in range(len(direct_x)): - nx = (player.x + dx) + direct_x[i] * 14 - ny = (player.y + dy) + direct_y[i] * 14 - x = nx // grid_x - y = ny // grid_y - if not is_valid(x, y): - return False - - return True - 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') @@ -93,12 +63,11 @@ class Game(): siren_sound.play(-1) running = True + # Main game loop while running: # setting game fps - clock.tick(Settings.settings.fps) - - + clock.tick(settings.fps) # counter logic for cycling between pacman different sprites if counter < 19: @@ -118,7 +87,6 @@ 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 @@ -137,24 +105,29 @@ class Game(): ty = 0 # Necssarry to move only horizontal or vertical keys = pygame.key.get_pressed() - # if not pygame.mixer.get_busy(): + + # Simulates holding the key which adds better playability for pacman if keys[pygame.K_w]: + player.direction = DIRECTION.UP ty = -player.speed tx = 0 elif keys[pygame.K_s]: + player.direction = DIRECTION.DOWN ty = player.speed tx = 0 elif keys[pygame.K_a]: + player.direction = DIRECTION.LEFT tx = -player.speed ty = 0 elif keys[pygame.K_d]: + player.direction = DIRECTION.RIGHT tx = player.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 check_collision(tx, ty): + if player.check_collision(maze, tx, ty, TILE_WIDTH, TILE_HEIGHT): dx = tx dy = ty @@ -167,18 +140,21 @@ class Game(): elif dy > 0: player.direction = DIRECTION.DOWN - if check_collision(dx, dy): + if player.check_collision(maze, dx, dy, TILE_WIDTH, TILE_HEIGHT): player.x += dx player.y += dy + # Move ghosts blinky.move(maze.maze, player) pinky.move(maze.maze, player) inky.move(maze.maze, player) clyde.move(maze.maze, player) + # Draw the map on each frame maze.draw_map(screen) + # Draw the player and the ghosts player.draw(screen, counter) blinky.draw(screen) pinky.draw(screen) diff --git a/src/Ghost.py b/src/ghost.py index 668c943..98b7deb 100644 --- a/src/Ghost.py +++ b/src/ghost.py @@ -3,6 +3,12 @@ from util import get_sprites from settings import settings import map as Map +dx = [1, 0, -1, 0] +dy = [0, 1, 0, -1] + +inv_dir = [2, 3, 0, 1] +sprite_sheet = [2, 0, 3, 1] + class Ghost(): def __init__(self,sprite_sheet, color, x, y): self.x = x @@ -46,21 +52,10 @@ class Ghost(): def get_next_move(self, pacman, maze): - dx = [1, 0, -1, 0] - dy = [0, 1, 0, -1] ret = len(dx) * [math.inf] - forbidden = 0 - - if self.last_move == 0: - forbidden = 2 - if self.last_move == 1: - forbidden = 3 - if self.last_move == 2: - forbidden = 0 - if self.last_move == 3: - forbidden = 1 + forbidden = inv_dir[self.last_move] for i in range(len(dx)): if i != forbidden: @@ -75,8 +70,6 @@ class Ghost(): def move(self, maze, pacman): min_idx = self.get_next_move(pacman, maze) - dx = [1, 0, -1, 0] - dy = [0, 1, 0, -1] new_dx = dx[min_idx] * self.speed new_dy = dy[min_idx] * self.speed self.x += new_dx @@ -86,11 +79,4 @@ class Ghost(): def draw(self, screen): radius = 30 // 2 pos = (self.x - radius , self.y - radius) - if self.last_move == 0: - screen.blit(self.sprite[2], pos) - elif self.last_move == 1: - screen.blit(self.sprite[0], pos) - elif self.last_move == 2: - screen.blit(self.sprite[3], pos) - elif self.last_move == 3: - screen.blit(self.sprite[1], pos) + screen.blit(self.sprite[sprite_sheet[self.last_move]], pos) diff --git a/src/inky.py b/src/inky.py index 926a8d1..95eb80b 100644 --- a/src/inky.py +++ b/src/inky.py @@ -1,4 +1,4 @@ -from Ghost import Ghost +from ghost import Ghost class Inky(Ghost): def __init__(self, sprite_sheet, x, y): diff --git a/src/macpan.py b/src/macpan.py index a96a5fc..712fdde 100644 --- a/src/macpan.py +++ b/src/macpan.py @@ -1,5 +1,5 @@ -import Game +from game import Game if __name__ == "__main__": - game = Game.Game() - game.init() + game = Game() + game.run() diff --git a/src/pinky.py b/src/pinky.py index c8324bb..8b2bee1 100644 --- a/src/pinky.py +++ b/src/pinky.py @@ -1,8 +1,8 @@ from typing_extensions import override -from Direction import DIRECTION +from direction import DIRECTION from settings import settings import math -from Ghost import Ghost +from ghost import Ghost class Pinky(Ghost): def __init__(self, sprite_sheet, x, y): @@ -10,7 +10,6 @@ class Pinky(Ghost): def get_four_tiles_ahead_of_pacman(self, pacman): - print("Before", pacman.x, pacman.y) if pacman.direction == DIRECTION.UP: new_target = (pacman.x - 30 * 4, pacman.y - 30 * 4) if self.in_bounds(new_target): @@ -55,7 +54,6 @@ class Pinky(Ghost): forbidden = 1 new_target = self.get_four_tiles_ahead_of_pacman(target) - print("After: ", new_target) for i in range(len(dx)): if i != forbidden: diff --git a/src/player.py b/src/player.py new file mode 100644 index 0000000..34dc4a4 --- /dev/null +++ b/src/player.py @@ -0,0 +1,55 @@ +from typing import List +from direction import DIRECTION +import map as Map +from util import get_sprites +import pygame + + +class Player(): + def __init__(self, sprite_sheet): + self.x = 30 * 17 - 15 + self.y = 30 * 25 - 15 + self.sprite = get_sprites(sprite_sheet) + self.speed = 6 + self.direction = DIRECTION.LEFT + + # checks if the current position of pacman is either a dot, big dot or free + def is_valid(self,maze, x, y): + is_dot = maze.maze[y][x] == Map.D + is_big_dot = maze.maze[y][x] == Map.BD + is_free = maze.maze[y][x] == 0 + if is_dot or is_big_dot: + maze.maze[y][x] = 0 + + return (is_dot or is_free or is_big_dot) + + + # checks collision with pacman and obstacles returns false if there is a collision and true otherwise + def check_collision(self, maze, dx, dy, tile_width, tile_height): + direct_x = [1, 0, -1, 0, 1, 1, -1, -1] + direct_y = [0, 1, 0, -1, -1, 1, -1, 1] + + for i in range(len(direct_x)): + ddx = dx + ddy = dy + nx = (self.x + ddx) + direct_x[i] * 14 + ny = (self.y + ddy) + direct_y[i] * 14 + x = nx // tile_width + y = ny // tile_height + if not self.is_valid(maze, x, y): + return False + + return True + + def draw(self, screen, counter): + radius = 30 // 2 + pos = (self.x - radius , self.y - radius) + # pygame.draw.circle(screen, 'green', pos, radius) + if self.direction == DIRECTION.UP: + screen.blit(pygame.transform.rotate(self.sprite[counter // 5], 270), pos) + elif self.direction == DIRECTION.DOWN: + screen.blit(pygame.transform.rotate(self.sprite[counter // 5], 90), pos) + elif self.direction == DIRECTION.RIGHT: + screen.blit(pygame.transform.flip(self.sprite[counter // 5], True, False), pos) + elif self.direction == DIRECTION.LEFT: + screen.blit(self.sprite[counter // 5], pos) |
