From c7c473177086399a8fb97936b4c3c2b67a43fce0 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 13:07:48 +0300 Subject: Finished inky's algorithm and also added an option in settings to disable audio --- src/player.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/player.py') diff --git a/src/player.py b/src/player.py index 2c4c4f4..903c358 100644 --- a/src/player.py +++ b/src/player.py @@ -1,4 +1,3 @@ -from typing import List from direction import DIRECTION import map as Map from util import get_sprites @@ -14,7 +13,7 @@ class Player(): 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): + def is_valid(self, maze, x, y): if x >= 0 and x < 30: is_dot = maze.maze[y][x] == Map.D is_big_dot = maze.maze[y][x] == Map.BD @@ -24,8 +23,8 @@ class Player(): return (is_dot or is_free or is_big_dot) return True - - # checks collision with pacman and obstacles returns false if there is a collision and true otherwise + # 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] @@ -44,7 +43,7 @@ class Player(): def draw(self, screen, counter): radius = 30 // 2 - pos = (self.x - radius , self.y - radius) + pos = (self.x - radius, self.y - radius) image = pygame.transform.scale(self.sprite[counter // 5], (35, 35)) if self.direction == DIRECTION.UP: screen.blit(pygame.transform.rotate(image, 270), pos) -- cgit v1.2.3 From 9621f880a03337a8a252cf5cd3993d5a1a29969c Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 17:59:55 +0300 Subject: Added frightened mode --- src/player.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src/player.py') diff --git a/src/player.py b/src/player.py index 903c358..97d2ea5 100644 --- a/src/player.py +++ b/src/player.py @@ -1,4 +1,5 @@ from direction import DIRECTION +from timer import Timer import map as Map from util import get_sprites import pygame @@ -8,9 +9,12 @@ class Player(): def __init__(self, sprite_sheet): self.x = 30 * 17 - 15 self.y = 30 * 25 - 15 + self.sprite_sheet = sprite_sheet self.sprite = get_sprites(sprite_sheet) self.speed = 6 self.direction = DIRECTION.LEFT + self.powerup = False + self.timer = None # checks if the current position of pacman is either a dot, big dot or free def is_valid(self, maze, x, y): @@ -20,6 +24,9 @@ class Player(): is_free = maze.maze[y][x] == 0 if is_dot or is_big_dot: maze.maze[y][x] = 0 + if is_big_dot: + self.powerup = True + self.timer = Timer(5 * 1000) return (is_dot or is_free or is_big_dot) return True @@ -42,14 +49,28 @@ class Player(): return True def draw(self, screen, counter): + if self.timer is not None: + elapsed_time = pygame.time.get_ticks() - self.timer.start + if elapsed_time > self.timer.duration: + self.powerup = False + radius = 30 // 2 pos = (self.x - radius, self.y - radius) - image = pygame.transform.scale(self.sprite[counter // 5], (35, 35)) - if self.direction == DIRECTION.UP: - screen.blit(pygame.transform.rotate(image, 270), pos) - elif self.direction == DIRECTION.DOWN: - screen.blit(pygame.transform.rotate(image, 90), pos) - elif self.direction == DIRECTION.RIGHT: - screen.blit(pygame.transform.flip(image, True, False), pos) - elif self.direction == DIRECTION.LEFT: + sprite_sheet = [2, 0, 3, 1] + if self.powerup: + self.sprite = get_sprites(pygame.image.load( + '../assets/pacman_as_ghost.png').convert_alpha()) + image = pygame.transform.scale( + self.sprite[sprite_sheet[self.direction.value]], (40, 40)) screen.blit(image, pos) + else: + self.sprite = get_sprites(self.sprite_sheet) + image = pygame.transform.scale(self.sprite[counter // 5], (35, 35)) + if self.direction == DIRECTION.UP: + screen.blit(pygame.transform.rotate(image, 270), pos) + elif self.direction == DIRECTION.DOWN: + screen.blit(pygame.transform.rotate(image, 90), pos) + elif self.direction == DIRECTION.RIGHT: + screen.blit(pygame.transform.flip(image, True, False), pos) + elif self.direction == DIRECTION.LEFT: + screen.blit(image, pos) -- cgit v1.2.3 From aaf0194f9b5d93bd6612bc0b419c4b8f89b4aa21 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 23:02:03 +0300 Subject: Added a simple Wining screen when the user collects all the food --- src/player.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/player.py') diff --git a/src/player.py b/src/player.py index 97d2ea5..92c4a84 100644 --- a/src/player.py +++ b/src/player.py @@ -17,22 +17,26 @@ class Player(): self.timer = None # checks if the current position of pacman is either a dot, big dot or free - def is_valid(self, maze, x, y): + def is_valid(self, game_state, x, y): if x >= 0 and x < 30: - is_dot = maze.maze[y][x] == Map.D - is_big_dot = maze.maze[y][x] == Map.BD - is_free = maze.maze[y][x] == 0 + is_dot = game_state.map.maze[y][x] == Map.D + is_big_dot = game_state.map.maze[y][x] == Map.BD + is_free = game_state.map.maze[y][x] == 0 if is_dot or is_big_dot: - maze.maze[y][x] = 0 + game_state.map.maze[y][x] = 0 + game_state.food += 1 + if is_dot: + game_state.score += 10 if is_big_dot: self.powerup = True self.timer = Timer(5 * 1000) + game_state.score += 50 return (is_dot or is_free or is_big_dot) return True # 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): + def check_collision(self, game_state, 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] @@ -43,7 +47,7 @@ class Player(): ny = (self.y + ddy) + direct_y[i] * 14 x = nx // tile_width y = ny // tile_height - if not self.is_valid(maze, x, y): + if not self.is_valid(game_state, x, y): return False return True -- cgit v1.2.3