class SinglePlayer: def __init__(self): self.screen = pygame.display.set_mode( (Settings.single_screen_width, Settings.screen_height)) self.menu_button = Button( self.screen, (Settings.single_screen_width - Settings.button_width) // 2, Settings.screen_height * 0.9, 'BACK TO MENU') self.game = Game(self.screen, 0, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN, pygame.K_SPACE) def run(self): while True: for event in pygame.event.get().copy(): self.game.check_event(event) if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: mouse = pygame.mouse.get_pos() if self.menu_button.rect.collidepoint(mouse): return 'menu' self.screen.blit(Settings.bg_image, (0, 0)) self.menu_button.show() self.game.refresh() pygame.display.flip()
def __init__(self, game): super().__init__(game) # Copy the small display so that the main game is visible # in the background: self.surface = pygame.Surface(constants.SMALL_DISPLAY_SIZE, pygame.SRCALPHA) self.rect = pygame.Rect(0, 0, constants.SMALL_DISPLAY_WIDTH // 3, constants.SMALL_DISPLAY_HEIGHT // 2) self.rect.center = self.surface.get_rect().center radius = 10 pygame.draw.rect(self.surface, (0, 0, 0), self.rect, border_radius=radius) pygame.draw.rect(self.surface, constants.BUTTON_OUTLINE_COLOR, self.rect, 1, border_radius=radius) self.buttons = ( Button("Resume", (75, 25), (self.rect.centerx, self.rect.y + 25), self.resume_game), Button("Options", (75, 25), (self.rect.centerx, self.rect.centery), self.goto_options), Button("Quit", (75, 25), (self.rect.centerx, self.rect.bottom - 25), self.goto_main_menu) # TODO: add button for option menu )
def __init__(self, pygame, res, surface, game_manager): Screen.__init__(self, pygame, res, surface) self.game_manager = game_manager self.font = pygame.font.SysFont('cambria', 60) self.font2 = pygame.font.SysFont('cambria', 30) self.buttons['Restart'] = Button(pygame, res, surface, [20, 290, 300, 50], "Restart") self.buttons['Back'] = Button(pygame, res, surface, [20, 360, 300, 50], "Back")
def __init__(self, method_id, args, size, pos, sprite, sprite_hover, margin, values, current_value, base_title, base_sprite, base_sprite_hover, linked_object=None): Button.__init__(self, method_id, args, size, pos, sprite, sprite_hover, margin, linked_object) self.values = values self.current_value_ind = current_value self.base_title = base_title self.base_sprite = base_sprite self.base_sprite_hover = base_sprite_hover self.args.append(ANIMATION_SPEED)
class MultiPlayer: def __init__(self): self.screen = pygame.display.set_mode((Settings.multi_screen_width, Settings.screen_height)) self.menu_button = Button(self.screen, (Settings.multi_screen_width - Settings.button_width) // 2, Settings.screen_height * 0.9, 'BACK TO MENU') self.game1 = Game(self.screen, 0, pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s, pygame.K_ESCAPE) self.game2 = Game(self.screen, Settings.single_screen_width, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN, pygame.K_SPACE) self.winner_alert = None def run(self): while True: for event in pygame.event.get(): self.game1.check_event(event) self.game2.check_event(event) if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: mouse = pygame.mouse.get_pos() if self.menu_button.rect.collidepoint(mouse): return 'menu' self.screen.blit(Settings.bg_image, (0, 0)) self.menu_button.show() self.game1.refresh() self.game2.refresh() if self.game1.over and self.game2.over: if self.winner_alert is None: self.init_winner_label() self.winner_alert.draw() pygame.display.flip() def init_winner_label(self): score_difference = self.game2.score - self.game1.score if score_difference < 0: self.winner_alert = Alert(self.screen, 'PLAYER 1 WON!', Settings.font_result, Settings.multi_screen_width // 2, Settings.screen_height * 0.5) elif score_difference > 0: self.winner_alert = Alert(self.screen, 'PLAYER 2 WON!', Settings.font_result, Settings.multi_screen_width // 2, Settings.screen_height * 0.5) else: self.winner_alert = Alert(self.screen, 'IT\'S A TIE!', Settings.font_result, Settings.multi_screen_width // 2, Settings.screen_height * 0.5)
def __init__(self, pygame, res, surface): Screen.__init__(self, pygame, res, surface) self.font = pygame.font.SysFont('cambria', 60) self.font2 = pygame.font.SysFont('cambria', 30) self.buttons['Start Game'] = Button(pygame, res, surface, [20, 150, 300, 50], "Start Game") self.buttons['Tutorial'] = Button(pygame, res, surface, [20, 220, 300, 50], "Tutorial") self.buttons['Settings'] = Button(pygame, res, surface, [20, 290, 300, 50], "Settings") self.buttons['Exit'] = Button(pygame, res, surface, [20, 360, 300, 50], "Exit")
def __init__(self): self.screen = pygame.display.set_mode( (Settings.single_screen_width, Settings.screen_height)) self.menu_button = Button( self.screen, (Settings.single_screen_width - Settings.button_width) // 2, Settings.screen_height * 0.9, 'BACK TO MENU') self.game = Game(self.screen, 0, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN, pygame.K_SPACE)
def update_screen(ai_settings: Settings, screen, ship: Ship, aliens: Group, bullets: Group, stats: GameStats, play_button: Button): # Draw(update) the screen. screen.fill(ai_settings.bg_color) ship.blitme() aliens.draw(screen) for bullet in bullets: bullet.draw_bullet() if not stats.game_active: play_button.draw_botton() pygame.display.flip()
def __init__(self, game): super().__init__(game) self.buttons = (Button("New Game", (100, 50), (constants.SMALL_DISPLAY_WIDTH // 2, 50), self.new_game), Button("Options", (100, 50), (constants.SMALL_DISPLAY_WIDTH // 2, constants.SMALL_DISPLAY_HEIGHT // 2), self.goto_options), Button("Quit", (100, 50), (constants.SMALL_DISPLAY_WIDTH // 2, 200), self.game.quit))
def run_game(): pygame.init() screen_settings = Settings() stats = GameStats(screen_settings) screen = pygame.display.set_mode( (screen_settings.screen_width, screen_settings.screen_height)) ship = Ship(screen) pygame.display.set_caption('Alien Inv') screen_y = 0 background = Background(screen_settings.bg_image, [0, 0]) play_button = Button(screen_settings, screen, "Play") bullets = Group() enemies = Group() while True: # Game Loop gf.check_events( screen_settings, screen, ship, bullets, stats, play_button, enemies, ) if stats.game_active: ship.update() gf.update_bullets(bullets, enemies) gf.update_enemies(enemies, ship, screen_settings, stats, screen, bullets) gf.update_screen(screen_settings, screen, ship, screen_y, background, bullets, enemies, play_button, stats) screen_y += screen_settings.bg_speed
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() pygame.display.set_caption('Alien Invasion') ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) ship = Ship(ai_settings, screen) play_button = Button(ai_settings, screen, 'Play') bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, aliens, ship) stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # 开始游戏的主循环 while True: gf.check_events(ai_settings, screen, ship, bullets, stats, play_button, aliens, sb) if not stats.game_over: ship.update() gf.update_bullets(ai_settings, screen, bullets, aliens, ship, stats, sb) gf.update_aliens(ai_settings, screen, aliens, bullets, ship, stats, sb) gf.update_screen(ai_settings, screen, ship, bullets, aliens, stats, play_button, sb)
def __init__(self): self.screen = pygame.display.set_mode((Settings.multi_screen_width, Settings.screen_height)) self.menu_button = Button(self.screen, (Settings.multi_screen_width - Settings.button_width) // 2, Settings.screen_height * 0.9, 'BACK TO MENU') self.game1 = Game(self.screen, 0, pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s, pygame.K_ESCAPE) self.game2 = Game(self.screen, Settings.single_screen_width, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN, pygame.K_SPACE) self.winner_alert = None
def on_controller_input(joystick): button = None eventlist = pygame.event.get() for e in eventlist: try: if e.type == pygame.locals.JOYHATMOTION: x, y = joystick.get_hat(0) button = Button((x, y)) break elif e.type == pygame.locals.JOYBUTTONDOWN: button = Button(e.button) break except Exception: pass return button
def __init__(self): self.screen = pygame.display.set_mode( (Settings.single_screen_width, Settings.screen_height)) self.screen.blit(Settings.bg_image, (0, 0)) header = Settings.font_header.render('TETRIS', 1, Settings.font_color) self.screen.blit(header, ((Settings.single_screen_width - header.get_width()) / 2, Settings.screen_height * 0.1)) self.single_play_button = Button( self.screen, (Settings.single_screen_width - Settings.button_width) // 2, Settings.screen_height * 0.4, 'SINGLE PLAYER') self.multi_play_button = Button( self.screen, (Settings.single_screen_width - Settings.button_width) // 2, Settings.screen_height * 0.6, 'MULTI PLAYER') self.exit_button = Button( self.screen, (Settings.single_screen_width - Settings.button_width) // 2, Settings.screen_height * 0.8, 'EXIT')
def run_game(): """The function opens the game and creates a screen""" # Initialize pygame, settings, and screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.display_width, ai_settings.display_height)) pygame.display.set_caption("Alien Invasion") # Make a rocket ship rocket_ship = RocketShip(screen, ai_settings) # Make a group to store bullets in. bullets = Group() # Make a group of Aliens and the fleet. aliens = Group() game_functions.create_fleet(ai_settings, screen, rocket_ship, aliens) # Create an instance to store game statistics.# Create an instance to store game statistics and create a scoreboard stats = GameStats(ai_settings) empty_ship = EmptyShip(ai_settings, screen) sb = Scoreboard(ai_settings, screen, stats) # Make the Play button. play_button = Button(ai_settings, screen) # Start the main loop for the game. while True: game_functions.check_events(ai_settings, screen, stats, sb, play_button, rocket_ship, aliens, bullets) if stats.game_active: rocket_ship.update() game_functions.update_bullets(ai_settings, screen, stats, sb, rocket_ship, aliens, bullets) game_functions.update_aliens(ai_settings, stats, sb, screen, rocket_ship, aliens, bullets) game_functions.update_screen(ai_settings, screen, stats, sb, rocket_ship, aliens, bullets, play_button, empty_ship)
def determine_height(self, close_button): # Margin to be add at begin and at end height = MARGIN_BOX * 2 self.sep['height'] -= height self.sep['posY'] += height for row in self.elements: max_height = 0 for el in row: el_height = el.get_height() + MARGIN_TOP if el_height > max_height: max_height = el_height height += max_height row.insert(0, max_height) if close_button > 0: close_button_height = CLOSE_BUTTON_SIZE[ 1] + MARGIN_TOP + CLOSE_BUTTON_MARGINTOP height += close_button_height self.sep['height'] -= close_button_height # Button sprites name = fonts['ITEM_FONT'].render("Close", 1, WHITE) sprite = pg.transform.scale( pg.image.load(BUTTON_INACTIVE).convert_alpha(), CLOSE_BUTTON_SIZE) sprite.blit(name, (sprite.get_width() // 2 - name.get_width() // 2, sprite.get_height() // 2 - name.get_height() // 2)) sprite_hover = pg.transform.scale( pg.image.load(BUTTON_ACTIVE).convert_alpha(), CLOSE_BUTTON_SIZE) sprite_hover.blit( name, (sprite_hover.get_width() // 2 - name.get_width() // 2, sprite_hover.get_height() // 2 - name.get_height() // 2)) self.elements.append([ close_button_height, Button(GenericActions.CLOSE, [close_button], CLOSE_BUTTON_SIZE, (0, 0), sprite, sprite_hover, (CLOSE_BUTTON_MARGINTOP, 0, 0, 0)) ]) return height
def main(): # retrieve configuration file information config = ConfigParser() config.parse_config() config.check_config() # create header header = Header(config) header.create_main_header() button = [] beam = [] # loop over the cbpm(s) for idx_cbpm in range(len(config.cbpm)): # open output text file to append cbpm data file_object = open(config.output_file_name, 'a') # create cbpm specific header header.create_cbpm_header(idx_cbpm) # create class instances button.append(Button(config)) beam.append(Beam(config)) # loop over the turn(s) for idx_turn in range(config.n_turns): # retrieve x,y position for a given turn x_pos, y_pos = beam[idx_cbpm].get_beam_position(idx_cbpm, idx_turn) # retrieve button reading given beam position button_reading = button[idx_cbpm].get_reading( x_pos, y_pos, idx_cbpm) # write button reading to output text file file_object.write('0 0000000 ' + str(button_reading[0]) + ' ' + str(button_reading[1]) + ' ' + str(button_reading[2]) + ' ' + str(button_reading[3]) + ' \n')
def run_game(): """Initialize game, settings and create a screen object""" pygame.init() sounds = Sounds() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make the play button. play_button = Button(ai_settings, screen, 'Play') # Make a ship, a group of aliens and a group of bullets ship = Ship(ai_settings, screen) aliens = Group() bullets = Group() # Create a fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) # Create an instance to store the game statistics and scoreboard stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, sounds, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, sounds, aliens, bullets) gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, sb, ship, aliens, bullets) else: gf.display_menu_screen(ai_settings, screen, play_button)
def rungame(): # Initialize game,settings and create screen object. pygame.init() sett = Settings() screen = pygame.display.set_mode((sett.screen_width, sett.screen_height)) pygame.display.set_caption("Alien Invasion") """"Instance initialization""" # Make instance of game stats. stats = GameStats(sett) # Make instance of scoreboard. sb = ScoreBoard(sett, screen, stats) # Make the play button. b_play = Button(sett, screen, "PLAY") # Make instance of ship. ship = Ship(sett, screen) # Make a group to store bullets and aliens. aliens = Group() bullets = Group() # Make instance of alien. alien = Alien(sett, screen) gf.create_fleet(sett, screen, ship, aliens) # Start the main loop for the game. while True: # Watch for keyboard and mouse events. gf.check_events(sett, screen, stats, sb, b_play, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(sett, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(sett, stats, sb, screen, ship, aliens, bullets) # Redraw the screen during each pass through the loop. gf.update_screen(sett, screen, stats, sb, ship, aliens, bullets, b_play)
def __init__(self, parent=None): super().__init__(parent) self._generator = PiGenerator() self.setSceneRect(QRectF(QPointF(-self.SCENE_WIDTH/2, -self.SCENE_HEIGHT/2), QPointF(self.SCENE_WIDTH/2, self.SCENE_HEIGHT/2))) # scale type self.addItem(TextItem('Scale', TextItem.CENTER, self.SCALE_TYPE_LABEL_POS)) scaleTypeLeftButton = TriangleButton(TriangleButton.LEFT) scaleTypeLeftButton.setPos(self.SCALE_TYPE_LEFT_BUTTON_POS) scaleTypeLeftButton.clicked.connect(self._generator.setPreviousScaleType) self.addItem(scaleTypeLeftButton) scaleTypeRightButton = TriangleButton(TriangleButton.RIGHT) scaleTypeRightButton.setPos(self.SCALE_TYPE_RIGHT_BUTTON_POS) scaleTypeRightButton.clicked.connect(self._generator.setNextScaleType) self.addItem(scaleTypeRightButton) self._scaleTypeSelectedTextItem = TextItem(self._generator.scaleType, TextItem.CENTER, self.SCALE_TYPE_CENTER_POS) self._scaleTypeLeftTextItem = TextItem(self._generator.previousScaleType, TextItem.LEFT, self.SCALE_TYPE_LEFT_POS) self._scaleTypeRightTextItem = TextItem(self._generator.nextScaleType, TextItem.RIGHT, self.SCALE_TYPE_RIGHT_POS) self.addItem(self._scaleTypeSelectedTextItem) self.addItem(self._scaleTypeLeftTextItem) self.addItem(self._scaleTypeRightTextItem) # root note rootNoteLeftButton = TriangleButton(TriangleButton.LEFT) rootNoteLeftButton.setPos(self.ROOT_NOTE_LEFT_BUTTON_POS) rootNoteLeftButton.clicked.connect(self._generator.setPreviousRootNote) self.addItem(rootNoteLeftButton) rootNoteRightButton = TriangleButton(TriangleButton.RIGHT) rootNoteRightButton.setPos(self.ROOT_NOTE_RIGHT_BUTTON_POS) rootNoteRightButton.clicked.connect(self._generator.setNextRootNote) self.addItem(rootNoteRightButton) self._rootNoteSelectedTextItem = TextItem(self._generator.rootNote, TextItem.CENTER, self.ROOT_NOTE_CENTER_POS) self._rootNoteLeftTextItem = TextItem(self._generator.previousRootNote, TextItem.LEFT, self.ROOT_NOTE_LEFT_POS) self._rootNoteRightTextItem = TextItem(self._generator.nextRootNote, TextItem.RIGHT, self.ROOT_NOTE_RIGHT_POS) self.addItem(self._rootNoteSelectedTextItem) self.addItem(self._rootNoteLeftTextItem) self.addItem(self._rootNoteRightTextItem) # scale self._scaleItem = ScaleTextItem() self._scaleItem.setScale(self._generator.scale) self._scaleItem.setPos(self.SCALE_POS) self.addItem(self._scaleItem) # solo length self._soloLengthLabelTextItem = TextItem('Solo length', TextItem.LEFT, self.SOLO_LENGTH_LABEL_POS) self.addItem(self._soloLengthLabelTextItem) self._soloLengthMaxLabelTextItem = TextItem('Max length: '+str(self._generator.maxSoloLength), TextItem.RIGHT, self.SOLO_LENGTH_MAX_POS) self.addItem(self._soloLengthMaxLabelTextItem) self._soloLengthTextItem = TextItem(str(self._generator.soloLength), TextItem.CENTER, self.SOLO_LENGTH_POS) self.addItem(self._soloLengthTextItem) soloLengthLeftButton = TriangleButton(TriangleButton.LEFT) soloLengthLeftButton.setPos(self.SOLO_LENGTH_LEFT_BUTTON_POS) soloLengthLeftButton.clicked.connect(self._generator.decreaseSoloLength) self.addItem(soloLengthLeftButton) soloLengthRightButton = TriangleButton(TriangleButton.RIGHT) soloLengthRightButton.setPos(self.SOLO_LENTGH_RIGHT_BUTTON_POS) soloLengthRightButton.clicked.connect(self._generator.increaseSoloLength) self.addItem(soloLengthRightButton) # generate button generateButton = Button('Generate', self.GENERATE_BUTTON_WIDTH, self.GENERATE_BUTTON_HEIGHT) generateButton.setPos(self.GENERATE_BUTTON_POS) self.addItem(generateButton) # about button aboutButton = Button('?', self.ABOUT_BUTTON_WIDTH, self.ABOUT_BUTTON_HEIGHT) aboutButton.setPos(self.ABOUT_BUTTON_POS) self.addItem(aboutButton) # solo self._soloTextItem1 = TextItem('', TextItem.CENTER, self.SOLO_POS_1) self.addItem(self._soloTextItem1) self._soloTextItem2 = TextItem('', TextItem.CENTER, self.SOLO_POS_2) self.addItem(self._soloTextItem2) self._soloTextItem3 = TextItem('', TextItem.CENTER, self.SOLO_POS_3) self.addItem(self._soloTextItem3) self._soloTextItem4 = TextItem('', TextItem.CENTER, self.SOLO_POS_4) self.addItem(self._soloTextItem4) self._soloTextItem5 = TextItem('', TextItem.CENTER, self.SOLO_POS_5) self.addItem(self._soloTextItem5) self._soloTextItem6 = TextItem('', TextItem.CENTER, self.SOLO_POS_6) self.addItem(self._soloTextItem6) self._soloTextItem7 = TextItem('', TextItem.CENTER, self.SOLO_POS_7) self.addItem(self._soloTextItem7) self._soloTextItem8 = TextItem('', TextItem.CENTER, self.SOLO_POS_8) self.addItem(self._soloTextItem8) self._soloAreas = ((self._soloTextItem3,), (self._soloTextItem3, self._soloTextItem4), (self._soloTextItem3, self._soloTextItem4, self._soloTextItem5), (self._soloTextItem2, self._soloTextItem3, self._soloTextItem4, self._soloTextItem5), (self._soloTextItem2, self._soloTextItem3, self._soloTextItem4, self._soloTextItem5, self._soloTextItem6), (self._soloTextItem2, self._soloTextItem3, self._soloTextItem4, self._soloTextItem5, self._soloTextItem6, self._soloTextItem7), (self._soloTextItem1, self._soloTextItem2, self._soloTextItem3, self._soloTextItem4, self._soloTextItem5, self._soloTextItem6, self._soloTextItem7), (self._soloTextItem1, self._soloTextItem2, self._soloTextItem3, self._soloTextItem4, self._soloTextItem5, self._soloTextItem6, self._soloTextItem7, self._soloTextItem8)) scaleTypeLeftButton.clicked.connect(self._updateTextItems) scaleTypeRightButton.clicked.connect(self._updateTextItems) rootNoteLeftButton.clicked.connect(self._updateTextItems) rootNoteRightButton.clicked.connect(self._updateTextItems) soloLengthLeftButton.clicked.connect(self._updateTextItems) soloLengthRightButton.clicked.connect(self._updateTextItems) generateButton.clicked.connect(self._updateTextItems) aboutButton.clicked.connect(self.aboutButtonClicked)
def create_buttons(self): """Method to create buttons to be used in the GUI """ # Row sizing row0 = self.window_height - (self.button_height * 5) row1 = row0 + self.button_height row2 = row0 + self.button_height * 2 row3 = row0 + self.button_height * 3 row4 = row0 + self.button_height * 4 # Column sizing title_width = self.window_width / 6 column0_x = 0 column1_x = self.window_width * 1 / 6 column2_x = self.window_width * 2 / 6 column3_x = self.window_width * 2 / 6 + (2 / 3) * title_width - 1 column4_x = self.window_width * 3 / 6 - 2 column5_x = self.window_width * 3 / 6 + (1 / 3) * title_width - 2 column6_x = column5_x + title_width * 8 / 6 - 1 column0_width = self.window_width / 6 column1_width = self.window_width / 6 column2_width = title_width * 2 / 3 column3_width = title_width / 3 column4_width = title_width / 3 column5_width = (title_width * 4 / 6) * 2 column6_width = self.window_width - column6_x column0a_width = column0_width / 4 column0b_width = column0_width / 2 column0c_width = column0_width / 4 + 1 column0b_x = column0_x + column0a_width column0c_x = column0b_x + column0b_width - 1 column1b_x = column1_x + column0a_width column1c_x = column1b_x + column0b_width - 1 column5a_width = title_width * 4 / 6 + 2 column5c_width = column5a_width / 4 column5d_width = column5a_width / 2 column5e_width = column5a_width / 4 column5b_x = column5_x + column5a_width - 1 column5d_x = column5b_x + column5c_width - 1 column5e_x = column5d_x + column5d_width column6a_width = column6_width / 2 column6b_width = column6_width / 8 column6c_width = column6_width / 4 + 2 column6b_x = column6_x + column6_width / 2 column6c_x = column6b_x + column6b_width - 1 column6d_x = column6c_x + column6c_width self.titles = { # Titles "Generate": Button(DARK_GREY, column0_x, row0, column0_width, self.button_height, "Generate"), "Options1": Button(DARK_GREY, column1_x, row0, column1_width, self.button_height, "Options"), "Search": Button(DARK_GREY, column2_x, row0, column2_width, self.button_height, "Search"), "Length": Button(DARK_GREY, column3_x, row0, column3_width, self.button_height, "Length"), "Visits": Button(DARK_GREY, column4_x, row0, column4_width, self.button_height, "Visits"), "Simulate": Button(DARK_GREY, column5_x, row0, column5_width, self.button_height, "Simulate"), "Options2": Button(DARK_GREY, column6_x, row0, column6_width, self.button_height, "Options"), # Maze Length buttons "lengthA*": Button(GREY, column3_x, row1, column3_width, self.button_height, str('-')), "lengthBFS": Button(GREY, column3_x, row2, column3_width, self.button_height, str('-')), "lengthDFS": Button(GREY, column3_x, row3, column3_width, self.button_height, str('-')), "lengthBlank": Button(GREY, column3_x, row4, column3_width, self.button_height, ""), # Maze Visits buttons "visitsA*": Button(GREY, column4_x, row1, column4_width, self.button_height, str('-')), "visitsBFS": Button(GREY, column4_x, row2, column4_width, self.button_height, str('-')), "visitsDFS": Button(GREY, column4_x, row3, column4_width, self.button_height, str('-')), "visitsBlank": Button(GREY, column4_x, row4, column4_width, self.button_height, ""), # Reinforcement Learning parameter buttons "step_size": Button(GREY, column5_x, row2, column5a_width, self.button_height, "Step Size:"), "epsilon": Button(GREY, column5_x, row3, column5a_width, self.button_height, "Epsilon:"), "discount": Button(GREY, column5_x, row4, column5a_width, self.button_height, "Discount:"), "Size": Button(GREY, column1b_x, row2, column0b_width, self.button_height, str(self.maze_size)), "stepDisplay": Button(GREY, column5d_x, row2, column5d_width, self.button_height, str(self.step_size)), "epsilonDisplay": Button(GREY, column5d_x, row3, column5d_width, self.button_height, str(self.epsilon)), "discountDisplay": Button(GREY, column5d_x, row4, column5d_width, self.button_height, str(self.discount)), "dispEpisode": Button(GREY, column6c_x, row1, column6c_width, self.button_height, str(self.num_episodes)), "dispRuns": Button(GREY, column6c_x, row2, column6c_width, self.button_height, str(self.num_runs)), } self.buttons = { # Maze generation buttons "genDFS": Button(GREY, column0_x, row1, column0_width, self.button_height, "DFS"), "Prims": Button(GREY, column0_x, row2, column0_width, self.button_height, "Prims"), "Recursive": Button(GREY, column0_x, row3, column0_width, self.button_height, "Recursive"), "Start": Button(GREY, column0_x, row4, column0a_width, self.button_height, "S"), "Click": Button(GREY, column0b_x, row4, column0b_width, self.button_height, "Click"), "End": Button(GREY, column0c_x, row4, column0c_width, self.button_height, "E"), # Maze Options "Maze Size": Button(GREY, column1_x, row1, column1_width, self.button_height, "Maze Size"), "Minus": Button(GREY, column1_x, row2, column0a_width, self.button_height, "-"), "Plus": Button(GREY, column1c_x, row2, column0c_width, self.button_height, "+"), "Build": Button(GREY, column1_x, row3, column1_width, self.button_height, "Show Build"), "Explore": Button(GREY, column1_x, row4, column1_width, self.button_height, "Show Explore"), # Maze searching buttons "A*": Button(GREY, column2_x, row1, column2_width, self.button_height, "A*"), "BFS": Button(GREY, column2_x, row2, column2_width, self.button_height, "BFS"), "DFS": Button(GREY, column2_x, row3, column2_width, self.button_height, "DFS"), "Blank": Button(GREY, column2_x, row4, column2_width, self.button_height, ""), # Reinforcement Learning "Q-Learning": Button(GREY, column5_x, row1, column5a_width, self.button_height, "Q-Learning"), "Expected SARSA": Button(GREY, column5b_x, row1, column5a_width, self.button_height, "Exp. SARSA"), "stepMinus": Button(GREY, column5b_x, row2, column5c_width, self.button_height, "-"), "stepPlus": Button(GREY, column5e_x, row2, column5e_width, self.button_height, "+"), "epsilonMinus": Button(GREY, column5b_x, row3, column5c_width, self.button_height, "-"), "epsilonPlus": Button(GREY, column5e_x, row3, column5e_width, self.button_height, "+"), "discountMinus": Button(GREY, column5b_x, row4, column5c_width, self.button_height, "-"), "discountPlus": Button(GREY, column5e_x, row4, column5e_width, self.button_height, "+"), # RL Options "Num Episodes": Button(GREY, column6_x, row1, column6a_width, self.button_height, "Episodes:"), "minusEpisode": Button(GREY, column6b_x, row1, column6b_width, self.button_height, "-"), "plusEpisode": Button(GREY, column6d_x, row1, column6b_width, self.button_height, "+"), "Num Runs": Button(GREY, column6_x, row2, column6a_width, self.button_height, "Runs:"), "minusRun": Button(GREY, column6b_x, row2, column6b_width, self.button_height, "-"), "plusRun": Button(GREY, column6d_x, row2, column6b_width, self.button_height, "+"), "Heat Map": Button(GREY, column6_x, row3, column6a_width, self.button_height, "Heat Map"), "Show Run": Button(GREY, column6b_x, row3, column6a_width, self.button_height, "Show Run"), "Reset": Button(RED, column6_x, row4, column6_width, self.button_height, "Reset") }
def start(): global window0, widgets, batch, focus window0 = pyglet.window.Window(WIDTH, HEIGHT, "Handball Score Table", vsync=True) window0.set_icon(icon1, icon2) button1 = Button(15, HEIGHT - 35, "Load last configuration", 16, (0, 0, 0, 255), secondary_color=(200, 200, 200, 255)) button2 = Button(295, HEIGHT - 65, "Load custom configuration", 16, (0, 0, 0, 255), secondary_color=(200, 200, 200, 255)) button3 = Button(295, HEIGHT - 35, "Save custom configuration", 16, (0, 0, 0, 255), secondary_color=(200, 200, 200, 255)) button4 = Button(570, HEIGHT - 335, "Start match", 26, (0, 0, 0, 255), secondary_color=(200, 200, 200, 255)) buttons = (button1, button2, button3, button4) @window0.event def on_draw(): pyglet.gl.glClearColor(0.98, 0.98, 0.98, 1) window0.clear() for widget in widgets: widget.render() batch.draw() for button in buttons: button.render() @window0.event def on_mouse_motion(x, y, dx, dy): for widget in widgets: if widget.hit_test(x, y): window0.set_mouse_cursor(text_cursor) break else: window0.set_mouse_cursor(None) for button in buttons: button.pressed(x, y) @window0.event def on_mouse_release(x, y, button, modifiers): global window0, widgets, batch, focus for widget in widgets: if widget.hit_test(x, y): set_focus(widget) break else: set_focus(None) if focus: focus.caret.on_mouse_press(x, y, button, modifiers) if button1.pressed(x, y): load_configuration() elif button2.pressed(x, y): load_configuration(join("data", "custom_configs", "{}.ini".format(get_text(67)))) elif button3.pressed(x, y): save_configuration(join("data", "custom_configs", "{}.ini".format(get_text(68)))) elif button4.pressed(x, y): if start_table(): # open the actual table interface thingy what am I saying save_configuration() window0.close() del window0 del widgets, batch, focus @window0.event def on_mouse_drag(x, y, dx, dy, buttons, modifiers): if focus: focus.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers) @window0.event def on_text(text): if focus: focus.caret.on_text(text) @window0.event def on_text_motion(motion): if focus: focus.caret.on_text_motion(motion) @window0.event def on_text_motion_select(motion): if focus: focus.caret.on_text_motion_select(motion) @window0.event def on_key_release(symbol, modifiers): global window0, widgets, batch, focus if symbol == pyglet.window.key.TAB: if modifiers & pyglet.window.key.MOD_SHIFT: direction = -1 else: direction = 1 if focus in widgets: i = widgets.index(focus) else: i = 0 direction = 0 set_focus(widgets[(i + direction) % len(widgets)]) elif symbol == pyglet.window.key.ENTER: if start_table(): # open the actual table interface thingy... what am I saying save_configuration() window0.close() del window0 del widgets, batch, focus elif symbol == pyglet.window.key.ESCAPE: pyglet.app.exit() @window0.event def on_close(): pyglet.app.exit() batch = pyglet.graphics.Batch() create_labels() widgets = create_widgets() text_cursor = window0.get_system_mouse_cursor('text') focus = None set_focus(widgets[0])
#!/usr/bin/python3 from time import sleep import RPi.GPIO as GPIO from src import utils from src.cfg import Cfg from src.connection_manager import ConnectionManager from src.button import Button button = Button() cfg = Cfg() connection_manager = ConnectionManager(cfg) GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(17, GPIO.FALLING, callback=button.on_pull, bouncetime=200) while True: status = button.requested_status internet = utils.internet_connection_is_up() # print(f"Device set to be: {status}" ) connection_manager.ensure_status(status, internet) sleep(1)
def start_game_screen(screen, clock, welcome_music, welcome_bg, help): global play_intro_music pygame.mouse.set_visible(False) button_list = pygame.sprite.Group() button = Button('src/level01_gray.jpg', 280, 250, 1) button_list.add(button) button = Button('src/level02_gray.jpg', 550, 250, 2) button_list.add(button) button = Button('src/help.png', 930, 20, 3) button_list.add(button) x = pygame.mouse.get_pos()[0] y = pygame.mouse.get_pos()[1] cursor_list = pygame.sprite.Group() cursor = Cursor('src/cursor.png', x, y) cursor_list.add(cursor) welcome_music.set_volume(.5) if play_intro_music: welcome_music.play() pygame.display.flip() waiting = True isOverOut = False while waiting: x = pygame.mouse.get_pos()[0] y = pygame.mouse.get_pos()[1] cursor.update(x, y) clock.tick(60) isOverOut = False for button in pygame.sprite.spritecollide(cursor, button_list, False): isOverOut = True level = button.selected() if level == 1: button.isOver('src/level01_color.jpg') elif level == 2: button.isOver('src/level02_color.jpg') else: button.isOver('src/help_over.png') if not isOverOut: for button in button_list: level = button.selected() if level == 1: button.isOver('src/level01_gray.jpg') elif level == 2: button.isOver('src/level02_gray.jpg') else: button.isOver('src/help.png') for event in pygame.event.get(): if event.type == pygame.QUIT: choice = -1 waiting = False if event.type == pygame.MOUSEBUTTONDOWN and pygame.sprite.spritecollide(cursor, button_list, False): buttons = pygame.sprite.spritecollide(cursor, button_list, False) for button in buttons: choice = button.selected() if choice == 1: play_intro_music = True waiting = False welcome_music.stop() elif choice == 2: play_intro_music = True waiting = False welcome_music.stop() elif choice == 3: waiting = False screen.blit(welcome_bg, (0, 0)) button_list.draw(screen) cursor_list.draw(screen) pygame.display.flip() return choice
def main(): global play_intro_music """ Main Program """ pygame.time.set_timer(USEREVENT + 1, 1000) pygame.time.set_timer(USEREVENT + 2, 1000) # Sound pygame.mixer.pre_init(44100, -16, 1, 512) pygame.init() pygame.mixer.init() bg_sound = pygame.mixer.Sound("src/sound/bg.wav") welcome_music = pygame.mixer.Sound("src/sound/welcome.wav") sound_coin = pygame.mixer.Sound("src/sound/coin.wav") oof = pygame.mixer.Sound("src/sound/oof.wav") oof.set_volume(0.6) # Set the height and width of the screen size = [SCREEN_WIDTH, SCREEN_HEIGHT] screen = pygame.display.set_mode(size) pygame.display.set_caption("Lode Runner") # health heart = pygame.image.load('src/heart.png') font = pygame.font.Font('src/Pixel.otf', 20) # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() FPS = 50 score = 0 removed_blocks = [] health = 5 last_time_hit = 0 removed_enemies = [] game_over = True start_game = True bg = pygame.image.load("src/oof.jpg") welcome_bg = pygame.image.load("src/welcome_bg.jpg") help = pygame.image.load('src/how_to.jpg') # -------- Main Program Loop ----------- while not done: if game_over: if start_game: choice = start_game_screen(screen, clock, welcome_music, welcome_bg, help) start_game = False game_over = False score = 0 removed_blocks = [] health = 5 last_time_hit = 0 removed_enemies = [] # restart game # player if choice == 1: # Create the player bg_sound.play() player = Player() player_list = pygame.sprite.Group() player_list.add(player) # Create all the levels level_list = [Level_01(player, 'src/brick_2.jpg')] # Set the current level current_level_no = 0 current_level = level_list[current_level_no] player.level = current_level # initialize player player.rect.x = 500 player.rect.y = 490 # ladder ladder_list = pygame.sprite.Group() for i in range(10): ladder = Ladder(210, 410 - (20 * (i + 1)), 'src/ladder.png') # 1 ladder_list.add(ladder) for i in range(7): ladder = Ladder(610, 215 - (20 * (i + 1)), 'src/ladder.png') # 2 ladder_list.add(ladder) for i in range(11): ladder = Ladder(790, 570 - (20 * (i + 1)), 'src/ladder.png') # 3 ladder_list.add(ladder) for i in range(9): ladder = Ladder(2.5, 570 - (20 * (i + 1)), 'src/ladder.png') # 4 ladder_list.add(ladder) for i in range(8): ladder = Ladder(680, 360 - (20 * (i + 1)), 'src/ladder.png') # 5 ladder_list.add(ladder) # enemy enemy_list = pygame.sprite.Group() enemy = Enemy(5, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(3, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(1, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(0, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(6, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) # coins for i in range(1): # This represents a block block = Block('src/coin.png') block_list = pygame.sprite.Group() all_sprites_list = pygame.sprite.Group() prev_level = random.randrange(0, 6) prev_level = block.new_coin(current_level.get_levels(), prev_level) while True: if pygame.sprite.spritecollide(block, ladder_list, False): prev_level = block.new_coin(current_level.get_levels(), prev_level) else: break # Add the block to the list of objects block_list.add(block) all_sprites_list.add(block) elif choice == 2: # Create the player bg_sound.play() player = Player() player_list = pygame.sprite.Group() player_list.add(player) # Create all the levels level_list = [Level_02(player, 'src/brick_purple.jpg')] # Set the current level current_level_no = 0 current_level = level_list[current_level_no] player.level = current_level # initialize player player.rect.x = 500 player.rect.y = 490 # ladder ladder_list = pygame.sprite.Group() for i in range(25): ladder = Ladder(180, 570 - (20 * (i + 1)), 'src/ladder.png') # 1 ladder_list.add(ladder) for i in range(25): ladder = Ladder(770, 570 - (20 * (i + 1)), 'src/ladder.png') # 2 ladder_list.add(ladder) # enemy enemy_list = pygame.sprite.Group() enemy = Enemy(5, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(3, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(1, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(0, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) enemy = Enemy(6, 64, 64, current_level.get_platform_list(), current_level.get_levels()) enemy_list.add(enemy) # blocks for i in range(1): # This represents a block block = Block('src/coin.png') block_list = pygame.sprite.Group() all_sprites_list = pygame.sprite.Group() prev_level = random.randrange(0, 6) prev_level = block.new_coin(current_level.get_levels(), prev_level) while True: if pygame.sprite.spritecollide(block, ladder_list, False): prev_level = block.new_coin(current_level.get_levels(), prev_level) else: break # Add the block to the list of objects block_list.add(block) all_sprites_list.add(block) elif choice == -1: done = True elif choice == 3: return_button = Button('src/return_white.png', 930, 20, 10) button_list = pygame.sprite.Group() button_list.add(return_button) x = pygame.mouse.get_pos()[0] y = pygame.mouse.get_pos()[1] cursor_list = pygame.sprite.Group() cursor = Cursor('src/cursor.png', x, y) cursor_list.add(cursor) else: if choice == 1: choice = show_go_screen(screen, clock, bg_sound, 'src/oof.jpg', score, choice) elif choice == 2: choice = show_go_screen(screen, clock, bg_sound, 'src/oof_level02.jpg', score, choice) start_game = True if choice != -1 and choice != 3: screen.fill(BLACK) if pygame.sprite.spritecollide(player, ladder_list, False): climbing = True player.set_is_climbing(True) player.setchangey(0) else: climbing = False player.setpressingbutton(False) player.set_is_climbing(False) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == USEREVENT + 1: for block in removed_blocks: if block[1] >= 3: replace_block = current_level.replace_block(block[0]) removed_blocks.remove(block) hit_list = pygame.sprite.spritecollide(replace_block, enemy_list, False) for enemies in hit_list: enemies.set_vel(0) removed_enemy = enemies.getID() removed_enemies.append([removed_enemy, 0]) enemy_list.remove(enemies) hit_list = pygame.sprite.spritecollide(replace_block, player_list, False) for players in hit_list: health = 0 player_list.remove(players) platform_list = current_level.get_platform_list() for enemies in enemy_list: enemies.set_platform(platform_list) else: block[1] += 1 if event.type == USEREVENT + 2: for enemies in removed_enemies: enemies[1] += 1 if enemies[1] >= 10: enemy = Enemy(enemies[0][0], enemies[0][1], enemies[0][2], enemies[0][3], enemies[0][4]) enemy_list.add(enemy) removed_enemies.remove(enemies) if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player.go_left(screen) if event.key == pygame.K_RIGHT: player.go_right(screen) if event.key == pygame.K_SPACE: player.jump() if event.key == pygame.K_UP and climbing == True: player.climb(-3, True) if event.key == pygame.K_DOWN and climbing == True: player.climb(3, True) if event.key == pygame.K_r: block_for_remove = player.destroy_block(current_level) if block_for_remove != 0: removed_blocks.append([block_for_remove, 0]) current_level.remove_block(block_for_remove) platform_list = current_level.get_platform_list() for enemies in enemy_list: enemies.set_platform(platform_list) if event.type == pygame.KEYUP: if event.key == pygame.K_UP: player.climb(0, False) if event.key == pygame.K_DOWN: player.climb(0, False) if event.key == pygame.K_LEFT and player.change_x < 0: player.stop() if event.key == pygame.K_RIGHT and player.change_x > 0: player.stop() # Update items in the level current_level.update() # If the player gets near the right side, shift the world left (-x) if player.rect.right > SCREEN_WIDTH: player.rect.right = SCREEN_WIDTH # If the player gets near the left side, shift the world right (+x) if player.rect.left < 0: player.rect.left = 0 # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False) # Check the list of collisions. for block in blocks_hit_list: score += 1 sound_coin.play() prev_level = block.new_coin(current_level.get_levels(), prev_level) while True: if pygame.sprite.spritecollide(block, ladder_list, False): prev_level = block.new_coin(current_level.get_levels(), prev_level) else: break for players in player_list: if pygame.time.get_ticks() > last_time_hit + 3000 or last_time_hit == 0: if pygame.sprite.spritecollide(players, enemy_list, False): health -= 1 oof.play() last_time_hit = pygame.time.get_ticks() if health == 0: player_list.remove(players) # health for i in range(health): if choice == 1: screen.blit(heart, [850 + (i * 30), 30]) elif choice == 2: screen.blit(heart, [437 + (i * 30), 30]) # Draw all the sprites # Update the player. text2 = font.render("Score: " + str(score), True, WHITE) if choice == 1: screen.blit(text2, [850, 0]) elif choice == 2: screen.blit(text2, [450, 0]) current_level.draw(screen) block_list.update(screen) ladder_list.draw(screen) player_list.update(screen) for enemies in enemy_list: enemies.movement(screen) # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT if health == 0: game_over = True elif choice == 3: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.MOUSEBUTTONDOWN and pygame.sprite.spritecollide(cursor, button_list, False): buttons = pygame.sprite.spritecollide(cursor, button_list, False) for button in buttons: level = button.selected() if level == 10: start_game = True game_over = True play_intro_music = False image = pygame.image.load('src/how_to.jpg') screen.blit(image, (0, 0)) isOverOut = False for button in pygame.sprite.spritecollide(cursor, button_list, False): isOverOut = True level = button.selected() if level == 10: button.isOver('src/return.png') if not isOverOut: for button in button_list: level = button.selected() if level == 10: button.isOver('src/return_white.png') button_list.draw(screen) x = pygame.mouse.get_pos()[0] y = pygame.mouse.get_pos()[1] cursor.update(x, y) cursor_list.draw(screen) # Limit to 50 frames per second clock.tick(FPS) # Go ahead and update the screen with what we've drawn. pygame.display.flip() pygame.quit()
def run_game(): # 初始化背景设置 pygame.init() # 创建游戏设置类的实例 ai_settings = Settings() # 创建屏幕 screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) # 创建surface对象 pygame.display.set_caption("Alien Invasion") # 创建Play按钮 play_button = Button(screen, "Play") # 创建一艘飞船 ship = Ship(ai_settings, screen) # 创建一个用于存储子弹的编组 bullets = Group() # 创建一组外星人 aliens = Group() gf.create_fleet(ai_settings, screen, aliens) # 创建游戏角色 character = Character(screen) # 创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) # 创建记分牌 sb = Scoreboard(ai_settings, screen, stats) # 开始游戏的主循环 while True: # 监听事件 gf.check_events(ai_settings=ai_settings, screen=screen, stats=stats, play_button=play_button, sb=sb, ship=ship, aliens=aliens, bullets=bullets) if stats.game_active: # 根据标志修改飞船位置 ship.update() # 更新子弹 gf.update_bullets(ai_settings, screen, stats, sb, aliens, bullets) # 更新外星人 gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) # 更新屏幕 gf.update_screen(ai_settings=ai_settings, screen=screen, stats=stats, ship=ship, character=character, aliens=aliens, bullets=bullets, play_button=play_button, sb=sb)
play_label_on.fill(BACKGROUND_COLOR) pygame.draw.polygon(play_label_on, (200, 200, 200), [(5, 5), (5, 35), (35, 20)]) # White rects ON pause_label_on = pygame.Surface((40, 40)) pause_label_on.fill(BACKGROUND_COLOR) pygame.draw.rect(pause_label_on, (200, 200, 200), pygame.Rect((5, 5), (10, 30))) pygame.draw.rect(pause_label_on, (200, 200, 200), pygame.Rect((20, 5), (10, 30))) # DoubleTriangleBackwards back_label = pygame.Surface((60, 40)) back_label.fill((BACKGROUND_COLOR)) pygame.draw.rect(back_label, (200, 200, 200), pygame.Rect((5, 5), (5, 30))) pygame.draw.polygon(back_label, (200, 200, 200), [(7, 20), (25, 5), (25, 35)]) pygame.draw.polygon(back_label, (200, 200, 200), [(20, 20), (38, 5), (38, 35)]) # BUTTONS recordingButton = Button((5, 5), (40, 40), recording_label, not_recording_label, True) playButton = Button((50, 5), (40, 40), pause_label_on, play_label_on, True) back_button = Button((95, 5), (60, 40), back_label, back_label, True) # FONTS fonts = {} for x in [12, 15, 20, 25, 35, 40, 50, 75]: fonts[x] = pygame.font.SysFont("monospace", x)
pygame.init() gameDisplay = pygame.display.set_mode((1000, 600)) crashed = False clock = pygame.time.Clock() dialog = CPD(60, 60) dialog.get_top_button().fill(pygame.Color(0, 255, 0)) # dialog.set_alpha(255) dialog.set_is_visible(False) # Create a button that pulls up the dialog button = Button(100, 100, 200, 50) button.fill(pygame.Color(100, 100, 0)) button.set_alpha(255) def show_dialog(args): s = Card("Colonel Mustard", CardType.SUSPECT, "Colonel Mustard") args['d'].set_unavailable_players([s]) args['d'].set_is_visible(True) button.set_on_click(show_dialog, {'d': dialog}) backgroundview = View(0, 0, 1000, 1000) backgroundview.fill(pygame.Color(255, 255, 255)) backgroundview.set_alpha(255)
def main(): """ Main function used to run the Sudoku game uses pygame functionality to track game events """ # initialize pygame pygame.init() pygame.display.set_caption("Sudoku") # Initialize the board, check board button the game window and FPS clock and the variable holding key strokes global FPSCLOCK, DISPLAYSURF FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode( (WINDOW_WIDTH, WINDOW_HEIGHT + BOTTOMGAP)) key = None sudoku_board = Board(WINDOW_WIDTH, WINDOW_HEIGHT, SQUARE_SIZE, CELL_SIZE, DISPLAYSURF, board) check_board = Button((LIGHTGRAY), 20, WINDOW_HEIGHT + 20, 50, 25, 25, "check") # Fill window background with Black DISPLAYSURF.fill(BLACK) # Call board and button commands to draw initial board state sudoku_board.draw_grid() check_board.draw(DISPLAYSURF) # game loop watches for events such as key strokes and mouse clicks # redraws board and button every loop instance while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: key = 1 if event.key == pygame.K_2: key = 2 if event.key == pygame.K_3: key = 3 if event.key == pygame.K_4: key = 4 if event.key == pygame.K_5: key = 5 if event.key == pygame.K_6: key = 6 if event.key == pygame.K_7: key = 7 if event.key == pygame.K_8: key = 8 if event.key == pygame.K_9: key = 9 if event.key == pygame.K_KP1: key = 1 if event.key == pygame.K_KP2: key = 2 if event.key == pygame.K_KP3: key = 3 if event.key == pygame.K_KP4: key = 4 if event.key == pygame.K_KP5: key = 5 if event.key == pygame.K_KP6: key = 6 if event.key == pygame.K_KP7: key = 7 if event.key == pygame.K_KP8: key = 8 if event.key == pygame.K_KP9: key = 9 if event.key == pygame.K_DELETE: sudoku_board.delete_pos() key = None if event.key == pygame.K_RETURN: sudoku_board.place(key) key = None if event.key == pygame.K_SPACE: sudoku_board.solve_self() if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() clicked = sudoku_board.click(pos) if clicked: sudoku_board.selected = clicked elif check_board.isOver(pos): sudoku_board.check_board() key = None if sudoku_board.selected != (-1, -1) and key is not None: sudoku_board.guess(key) redraw(DISPLAYSURF, sudoku_board, check_board, WINDOW_HEIGHT, BOTTOMGAP) pygame.display.update()
def __init__(self, pygame, res, surface): Screen.__init__(self, pygame, res, surface) self.font = pygame.font.SysFont('cambria', 60) self.font2 = pygame.font.SysFont('cambria', 30) self.buttons['Back'] = Button(pygame, res, surface, [20, 360, 300, 50], "Back")