def __init__(self): """初始化游戏""" # 初始化pygame库 pygame.init() # 创建窗口 self._create_window() # 设置窗口 self._set_window() # 播放背景音乐 self._play_bg_music() # 创建一架我方飞机 self.my_plane = MyPlane(self.window) # 创建一个可视化炸弹组 self.visual_bomb_group = VisualBombGroup(self.window) # 创建一个得分板 self.score_board = ScoreBoard(self.window) # 创建一个暂停按钮 self.pause_button = PauseButton(self.window) # 创建管理精灵的分组 self._create_groups() # 创建一个用于跟踪时间的时钟对象 self.clock = pygame.time.Clock() # 设置定时器 self._set_timers() # 标记游戏没有结束 self.is_gameover = False # 获得字体对象 self._get_fonts() # 双发子弹的计数器 self.double_bullet_counter = 0 # 标记还没有更新关数2的速度 self.is_update_level2_speed = False # 标记还没有更新关数3的速度 self.is_update_level3_speed = False # 标记还没有更新关数4的速度 self.is_update_level4_speed = False # 标记还没有更新关数5的速度 self.is_update_level5_speed = False
def initUI(self): '''initiates application UI''' self.board = Board(self) self.setCentralWidget(self.board) self.scoreBoard = ScoreBoard() self.addDockWidget(Qt.RightDockWidgetArea, self.scoreBoard) self.scoreBoard.make_connection(self.board) self.resize(800, 800) self.center() self.setWindowTitle('Go') self.show()
def create_score(): dim = 0 sb = ScoreBoard() while True: risp=int(input("Desidera creare uno Scoreboard di una opportuna dimensione?(1-yes or 2-no):\n")) if risp == 1: dim = input("Inserisca la dimensione che desidera impostare:\n") sb = ScoreBoard(int(dim)) break elif risp == 2: print("ScoreBoard creato di dimensione 10") break else: print("Scelta errata, ripeta") return sb
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # 添加背景图,创建一艘飞船、一个子弹编组和一群外星人 bgimage = bg_image_load() bg_position = [0, 0] ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # 创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) # 创建用于存储游戏统计信息的实例,并创建记分牌 stats = GameStats(ai_settings) sb = ScoreBoard(ai_settings, screen, stats) # 创建Play按钮 play_button = Button(ai_settings, screen, "Play") # 开始游戏的主循环 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, screen, stats, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, bgimage, bg_position, play_button)
def run_game(): """main feature""" pygame.init() stn = Settings() screen=pygame.display.set_mode((stn.width,stn.height)) pygame.display.set_caption('Alien Invasion') bg_color = stn.bg_color ship = Ship(screen, stn) #create group for keeping bullets bullets = Group() #create group for keeping aliens aliens = Group() gf.create_fleet(stn, screen, aliens, ship) #create stats stats = GameStats(stn) #create button button = Button(screen, stn, 'Play') #create scoreboard sb = ScoreBoard(screen, stn, stats) while True: gf.check_events(ship, stn, screen, bullets, stats, button, aliens, sb) if stats.game_active: ship.update() gf.update_bullets(bullets, aliens, stn, screen, ship, stats, sb) gf.update_aliens(stn, aliens, ship, stats, screen, bullets, sb) gf.update_screen(stn, screen, ship, bullets, aliens, stats, button, sb)
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Alien Invasion') stats = GameStats(ai_settings) sb = ScoreBoard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) play_button = Button(ai_settings, screen, 'PLAY') while True: gf.check_events(ai_settings, screen, ship, bullets, stats, play_button, aliens, sb) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets, stats, sb) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets, sb) gf.update_screen(ai_settings, screen, ship, aliens, bullets, stats, play_button, sb)
def run_game(): pygame.init() pygame.mouse.set_visible(False) ai_settings = Settings() res = GameResources() # screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) screen = pygame.display.set_mode((1366, 768), pygame.FULLSCREEN, 32) pygame.display.set_caption("外星人入侵") play_button = Button(ai_settings, screen, "Play") stats = GameStats(ai_settings) score_board = ScoreBoard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, aliens) res.game_sound.play(-1) while True: gf.check_event(ai_settings, screen, stats, ship, bullets, aliens, play_button, res) if stats.game_active: ship.update() gf.update_aliens(ai_settings, screen, stats, ship, bullets, aliens, score_board, res) gf.update_bullets(ai_settings, screen, stats, bullets, aliens, score_board, res) gf.update_screen(ai_settings, screen, stats, ship, bullets, aliens, play_button, score_board)
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("AI INVASION") ship = Ship(screen, ai_settings) bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, aliens, ship) stats = GameStats(ai_settings) # 创建Play按钮 play_button = Button(ai_settings, screen, "Play") # 创建计分牌 sb = ScoreBoard(screen, ai_settings, stats) while True: gf.check_events(ai_settings, screen, ship, aliens, bullets, stats, play_button, sb) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets, stats, sb) gf.update_aliens(ai_settings, stats, bullets, screen, ship, aliens, sb) gf.update_screen(ai_settings, screen, ship, bullets, aliens, stats, play_button, sb)
def __init__(self, hub): """ Initializing default values """ self.hub = hub self.screen = self.hub.main_screen # Initialize player ship: This will be remove as we will loading in levels soon self.player_ship = Ship(self.hub) # Create a group for bullets self.bullets = Group() # Create a group for enemies self.enemies = Group() # Create a group for player destruction self.destructions = Group() # Create a group for enemy destruction self.enemy_destructions = Group() # Create a group for random ufos self.ufos = Group() # Create a group for walls self.walls = Group() # Wall is only initialize onced during the entire game. self.create_walls() # Create enemy bullets when spawned self.enemy_bullets = Group() # Get a test enemy to get size and width self.enemy = Enemy(self.hub) self.available_space_x = self.hub.WINDOW_WIDTH - 2 * self.enemy.rect.width self.number_enemies_x = int(self.available_space_x / (2 * self.enemy.rect.width)) self.number_of_rows = 3 # Create score board self.sb = ScoreBoard(self.hub) # Background Image self.bg_image = pygame.image.load('imgs/Backgrounds/black.png') self.bg_image = pygame.transform.scale(self.bg_image, (self.hub.WINDOW_WIDTH, self.hub.WINDOW_HEIGHT)) self.bg_rect = self.bg_image.get_rect()
def run_game(): # Pygame window initialization pygame.init() game_settings = Settings() screen = pygame.display.set_mode( (game_settings.screen_width, game_settings.screen_height)) pygame.display.set_caption("Alien Invasion") play_button = Button(game_settings, screen, "Play") quit_button = Button(game_settings, screen, "Quit") # Objects initialization ship = Ship(game_settings, screen) bullets = Group() aliens = Group() player_score = ScoreBoard(game_settings, screen) stats = GameStats(game_settings, screen, ship, aliens, bullets, player_score) # The main game process while True: gf.check_events(game_settings, stats, play_button, quit_button, ship, aliens, bullets, player_score) if stats.game_active: pygame.mouse.set_visible(False) if stats.paused: pygame.mouse.set_visible(True) else: ship.update() if aliens: ship.bullet_firing(game_settings, bullets) bullets.update() gf.enemy_neutralized(game_settings, stats, ship, aliens, bullets, player_score) if aliens: gf.check_fleet_edge(game_settings, screen, aliens) aliens.update() stats.check_aliens_win(game_settings, ship, aliens) gf.update_screen(game_settings, screen, stats, ship, bullets, aliens, player_score) if (not aliens) and (not bullets): stats.speed_increase(game_settings, ship, aliens) time.sleep(0.5) else: pygame.mouse.set_visible(True) gf.display_title(game_settings, screen, play_button, quit_button) # If any alien succeeds in invasion, game over if not stats.undefeated: stats.game_over(game_settings) pygame.display.flip() while stats.game_active: gf.check_events(game_settings, stats, play_button, quit_button, ship, aliens, bullets, player_score) stats.reset_stats(game_settings, ship, aliens, bullets, player_score)
def __init__(self): """Initialize the manage game""" pygame.init() # instance of settings self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) self.bg_color = self.settings.background_color pygame.display.set_caption("Shooting Chicken") self.starts = GameStart(self) self.score_board = ScoreBoard(self) self.ship = Ship(self) self.bullets = pygame.sprite.Group() self.chickens = pygame.sprite.Group() self.file_save = 'score.txt' self._create_fleet() self.HIGH_SCORE = [] self.play_button = Button(self, "Play")
def aline_invasion_game_handle(): #创建默认设置对象(获取默认设置参数) settings = Setting() screen_size = settings.get_screen_size() #初始化pygame模块、创建一个屏幕对象、设置标题栏标题 pygame.init() screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("疯狂外星人") #初始化游戏状态 status = GameStats(settings) game.game_init_deal(status, settings) new_ship = game.create_new_ship(screen, settings) #创建PLAY按钮 button_attr = settings.button button_attr["text_msg"] = "PLAY" play_bubtton = Button(screen, button_attr) #STOP提示栏 button_attr["text_msg"] = "STOP" stop_bubtton = Button(screen, button_attr) #计分板 score = ScoreBoard(screen, settings, status) #游戏主循环 while True: #屏幕背景色颜色绘制 screen_attr = settings.screen.copy() surface_screen_color_draw(screen, screen_attr["color"]) #处理pygame系统事件 game.event_traverl_deal(settings, \ screen, new_ship, play_bubtton, status) #暂停(提示,按钮不可按) if status.game_stop: stop_bubtton.__draw__() score_board_draw(score) pygame.display.flip() continue if not status.game_over: #处理外星人、子弹、飞船在屏幕中的位置以及相关逻辑 game.update_game_status(screen, settings, new_ship, status) #游戏结束则绘制按钮并处理一些统计信息等数据 game.game_over_check(status, new_ship, play_bubtton, settings) #绘制最新屏幕(刷新) score_board_draw(score) pygame.display.flip()
def run_game(): # initialize game and create a screen object. # initialize game pygame.init() # create game display with 1200 pixels wide and 800 pixels in height ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # create an instance to store game stats filename = 'high_score.txt' stats = GameStats(ai_settings, filename) ship = Ship(screen, ai_settings) bullets = Group() aliens = Group() rewards = Group() missiles = Group() shields = Group() # create a play button msg1 = 'Press "P" to Play' msg2 = 'Press "Q" to Quit' play_button = Button(screen, ai_settings, msg1, msg2) score_board = ScoreBoard(screen, ai_settings, stats) # create an alient fleet gf.create_alien_fleet(screen, ai_settings, aliens, ship, stats) # The main loop of the game while True: gf.check_events(ai_settings, screen, ship, bullets, play_button, stats, aliens, score_board, filename, rewards, missiles) if stats.game_active: ship.update() gf.fire_bullet(ai_settings, screen, ship, bullets) gf.update_bullets(screen, ai_settings, aliens, ship, bullets, stats, score_board, rewards, missiles) gf.update_aliens(stats, aliens, bullets, ship, screen, ai_settings, score_board, rewards, missiles, shields) gf.update_rewards(ship, rewards, ai_settings, stats, score_board, shields, screen) gf.update_missiles(stats, aliens, bullets, ship, screen, ai_settings, score_board, rewards, missiles, shields) gf.update_shields(shields, missiles, ai_settings, aliens) gf.update_screen(ai_settings, screen, ship, bullets, aliens, play_button, stats, score_board, rewards, missiles, shields)
def run_game(): # initialize game and create a screen object. # initialize game pygame.init() # create game display ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Flying Piggy") # create an instance to store game stats filename = 'high_round.txt' stats = GameStats(ai_settings, filename) piggy = Piggy(screen, ai_settings) bullets = Group() rocks = Group() rock_stats = RockStats() rewards = Group() shields = Group() score_board = ScoreBoard(screen, ai_settings, stats) gf.create_initial_rocks(screen, ai_settings, rock_stats, rocks) # The main loop of the game while True: gf.check_events(stats, piggy, rocks, bullets, screen, ai_settings, rock_stats, shields, rewards, score_board, filename) if stats.game_active: piggy.update() gf.fire_bullet(ai_settings, screen, piggy, bullets) gf.update_bullets(screen, ai_settings, rocks, bullets, rewards, stats, score_board) gf.update_rocks(screen, ai_settings, rock_stats, rocks, piggy, stats, score_board) gf.update_rewards(shields, screen, ai_settings, piggy, rewards, score_board) gf.update_shields(shields, ai_settings, rocks) gf.check_round(stats, score_board, ai_settings) # create a play button if stats.piggy_hit: msg1 = "Game Over" else: msg1 = 'Round ' + str(stats.round) msg2 = 'Press "P" to Play' play_button = Button(screen, ai_settings, msg1, msg2) gf.update_screen(ai_settings, screen, piggy, bullets, stats, play_button, rocks, rewards, shields, score_board)
def run_game(): #初始化游戏并创建一个屏幕对象 pygame.init(); ai_setting = Setting(); screen = pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height)); pygame.display.set_caption("alien_invasion"); #创建play按钮 play_button = Button(ai_setting,screen,"Play"); #创建一个用于存储游戏统计信息的实例 status = GameStatus(ai_setting); #创建计分实例 sb = ScoreBoard(ai_setting,screen,status); #创建一艘飞船 ship = Ship(ai_setting,screen); #创建一个外星人 #alien = Alien(ai_setting,screen); #创建一个用于存放子弹的组 bullets = Group(); #设置背景色 #bg_color = (230,230,230); #创建一个用于存放外星人的组 aliens = Group(); #创建外星人群 gf.create_fleet(ai_setting,screen,ship,aliens); #开始游戏主循环 while True: #监视键盘和鼠标事件 gf.check_events(ai_setting,screen,status,sb,play_button,ship,aliens,bullets); #还有飞船的时候更新 if status.game_active: #更新飞船位置 ship.update(); #更新外星人位置 gf.update_aliens(ai_setting,status,sb,screen,ship,aliens,bullets); #更新子弹信息 gf.update_bullets(ai_setting,screen,status,sb,ship,aliens,bullets); #刷新屏幕 gf.update_screen(ai_setting,screen,status,sb,ship,aliens,bullets,play_button);
def run_game(): """游戏主进程""" # 创建游戏设置类 game_settings = Settings() # 初始化pygame并创建屏幕 pygame.init() screen = pygame.display.set_mode( (game_settings.screen_width, game_settings.screen_height)) pygame.display.set_caption("Tetris (Roy)") # 创建pygame时钟控制 fps_clock = pygame.time.Clock() # 创建新落下的方块 fall_cubics = FallCubic(screen, game_settings) # 创建检测碰撞用的临时方块实例组 temp_cubics = Group() # 创建被固定的dead方块 dead_cubics = DeadCubic(screen, game_settings) # 创建消除时显示的黑行组 black_lines = BlackLines() # 创建得分板 score_board = ScoreBoard(screen, game_settings) # 创建多线程 thread_lock = threading.Lock() # 初始化游戏 game_settings.game_over = True game_settings.game_wait = True ss.game_start(screen, game_settings, fall_cubics, dead_cubics, temp_cubics, thread_lock, black_lines, score_board) while True: # 限制运行速度 fps_clock.tick(game_settings.FPS) # game_over后重启程序 ss.game_start(screen, game_settings, fall_cubics, dead_cubics, temp_cubics, thread_lock, black_lines, \ score_board) # 事件检测 gf.check_events(screen, game_settings, fall_cubics, dead_cubics, temp_cubics, black_lines, score_board, \ thread_lock) # 更新所有屏幕元素 gf.update_screen(screen, game_settings, fall_cubics, dead_cubics, black_lines, score_board)
def __init__(self, hub): """ Initializing default values """ self.game_hub = hub # Initialize player ship: This will be remove as we will loading in levels soon self.player_ship = Ship(self.game_hub) # Create a group for bullets self.bullets = Group() # Create a group for enemies self.enemies = Group() self.enemy = Enemy(self.game_hub) self.available_space_x = self.game_hub.WINDOW_WIDTH - 2 * self.enemy.rect.width self.number_enemies_x = int(self.available_space_x / (2 * self.enemy.rect.width)) self.number_of_rows = 3 # Create play button self.play_button = Button(self.game_hub, "Play") # Create score board self.sb = ScoreBoard(self.game_hub)
def __init__(self, **kwargs): kwargs["cols"] = 1 print("FindPairs cols=", kwargs["cols"]) super(FindPairs, self).__init__(**kwargs) self.score_board = ScoreBoard(200, self.login_button_handler, self.join_game_handler,\ size_hint=(1, None), height="30sp") self.add_widget(self.score_board) self.board = Board(self.card_click_handler, self.cb_after_faceup_delay) self.add_widget(self.board.get_widget()) self.controller = Controller(self.board, self.score_board) self.size_text_frame = GridLayout(cols=1, size_hint=(1, None), height="30sp") self.size_text = Label(text="Number of pairs") self.size_text_frame.add_widget(self.size_text) self.size_text_frame.bind( size=lambda *args: self.set_bg(self.size_text_frame)) self.levels = [] self.current_level = 0 self.num_of_pairs_frame = GridLayout(cols=7, size_hint=(1, None), height="50sp") self.num_of_pairs_frame.bind( size=lambda *args: self.set_bg(self.num_of_pairs_frame)) for num_pairs in [5, 10, 15, 20, 25, 30]: self.levels.append( LevelButton(num_pairs, self.handle_click_num_pairs)) self.num_of_pairs_frame.add_widget(self.levels[-1]) self.num_of_pairs_frame.add_widget(Button(text="Next Level",\ on_press=self.handle_click_next_level)) self.add_widget(self.size_text_frame) self.add_widget(self.num_of_pairs_frame)
def run_game(): pygame.init() setting = Setting() game_stats = GameStats(setting) screen = pygame.display.set_mode(setting.size) # 窗口 pygame.display.set_caption("ALIEN INVASION") # 标题 ship = Ship(screen, setting, game_stats) bullets = Group() aliens = Group() play_button = Button(setting, screen, "PLAY") socre_board = ScoreBoard(setting, screen, game_stats) # 游戏主循环 while True: check_event(setting, screen, ship, bullets, aliens, game_stats, play_button, socre_board) update_screen(screen, setting, ship, bullets, aliens, game_stats, play_button, socre_board)
def insert_score(sb): risp = 1 while risp == 1: score = float(input("Inserisca lo score totalizzato:\n")) name = input("Inserisca il nome del player che ha totalizzato lo score:\n") print("Inserisca la data in cui è stato totalizzato lo score") Y = int(input("Anno:\n")) M = int(input("Mese:\n")) D = int(input("Giorno:\n")) sb.insert(ScoreBoard.Score(name, score, date(Y, M, D))) while True: risp = int(input("Vuole inserire un altro score?(1-yes or 2-no):\n")) if risp == 2 or risp == 1: break else: print("Scelta errata, ripeta") return sb
def run_game(): #初始化pygame、设置和屏幕对象 pygame.init() #初始化背景设置 ai_settings = Settings() #创建窗口设置的实例,下方再调用它 screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) #显示窗口,名为screen pygame.display.set_caption("Alien Invasion") play_button = Button(ai_settings, screen, "play") stats = GameStats(ai_settings) #创建一个统计游戏信息的stats实例 sb = ScoreBoard(ai_settings, screen, stats) #创建记分牌实例 #创建一艘飞船 ship = Ship(ai_settings, screen) #提供screen实参,表示将在screen上绘制飞船 #创建一个用于存储子弹的编组 bullets = Group() #创建一个外星人空的编组 aliens = Group() #创建外星人群 gf.creat_fleet(ai_settings, stats, screen, ship, aliens) #开始游戏的主循环 while True: #开始监视鼠标和键盘事件 gf.check_event(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: #调用飞船移动位置坐标更新的方法 ship.update() bullets.update() #删除已消失的子弹 gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) #更新外星人的坐标位置 gf.update_alien(ai_settings, stats, screen, sb, ship, aliens, bullets) #更新屏幕,包括screen和ship gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def run_game(): # initialize game and create a screen object. # initialize game pygame.init() # create game display ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Tower Builder") # create an instance to store game stats filename_block = 'max_block.txt' filename_score = 'high_score.txt' stats = GameStats(ai_settings, filename_block, filename_score) # the blocks already built and the newly appeared blocks need two different groups built_blocks = Group() new_blocks = Group() messages = Group() # blocks specifically for score_board use sc_blocks = Group() score_board = ScoreBoard(screen, ai_settings, stats, sc_blocks) # The main loop of the game while True: gf.check_events(stats, ai_settings, new_blocks, built_blocks, screen, filename_block, filename_score, score_board) if stats.game_active: gf.update_block(new_blocks, built_blocks, screen, ai_settings, stats, score_board, messages) # create a play button if stats.falls_left == 0: msg1 = "Game Over" else: msg1 = 'Press "Q" to Quit' msg2 = 'Press "P" to Play' play_button = Button(screen, ai_settings, msg1, msg2) gf.update_screen(ai_settings, screen, new_blocks, built_blocks, stats, play_button, score_board, messages)
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() # 初始化设置对象 ai_settings = Setting() # 设置一个长为1200,宽为800的窗口 # pygame.display.set_mode.() 参数为元祖 screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # 创建一个Play按钮 play_button = Button(ai_settings, screen, "Play") # 创建一艘飞船 ship = Ship(ai_settings, screen) # 创建一个用于存储子弹的编组 bullets = Group() # 创建一个外星人 # alien = Alien(ai_settings, screen) # 创建一群外星人 aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) # 创建用于存储游戏统计信息的实例 status = GameStats(ai_settings) # 创建存储游戏统计信息的实例,并创建记分牌 sb = ScoreBoard(ai_settings, screen, status) # 开始游戏的主循环 while True: # 检查退出 gf.check_events(ai_settings, screen, sb, status, play_button, ship, aliens, bullets) if status.game_active: ship.update() gf.update_bullets(ai_settings, screen, status, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, status, sb, screen, ship, aliens, bullets) # 刷新屏幕 gf.update_screen(ai_settings, status, sb, screen, ship, aliens, bullets, play_button)
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Alien Invasion') ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # gf.create_fleet(ai_settings, screen, ship, aliens) stats = GameStats(ai_settings) play_button = Button(ai_settings, screen, 'Play') sb = ScoreBoard(ai_settings, screen, stats) gf.init_game(ai_settings, stats, sb) while True: t0 = time() gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(bullets) gf.update_aliens(ai_settings, aliens) # 子弹碰撞外星人 gf.check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets) # 外星人碰撞飞船 gf.check_ship_hit(ai_settings, stats, sb, screen, ship, bullets, aliens) gf.update_screen(ai_settings, screen, stats, ship, bullets, aliens, play_button, sb) t = time() - t0 if t < 0.01: sleep(0.01 - t) else: print(t)
def run_game(): #初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width, \ ai_settings.screen_height)) #(850,600)是一个元组实参 pygame.display.set_caption("Alien Invasion") #bg_color = (230, 230, 230) play_button = Button(ai_settings, screen, "Play") #创建一个用于存储统计信息的实例,并创建积分牌 stats = GameStats(ai_settings) sb = ScoreBoard(ai_settings, screen, stats) #创建飞船、一个用于存储子弹、外星人的编组 ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() #创建一个外星人群 gf.creat_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, sb, screen, ship, aliens, bullets) #每次循环重绘屏幕 gf.update_screen(ai_settings, screen, stats, sb, \ ship, aliens, bullets, play_button)
def run_game(): '''主函数,包含初始化屏幕,刷新屏幕的循环功能''' pygame.init()#初始化 setting=Setting()#创建一个Setting实例,储存在setting变量中 screen=pygame.display.set_mode((setting.width,setting.high))#创建一个屏幕,放在变量screen中,屏幕的宽为setting变量的width属性,高为high属性 pygame.display.set_caption("game1")#命名屏幕的名字为“game1” button=Button(screen,'Start')#创建一个Button(开始按钮)实例放在button变量中 ship=Ship(screen,setting)#创建一个Ship(玩家飞船)实例放在ship变量中 bullets=Group()#创建一个Group(子弹)实例,放在bullets中 enemies=Group()#创建一个Group(敌人)实例,放在enemies中 stats=GameStats(setting)#创建一个GameStats(游戏记录)实例,放在stats中 f.create_enemies(setting, screen, enemies)#调用f模块中的create_enemies函数,创建一群敌人 sb=ScoreBoard(setting,screen,stats)#创建一个ScoreBoard(计分板)实例放在sb变量中 while True: '''游戏主循环''' f.check_events(ship,setting,screen,bullets,stats,button,enemies,sb)#调用f模块中的check_event(检验事件)函数 if stats.game_active: '''判断游戏是否为活跃状态,然后执行以下程序''' f.update_ship(ship,enemies,bullets,setting,screen,stats,sb)#调用f模块中的update_ship(更新飞船)函数 f.update_bulllets(bullets,enemies,setting,screen,stats,sb)#调用f模块中的update_bullets(更新子弹)函数 f.update_enemies(enemies,stats,bullets,ship,setting,screen,sb)#调用f模块中的update_enemies(更新敌人)函数 f.delete_bullet(bullets)##调用f模块中的delete_bullet(删除子弹)函数 f.update_screen(screen,setting,ship,bullets,enemies,stats,button,sb)#调用f模块中的update_screen(更新屏幕)函数,不止在游戏活跃状态下更新屏幕
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() pygame.mixer.init() pygame.time.delay(1000) # 等待1秒让mixer完成初始化 pygame.mixer.music.load("../music/game_music.mp3") pygame.mixer.music.play() settings = Settings() screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) pygame.display.set_caption("狙击外星人") ship = Ship(screen, settings) alien = Alien(screen, settings) game_stats = GameStats(settings) play_button = Button(settings, screen, 'Play') # 创建一个用于存储子弹的编组 bullets = Group() # 创建一个用于存储外星人的编组 aliens = Group() gf.create_aliens(settings, screen, aliens, alien) # 创建一个记分牌 score_board = ScoreBoard(settings, screen, game_stats) # 从子弹列表中删除消失的子弹 # 开始游戏的主旋律 while 1: # 监视用户的键盘和鼠标时间 gf.check_events(ship, bullets, settings, screen, game_stats, play_button, aliens, alien, score_board) # 只有游戏处于激活状态(飞船最大销毁次数不等于0),才会更新各种游戏元素 if game_stats.game_active: ship.update() # 更新子弹列表(更新子弹位置和数量) gf.update_bullets(bullets, aliens, settings, screen, alien, game_stats, score_board) # 更新外星人列表(外星人的位置) gf.update_aliens(screen, settings, aliens, ship, game_stats, bullets, alien, score_board) # 更新屏幕的渲染 gf.update_screen(ship, bullets, settings, screen, aliens, game_stats, play_button, score_board)
def run_game(): pygame.init() ai_setting = Setting() screen = pygame.display.set_mode((ai_setting.width, ai_setting.height)) pygame.display.set_caption('Alien Invasion') # Creat a ship Bullets group and aliens fleet ship = Ship(screen, ai_setting) bullets = Group() aliens = Group() # Creat game state states = GameStates(ai_setting) # Creat a button instance play_button = Button(ai_setting, screen, 'Play') # Creat score board sb = ScoreBoard(ai_setting, screen, states) # Creat aliens fleet gf.creat_fleet(aliens, ship, screen, ai_setting) while True: gf.check_event(ship, bullets, aliens, screen, ai_setting, states, play_button, sb) if states.game_active: ship.update(ai_setting.width, ai_setting.height) gf.update_bullet(ai_setting, screen, states, sb, ship, bullets, aliens) gf.update_aliens(screen, ai_setting, states, sb, aliens, ship, bullets) gf.update_screen(screen, ai_setting, states, sb, ship, bullets, aliens, play_button) pygame.display.flip()
def run_game(): # 初始化游戏,并创建一个屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") ship = Ship(ai_settings, screen) stats = GameStats(ai_settings) play_button = Button(screen, ai_settings, 'PLAY') score_board = ScoreBoard(ai_settings, screen, stats) # a group for storing bullets bullets = Group() # a group for aliens aliens = Group() gf.create_fleet(ai_settings, screen, aliens) # alien = Alien(ai_settings, screen) while True: gf.check_events(ship, ai_settings, screen, bullets, play_button, stats, aliens) # create PLAY button if stats.game_active: ship.update() gf.update_aliens(ai_settings, screen, aliens, stats, bullets, ship) # aliens.update() gf.update_bullets(bullets, aliens, stats, ai_settings, score_board) gf.update_screen(ai_settings, screen, ship, bullets, aliens, play_button, stats, score_board)
class GameScreen: """ Game screen, were the game starts """ def __init__(self, hub): """ Initializing default values """ self.game_hub = hub # Initialize player ship: This will be remove as we will loading in levels soon self.player_ship = Ship(self.game_hub) # Create a group for bullets self.bullets = Group() # Create a group for enemies self.enemies = Group() self.enemy = Enemy(self.game_hub) self.available_space_x = self.game_hub.WINDOW_WIDTH - 2 * self.enemy.rect.width self.number_enemies_x = int(self.available_space_x / (2 * self.enemy.rect.width)) self.number_of_rows = 3 # Create play button self.play_button = Button(self.game_hub, "Play") # Create score board self.sb = ScoreBoard(self.game_hub) def run(self): self.run_event() if not self.game_hub.game_mode.game_active: self.play_button.draw() else: self.run_update() self.run_draw() def run_event(self): for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == K_a: self.game_hub.controller['left'] = True self.game_hub.controller['right'] = False if event.key == K_d: self.game_hub.controller['right'] = True self.game_hub.controller['left'] = False if event.key == K_SPACE: self.add_bullet() if event.key == K_t: self.create_fleet() if event.type == KEYUP: if event.key == K_a: self.game_hub.controller['left'] = False if event.key == K_d: self.game_hub.controller['right'] = False if event.type == MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() self.check_play_button(mouse_x, mouse_y) def run_update(self): self.player_ship.update() self.update_bullets() self.update_enemies() self.update_collision() def run_draw(self): self.player_ship.draw() self.sb.draw() self.draw_bullets() self.draw_enemies() def add_bullet(self): new_bullet = Bullet(self.game_hub, self.player_ship) self.bullets.add(new_bullet) self.game_hub.shoot_sound.play() def update_bullets(self): for bullet in self.bullets: bullet.update() if bullet.rect.top < 0: self.bullets.remove(bullet) def draw_bullets(self): for bullet in self.bullets: bullet.draw() def create_fleet(self): for enemy_number in range(self.number_enemies_x): for number_row in range(self.number_of_rows): # Create an alien and place it in the row new_enemy = Enemy(self.game_hub) new_enemy.rect.x = self.enemy.rect.width + 2 * self.enemy.rect.width * enemy_number new_enemy.rect.y = self.enemy.rect.height + 2 * self.enemy.rect.height * number_row self.enemies.add(new_enemy) def update_enemies(self): # Loop through all the enemies in the list enemy_moved_down = False # Update the enemy for enemy in self.enemies: # Update the enemy enemy.update() # Check if the enemy has hit the bottom of the screen. if enemy.rect.top > self.game_hub.WINDOW_HEIGHT: self.enemies.remove(enemy) print(len(self.enemies)) # Check if the enemy collided with the player if pygame.sprite.spritecollideany(self.player_ship, self.enemies): self.ship_hit() self.game_hub.enemy_dies_sound.play() def draw_enemies(self): for enemy in self.enemies: enemy.draw() def update_collision(self): collisions = pygame.sprite.groupcollide(self.bullets, self.enemies, True, True) # No more enemies on the screen, increase level if len(self.enemies) <= 0: self.bullets.empty() self.game_hub.game_mode.increase_speed() self.create_fleet() self.game_hub.game_mode.level += 1 self.sb.prep_level() if collisions: for enemies in collisions.values(): self.game_hub.game_mode.score += self.game_hub.game_mode.enemy_point_value * len( enemies) self.sb.prep_score() self.game_hub.enemy_dies_sound.play() self.check_high_score() def ship_hit(self): """ Respond to ship being hit by aliens""" # Degrement ships left self.game_hub.game_mode.player_lives -= 1 # Update scoreboard self.sb.prep_ships() # Empty the list of aliens and bullets self.enemies.empty() self.bullets.empty() # Check if there is any lives left if self.game_hub.game_mode.player_lives > 0: # Create new fleet self.create_fleet() if self.game_hub.game_mode.player_lives <= 0: self.game_hub.game_mode.game_active = False pygame.mouse.set_visible(True) # Pause sleep(0.5) def check_play_button(self, mouse_x, mouse_y): if self.play_button.rect.collidepoint(mouse_x, mouse_y): self.game_hub.game_mode.game_active = True self.game_hub.game_mode.reset_stats() self.sb.prep_score() self.sb.prep_highscore() self.sb.prep_level() self.sb.prep_ships() self.create_fleet() pygame.mouse.set_visible(False) def check_high_score(self): """ Check to see if there's a new high score. """ if self.game_hub.game_mode.score > self.game_hub.game_mode.high_score: self.game_hub.game_mode.high_score = self.game_hub.game_mode.score self.sb.prep_highscore()