class App: def __init__(self): self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) self.clock = pygame.time.Clock() self.running = True self.state = 'start' self.cell_width = MAZE_WIDTH // COLS self.cell_height = MAZE_HEIGHT // ROWS self.walls = [] self.coins = [] self.enemies = [] self.e_pos = [] self.p_pos = None self.highscore = 0 self.load() self.player = Player(self, vec(self.p_pos)) self.make_enemies() def run(self): while self.running: if self.state == 'start': self.start_events() self.start_update() self.start_draw() elif self.state == 'playing': self.playing_events() self.playing_update() self.playing_draw() elif self.state == 'game over': self.game_over_events() self.game_over_update() self.game_over_draw() elif self.state == 'winner': self.winner_events() self.winner_update() self.winner_draw() else: self.running = False self.clock.tick(FPS) pygame.quit() try: sys.exit() except: print("Game over, bye") ################### HELPER FUNCTIONS ##################### def draw_text(self, words, screen, pos, size, color, font_name, centered=False): font = pygame.font.SysFont(font_name, size) text = font.render(words, False, color) text_size = text.get_size() if centered: pos[0] = pos[0] - text_size[0] // 2 pos[1] = pos[1] - text_size[1] // 2 screen.blit(text, pos) def load(self): self.background = pygame.image.load('maze.png') self.background = pygame.transform.scale(self.background, (MAZE_WIDTH, MAZE_HEIGHT)) # Opening walls file # Creating walls list with co-ords of walls with open("walls.txt", mode='r') as file: for yidx, line in enumerate(file): for xidx, char in enumerate(line): if char == "1": self.walls.append(vec(xidx, yidx)) elif char == "C": self.coins.append(vec(xidx, yidx)) elif char == "P": self.p_pos = [xidx, yidx] elif char in ["2", "3", "4", "5", "6", "7"]: self.e_pos.append([xidx, yidx]) elif char == "B": pygame.draw.rect( self.background, BLACK, (xidx * self.cell_width, yidx * self.cell_height, self.cell_width, self.cell_height)) #print(self.walls) # read Highscore from highscore.txt with open("highscore.txt", mode='r') as highscore_file: for line in highscore_file: name, var = line.partition("=")[::2] self.highscore = int(var) def make_enemies(self): for idx, pos in enumerate(self.e_pos): self.enemies.append(Enemy(self, vec(pos), idx)) def draw_grid(self): for x in range(WIDTH // self.cell_width): pygame.draw.line(self.background, GREY, (x * self.cell_width, 0), (x * self.cell_width, HEIGHT)) for x in range(HEIGHT // self.cell_height): pygame.draw.line(self.background, GREY, (0, x * self.cell_height), (WIDTH, x * self.cell_height)) # Shows wall as rectangles # for wall in self.walls: # pygame.draw.rect(self.background, (112,55,163), (wall.x*self.cell_width, wall.y*self.cell_height, self.cell_width, self.cell_width)) # for coin in self.coins: # pygame.draw.rect(self.background, (167, 179, 34), (coin.x*self.cell_width, coin.y*self.cell_height, self.cell_width, self.cell_width)) def reset(self): self.player.lives = 3 self.player.current_score = 0 self.player.grid_pos = vec(self.player.starting_pos) self.player.pix_pos = self.player.get_pix_pos() self.player.direction *= 0 for enemy in self.enemies: enemy.grid_pos = vec(enemy.starting_pos) enemy.pix_pos = enemy.get_pix_pos() enemy.direction *= 0 self.coins = [] with open("walls.txt", mode='r') as file: for yidx, line in enumerate(file): for xidx, char in enumerate(line): if char == 'C': self.coins.append(vec(xidx, yidx)) self.state = "playing" ################### INIT FUNCTIONS ##################### def start_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: self.state = 'playing' def start_update(self): pass def start_draw(self): self.screen.fill(BLACK) self.draw_text('PUSH SPACE BAR', self.screen, [WIDTH // 2, HEIGHT // 2 - 50], START_TEXT_SIZE, (170, 132, 58), START_FONT, centered=True) self.draw_text('1 PLAYER ONLY', self.screen, [WIDTH // 2, HEIGHT // 2 + 50], START_TEXT_SIZE, (44, 167, 198), START_FONT, centered=True) # self.draw_text('HIGH SCORE', self.screen, [4,0], #START_TEXT_SIZE, (255, 255, 255), START_FONT) pygame.display.update() ################### PLAYING FUNCTIONS ##################### def playing_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.player.move(vec(-1, 0)) if event.key == pygame.K_RIGHT: self.player.move(vec(1, 0)) if event.key == pygame.K_UP: self.player.move(vec(0, -1)) if event.key == pygame.K_DOWN: self.player.move(vec(0, 1)) def playing_update(self): self.player.update() for enemy in self.enemies: enemy.update() for enemy in self.enemies: if enemy.grid_pos == self.player.grid_pos: self.remove_life() if self.player.current_score == 287: if self.highscore < self.player.current_score: self.write_highscore(self.player.current_score) self.state = "winner" def write_highscore(self, new_highscore): with open("highscore.txt", "a+") as highscore_file: line = highscore_file.readline() highscore_file.truncate(0) self.highscore = new_highscore highscore_file.write('highscore={}'.format(self.highscore)) highscore_file.close() def playing_draw(self): self.screen.fill(BLACK) self.screen.blit(self.background, (TOP_BOTTOM_BUFFER // 2, TOP_BOTTOM_BUFFER // 2)) self.draw_grid() self.draw_coins() self.draw_text('CURRENT SCORE: {}'.format(self.player.current_score), self.screen, [70, 5], 30, WHITE, START_FONT) self.draw_text('HIGH SCORE: {}'.format(self.highscore), self.screen, [WIDTH // 2 + 60, 5], 30, WHITE, START_FONT) self.player.draw() for enemy in self.enemies: enemy.draw() pygame.display.update() def remove_life(self): self.player.lives -= 1 if self.player.lives == 0: # if self.highscore < self.player.current_score: # self.highscore = self.player.current_score self.state = "game over" else: self.player.grid_pos = vec(self.player.starting_pos) self.player.pix_pos = self.player.get_pix_pos() self.player.direction *= 0 for enemy in self.enemies: enemy.grid_pos = vec(enemy.starting_pos) enemy.pix_pos = enemy.get_pix_pos() enemy.direction *= 0 def draw_coins(self): for coin in self.coins: pygame.draw.circle( self.screen, (124, 123, 7), (int(coin.x * self.cell_height) + self.cell_width // 2 + TOP_BOTTOM_BUFFER // 2, int(coin.y * self.cell_height) + self.cell_width // 2 + TOP_BOTTOM_BUFFER // 2), 5) ################### GAME OVER FUNCTIONS ##################### def game_over_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: self.reset() if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: self.state = False def game_over_update(self): pass def game_over_draw(self): self.screen.fill(BLACK) again_text = "Press SPACE bar to PLAY AGAIN" quit_text = "Press the escape button to QUIT" self.draw_text("GAME OVER", self.screen, [WIDTH // 2, 100], 36, RED, "arial", centered=True) if self.highscore < self.player.current_score: self.write_highscore(self.player.current_score) highscore_text = "New Highscore is {}".format(self.highscore) else: highscore_text = "Highscore is {}".format(self.highscore) self.draw_text(highscore_text, self.screen, [WIDTH // 2, 150], 36, YELLOW, "arial", centered=True) self.draw_text(again_text, self.screen, [WIDTH // 2, HEIGHT // 2], 36, (190, 190, 190), "arial", centered=True) self.draw_text(quit_text, self.screen, [WIDTH // 2, HEIGHT // 1.5], 36, (190, 190, 190), "arial", centered=True) pygame.display.update() ##################### WINNER FUNCTIONS ##################### def winner_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: self.reset() if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: self.state = False def winner_update(self): pass def winner_draw(self): self.screen.fill(BLACK) again_text = "Press SPACE bar to PLAY AGAIN" quit_text = "Press the escape button to QUIT" winner_text = "YOU ARE A WINNER WITH {} POINTS".format( self.player.current_score) highscore_text = "Highscore is {} points!".format(self.highscore) self.draw_text(winner_text, self.screen, [WIDTH // 2, 100], 36, YELLOW, "arial", centered=True) self.draw_text(highscore_text, self.screen, [WIDTH // 2, 150], 36, YELLOW, "arial", centered=True) self.draw_text(again_text, self.screen, [WIDTH // 2, HEIGHT // 2], 36, (190, 190, 190), "arial", centered=True) self.draw_text(quit_text, self.screen, [WIDTH // 2, HEIGHT // 1.5], 36, (190, 190, 190), "arial", centered=True) pygame.display.update()
class App: def __init__(self): self.screen = pygame.display.set_mode((setting.WIDTH, setting.HEIGHT)) self.clock = pygame.time.Clock() self.running = True self.state = 'start' self.cell_width = setting.MAZE_WIDTH // setting.COLS self.cell_height = setting.MAZE_HEIGHT // setting.ROWS #load from environment file self.walls = [] self.coins = [] self.powerups = [] self.turns = [] self.enemies = [] self.enemies_to_respawn = [] self.scatter_targets = [] self.e_pos = [] self.p_pos = None self.respawn = None self.load() self.player = Player(self, vec(self.p_pos)) self.make_enemies() self.current_score = 0 self.highscore = self.get_highscore() self.level = 0 self.difficulty = 'normal' self.map_type = 'basic' def run(self): while self.running: if self.state == 'start': self.start_events() self.start_update() self.start_draw() elif self.state == 'playing': self.playing_events() self.playing_update() self.playing_draw() elif self.state == 'game over': self.game_over_events() self.game_over_update() self.game_over_draw() elif self.state == 'settings': self.settings_events() self.settings_update() self.settings_draw() else: self.running = False self.clock.tick(setting.fps + self.level) pygame.quit() sys.exit() #######################HELPER FUNCTIONS#################################### def load(self): self.background = pygame.image.load('maze.png') self.background = pygame.transform.scale( self.background, (setting.MAZE_WIDTH, setting.MAZE_HEIGHT)) # Opening walls file # Creating walls list with co-ords of walls # stored as a vector with open("walls.txt", 'r') as file: for yidx, line in enumerate(file): for xidx, char in enumerate(line): if char in "1": self.walls.append(vec(xidx, yidx)) elif char == "U": self.powerups.append( Item(self, "power", vec(xidx, yidx))) elif char == "R": self.respawn = vec(xidx, yidx) print(self.respawn) elif char in ["C", "T", "b", "p", "i", "c"]: self.coins.append(Item(self, "coin", vec(xidx, yidx))) if char in ["b", "p", "i", "c"]: self.scatter_targets.append(vec(xidx, yidx)) if char == "T": self.turns.append(vec(xidx, yidx)) elif char == "P": self.p_pos = [xidx, yidx] elif char in ["2", "3", "4", "5"]: self.e_pos.append([xidx, yidx]) elif char == "B": pygame.draw.rect( self.background, setting.BLACK, (xidx * self.cell_width, yidx * self.cell_height, self.cell_width, self.cell_height)) def draw_text(self, words, screen, pos, font_size, font_colour, font_name, centered=False): font = pygame.font.SysFont(font_name, font_size) text = font.render(words, False, font_colour) text_size = text.get_size() if centered: pos[0] = pos[0] - text_size[0] // 2 pos[1] = pos[1] - text_size[1] // 2 screen.blit(text, pos) def reset(self, condition): if condition == "hard reset": if self.current_score > self.highscore: self.highscore = self.current_score f = open("highscore.txt", "w") f.write(str(self.highscore)) f.close self.current_score = 0 self.player.lives = 3 self.player.grid_pos = vec(self.player.starting_pos) self.player.pix_pos = self.player.get_pix_pos() self.direction = vec(1, 0) for enemy in self.enemies: enemy.grid_pos = vec(enemy.starting_pos) enemy.pix_pos = enemy.get_pix_pos() enemy.direction *= 0 self.coins = [] self.turns = [] self.powerups = [] with open("walls.txt", 'r') as file: for yidx, line in enumerate(file): for xidx, char in enumerate(line): if char == 'C' or char == "T": self.coins.append(Item(self, "coin", vec(xidx, yidx))) if char == "T": self.turns.append(vec(xidx, yidx)) elif char == "U": self.powerups.append( Item(self, "power", vec(xidx, yidx))) self.state = "playing" ######################DEBUGGING FUNCTIONS################################## def draw_grid(self): for i in range(setting.WIDTH // self.cell_width): pygame.draw.line(self.background, setting.GREY, (i * self.cell_width, 0), (i * self.cell_width, setting.HEIGHT)) for i in range(setting.HEIGHT // self.cell_height): pygame.draw.line(self.background, setting.GREY, (0, i * self.cell_height), (setting.WIDTH, i * self.cell_height)) #######################START FUNCTIONS#################################### def start_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and (event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER): #if self.difficulty == 'advanced': self.state = 'playing' if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: print('pressed') self.state = 'settings' def start_update(self): pass def start_draw(self): self.screen.fill(setting.BLACK) if self.highscore > 0: self.draw_text('HIGH SCORE: {}'.format(self.highscore), self.screen, [25, 0], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT) else: self.draw_text('HIGH SCORE: {}'.format(self.current_score), self.screen, [25, 0], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT) self.draw_text("PACMAN", self.screen, [setting.WIDTH // 2, 100], 52, setting.RED, setting.START_FONT, centered=True) self.draw_text("EMULATOR", self.screen, [setting.WIDTH // 2, 160], 52, setting.RED, setting.START_FONT, centered=True) self.draw_text('START [ENTER]', self.screen, [setting.WIDTH // 2, setting.HEIGHT // 2], setting.START_FONT_SIZE, setting.ORANGE, setting.START_FONT, centered=True) self.draw_text('SETTINGS [SPACE]', self.screen, [setting.WIDTH // 2, setting.HEIGHT // 2 + 80], setting.START_FONT_SIZE, setting.LILAC, setting.START_FONT, centered=True) self.draw_text('1 PLAYER ONLY', self.screen, [setting.WIDTH // 2, setting.HEIGHT // 2 + 200], setting.START_FONT_SIZE, setting.AQUA, setting.START_FONT, centered=True) pygame.display.update() #######################Settings FUNCTIONS#################################### def settings_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: self.state = 'start' if event.key == pygame.K_n: self.difficulty = 'normal' if event.key == pygame.K_a: self.difficulty = 'advanced' if event.key == pygame.K_b: self.map_type = 'basic' if event.key == pygame.K_h: self.map_type = 'hexagonal' def settings_update(self): pass def settings_draw(self): self.screen.fill(setting.BLACK) self.draw_text('MENU[BKSP]', self.screen, [setting.WIDTH // 2 - 50, 0], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT) self.draw_text("SETTINGS", self.screen, [setting.WIDTH // 2, 100], 52, setting.RED, setting.START_FONT, centered=True) self.draw_text('DIFFICULTY', self.screen, [setting.WIDTH // 2, setting.HEIGHT // 2 - 100], 32, (190, 190, 190), setting.START_FONT, centered=True) if self.difficulty == 'normal': self.draw_text( 'NORMAL[N]', self.screen, [setting.WIDTH // 2 - 100, setting.HEIGHT // 2 - 50], setting.START_FONT_SIZE, setting.BLUE, setting.START_FONT, centered=True) self.draw_text( 'ADVANCED[A]', self.screen, [setting.WIDTH // 2 + 100, setting.HEIGHT // 2 - 50], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT, centered=True) else: self.draw_text( 'NORMAL[N]', self.screen, [setting.WIDTH // 2 - 100, setting.HEIGHT // 2 - 50], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT, centered=True) self.draw_text( 'ADVANCED[A]', self.screen, [setting.WIDTH // 2 + 100, setting.HEIGHT // 2 - 50], setting.START_FONT_SIZE, setting.BLUE, setting.START_FONT, centered=True) self.draw_text('MAP TYPE', self.screen, [setting.WIDTH // 2, setting.HEIGHT // 2 + 100], 32, (190, 190, 190), setting.START_FONT, centered=True) if self.map_type == 'basic': self.draw_text( 'BASIC[B]', self.screen, [setting.WIDTH // 2 - 100, setting.HEIGHT // 2 + 150], setting.START_FONT_SIZE, setting.BLUE, setting.START_FONT, centered=True) self.draw_text( 'HEXAGONAL[H]', self.screen, [setting.WIDTH // 2 + 100, setting.HEIGHT // 2 + 150], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT, centered=True) else: self.draw_text( 'BASIC[B]', self.screen, [setting.WIDTH // 2 - 100, setting.HEIGHT // 2 + 150], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT, centered=True) self.draw_text( 'HEXAGONAL[H]', self.screen, [setting.WIDTH // 2 + 100, setting.HEIGHT // 2 + 150], setting.START_FONT_SIZE, setting.BLUE, setting.START_FONT, centered=True) pygame.display.update() #######################LEVEL CHANGER FUNCTIONS#################################### def new_level(self): self.level += 5 self.reset("new level") self.state = "playing" #######################PLAYING FUNCTIONS#################################### def playing_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.player.move(vec(-1, 0)) if event.key == pygame.K_RIGHT: self.player.move(vec(1, 0)) if event.key == pygame.K_UP: self.player.move(vec(0, -1)) if event.key == pygame.K_DOWN: self.player.move(vec(0, 1)) if event.key == pygame.K_BACKSPACE: self.reset('hard reset') self.state = 'start' def playing_update(self): self.player.update() for enemy in self.enemies: enemy.update() self.enemy_collision_handler() self.enemy_respawn_handler() def playing_draw(self): self.screen.fill(setting.BLACK) self.screen.blit( self.background, (setting.TOP_BOTTOM_BUFFER // 2, setting.TOP_BOTTOM_BUFFER // 2)) # self.draw_grid() self.draw_items() if len(self.coins) == 0: self.new_level() self.draw_text('MENU[BKSP]', self.screen, [setting.WIDTH // 2 - 50, 0], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT) if self.highscore > 0: self.draw_text('HIGH SCORE: {}'.format(self.highscore), self.screen, [25, 0], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT) else: self.draw_text('HIGH SCORE: {}'.format(self.current_score), self.screen, [25, 0], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT) self.draw_text('SCORE: {}'.format(self.current_score), self.screen, [setting.WIDTH - 135, 0], setting.START_FONT_SIZE, setting.WHITE, setting.START_FONT) self.player.draw() for enemy in self.enemies: enemy.draw() pygame.display.update() ########################### GAME OVER FUNCTIONS ################################ def game_over_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: self.reset("hard reset") self.state = "start" if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE: self.state = 'start' def game_over_update(self): pass def game_over_draw(self): self.screen.fill(setting.BLACK) again_text = "CONTINUE [SPACE]" self.draw_text("GAME OVER", self.screen, [setting.WIDTH // 2, 100], 52, setting.RED, setting.START_FONT, centered=True) self.draw_text(again_text, self.screen, [setting.WIDTH // 2, setting.HEIGHT // 2], 36, (190, 190, 190), setting.START_FONT, centered=True) pygame.display.update() ########################### EVENT HANDLERS ################################ def draw_items(self): if len(self.coins) > 0: for coin in self.coins: coin.draw() for powerup in self.powerups: powerup.draw() else: self.state = "next level" def make_enemies(self): for idx, pos in enumerate(self.e_pos): self.enemies.append( Enemy(self, vec(pos), idx, self.scatter_targets[idx])) def enemy_collision_handler(self): for idx, enemy in enumerate(self.enemies): if enemy.grid_pos == self.player.grid_pos and enemy.state == "chase": self.remove_life() elif enemy.grid_pos == self.player.grid_pos and enemy.state == "scatter": enemy.respawn_wait_time = 100 * self.level self.enemies_to_respawn.append(enemy) self.enemies.pop(idx) self.current_score += 100 def enemy_respawn_handler(self): if len(self.enemies_to_respawn) > 0: for idx, enemy in enumerate(self.enemies_to_respawn): if enemy.respawn_wait_time == 0: enemy.respawn() self.enemies.append(enemy) self.enemies_to_respawn.pop(idx) else: enemy.respawn_wait_time -= 1 def remove_life(self): self.player.lives -= 1 if self.player.lives == 0: self.state = "game over" else: self.player.grid_pos = vec(self.player.starting_pos) self.player.pix_pos = self.player.get_pix_pos() self.player.direction *= 0 for enemy in self.enemies: enemy.grid_pos = vec(enemy.starting_pos) enemy.pix_pos = enemy.get_pix_pos() enemy.direction *= 0 def get_highscore(self): try: f = open("highscore.txt", "r") highscore = int(f.readline()) print("highscore", highscore) f.close() except: highscore = 0 return highscore
class App(settings.Setting): def __init__(self): self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT)) self.clock = pygame.time.Clock() self.running = True self.state = 'start' self.cell_width = self.MAZE_WIDTH // self.COLS self.cell_height = self.MAZE_HEIGHT / self.ROWS self.walls = [] self.coins = [] self.enemies = [] self.e_pos = [] self.p_pos = None self.load() self.player = Player(self, vec(self.p_pos)) self.make_enemies() def run(self): while self.running: if self.state == 'start': self.start_events() self.start_update() self.start_draw() elif self.state == 'playing': self.playing_events() self.playing_update() self.playing_draw() elif self.state == 'game over': self.game_over_events() self.game_over_update() self.game_over_draw() else: self.running = False self.clock.tick(self.FPS) pygame.quit() print("\nThanks for playing the game x)") ############################ HELPER FUNCTIONS ################################## def draw_text(self, words, screen, pos, size, colour, font_name, centered=False): font = pygame.font.SysFont(font_name, size) text = font.render(words, False, colour) text_size = text.get_size() if centered: pos[0] = pos[0] - text_size[0] // 2 pos[1] = pos[1] - text_size[1] // 2 screen.blit(text, pos) def load(self): self.background = pygame.image.load('maze.png') self.background = pygame.transform.scale( self.background, (self.MAZE_WIDTH, self.MAZE_HEIGHT)) # Opening walls file # Creating walls list with co-ords of walls # stored as a vector with open("walls.txt", 'r') as file: for yidx, line in enumerate(file): for xidx, char in enumerate(line): if char == "1": self.walls.append(vec(xidx, yidx)) elif char == "C": self.coins.append(vec(xidx, yidx)) elif char == "P": self.p_pos = [xidx, yidx] elif char in ["2", "3", "4", "5"]: self.e_pos.append([xidx, yidx]) elif char == "B": pygame.draw.rect( self.background, self.BLACK, (xidx * self.cell_width, yidx * self.cell_height, self.cell_width, self.cell_height)) def make_enemies(self): for idx, pos in enumerate(self.e_pos): self.enemies.append(Enemy(self, vec(pos), idx)) def draw_grid(self): for x in range(self.WIDTH // self.cell_width): pygame.draw.line(self.background, self.GREY, (x * self.cell_width, 0), (x * self.cell_width, HEIGHT)) for x in range(self.HEIGHT / self.cell_height): pygame.draw.line(self.background, self.GREY, (0, x * self.cell_height), (self.WIDTH, x * self.cell_height)) for coin in self.coins: pygame.draw.rect( self.background, (167, 179, 34), (coin.x * self.cell_width, coin.y * self.cell_height, self.cell_width, self.cell_height)) def reset(self): self.player.lives = 3 self.player.current_score = 0 self.player.grid_pos = vec(self.player.starting_pos) self.player.pix_pos = self.player.get_pix_pos() self.player.direction *= 0 for enemy in self.enemies: enemy.grid_pos = vec(enemy.starting_pos) enemy.pix_pos = enemy.get_pix_pos() enemy.direction *= 0 self.coins = [] with open("walls.txt", 'r') as file: for yidx, line in enumerate(file): for xidx, char in enumerate(line): if char == 'C': self.coins.append(vec(xidx, yidx)) self.state = "playing" ########################### INTRO FUNCTIONS #################################### def start_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: self.state = 'playing' def start_update(self): pass def start_draw(self): self.screen.fill(self.BLACK) self.draw_text('PUSH SPACE BAR', self.screen, [self.WIDTH // 2, self.HEIGHT / 2 - 50], self.START_TEXT_SIZE, (170, 132, 58), self.START_FONT, centered=True) self.draw_text('1 PLAYER ONLY', self.screen, [self.WIDTH // 2, self.HEIGHT / 2 + 50], self.START_TEXT_SIZE, (44, 167, 198), self.START_FONT, centered=True) self.draw_text('HIGH SCORE', self.screen, [4, 0], self.START_TEXT_SIZE, (255, 255, 255), self.START_FONT) pygame.display.update() ########################### PLAYING FUNCTIONS ################################## def playing_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: # left arrow is pressed if event.key == pygame.K_LEFT: self.player.move(vec(-1, 0)) # right arrow is pressed if event.key == pygame.K_RIGHT: self.player.move(vec(1, 0)) # up arrow is pressed if event.key == pygame.K_UP: self.player.move(vec(0, -1)) # down arrow is pressed if event.key == pygame.K_DOWN: self.player.move(vec(0, 1)) def playing_update(self): self.player.update() for enemy in self.enemies: enemy.update() for enemy in self.enemies: if enemy.grid_pos == self.player.grid_pos: self.remove_life() def playing_draw(self): self.screen.fill(self.BLACK) self.screen.blit( self.background, (self.TOP_BOTTOM_BUFFER // 2, self.TOP_BOTTOM_BUFFER // 2)) self.draw_coins() # self.draw_grid() self.draw_text('CURRENT SCORE: {}'.format(self.player.current_score), self.screen, [60, 0], 18, self.WHITE, self.START_FONT) self.draw_text('HIGH SCORE: 0', self.screen, [self.WIDTH // 2 + 60, 0], 18, self.WHITE, self.START_FONT) self.player.draw() for enemy in self.enemies: enemy.draw() pygame.display.update() def remove_life(self): self.player.lives -= 1 if self.player.lives == 0: self.state = "game over" else: self.player.grid_pos = vec(self.player.starting_pos) self.player.pix_pos = self.player.get_pix_pos() self.player.direction *= 0 for enemy in self.enemies: enemy.grid_pos = vec(enemy.starting_pos) enemy.pix_pos = enemy.get_pix_pos() enemy.direction *= 0 def draw_coins(self): for coin in self.coins: pygame.draw.circle( self.screen, (124, 123, 7), (int(coin.x * self.cell_width) + self.cell_width // 2 + self.TOP_BOTTOM_BUFFER // 2, int(coin.y * self.cell_height) + self.cell_height / 2 + self.TOP_BOTTOM_BUFFER // 2), 5) ########################### GAME OVER FUNCTIONS ################################ def game_over_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: self.reset() if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: self.running = False def game_over_update(self): pass def game_over_draw(self): self.screen.fill(self.BLACK) quit_text = "Press the escape button to QUIT" again_text = "Press SPACE bar to PLAY AGAIN" self.draw_text("GAME OVER", self.screen, [self.WIDTH // 2, 100], 52, RED, "arial", centered=True) self.draw_text(again_text, self.screen, [self.WIDTH // 2, self.HEIGHT / 2], 36, (190, 190, 190), "arial", centered=True) self.draw_text(quit_text, self.screen, [self.WIDTH // 2, self.HEIGHT / 1.5], 36, (190, 190, 190), "arial", centered=True) pygame.display.update()
player.tick() bottom_barrier.draw(screen) #draw cyan barrier at bottom top_barrier.draw(screen) left_barrier.draw(screen) right_barrier.draw(screen) for i, l in enumerate(lasers): if l.end_of_life(): del lasers[i] l.tick() l.draw(screen) if player.collision(l): death = True if player.victory(): victory = True if victory: victory_text.draw(screen) elif death: death_text.draw(screen) else: player.draw(screen) #draw player pygame.display.flip() clock.tick(60) pygame.display.quit() laser_timer.stop() sys.exit()