def __init__(self): TScreen = pygame.image.load('images/demo/titlescreen.png').convert() EXIT_GAME = Main_TitleScreen() while not EXIT_GAME: SCREEN.blit(TScreen, (0, 0)) EXIT_GAME = Level_Select(TScreen)
def draw(self, degrade_alpha): if len(self.p_list1) > 1 and len(self.p_list2) > 1 and len( self.p_list3) > 1: self.surface.fill(BLACK) self.init_color_length -= 1 p_list1 = CAMERA.apply_points(self.p_list1) p_list2 = CAMERA.apply_points(self.p_list2) p_list3 = CAMERA.apply_points(self.p_list3) if self.init_color_length < 0: pygame.draw.aalines(self.surface, (self.color1), False, p_list1, 1) pygame.draw.aalines(self.surface, (self.color2), False, p_list2, 1) pygame.draw.aalines(self.surface, (self.color3), False, p_list3, 1) else: pygame.draw.aalines(self.surface, (self.init_color), False, p_list1, 1) pygame.draw.aalines(self.surface, (self.init_color), False, p_list2, 1) pygame.draw.aalines(self.surface, (self.init_color), False, p_list3, 1) self.surface.set_alpha(self.alpha) if degrade_alpha: self.alpha -= ALPHA_RATE SCREEN.blit(self.surface, (0, 0))
def run(self): # SPRITE_MANAGER.instance.clear(SCREEN, BACKGROUND) # for debug purposes... f = open("log3", 'a') sys.stdout = f while not self.exit: SCREEN.fill((0, 0, 0)) move_and_draw_stars(SCREEN, CAMERA) pygame.draw.rect(SCREEN, (255, 0, 0), CAMERA.inner_rect, 2) # pygame.display.flip() # SCREEN.blit(BACKGROUND, BACKGROUND_POS) # SPRITE_MANAGER.instance.clear(SCREEN, BACKGROUND) # print("Background pos") # print(BACKGROUND_POS) # pygame.draw.rect(SCREEN, (255, 0, 0), CAMERA.inner_rect, 1) for event in pygame.event.get(): if event.type == pygame.QUIT: self.exit = True self.clock.tick(50) self.update() self.draw() # should call this once per game loop to ensure pygame talks to sys pygame.event.pump() f.close() sys.stdout = sys.__stdout__ pygame.quit()
def draw(self, **kwargs): SCREEN.fill(self.background) if 'life' in kwargs: if kwargs['life'] != None: for cell in kwargs['life']: if cell.pos[0] >= 0 and cell.pos[0] <= WIDTH: if cell.pos[1] >= 0 and cell.pos[1] <= HEIGHT: cell.draw() for rect in self.rects: pygame.draw.rect( SCREEN, rect['color'], (rect['x'], rect['y'], rect['WIDTH'], rect['HEIGHT']), rect['stroke']) for line in self.lines: pygame.draw.line(SCREEN, line['color'], (line['startx'], line['starty']), (line['endx'], line['endy']), line['width']) for text in self.texts: SCREEN.blit(text[0], (text[1])) button_list = self.buttons.keys() for button in button_list: self.buttons[button].draw(SCREEN)
def level_2(self): clock = pygame.time.Clock() SCREEN.blit(self.menu_image, self.menu_pos) SCREEN.blit(self.cursor_image, (391, 103)) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return (True, True, True) if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return (True, True, True) elif event.key == pygame.K_RETURN: return (True, True, False) elif event.key == pygame.K_DOWN: sound.cursor.play() return (False, 'level5', False) elif event.key == pygame.K_RIGHT: sound.cursor.play() return (False, 'level3', False) elif event.key == pygame.K_LEFT: sound.cursor.play() return (False, 'level1', False) clock.tick(10)
def init_game(self): # set game constants SCREEN.fill((0, 0, 0)) init_stars(SCREEN) load_level(self.joysticks) self.exit = False
def __init__(self): TScreen = pygame.image.load('images/demo/titlescreen.png').convert() self.menu_image = pygame.image.load( 'images/demo/stageselect.png').convert() self.menu_rect = self.menu_image.get_rect() self.menu_pos = (SCREEN_WIDTH / 2 - self.menu_rect.w / 2, SCREEN_HEIGHT / 2 - self.menu_rect.h / 2) self.cursor_image = pygame.image.load( 'images/demo/stageselect_cursor.png').convert_alpha() self.cursor_rect = self.cursor_image.get_rect() self.state = 'level1' EXIT_GAME = Demo_TitleScreen() while not EXIT_GAME: if MUSIC: music = pygame.mixer.music.load( 'sound/music/s3kfileselect.mp3') pygame.mixer.music.play(-1) SCREEN.blit(TScreen, (0, 0)) SCREEN.blit(self.menu_image, self.menu_pos) pygame.display.flip() selected = False while not selected: change = False while not change: if self.state == 'level1': selected, change, EXIT_GAME = self.level_1() elif self.state == 'level2': selected, change, EXIT_GAME = self.level_2() elif self.state == 'level3': selected, change, EXIT_GAME = self.level_3() elif self.state == 'level4': selected, change, EXIT_GAME = self.level_4() elif self.state == 'level5': selected, change, EXIT_GAME = self.level_5() elif self.state == 'level6': selected, change, EXIT_GAME = self.level_6() if change != True: self.state = change change = False if MUSIC: pygame.mixer.music.fadeout(1000) if not EXIT_GAME: Finished, EXIT_GAME = Level( self.state) #Demo_Level(self.state) pygame.mixer.music.fadeout(250)
def main(): pygame.init() # Ressources loading ImageManager.load() FontManager.load() SoundManager.load() # loading of the maze tiles = [] Model.load_from_file(tiles) Controller.randomize_objects(tiles) # States of the game state = "rules" while True: pygame.time.Clock().tick(30) # Display SCREEN.fill((0, 0, 0)) View.draw_title() if state == "rules": View.draw_rules() else: View.draw_maze(tiles) View.draw_picked_objects(Controller.counter_object) if state == "win": View.draw_win_message() elif state == "lose": View.draw_lose_message() pygame.display.flip() # Events for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.KEYDOWN: if state == "rules": if event.key == pygame.K_q: return elif event.key == pygame.K_s: state = "running" elif state == "running": if event.key in BINDING_MOVE: Controller.move(tiles, View.index_macgyver(tiles), BINDING_MOVE[event.key]) # Logics if state == "running": state = Controller.check_win(tiles, View.index_macgyver(tiles)) elif state in ("win", "lose"): SoundManager.get(state).play().set_volume(0.1) pygame.time.wait(4000) break
def __init__(self, color1=(255, 0, 0), color2=(0, 255, 0), color3=(0, 0, 255)): self.surface = SCREEN.copy() self.surface.set_colorkey(BLACK) self.alpha = 255 self.p_list1, self.p_list2, self.p_list3 = [], [], [] self.init_color = (246, 255, 102) self.init_color_length = 10 self.color1, self.color2, self.color3 = color1, color2, color3
def help_menu(): help_img = pygame.image.load('images/menu/help_menu.png').convert() help_img.set_colorkey(COLOR_KEY) help_rect = help_img.get_rect() help_pos = (0, 0) clock = pygame.time.Clock() EXIT = False while not EXIT: SCREEN.blit(help_img, help_pos) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: return True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE: return False clock.tick(MAX_FPS)
def draw(self, degrade_alpha): if len(self.p_list1) > 1 and len(self.p_list2) > 1 and len(self.p_list3) > 1: self.surface.fill(BLACK) self.init_color_length -= 1 p_list1 = CAMERA.apply_points(self.p_list1) p_list2 = CAMERA.apply_points(self.p_list2) p_list3 = CAMERA.apply_points(self.p_list3) if self.init_color_length < 0: pygame.draw.aalines(self.surface, (self.color1), False, p_list1, 1) pygame.draw.aalines(self.surface, (self.color2), False, p_list2, 1) pygame.draw.aalines(self.surface, (self.color3), False, p_list3, 1) else: pygame.draw.aalines(self.surface, (self.init_color), False, p_list1, 1) pygame.draw.aalines(self.surface, (self.init_color), False, p_list2, 1) pygame.draw.aalines(self.surface, (self.init_color), False, p_list3, 1) self.surface.set_alpha(self.alpha) if degrade_alpha: self.alpha -= ALPHA_RATE SCREEN.blit(self.surface, (0, 0))
def Demo_TitleScreen(): title = pygame.image.load('images/demo/titlescreen.png').convert() ''' if MUSIC: music = pygame.mixer.music.load('sound/music/cheetah.mp3') pygame.mixer.music.play(-1) ''' clock = pygame.time.Clock() over = False while not over: for event in pygame.event.get(): if event.type == pygame.QUIT: return True if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return True elif event.key == pygame.K_RETURN: return False SCREEN.blit(title, (0, 0)) pygame.display.flip() clock.tick(10)
def Pause_Screen(prev_screen): menu_image = pygame.image.load('images/gui/pause_menu.png').convert() menu_rect = menu_image.get_rect() menu_pos = (SCREEN_WIDTH / 2 - menu_rect.w / 2, SCREEN_HEIGHT / 2 - menu_rect.h / 2) menu_list = [(425, 221), (425, 269), (425, 317), (425, 365)] #48 cursor_image = pygame.image.load( 'images/gui/cursor/pause_menu_cursor.png').convert() cursor_image.set_colorkey(CHROMA_KEY) cursor_rect = cursor_image.get_rect() cursor_state = 0 clock = pygame.time.Clock() pause = True while pause: if SHOW_FPS: SCREEN.blit(prev_screen, (0, 0)) SCREEN.blit(menu_image, menu_pos) FPS = clock.get_fps() if SHOW_FPS: show_fps(FPS) if cursor_state == -1: #Precaucion para que no salga fuera de rango cursor_state = 3 elif cursor_state == 4: cursor_state = 0 SCREEN.blit(cursor_image, menu_list[cursor_state]) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: return False, True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: if cursor_state == 0: return 'Continuar' elif cursor_state == 1: if Confirmation(prev_screen): return 'Reiniciar' elif cursor_state == 2: if Confirmation(prev_screen): return 'Menu' elif cursor_state == 3: if Confirmation(prev_screen): return 'Salir' elif event.key == pygame.K_DOWN: cursor_state += 1 sound.cursor.play() elif event.key == pygame.K_UP: cursor_state -= 1 sound.cursor.play() clock.tick(MAX_FPS)
def draw(self, **kwargs): SCREEN.fill(self.background) if 'life' in kwargs: if kwargs['life'] != None: for cell in kwargs['life']: if cell.pos[0] >= 0 and cell.pos[0] <= WIDTH: if cell.pos[1] >= 0 and cell.pos[1] <= HEIGHT: cell.draw() for rect in self.rects: pygame.draw.rect(SCREEN, rect['color'], (rect['x'],rect['y'],rect['WIDTH'],rect['HEIGHT']), rect['stroke']) for line in self.lines: pygame.draw.line(SCREEN, line['color'], (line['startx'],line['starty']), (line['endx'],line['endy']), line['width']) for text in self.texts: SCREEN.blit(text[0], (text[1])) button_list = self.buttons.keys() for button in button_list: self.buttons[button].draw(SCREEN)
def start_menu(): background = pygame.image.load('images/background/sky1.png').convert() startMenu_img = pygame.image.load('images/menu/start_menu.png').convert() startMenu_img.set_colorkey(COLOR_KEY) startMenu_rect = startMenu_img.get_rect() startMenu_pos = (SCREEN_WIDTH / 2 - startMenu_rect.w / 2, SCREEN_HEIGHT / 2 - startMenu_rect.h / 2) menu_list = [(282, 108), (282, 216), (282, 330)] select_button = pygame.image.load( 'images/menu/startMenu_btnSelect.png').convert() select_button.set_colorkey(COLOR_KEY) select_state = 0 clock = pygame.time.Clock() QUIT = False while not QUIT: SCREEN.blit(background, (0, 0)) SCREEN.blit(startMenu_img, startMenu_pos) SCREEN.blit(select_button, menu_list[select_state]) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: QUIT = True elif event.type == pygame.QUIT: QUIT = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE: if select_state == 0: QUIT = start_game() elif select_state == 1: QUIT = help_menu() elif select_state == 2: QUIT = True elif event.key == pygame.K_DOWN or event.key == pygame.K_s: if select_state == 0: select_state = 1 elif select_state == 1: select_state = 2 elif select_state == 2: select_state = 0 elif event.key == pygame.K_UP or event.key == pygame.K_w: if select_state == 0: select_state = 2 elif select_state == 1: select_state = 0 elif select_state == 2: select_state = 1 clock.tick(MAX_FPS) return True
def DeadPlayer(prev_screen): menu_image = pygame.image.load('images/gui/retry.png').convert() menu_rect = menu_image.get_rect() menu_pos = (SCREEN_WIDTH / 2 - menu_rect.w / 2, SCREEN_HEIGHT / 2 - menu_rect.h / 2) menu_list = [(399, 284), (518, 284)] cursor_image = pygame.image.load( 'images/gui/cursor/confirmation_cursor.png').convert() cursor_image.set_colorkey(CHROMA_KEY) cursor_rect = cursor_image.get_rect() cursor_state = 0 clock = pygame.time.Clock() pause = True while pause: if SHOW_FPS: SCREEN.blit(prev_screen, (0, 0)) SCREEN.blit(menu_image, menu_pos) FPS = clock.get_fps() if SHOW_FPS: show_fps(FPS) if cursor_state == -1: #Precaucion para que no salga fuera de rango cursor_state = 1 elif cursor_state == 2: cursor_state = 0 SCREEN.blit(cursor_image, menu_list[cursor_state]) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: return False, True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: if cursor_state == 0: return True, False elif cursor_state == 1: return False, False elif event.key == pygame.K_RIGHT: cursor_state += 1 sound.cursor.play() elif event.key == pygame.K_LEFT: cursor_state -= 1 sound.cursor.play() clock.tick(MAX_FPS)
def pause_menu(paused_screen): pause_img = pygame.image.load('images/menu/pause_menu.png').convert() pause_img.set_colorkey(COLOR_KEY) pause_rect = pause_img.get_rect() pause_pos = (SCREEN_WIDTH / 2 - pause_rect.w / 2, SCREEN_HEIGHT / 2 - pause_rect.h / 2) menu_list = [(282, 108), (282, 216), (282, 330)] select_button = pygame.image.load( 'images/menu/startMenu_btnSelect.png').convert() select_button.set_colorkey(COLOR_KEY) select_state = 0 clock = pygame.time.Clock() PAUSE = True while PAUSE: SCREEN.blit(paused_screen, (0, 0)) SCREEN.blit(pause_img, pause_pos) SCREEN.blit(select_button, menu_list[select_state]) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: return False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE: if select_state == 0: return 'Continue' elif select_state == 1: return help_menu() elif select_state == 2: return 'Main' elif event.key == pygame.K_DOWN or event.key == pygame.K_s: if select_state == 0: select_state = 1 elif select_state == 1: select_state = 2 elif select_state == 2: select_state = 0 elif event.key == pygame.K_UP or event.key == pygame.K_w: if select_state == 0: select_state = 2 elif select_state == 1: select_state = 0 elif select_state == 2: select_state = 1 clock.tick(MAX_FPS)
def draw(self, pos_x, pos_y): """Méthod allowing to blit an image""" SCREEN.blit(self.image, (pos_x, pos_y))
def show_fps(FPS): fonty = pygame.font.SysFont('Arial', 20) #Pokemon FireLeaf image = fonty.render('FPS:' + str(int(FPS)), False, (0, 0, 0)) SCREEN.blit(image, (0, 0))
def start_game(self): player_spawn_x = WIDTH / 2.13 player_spawn_y = HEIGHT / 2.25 player = Player(self.entities, self.moving_colliders, player_spawn_x, player_spawn_y, 100, 300) sheet = Location('Surface map.txt', self.location_cells, 'surface', 0, 0, player.speed, collider_group=None) sheet.location_creating() terrain = Location('Terrain map.txt', self.location_objects, 'terrain', 0, 0, player.speed, collider_group=self.static_colliders, sheet=sheet) terrain.location_creating() camera = Camera() previous_camera_works = False while True: self.frame_tick = self.clock.tick() / 1000 set_objects_ticks(self.frame_tick, player, camera) determine_direction(player) SCREEN.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: self.terminate_game() camera_works = camera.update(player) if not camera_works: if not previous_camera_works: check_colliders(self.moving_colliders, self.static_colliders, self.frame_tick) else: check_colliders(self.moving_colliders, self.static_colliders, self.frame_tick, clearing=True) else: if previous_camera_works: check_colliders(self.static_colliders, self.moving_colliders, self.frame_tick, camera=camera, location_groups=[ self.location_cells, self.location_objects ]) else: check_colliders(self.static_colliders, self.moving_colliders, self.frame_tick, clearing=True, camera=camera, location_groups=[ self.location_cells, self.location_objects ]) previous_camera_works = camera_works for cell in self.location_cells.get_sprites(): camera.apply(cell) for obj in self.location_objects.get_sprites(): camera.apply(obj) self.location_cells.draw(SCREEN) self.entities.update() self.entities.draw(SCREEN) self.location_objects.draw(SCREEN) if COLLIDERS_CONTOUR: draw_group_contour(self.moving_colliders, 'red', 2) draw_group_contour(self.static_colliders, 'red', 2) pygame.display.flip()