def __init__(self): super(Menu, self).__init__() screen_size = app.App.get_surface().get_size() # Calculate the button size's according to the screen resolution button_size = (screen_size[0] * 10 // 100, screen_size[0] * 10 // 100) # Create the UI elements self._uiElements += [ s.Button( # The start button (screen_size[0] // 2 - button_size[0] * 2, screen_size[1] // 2 + button_size[1] // 2 + 9), (button_size[0] * 4, int(button_size[1] * 0.5)), (0, 255, 0), text="START", font_size=50, ), s. HudText( # Just displays the text "SPACE SHOOTY SHOOTY" on the screen (screen_size[0] // 2 - button_size[0] * 2, screen_size[1] // 2 - button_size[1] * 2 - 40), (button_size[0] * 4, button_size[1]), (255, 0, 0), text="SPACE SHOOTY SHOOTY", font_size=150, text_centered=True), s.Button( # Back to main menu button (screen_size[0] // 2 - button_size[0] * 2, screen_size[1] // 2 + button_size[1] + 18), (button_size[0] * 4, int(button_size[1] * 0.5)), (110, 110, 100), text="BACK TO MENU", font_size=50, ) ] # Set the onclick methods for the buttons self._uiElements[0].set_on_mouse_click( lambda: app.App.set_current_scene(Game())) # Start's the game self._uiElements[2].set_on_mouse_click( lambda: app.App.set_current_scene(s.MainMenu() )) # Goes back to main menu self._generateMouseClickMapping() # Varaible to keep track of the starting level self._starting_level = 0
def _end(self): """ The function that gets called when the game ends """ # Set the game as over self._game_over = True # Add in the UI elements for the game over state screen_size = app.App.get_surface().get_size() button_size = (screen_size[1] // 10 * 2, screen_size[1] // 10) self._uiElements += [ s. Button( # This is just to have a back drop behind all the newly added ui elements (int(screen_size[0] // 2 - screen_size[0] * 0.6 // 2), int(screen_size[1] // 2 - screen_size[1] * 0.8 // 2)), (int(screen_size[0] * 0.6), int(screen_size[1] * 0.8)), (110, 110, 110)), s.HudText( # Displays "GAME OVER" (screen_size[0] // 2, screen_size[1] // 4), (0, 0), (255, 0, 0), "GAME OVER", 150, text_centered=True), s.Button( # Button that will return to the main menu (screen_size[0] // 2 - button_size[0] - 40, (screen_size[1] + button_size[1]) // 2), button_size, (255, 0, 0), text="Main Menu", text_color=(255, 255, 255), font_size=35), s.Button( # Button that will return to the level selection menu (screen_size[0] // 2 + 40, (screen_size[1] + button_size[1]) // 2), button_size, (255, 0, 0), text="Play Again", text_color=(255, 255, 255), font_size=35) ] self._uiElements[-2].set_on_mouse_click( lambda: app.App.set_current_scene(s.MainMenu())) self._uiElements[-1].set_on_mouse_click( lambda: app.App.set_current_scene(s.SnakeMenu())) # Since some buttons are added to the screen # Generate the mouse click mapping self._generateMouseClickMapping()
def __init__(self): super(BaseGame, self).__init__() self._gameObjects = [] self._pause = False screen_size = app.App.get_surface().get_size() # Create the _pause menu button_size = (screen_size[1] // 10 * 2, screen_size[1] // 10) self._pauseMenuUiElements = [ s. Button( # This is just to have a back drop behind all the newly added ui elements (int(screen_size[0] // 2 - screen_size[0] * 0.6 // 2), int(screen_size[1] // 2 - screen_size[1] * 0.8 // 2)), (int(screen_size[0] * 0.6), int(screen_size[1] * 0.8)), (110, 110, 110)), s.HudText( # Displays "PAUSE" (screen_size[0] // 2, screen_size[1] // 4), (0, 0), (255, 0, 0), "PAUSE", 150, text_centered=True), s.Button( # Button that will return to the main menu (screen_size[0] // 2 - button_size[0] - 40, (screen_size[1] + button_size[1]) // 2), button_size, (255, 0, 0), text="Main Menu", text_color=(255, 255, 255), font_size=35), s.Button( # Button that will unpause the game (screen_size[0] // 2 + 40, (screen_size[1] + button_size[1]) // 2), button_size, (255, 0, 0), text="Continue", text_color=(255, 255, 255), font_size=35) ] self._pauseMenuUiElements[2].set_on_mouse_click( lambda: app.App.set_current_scene(s.MainMenu())) self._pauseMenuUiElements[3].set_on_mouse_click( lambda: self.toggle_pause())
def main(): #setting up main loop flag and fps tick variable clock = pg.time.Clock() running = True #setting up scenes variable, starting with the menu activeScene = scenes.MainMenu(screen) activeScene.render() while running: for event in pg.event.get(): if event.type == pg.QUIT: running = False elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE: running = False else: activeScene.process_event(event) activeScene = activeScene.next pg.display.flip() clock.tick(30)
'''.strip().split('\n') pygame.init() try: # SIZE = width, height = 1600, 900 # FLAGS = pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.NOFRAME SIZE = width, height = 1920, 1080 FLAGS = pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.NOFRAME pygame.display.set_caption(TITLE) scene_manager = scenes.SceneManager(Display(SIZE, FLAGS)) scenes.MainMenu(scene_manager).init() scenes.Game(scene_manager).init() scenes.TextWall(ABOUT_TEXT, scene_manager).init('about') scenes.WinScreen("Player 1 wins!", colors.RED, scene_manager).init('win1') scenes.WinScreen("Player 2 wins!", colors.GREEN, scene_manager).init('win2') scenes.WinScreen("It's a tie!", colors.BLACK, scene_manager).init('tie') scene_manager.run('MainMenu') # scene_manager.run('Game') # scene_manager.run('tie') finally: pygame.quit()
clock = pygame.time.Clock() font_name = pygame.font.match_font('arial') def draw_text(surf, text, size, x, y): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, GREEN) text_rect = text_surface.get_rect() text_rect.midtop = (x, y) text_rect.center = (x, y) surf.blit(text_surface, text_rect) scene_manager = scenes.SceneManager(screen) scene_manager.new_scene(scenes.MainMenu(screen, scene_manager)) te = TextInput((100, 200), 30, 100, scene_manager.scene, {'color': (123, 18, 76), 'bd_color': (255, 255, 255), 'bd_width': 1}) running = True while running: clock.tick(FPS) screen.fill(BLACK) aa = pygame.event.get() scene_manager.next_step(aa) for i in aa: te.check_event(i) te.render(screen)
import scenes if __name__ == "__main__": play = scenes.MainMenu() play.run()