def __init__(self, ai_settings, stats): # show button self.button_stop = Button('material/image/game_pause_nor.png', 'material/image/game_pause_pressed.png', ai_settings.screen, (30, 20)) self.button_back_menu = Button( 'material/image/game_back_menu_nor.png', 'material/image/game_back_menu_pressed.png', ai_settings.screen, (80, 20))
def __init__(self, characterReader): self.mouseDown = False self.characterReader = characterReader pygame.font.init() # Icon icon = pygame.Surface((1, 1)) icon.set_alpha(0) pygame.display.set_icon(icon) pygame.display.set_caption(self.WINDOW_TITLE) self.surface = pygame.display.set_mode(self.SCREEN_SIZE) # Elements self.grid = Grid(self.surface, (self.GRID_MARGIN, self.GRID_MARGIN)) self.resetButton = Button(self.surface, (25, 275), "Reset") self.char_list = CharList(self.surface, (300, self.GRID_MARGIN))
def __init__(self, ai_settings, stats): # main button self.button_feiji = Button('material/image/feiji.png', 'material/image/feiji_down.png', ai_settings.screen, (200, 300)) # self.button_rank = Button('material/image/rank.png', 'material/image/rank_down.png', ai_settings.screen, (200,340)) self.button_setting = Button('material/image/setting.png', 'material/image/setting_down.png', ai_settings.screen, (200, 340)) self.button_about = Button('material/image/about.png', 'material/image/about_down.png', ai_settings.screen, (200, 380))
class Ui(): def __init__(self, ai_settings, stats): # main button self.button_feiji = Button('material/image/feiji.png', 'material/image/feiji_down.png', ai_settings.screen, (200, 300)) # self.button_rank = Button('material/image/rank.png', 'material/image/rank_down.png', ai_settings.screen, (200,340)) self.button_setting = Button('material/image/setting.png', 'material/image/setting_down.png', ai_settings.screen, (200, 340)) self.button_about = Button('material/image/about.png', 'material/image/about_down.png', ai_settings.screen, (200, 380)) def show(self, ai_settings, stats): self.button_feiji.show() # self.button_rank.show() self.button_setting.show() self.button_about.show() ai_settings.screen.blit( pygame.image.load('material/image/openning.png'), (130, 100)) # event for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() elif event.type == pygame.MOUSEBUTTONDOWN and self.button_feiji.isCover( ): stats.function = 1 # elif event.type == pygame.MOUSEBUTTONDOWN and self.button_rank.isCover(): # stats.function = 2 elif event.type == pygame.MOUSEBUTTONDOWN and self.button_setting.isCover( ): stats.function = 3 elif event.type == pygame.MOUSEBUTTONDOWN and self.button_about.isCover( ): stats.function = 4
def run_game(): """初始化游戏并创建一个屏幕对象""" pygame.init() ai_settings=Settings() size=(ai_settings.screen_width,ai_settings.screen_height) #创建主屏幕 screen=pygame.display.set_mode(size) pygame.display.set_caption("Alien Ivasion") #创建play按钮 play_button=Button(ai_settings,screen,"Play") #创建一艘飞船 ship=Ship(ai_settings,screen) #创建一个用于存储子弹的编组 bullets=Group() #创建一个外星人编组 aliens=Group() #创建外星人群 gf.creat_fleet(ai_settings, screen, ship,aliens) #创建一个用于存储有系统及信息的实例,并创建记分牌 stats=GameStats(ai_settings) sb=Scoreboard(ai_settings,screen,stats) #开始游戏的主循环 while True: #监视键盘和鼠标事件 gf.check_events(ai_settings,screen,ship,sb, aliens,bullets,stats,play_button) if stats.game_active: #刷新飞船 ship.update() #刷新子弹 gf.update_bullets(ai_settings,screen, ship,aliens,bullets,stats,sb) #刷新外星人群 gf.update_aliens(ai_settings,ship, aliens,bullets,stats,screen,sb) #刷新屏幕 gf.update_screen(ai_settings,screen,ship,sb, bullets,aliens,stats,play_button)
def f3(ai_settings, ui_back, stats, v): # button button_add = Button('material/image/plus.png', 'material/image/plus_down.png', ai_settings.screen, (200,290)) button_min = Button('material/image/minus.png', 'material/image/minus_down.png', ai_settings.screen, (260,290)) button_mute = Button('material/image/off.png', 'material/image/off_down.png', ai_settings.screen, (220,350)) button_nml = Button('material/image/on.png', 'material/image/on_down.png', ai_settings.screen, (220,350)) while 1: pygame.display.update() ai_settings.screen.blit(ui_back, (0, 0)) ai_settings.screen.blit(pygame.image.load('material/image/setboard.png'), (100,200)) button_add.show() button_min.show() if stats.mute == False: button_nml.show() else: button_mute.show() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() continue elif event.type == pygame.MOUSEBUTTONDOWN and button_add.isCover(): add_volume(v) continue elif event.type == pygame.MOUSEBUTTONDOWN and button_min.isCover(): min_volume(v) continue elif event.type == pygame.MOUSEBUTTONDOWN and button_nml.isCover() and stats.mute == False: mute() stats.mute = True continue elif event.type == pygame.MOUSEBUTTONDOWN and button_mute.isCover() and stats.mute == False: mute() stats.mute = True continue elif event.type == pygame.MOUSEBUTTONDOWN and button_mute.isCover() and stats.mute == True: normal() stats.mute = False continue elif event.type == pygame.MOUSEBUTTONDOWN and button_nml.isCover() and stats.mute == True: normal() stats.mute = False continue elif event.type == pygame.MOUSEBUTTONDOWN: stats.function = 0 if stats.function == 0: break
def f1(ai_settings, ui_back, stats): button_nml = Button('material/image/normal.png', 'material/image/normal_down.png', ai_settings.screen, (125,80)) button_inf = Button('material/image/infinite.png', 'material/image/infinite_down.png', ai_settings.screen, (270,80)) route = 'material/image/mode_choose.png' while 1: pygame.display.update() ai_settings.screen.blit(ui_back, (0, 0)) ai_settings.screen.blit(pygame.image.load(route), (50,50)) button_nml.show() button_inf.show() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() elif event.type == pygame.MOUSEBUTTONDOWN and button_nml.isCover(): stats.mode = 1 break elif event.type == pygame.MOUSEBUTTONDOWN and button_inf.isCover(): stats.mode = 2 break elif button_nml.isCover(): route = 'material/image/normal_mode.png' continue elif button_inf.isCover(): route = 'material/image/infinite_mode.png' continue elif event.type == pygame.MOUSEBUTTONDOWN: stats.function = 0 if stats.function == 0 or stats.mode != 0: break
class Surface: GRID_MARGIN = 25 SCREEN_SIZE = (500, 315) WINDOW_TITLE = "Détecteur de caractère - LesDominics" def __init__(self, characterReader): self.mouseDown = False self.characterReader = characterReader pygame.font.init() # Icon icon = pygame.Surface((1, 1)) icon.set_alpha(0) pygame.display.set_icon(icon) pygame.display.set_caption(self.WINDOW_TITLE) self.surface = pygame.display.set_mode(self.SCREEN_SIZE) # Elements self.grid = Grid(self.surface, (self.GRID_MARGIN, self.GRID_MARGIN)) self.resetButton = Button(self.surface, (25, 275), "Reset") self.char_list = CharList(self.surface, (300, self.GRID_MARGIN)) def getInput(self): for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: self.mouseDown = True self.mouseClick() if event.type == pygame.MOUSEBUTTONUP: self.mouseDown = False if event.type == pygame.MOUSEMOTION and self.mouseDown: self.mouseDragged() if event.type == pygame.KEYDOWN: if event.key == K_TAB: self.grid.resize() if (event.key >= K_0 and event.key <= K_9) or (event.key >= K_a and event.key <= K_z): keyPressed = chr(event.key) keyPressed = keyPressed.upper() self.grid.saveState(keyPressed) #self.grid.loadAverage(keyPressed) #pygame.image.save(self.surface, "./images/after/" + keyPressed + ".png") def mouseClick(self): self.mouseDragged() if self.resetButton.isClicked(): self.grid.reset() self.char_list.clean() def mouseDragged(self): if self.grid.mouseDragged(): state = self.grid.getState() self.grid.resize() resizedState = self.grid.getState() self.grid.setState(state) if self.characterReader.readFailed(resizedState): self.char_list.get_result(None) else: results = self.characterReader.read(resizedState) self.char_list.get_result(results) def display(self): while True: self.surface.fill((255, 255, 255)) self.grid.draw() self.resetButton.draw() self.char_list.draw() pygame.display.flip() self.getInput()
def replace(self, new_command: button.Button): """ Replace current command with `new_command`. """ for old_command in self._command: old_command.passive() self._command.add(new_command) new_command.active()