def offline_mode(self): self.options = [ Button("Player vs Player", (420, 340)), Button("Player vs AI", (660, 340)), Button("BACK", (100, 650)) ] self.selected = None
def main(self): self.options = [ Button("OFFLINE GAME", (540, 360)), Button("ONLINE GAME", (540, 450)) ] self.selected = None self.inputboxes = pygame.sprite.Group()
def offline_color(self): self.options = [ Button("White", (460, 340)), Button("Black", (620, 340)), Button("PLAY", (540, 450)), Button("BACK", (100, 650)) ] self.selected = self.options[0]
def login_register(self): self.inputboxes = pygame.sprite.Group() self.options = [ Button("Login", (270, 500)), Button("Register", (810, 500)), Button("BACK", (100, 650)) ] self.input_username = InputBox(440, 300, 200, 40, 'Username') self.input_password = InputBox(440, 380, 200, 40, 'Password') self.inputboxes.add(self.input_username, self.input_password)
def menu_screen(): """ menu screen handels title, start, quit, and instructions :return: """ #list of all buttons for menu screen menu_buttons = [ Button("Play", 140, 280, 150, 100, blue, 60), Button("Quit", 460, 280, 150, 100, red, 60), Button("Instructions", 180, 420, 400, 100, pink, 60) ] #list of all buttons for menu screen menu_messages = [ Message("Space Invaders", (width_display / 2), (height_display * (1 / 4)), white, 100) ] run = True #sets run = true while run: #while true gameDisplay.blit(space_background, (0, 0)) #displays the background at given coordinates for message in menu_messages: #gets each message in list message.draw( gameDisplay) #draws the message and sends the gameDisplay for btn in menu_buttons: #gets each button in list btn.draw(gameDisplay) #draws the button and sends the gameDisplay for event in p.event.get(): #collects all events if event.type == p.QUIT: #checks events if quit button has been pressed quit_game() #goes to def function to quit if event.type == p.MOUSEBUTTONDOWN: #checks events if mouse has been pressed pos = p.mouse.get_pos() #gets the current mouse position for btn in menu_buttons: #gets each button in list if btn.click( pos ): #sends the mouse position to check if button has been clicked if btn.text == "Play": #if button text = play main() #calls main function elif btn.text == "Quit": # if button text = quit quit_game() #goes to def function to quit elif btn.text == "Instructions": #if button text = instructions instructions() #calls instructions function clock.tick(60) #sets frame rate speed for game p.display.update() #updates display
def run_game(): # Game settings init pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien-Invasion") play_button = Button(ai_settings, screen, "Play") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) bg_color = (230, 230, 230) # Ship, bullets, and aliens init. ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def instructions(): """ instructions handels the title, controls and back button :return: """ #loads in the required images left_click = p.image.load("asserts/instructions/left click.png") space = p.image.load("asserts/instructions/space.png") uldr = p.image.load("asserts/instructions/uldr.png") wasd = p.image.load("asserts/instructions/wasd.png") #sets up the back button and title up back = Button("Back", 300, 420, 150, 100, red, 60) title = Message("Instructions", 400, 60, black, 100) run = True #sets run = true while run: #while true gameDisplay.fill(white) #fills the display with the color white for event in p.event.get(): #collects all events if event.type == p.QUIT: #checks events if quit button has been pressed quit_game() #goes to def function to quit if event.type == p.MOUSEBUTTONDOWN: #checks events if mouse has been pressed pos = p.mouse.get_pos() #gets the current mouse position if back.click( pos ): #sends the mouse position to check if button has been clicked if back.text == "Back": #if button text = back menu_screen() #returns the player to the menu_screen #draws the button and title back.draw(gameDisplay) title.draw(gameDisplay) #displays the image at that position gameDisplay.blit(left_click, (100, 150)) gameDisplay.blit(space, (400, 150)) gameDisplay.blit(uldr, (60, 375)) gameDisplay.blit(wasd, (490, 375)) clock.tick(60) #sets frame rate speed for game p.display.update() #updates display
def run_game(): #Initialize game and create a screen object. #Initializes Background settings that pygame needs to work. pygame.init() ai_settings = Settings() # we create a display window called screen where we'll draw # all the elements of our game # the screen object is called a surface in pygame # Each element on the screen is called a surface (alien, ships) # When we activate the game loop, the surface is automatically # redrawn on every pass through the loop screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption(ai_settings.window_caption) # Make the start button start_button = Button(ai_settings, screen, "Start") # Create an instance to store game statistics and create a scoreboard stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Make a ship, a group of bullets and a group of aliens ship = Ship(screen, ai_settings) bullets = Group() aliens = Group() # We create the fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) #start the main loop for the game while True: # we check for any events (keypresses or mouse events) gf.check_events(ai_settings, screen, stats, sb, start_button, ship, aliens, bullets) # In the main loop, we always need to call check_events(), even if the game # is inactive. For example, we still need to know if the user presses Q to quit # the game or clicks the button to close the window if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) # We update the aliens’ positions after the bullets have been updated, # because we’ll soon be checking to see whether any bullets hit any aliens gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, start_button)
def attach_buttons(): global buttons buttons = [] buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE), 5, 'pause', [GameState_PAUSED, GameState_PLAYING], pause_toggle)) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, 0, BUTTON_SIZE, BUTTON_SIZE), 5, 'restart', [GameState_PAUSED, GameState_GAMEOVER, GameState_PLAYING], restart, 0)) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 5, BUTTON_SIZE, BUTTON_SIZE), 5, 'left', [GameState_PLAYING], getattr(grid, 'move_block_left'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 4, BUTTON_SIZE, BUTTON_SIZE), 5, 'right', [GameState_PLAYING], getattr(grid, 'move_block_right'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 3, BUTTON_SIZE, BUTTON_SIZE), 5, 'rotate', [GameState_PLAYING], getattr(grid, 'rotate_block'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE * 2, BUTTON_SIZE, BUTTON_SIZE), 5, 'down', [GameState_PLAYING], getattr(grid, 'move_block_down'))) buttons.append( Button( screen, pygame.Rect(SCREEN_WIDTH - BUTTON_SIZE + 1, SCREEN_HEIGHT - BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE), 5, 'bottom', [GameState_PLAYING], getattr(grid, 'move_block_to_bottom')))
def manual(): led = Led(37) fled = Led(19) wled = Led(38) button = Button(11) food = Relay.get_instance(10) water = Relay.get_instance(12) while True: value = button.multi_checked(led) if value == 1: water.off() wled.off() food.off() fled.off() buzzer.stop() if value == 2: wled.on() water.on() if value == 3: fled.on() food.on() if value == 4: t2.start()
def run_game(): # Khởi tạo pygame, settings và screen object pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height )) # screen object vs chiều dài và chiều cao trong settings.py pygame.display.set_caption('Alien Invasion') # title for window # Tạo ra button play game play_button = Button(ai_settings, screen, 'Play') # Khởi tạo đối tượng dùng để thống kê dữ liệu trong trò chơi stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Tạo ra một ship object ship = Ship(ai_settings, screen) # Tạo một Group để chứa các bullet bullets = Group() # Tạo một Group để chứa các alien aliens = Group() # Tạo ra một hàng chứa các alien gf.create_fleet(ai_settings, screen, ship, aliens) # Bắt đầu main loop của trò chơi while True: # Theo dõi các event keyboard và mouse gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() # Update bullets gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) # update aline gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) # Vẽ lại screen gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def run_game(): # Initialise game and create a game object. pygame.init() ai_settings = Settings() #screen_width, screen_height=pygame.display.Info().current_w, pygame.display.Info().current_h screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Space Invaders") icon = pygame.image.load('resources/battleship32.png') pygame.display.set_icon(icon) # Make a play button. play_button = Button(ai_settings, screen, "Play") # Create an instance to store game statistics and create a scoreboard. stats = GameStats(ai_settings) sb = ScoreBoard(ai_settings, screen, stats) # Make a ship, a group of bullets and a group of enemies. ship = Ship(ai_settings, screen) bullets = Group() enemies = Group() ex = Explosion(ai_settings, screen) # Create a fleet of enemies. func.create_fleet(ai_settings, screen, ship, enemies) # Start main loop for the game. while True: # Watch for keyboard and mouse events. func.check_events(ai_settings, screen, stats, sb, play_button, ship, enemies, bullets) if stats.game_active: ship.update() func.update_bullets(ai_settings, screen, stats, sb, ship, enemies, ex, bullets) func.update_enemies(ai_settings, stats, sb, screen, ship, enemies, bullets) func.update_screen(ai_settings, screen, stats, sb, ship, enemies, ex, bullets, play_button)
def run_game(): pygame.init() game_settings = Settings() screen = pygame.display.set_mode( (game_settings.screen_width, game_settings.screen_height)) pygame.display.set_caption("Invaders!!") #create play button play_button = Button(game_settings, screen, "Play") # create an instance to store game statistics and create scoreboard stats = GameStats(game_settings) sb = Scoreboard(game_settings, screen, stats) # create player ship, a group of bullets, a group of aliens ship = Ship(game_settings, screen) bullets = Group() aliens = Group() # create alien fleet gf.create_fleet(game_settings, screen, ship, aliens) # main loop while True: gf.check_events(game_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(game_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(game_settings, stats, screen, ship, aliens, bullets) gf.update_screen(game_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def online(self): self.options = [ Button("Find a player", (540, 400)), Button("BACK", (100, 650)) ] self.selected = None