diff options
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | assets/greenboxz.png | bin | 0 -> 1004 bytes | |||
| -rw-r--r-- | src/GUIbutton.py | 41 | ||||
| -rw-r--r-- | src/MacPan.py (renamed from src/GUI.py) | 78 | ||||
| -rw-r--r-- | src/Testfunction.py (renamed from src/macpan.py) | 0 | ||||
| -rw-r--r-- | src/blinky.py | 5 | ||||
| -rw-r--r-- | src/clyde.py | 10 | ||||
| -rw-r--r-- | src/game.py | 42 | ||||
| -rw-r--r-- | src/game_state.py | 22 | ||||
| -rw-r--r-- | src/ghost.py | 12 | ||||
| -rw-r--r-- | src/inky.py | 6 | ||||
| -rw-r--r-- | src/pinky.py | 6 | ||||
| -rw-r--r-- | src/player.py | 16 | ||||
| -rw-r--r-- | src/settings.py | 11 |
14 files changed, 179 insertions, 72 deletions
@@ -8,7 +8,7 @@ ```bash cd src -python3 macpan.py +python3 Testfunction.py ``` # TODO diff --git a/assets/greenboxz.png b/assets/greenboxz.png Binary files differnew file mode 100644 index 0000000..1c8ec89 --- /dev/null +++ b/assets/greenboxz.png diff --git a/src/GUIbutton.py b/src/GUIbutton.py index 794f693..2c75a9c 100644 --- a/src/GUIbutton.py +++ b/src/GUIbutton.py @@ -1,3 +1,5 @@ + + class Button(): def __init__(self, image, pos, text_input, font, base_color, hovering_color): self.image = image @@ -26,4 +28,41 @@ class Button(): if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom): self.text = self.font.render(self.text_input, True, self.hovering_color) else: - self.text = self.font.render(self.text_input, True, self.base_color)
\ No newline at end of file + self.text = self.font.render(self.text_input, True, self.base_color) + +class ToggleSwitch(): + def __init__(self, image, pos, text_input, font, base_color, hovering_color): + self.image = image + self.x_pos = pos[0] + self.y_pos = pos[1] + self.font = font + self.base_color, self.hovering_color = base_color, hovering_color + self.text_input = text_input + self.text = self.font.render(self.text_input, True, self.base_color) + if self.image is None: + self.image = self.text + self.rect = self.image.get_rect(center=(self.x_pos, self.y_pos)) + self.text_rect = self.text.get_rect(center=(self.x_pos, self.y_pos)) + + def update(self, screen): + if self.image is not None: + screen.blit(self.image, self.rect) + screen.blit(self.text, self.text_rect) + + def checkForInput(self, position): + if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, + self.rect.bottom): + return True + return False + + def changeColor(self, position): + if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, + self.rect.bottom): + self.text = self.font.render(self.text_input, True, self.hovering_color) + else: + self.text = self.font.render(self.text_input, True, self.base_color) + + def changeimage(self, image): + self.image = image + + diff --git a/src/GUI.py b/src/MacPan.py index 21cec17..f23b752 100644 --- a/src/GUI.py +++ b/src/MacPan.py @@ -1,14 +1,15 @@ import pygame import sys -from GUIbutton import Button + +from GUIbutton import Button, ToggleSwitch from src.game import Game import ctypes -# icon on taskbar +from src.settings import Settings, settings -myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string -ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) +# icon on taskbar +ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('Pacman') pygame.init() @@ -20,10 +21,15 @@ BG = pygame.image.load("../assets/Background.png") icon = pygame.image.load("../assets/icon.png") pygame.display.set_icon(icon) +lolsettings = settings +lolsettings.debug = False -def get_font(size): # Returns Press-Start-2P in the desired size - return pygame.font.Font("../assets/PAC-FONT.TTF", size) +def get_font(size, number): # Returns Press-Start-2P in the desired size + if number == 1: + return pygame.font.Font("../assets/PressStart2P-Regular.ttf", size) + else: + return pygame.font.Font("../assets/PAC-FONT.TTF", size) # def play(): @@ -57,17 +63,32 @@ def options(): while True: OPTIONS_MOUSE_POS = pygame.mouse.get_pos() - SCREEN.fill("white") + SCREEN.fill("#1e1f22") - OPTIONS_TEXT = get_font(45).render("This is the OPTIONS screen.", True, "Black") - OPTIONS_RECT = OPTIONS_TEXT.get_rect(center=(640, 260)) + OPTIONS_TEXT = get_font(45, 1).render("OPTIONS SCREEN", True, "white") + OPTIONS_RECT = OPTIONS_TEXT.get_rect(center=(630, 150)) SCREEN.blit(OPTIONS_TEXT, OPTIONS_RECT) - OPTIONS_BACK = Button(image=None, pos=(640, 460), - text_input="BACK", font=get_font(75), base_color="Black", hovering_color="Green") + OPTIONS_BACK = Button(image=None, pos=(640, 520), + text_input="BACK", font=get_font(75, 1), base_color="white", hovering_color="yellow") + + Debug_Mode = ToggleSwitch(image=pygame.image.load('../assets/Quit Rect.png'), pos=(300, 300), + text_input="Debug Mode", font=get_font(20, 1), base_color="Black", + hovering_color="cyan") + + Sound_Mode = ToggleSwitch(image=pygame.image.load('../assets/Quit Rect.png'), pos=(900, 300), + text_input="Sound ", font=get_font(20, 1), base_color="Black", + hovering_color="cyan") + + # Debug_Mode_RECT = Debug_Mode.get_rect(center=(640, 150)) + # SCREEN.blit(Debug_Mode_RECT,Debug_Mode) OPTIONS_BACK.changeColor(OPTIONS_MOUSE_POS) OPTIONS_BACK.update(SCREEN) + Debug_Mode.changeColor(OPTIONS_MOUSE_POS) + Debug_Mode.update(SCREEN) + Sound_Mode.changeColor(OPTIONS_MOUSE_POS) + Sound_Mode.update(SCREEN) for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -76,25 +97,45 @@ def options(): if event.type == pygame.MOUSEBUTTONDOWN: if OPTIONS_BACK.checkForInput(OPTIONS_MOUSE_POS): main_menu() + elif Debug_Mode.checkForInput(OPTIONS_MOUSE_POS): + if lolsettings.debug : + lolsettings.debug = False + Debug_Mode.image = (pygame.image.load('../assets/Quit Rect.png')) + Debug_Mode.update(SCREEN) + options() + + else: + lolsettings.debug = True + Debug_Mode.image = (pygame.image.load('../assets/greenboxz.png')) + Debug_Mode.update(SCREEN) + elif Sound_Mode.checkForInput(OPTIONS_MOUSE_POS): + if lolsettings.sound : + lolsettings.sound=False + else: + lolsettings.sound = True + pygame.display.update() def main_menu(): + # Initialize Pygame + pygame.init() while True: SCREEN.blit(BG, (0, 0)) MENU_MOUSE_POS = pygame.mouse.get_pos() - MENU_TEXT = get_font(100).render("Mac-Pan", True, "#b68f40") + MENU_TEXT = get_font(100, 2).render("Mac-Pan", True, "#b68f40") MENU_RECT = MENU_TEXT.get_rect(center=(640, 100)) PLAY_BUTTON = Button(image=pygame.image.load("../assets/Play Rect.png"), pos=(640, 250), - text_input="PLAY", font=get_font(75), base_color="#d7fcd4", hovering_color="White") + text_input="PLAY", font=get_font(75, 2), base_color="#d7fcd4", hovering_color="White") OPTIONS_BUTTON = Button(image=pygame.image.load("../assets/Options Rect.png"), pos=(640, 400), - text_input="OPTIONS", font=get_font(75), base_color="#d7fcd4", hovering_color="White") + text_input="OPTIONS", font=get_font(75, 2), base_color="#d7fcd4", + hovering_color="White") QUIT_BUTTON = Button(image=pygame.image.load("../assets/Quit Rect.png"), pos=(640, 550), - text_input="QUIT", font=get_font(75), base_color="#d7fcd4", hovering_color="White") + text_input="QUIT", font=get_font(75, 2), base_color="#d7fcd4", hovering_color="White") SCREEN.blit(MENU_TEXT, MENU_RECT) @@ -108,10 +149,9 @@ def main_menu(): sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if PLAY_BUTTON.checkForInput(MENU_MOUSE_POS): - #play() - game = Game() + # play() + game = Game(lolsettings) game.run() - if OPTIONS_BUTTON.checkForInput(MENU_MOUSE_POS): options() if QUIT_BUTTON.checkForInput(MENU_MOUSE_POS): @@ -121,6 +161,4 @@ def main_menu(): pygame.display.update() - - main_menu() diff --git a/src/macpan.py b/src/Testfunction.py index 712fdde..712fdde 100644 --- a/src/macpan.py +++ b/src/Testfunction.py diff --git a/src/blinky.py b/src/blinky.py index 4c45644..b1bdcb4 100644 --- a/src/blinky.py +++ b/src/blinky.py @@ -2,5 +2,6 @@ from ghost import Ghost class Blinky(Ghost): - def __init__(self, sprite_sheet, x, y): - super().__init__(sprite_sheet, "red", x, y) + def __init__(self, sprite_sheet, x, y,settings): + super().__init__(sprite_sheet, "red", x, y, settings) + diff --git a/src/clyde.py b/src/clyde.py index 2fd4aee..acb9f42 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -8,8 +8,8 @@ import math class Clyde(Ghost): - def __init__(self, sprite_sheet, x, y): - super().__init__(sprite_sheet, "cyan", x, y) + def __init__(self, sprite_sheet, x, y,settings): + super().__init__(sprite_sheet, "cyan", x, y,settings) def is_eight_tiles_away(self, pacman): tile_width = 30 @@ -49,7 +49,7 @@ class Clyde(Ghost): if game_state.pacman.powerup is False and self.mode == MODE.FRIGHETENED: self.mode = MODE.CHASING - if settings.debug: + if self.settings.debug: pygame.draw.line(screen, self.color, (game_state.pacman.x, game_state.pacman.y), (self.x, self.y), 1) @@ -68,13 +68,13 @@ class Clyde(Ghost): if self.is_eight_tiles_away(game_state.pacman): ret[i] = self.heuristic( (nx, ny), bottom_left_corner[0], bottom_left_corner[1]) - if settings.debug: + if self.settings.debug: pygame.draw.line(screen, self.color, (bottom_left_corner), (self.x, self.y), 1) else: ret[i] = self.heuristic( (nx, ny), game_state.pacman.x, game_state.pacman.y) - if settings.debug: + if self.settings.debug: pygame.draw.line(screen, self.color, (game_state.pacman.x, game_state.pacman.y), (self.x, self.y), 1) elif self.mode == MODE.EATEN: 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() diff --git a/src/game_state.py b/src/game_state.py index 5ed98c0..41e7985 100644 --- a/src/game_state.py +++ b/src/game_state.py @@ -15,32 +15,32 @@ TILE_HEIGHT = HEIGHT // len(maze.maze) class GameState(): - def __init__(self, sprites): - self.pacman = Player(sprites[0]) + def __init__(self, sprites, settings): + self.pacman = Player(sprites[0],settings) self.blinky = Blinky(sprites[1], 12 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.pinky = Pinky(sprites[2], 11 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.inky = Inky(sprites[3], 13 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.clyde = Clyde(sprites[4], 14 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.map = Map.Map() self.food = 0 self.game_over = False self.score = 0 self.is_pacman_alive = True - def reset(self, sprites): + def reset(self, sprites ,settings): self.pacman = Player(sprites[0]) self.blinky = Blinky(sprites[1], 12 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.pinky = Pinky(sprites[2], 11 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.inky = Inky(sprites[3], 13 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.clyde = Clyde(sprites[4], 14 * TILE_WIDTH + - 15, 12 * TILE_HEIGHT + 15) + 15, 12 * TILE_HEIGHT + 15,settings) self.map = Map.Map() self.food = 0 self.game_over = False diff --git a/src/ghost.py b/src/ghost.py index 8568ff4..37c3d37 100644 --- a/src/ghost.py +++ b/src/ghost.py @@ -17,7 +17,7 @@ sprite_sheet = [2, 0, 3, 1] class Ghost(): - def __init__(self, sprite_sheet, color, x, y): + def __init__(self, sprite_sheet, color, x, y ,lol): self.x = x self.y = y self.sprite_sheet = sprite_sheet @@ -28,6 +28,7 @@ class Ghost(): self.speed = 3 self.timer = None self.mode = MODE.SCATTERED + self.settings = lol def in_bounds(self, pos): (x, y) = pos @@ -69,14 +70,19 @@ class Ghost(): return True def check_pacman_collision(self, game_state): + voiceofdot2 = pygame.mixer.Channel(4) + dotpickup = pygame.mixer.Sound('../assets/sfx/eat_ghost.wav') + if game_state.pacman.powerup and abs(game_state.pacman.x - self.x) <= 30 and abs(game_state.pacman.y - self.y) <= 30: initial_position = self.get_intial_tile() self.mode = MODE.EATEN self.timer = Timer(2 * 1000) - time.sleep(1) + time.sleep(0.3) game_state.score += 200 self.x = initial_position[0] self.y = initial_position[1] + if self.settings.sound: + voiceofdot2.play(dotpickup) elif not game_state.pacman.powerup and abs(game_state.pacman.x - self.x) <= 30 and abs(game_state.pacman.y - self.y) <= 30: if abs(game_state.pacman.x - self.x) <= 30 and abs(game_state.pacman.y - self.y) <= 30: game_state.is_pacman_alive = False @@ -116,7 +122,7 @@ class Ghost(): pos = self.get_intial_tile() self.x = pos[0] self.y = pos[1] - if settings.debug: + if self.settings.debug: pygame.draw.line(screen, self.color, (game_state.pacman.x, game_state.pacman.y), (self.x, self.y), 1) diff --git a/src/inky.py b/src/inky.py index be2e62c..e75b379 100644 --- a/src/inky.py +++ b/src/inky.py @@ -9,8 +9,8 @@ from ghost import Ghost class Inky(Ghost): - def __init__(self, sprite_sheet, x, y): - super().__init__(sprite_sheet, "orange", x, y) + def __init__(self, sprite_sheet, x, y,settings): + super().__init__(sprite_sheet, "orange", x, y,settings) def get_intermediate_tile(self, pacman): if pacman.direction == DIRECTION.UP: @@ -83,7 +83,7 @@ class Inky(Ghost): if game_state.pacman.powerup is False and self.mode == MODE.FRIGHETENED: self.mode = MODE.CHASING - if settings.debug: + if self.settings.debug: pygame.draw.line(screen, self.color, (target), (self.x, self.y), 1) diff --git a/src/pinky.py b/src/pinky.py index bb50c5b..e02fbaa 100644 --- a/src/pinky.py +++ b/src/pinky.py @@ -9,8 +9,8 @@ from ghost import Ghost class Pinky(Ghost): - def __init__(self, sprite_sheet, x, y): - super().__init__(sprite_sheet, "pink", x, y) + def __init__(self, sprite_sheet, x, y,settings): + super().__init__(sprite_sheet, "pink", x, y,settings) def get_four_tiles_ahead_of_pacman(self, pacman): if pacman.direction == DIRECTION.UP: @@ -95,7 +95,7 @@ class Pinky(Ghost): pos = self.get_intial_tile() self.x = pos[0] self.y = pos[1] - if settings.debug: + if self.settings.debug: pygame.draw.line(screen, self.color, (new_target), (self.x, self.y), 1) diff --git a/src/player.py b/src/player.py index 92c4a84..590405a 100644 --- a/src/player.py +++ b/src/player.py @@ -6,7 +6,9 @@ import pygame class Player(): - def __init__(self, sprite_sheet): + + def __init__(self, sprite_sheet, settings): + self.settings = settings self.x = 30 * 17 - 15 self.y = 30 * 25 - 15 self.sprite_sheet = sprite_sheet @@ -18,6 +20,7 @@ class Player(): # checks if the current position of pacman is either a dot, big dot or free def is_valid(self, game_state, x, y): + if x >= 0 and x < 30: is_dot = game_state.map.maze[y][x] == Map.D is_big_dot = game_state.map.maze[y][x] == Map.BD @@ -27,6 +30,8 @@ class Player(): game_state.food += 1 if is_dot: game_state.score += 10 + + if is_big_dot: self.powerup = True self.timer = Timer(5 * 1000) @@ -53,11 +58,15 @@ class Player(): return True def draw(self, screen, counter): + voice = pygame.mixer.Channel(2) + Soundpowerup = pygame.mixer.Sound('../assets/sfx/power_pellet.wav') 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) sprite_sheet = [2, 0, 3, 1] @@ -67,7 +76,12 @@ class Player(): image = pygame.transform.scale( self.sprite[sprite_sheet[self.direction.value]], (40, 40)) screen.blit(image, pos) + if self.settings.sound: + if not (voice.get_busy()): + voice.play(Soundpowerup) + else: + voice.stop() self.sprite = get_sprites(self.sprite_sheet) image = pygame.transform.scale(self.sprite[counter // 5], (35, 35)) if self.direction == DIRECTION.UP: diff --git a/src/settings.py b/src/settings.py index a8dfa24..cb4ec0d 100644 --- a/src/settings.py +++ b/src/settings.py @@ -1,10 +1,9 @@ class Settings(): - def __init__(self): - self.width = 900 - self.height = 990 - self.fps = 60 - self.debug = False - self.sound = False + width = 900 + height = 990 + fps = 60 + debug = False + sound = False settings = Settings() |
