def __init__(self): """ Initialize default values """ self.CLOCK = pygame.time.Clock() self.WINDOW_WIDTH = 1080 self.WINDOW_HEIGHT = 720 self.WINDOW_TITLE = "Legend of Peach" self.WINDOW_ICON = pygame.image.load('imgs/WINDOW_ICON.png') self.BG_COLOR = (0, 0, 0) self.FRAMERATE = 30 self.speed = 0 # PHYSICS VALUES self.GRAVITY = 10 self.velocityAI = 5 self.velocityMushroom = 5 self.velocityStar = 10 # DEBUG MODE self.modeFreeze = False self.modePlace = False # Load mario levels from mario level self.game_levels = self.get_levels() self.gamemode = GameMode(self) self.controller = Controller(self) self.camera = Camera(self) # Screen selector chooses what screen to display self.screen_selector = 1 self.level_name = '' # STATES self.WALK = 'WALK' self.LEFT = 'LEFT' self.RIGHT = 'RIGHT' self.STOMPED = 'STOMPED' self.STAND = 'STAND' self.SHELL = 'SHELL' self.SLIDE = 'SLIDE' self.HIT = 'HIT' self.DEATHFALL = 'DEATHFALL' self.RISE = "RISE" self.FALL = "FALL" # Brick States self.RESTING = 'RESTING' self.BUMPED = 'BUMPED' self.OPENED = 'OPENED' """ Initialize the type of screen possible to display game_screen is the only one that will probably be reinstance everytime a new level opens. """ self.sound_board = SoundBoard() self.main_screen = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT)) self.hud_screen = HudScreen(self) self.main_menu_screen = MainMenuScreen(self) self.level_screen = LevelSelectionScreen(self) self.game_screen = GameScreen(self)
def test_swapper(): mode = GameMode() arrangement = [(Unit(SwapMover()), 2, 2)] mode.set_arrangement(arrangement) mode.set_size(8) board = Board(mode) board[5, 5] = None x = 2 y = 2 for dx in range(-10, 10): for dy in range(-10, 10): if dx == 0 and dy == 0: continue to_x = x + dx to_y = y + dy if valid(to_x, to_y, 8): assert board.do_pofig_move(x, y, to_x, to_y) is False x = 1 y = 1 arrangement.clear() arrangement.append((Unit(SwapMover()), 1, 1)) for dx in range(-1, 2): for dy in range(-1, 2): if dx == 0 and dy == 0: continue arrangement.append((Unit(KingMover()), x + dx, y + dy)) mode.set_arrangement(arrangement) board = Board(mode) for i, j in product(range(5, 8), range(5, 8)): assert board.do_pofig_move(x, y, i, j) is True assert board.do_pofig_move(i, j, x, y) is True
def test_usual(): mode = GameMode() mode.set_size(8) mode.set_arrangement([(Unit(UsualMover()), 2, 2)]) x = 2 y = 2 board = Board(mode) for dx in range(-10, 10): for dy in range(-10, 10): if dx == 0 and dy == 0: continue to_x = x + dx to_y = y + dy if valid(to_x, to_y, 8): if abs(dx) <= 1 and abs(dy) <= 1 and dx * dy == 0: assert board.do_pofig_move(x, y, to_x, to_y) is True assert board.do_pofig_move(to_x, to_y, x, y) is True else: assert board.do_pofig_move(x, y, to_x, to_y) is False arrangement = [(Unit(UsualMover()), 4, 4)] x = 4 y = 4 for dx in range(-1, 2): for dy in range(-1, 2): if (dx == 0 and dy == 0) or dx * dy != 0: continue arrangement.append((Unit(PawnMover()), x + dx, y + dy)) mode.set_size(20) mode.set_arrangement(arrangement) board = Board(mode) for dx in range(-2, 3, 2): for dy in range(-2, 3, 2): if (dx == 0 and dy == 0) or dx * dy != 0: continue to_x = x + dx to_y = y + dy assert board.do_pofig_move(x, y, to_x, to_y) is True assert board.do_pofig_move(to_x, to_y, x, y) is True
def test_bishop(): mode = GameMode() mode.set_size(8) mode.set_arrangement([(Unit(BishopMover()), 2, 2), (Unit(BishopMover()), 3, 3)]) board = Board(mode) x = 2 y = 2 assert board.do_pofig_move(2, 2, 4, 4) is False for i in range(8): for j in range(8): if i != x and j != y: board[i, j] = None for dx in range(-10, 10): for dy in range(-10, 10): if dx == 0 and dy == 0: continue to_x = x + dx to_y = y + dy if valid(to_x, to_y, 8): if abs(dx) == abs(dy): assert board.do_pofig_move(x, y, to_x, to_y) is True assert board.do_pofig_move(to_x, to_y, x, y) is True else: assert board.do_pofig_move(x, y, to_x, to_y) is False
def __init__(self): """ Initializing properties """ self.WINDOW_WIDTH = 1280 self.WINDOW_HEIGHT = 720 self.BG_COLOR = (125, 125, 125) self.main_screen = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT)) self.WINDOW_TITLE = "Space Invader by Randy Le" self.CLOCK = pygame.time.Clock() self.game_mode = GameMode(self) self.shoot_sound = pygame.mixer.Sound('wav/Laser.wav') self.enemy_dies_sound = pygame.mixer.Sound('wav/Enemy_Dies.wav') self.bg_music = pygame.mixer.Sound('wav/bg_music.wav') self.prep_sounds() self.play_bg_music() self.controller = { 'up': False, 'down': False, 'left': False, 'right': False, 'shooting': False } self.delayShot = 360 self.nextShot = pygame.time.get_ticks() + self.delayShot self.game_screen = GameScreen(self) self.main_menu_screen = MainMenuScreen(self) self.high_score_screen = HighScoreScreen(self) self.screen_type = { 'GameScreen': 1, 'MainMenuScreen': 2, 'HighScoreScreen': 3 } self.screen_mode = 2 self.high_score_board = [400, 600, 300, 25, 120, 1500]
def __init__(self): """ Initializing properties """ self.CLOCK = pygame.time.Clock() self.WINDOW_WIDTH = 1280 self.WINDOW_HEIGHT = 720 self.BG_COLOR = (125, 125, 125) self.main_screen = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT)) self.game_mode = GameMode(self) self.shoot_sound = pygame.mixer.Sound('wav/Laser.wav') self.enemy_dies_sound = pygame.mixer.Sound('wav/Enemy_Dies.wav') self.controller = { 'up': False, 'down': False, 'left': False, 'right': False } self.game_screen = GameScreen(self)
def test_pawn(): mode = GameMode() mode.set_size(8) arrangement = [(Unit(PawnMover()), 2, 2)] mode.set_arrangement(arrangement) board = Board(mode) x = 2 y = 2 for dx in range(-10, 10): for dy in range(-10, 10): if dx == 0 and dy == 0: continue to_x = x + dx to_y = y + dy if valid(to_x, to_y, 8): if abs(dx) <= 1 and abs(dy) <= 1 and dx * dy == 0: assert board.do_pofig_move(x, y, to_x, to_y) is True assert board.do_pofig_move(to_x, to_y, x, y) is True else: assert board.do_pofig_move(x, y, to_x, to_y) is False
def test_rook(): mode = GameMode() mode.set_size(8) mode.set_arrangement([(Unit(RookMover()), 2, 2), (Unit(RookMover()), 2, 3)]) board = Board(mode) x = 2 y = 2 assert board.do_pofig_move(2, 2, 2, 6) is False board[2, 3] = None for dx in range(-10, 10): for dy in range(-10, 10): if dx == dy == 0: continue to_x = x + dx to_y = y + dy if valid(to_x, to_y, 8): if dx == 0 or dy == 0: assert board.do_pofig_move(x, y, to_x, to_y) is True assert board.do_pofig_move(to_x, to_y, x, y) is True else: assert board.do_pofig_move(x, y, to_x, to_y) is False
def test_king(): mode = GameMode() mode.set_size(8) mode.set_arrangement([(Unit(KingMover()), 2, 2), (Unit(KingMover()), 1, 1)]) board = Board(mode) x = 2 y = 2 assert board.do_pofig_move(2, 2, 1, 1) is False board[1, 1] = None for dx in range(-10, 10): for dy in range(-10, 10): if dx == dy == 0: continue to_x = x + dx to_y = y + dy if valid(to_x, to_y, 8): if abs(dx) <= 1 and abs(dy) <= 1: assert board.do_pofig_move(x, y, to_x, to_y) is True assert board.do_pofig_move(to_x, to_y, x, y) is True else: assert board.do_pofig_move(x, y, to_x, to_y) is False
def test_police(): mode = GameMode() mode.set_size(15) arrangement = [(Unit(PoliceManMover()), 4, 4)] x = 4 y = 4 for dx in range(-1, 2): for dy in range(-1, 2): if dx != 0 or dy != 0 or dx != dy: arrangement.append((Unit(KingMover()), x + dx, y + dy)) mode.set_arrangement(arrangement) board = Board(mode) for i in range(15): for j in range(15): if board[i, j] is not None and board[i, j].player == board.player_B: board[i, j] = None for dx in range(-1, 2): for dy in range(-1, 2): p_x = x + dx p_y = y + dy for d2x in range(-1, 2): for d2y in range(-1, 2): if board[p_x + dx, p_y + dy] is None: assert board.do_pofig_move(p_x, p_y, p_x + dx, p_y + dy) is True assert board.do_pofig_move(p_x + dx, p_y + dy, p_x, p_y) is True for i in range(15): for j in range(15): if board[i, j] is not None and (i != 4 or j != 4): board[i, j].player = board.player_B for dx in range(-1, 2): for dy in range(-1, 2): if dx == 0 and dy == 0: continue p_x = x + dx p_y = y + dy for d2x in range(-1, 2): for d2y in range(-1, 2): assert board.do_pofig_move(p_x, p_y, p_x + dx, p_y + dy) is False x = 2 y = 2 mode.set_size(5) mode.set_arrangement([(Unit(PoliceManMover()), 2, 2)]) board = Board(mode) for dx in range(-6, 6): for dy in range(-6, 6): if dx == dy and dx == 0: continue to_x = x + dx to_y = y + dy if valid(to_x, to_y, 6): if abs(dx) <= 1 and abs(dy) <= 1: assert board.do_pofig_move(x, y, to_x, to_y) is True assert board.do_pofig_move(to_x, to_y, x, y) is True else: assert board.do_pofig_move(x, y, to_x, to_y) is False
class Hub: """ Hub class, provides a central module to hold all the properties that are constantly being accessed """ def __init__(self): """ Initialize default values """ self.CLOCK = pygame.time.Clock() self.WINDOW_WIDTH = 1080 self.WINDOW_HEIGHT = 720 self.WINDOW_TITLE = "Legend of Peach" self.WINDOW_ICON = pygame.image.load('imgs/WINDOW_ICON.png') self.BG_COLOR = (0, 0, 0) self.FRAMERATE = 30 self.speed = 0 # PHYSICS VALUES self.GRAVITY = 10 self.velocityAI = 5 self.velocityMushroom = 5 self.velocityStar = 10 # DEBUG MODE self.modeFreeze = False self.modePlace = False # Load mario levels from mario level self.game_levels = self.get_levels() self.gamemode = GameMode(self) self.controller = Controller(self) self.camera = Camera(self) # Screen selector chooses what screen to display self.screen_selector = 1 self.level_name = '' # STATES self.WALK = 'WALK' self.LEFT = 'LEFT' self.RIGHT = 'RIGHT' self.STOMPED = 'STOMPED' self.STAND = 'STAND' self.SHELL = 'SHELL' self.SLIDE = 'SLIDE' self.HIT = 'HIT' self.DEATHFALL = 'DEATHFALL' self.RISE = "RISE" self.FALL = "FALL" # Brick States self.RESTING = 'RESTING' self.BUMPED = 'BUMPED' self.OPENED = 'OPENED' """ Initialize the type of screen possible to display game_screen is the only one that will probably be reinstance everytime a new level opens. """ self.sound_board = SoundBoard() self.main_screen = pygame.display.set_mode((self.WINDOW_WIDTH, self.WINDOW_HEIGHT)) self.hud_screen = HudScreen(self) self.main_menu_screen = MainMenuScreen(self) self.level_screen = LevelSelectionScreen(self) self.game_screen = GameScreen(self) def display_screen(self): if self.screen_selector is 1: self.main_menu_screen.run() elif self.screen_selector is 2: self.level_screen.run() elif self.screen_selector is 3: # display gameover screen pass elif self.screen_selector is 0: # runs the game screen self.game_screen.run() if self.gamemode.mario_is_dead: if self.gamemode.lives == 0: # When mario has no more lives self.gamemode.reset_gamemode() self.sound_board.gameover.play() self.screen_selector = 1 else: self.open_level(self.level_name) self.gamemode.mario_is_dead = False self.hud_screen.run() @staticmethod def exit_game(): pygame.quit() sys.exit() @staticmethod def get_levels(): """ Grab the level data from the JSON file""" filename = 'levels.json' with open(filename, 'r') as read_file: data = json.load(read_file) return data def open_level(self, level_name, world_offset=0): """ Opens new gamee_screen and level""" print('new game_screen instantiated') self.game_screen = GameScreen(self, level_name) self.level_name = level_name theme = self.game_levels[level_name]["theme"] self.camera.reset_camera() self.camera.world_offset_x = world_offset if theme is "0": self.sound_board.play_main_theme_overworld() elif theme is "1": self.sound_board.play_underworld() elif theme is "2": self.sound_board.play_castle() else: self.sound_board.play_main_theme_overworld()