def run_game(): pygame.init() ai_setting = Setting() # 导入设置 screen = pygame.display.set_mode( (ai_setting.screen_wide, ai_setting.screen_height)) pygame.display.set_caption("Alien Invasion") # 设置屏幕宽度、高度,标题 ship = Ship(ai_setting, screen) bullets = Group() bullets_a = Group() aliens = Group() gf.creat_fleet(ai_setting, screen, aliens, ship) stats = GameStats(ai_setting) sb = ScoreBoard(ai_setting, screen, stats) play_button = Button(ai_setting, screen, u"PLAY") while True: gf.check_event(ai_setting, sb, screen, stats, play_button, ship, aliens, bullets, bullets_a) if stats.game_active: ship.update() gf.update_bullets(ai_setting, screen, stats, sb, ship, aliens, bullets, bullets_a) gf.update_aliens(ai_setting, stats, sb, screen, ship, aliens, bullets, bullets_a) gf.update_screen(ai_setting, screen, stats, sb, ship, aliens, bullets, bullets_a, 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") play_button = Button(screen, 'Play') stats = GameStats(ai_settings) sb = Scoreboard(screen, ai_settings, stats) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() pills = Pill(ai_settings, screen) gf.creat_fleet(ai_settings, screen, aliens) while True: gf.check_events(stats, play_button, aliens, bullets, ship, ai_settings, screen, sb) if stats.game_active: ship.update() gf.update_bullets(bullets, aliens, ai_settings, screen, ship, stats, sb) gf.update_aliens(ai_settings, aliens, ship, bullets, stats, screen, sb, pills) if pills.apperence: gf.update_pills(pills, ship, ai_settings, screen) gf.update_screen(ai_settings,stats,sb,screen,ship,bullets, aliens,play_button, pills)
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(ai_setting.name) ship = Ship(ai_setting, screen) bullets = Group() aliens = Group() gf.creat_fleet(ai_setting, screen, aliens, ship) stats = GameStatus(ai_setting) play_button = Button(ai_setting, screen, "Play") sb = Scoreboard(ai_setting, stats, screen) while True: gf.check_events(ai_setting, stats, screen, aliens, ship, bullets, play_button, sb) if stats.game_active: ship.update() gf.update_buttlets(ai_setting, screen, ship, aliens, bullets, stats, sb) gf.update_alients(ai_setting, screen, ship, aliens, stats, bullets, sb) gf.update_screen(ai_setting, screen, ship, bullets, aliens, stats, play_button, sb)
def run_game(): # 初始化游戏并创建一个屏幕对象 # screen = pygame.display.set_mode((600, 400)) # bg_color = (230, 230, 230) 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) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # alien = Alien(ai_settings, screen) 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, screen, stats, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, 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 Invansion") #创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) #创建一艘飞船,一个子弹编组和一个外星人编组 ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() #创建外星人群 gf.creat_fleet(ai_settings, screen, ship, aliens) #创建play按钮 #play_button = Button(ai_settings, screen, "Play") #开始游戏主循环 while True: gf.check_events(ai_settings, screen, ship, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets)
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') #game window title ship = Ship(ai_settings, screen) #creat a ship bullets = Group() aliens = Group() stats = GameStats(ai_settings) play_button = Button(ai_settings, screen, "Play") sb = Scoreboard(ai_settings, screen, stats) #creat alien group game_functions.creat_fleet(ai_settings, screen, ship, aliens) game_functions.read_high_score(stats, sb) #游戏主循环 while True: #键盘和鼠标事件 game_functions.check_events(ai_settings, screen, stats, ship, aliens, bullets, sb, play_button) if stats.game_active: ship.update() game_functions.update_bullets(ai_settings, screen, stats, ship, aliens, bullets, sb) game_functions.update_aliens(ai_settings, stats, screen, ship, aliens, bullets, sb) game_functions.update_screen(ai_settings, screen, stats, ship, aliens, bullets, sb, 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("史大炮大战外星人") play_button = Button(ai_settings, screen, "Play") states = GameStates(ai_settings) sb = Scoreboard(ai_settings, screen, states) ship = Ship(ai_settings, screen) bullets = Group() alien = Alien(ai_settings, screen) aliens = Group() gf.creat_fleet(ai_settings, screen, ship, aliens) while 1: gf.check_events(ai_settings, screen, states, sb, play_button, ship, aliens, bullets) if states.game_active: ship.update() gf.update_bullets(ai_settings, screen, states, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, states, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, states, sb, ship, aliens, bullets, play_button)
def run_game(): #初始化屏幕并且创建一个屏幕对象 pygame.init() #初始化 ai_settings = Setting() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) #创建了一个屏幕对象screen设置屏幕大小 bg_color = (ai_settings.screen_color) #设置背景颜色 pygame.display.set_caption("Super_ship") #设置屏幕框显示的文字 ship = Ship(ai_settings, screen) #创建一艘飞船,不可以放在while循环中 ship_height = ship.rect.height bullets = Group() aliens = Group() gf.creat_fleet(ai_settings, screen, aliens, ship_height) stats = GameStates(ai_settings) ship_limit = stats.ai_settings.ship_limit #开始游戏的主循环 while True: #每经过一次循环就刷新一下屏幕 #监视键盘和鼠标事件 gf.check_events(ai_settings, screen, ship, bullets) ship.update() gf.update_aliens(ai_settings, aliens, ship, bullets, stats, screen, ship_height) gf.update_bullets(ai_settings, screen, ship, bullets, aliens, ship_height) gf.update_screen( ai_settings, screen, ship, bullets, aliens) #每次循环都会重新绘制屏幕 #必须先进行填充背景色#将飞船放置在底端中间#让最近绘制的屏幕可见
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 Invision") 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) # Start the main loop for the game. 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, play_button)
def run_game(): pygame.init() setting = Setting() stats = GameStats(setting) screen = pygame.display.set_mode((setting.width, setting.height)) pygame.display.set_caption("Alien Invasion") ship = Ship(screen, setting) bullets = Group() aliens = Group() # Alien(setting,screen) gf.creat_fleet(setting, screen, aliens, ship) playbutton = Button(screen, setting, 'play') board = Board(setting, screen, stats) while True: gf.check_events(aliens, playbutton, stats, setting, screen, ship, bullets) if stats.game_active: ship.update() gf.update_bullet(setting, screen, ship, bullets, aliens, board, stats) gf.update_aliens(setting, screen, ship, bullets, aliens, stats, board) #print(len(aliens))444 gf.update_window(setting, screen, ship, bullets, aliens, playbutton, stats, 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('Alien Invasion') play_button = Button(ai_settings, screen, 'Play') stats = GameStats(ai_settings) ship = Ship(screen, ai_settings) # 创建飞船实例 bullets = Group() # 精灵模块中的Group类,bullets实例用于编组所以的bullet实例,类似列表 aliens = Group() gf.creat_fleet(ai_settings, screen, ship, aliens) # ship用于计算各行,列能放多少外星人 while True: gf.check_events(ai_settings, screen, ship, aliens, bullets, stats, play_button) # 这里面创建bullet实例加入bullets编组,改ship的rect属性 if stats.game_active: ship.update() # 更新飞船位置 gf.update_bullets(ai_settings, screen, ship, bullets, aliens) # 更新子弹位置,并删除编组中到达边缘的子弹 # 删除碰撞的飞船子弹,外星人打完了重新创建一批,并且aliens,bullets,ship的速度属性都提升了 gf.update_aliens(ai_settings, screen, ship, aliens, bullets, stats) # 检查aliens边缘,更新aliens左右位置,检测外星人与飞船是否碰撞,到达底部,若是则子弹,外星人清空,重新创建aliens,减一条命 # 三条命用完了,就无法再创建aliens,游戏状态改成非活跃 gf.update_screen(ai_settings, screen, ship, bullets, aliens, stats, 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") #创建一个用于存储游戏统计的信息的实例 stats = GameStats(ai_settings) #设置背景颜色,代表红色,绿色,蓝色 bg_color = (230, 230, 230) #创建一艘飞船 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, ship, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, ship, aliens, bullets)
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") bg_color = (230, 230, 230) # 创建飞船 ship = Ship(ai_settings, screen) # 创建一个存储子弹的编组 bullets = Group() # 创建一个外星人 alien = Alien(ai_settings, screen) # 创建外星人编组 aliens = Group() # 创建外星人群 gf.creat_fleet(ai_settings, screen, ship, aliens) # 创建一个游戏信息存储示例 stats = GameStats(ai_settings) while True: gf.check_events(ai_settings, screen, ship, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, ship, aliens, bullets)
def run_game(): #initial game and make a screen target pygame.init() setting = Setting() screen = pygame.display.set_mode((setting.screen_width, setting.screen_height)) ship = Ship(setting, screen) bullets = Group() aliens = Group() gf.creat_fleet(setting, screen, aliens) pygame.display.set_caption(setting.caption) #begin main loop while True: #watch keyboard and mouse gf.check_events(ship, bullets, setting, screen) ship.update_position() bullets.update() aliens.update() #delete bullet outside of the screen for bullet in bullets.copy(): if bullet.rect.bottom < 0: bullets.remove(bullet) #move aliens gf.get_fleet_direction(aliens, setting) #kill aliens gf.kill(aliens, bullets) #refresh screen gf.update_screen(screen, setting, ship, bullets, aliens)
def run_game(): # 初始化pygame、设置和屏幕对象 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) # 创建飞船 ship = Ship(ai_settings, screen) # 创建一个用于存储子弹的编组 bullets = Group() # 创建外星人群 aliens = Group() # alien = Alien(ai_settings, screen) gf.creat_fleet(ai_settings, screen, ship, aliens) # 开始游戏的主循环 while True: # 监视键盘和鼠标事件 gf.check_events(ai_settings, screen, ship, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) # 更新屏幕 gf.update_screen(ai_settings, screen, ship, aliens, bullets)
def run_game(): # 初始化游戏背景设置; pygame.init() # 创建 screen 窗口,参数为元组; ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) pygame.display.set_caption("外星人入侵") # 创建play按钮; 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: # screen.fill(ai_settings.bg_color) 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,play_button,ship,aliens,bullets)
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) # 创建一个用于统计游戏信息的实例 ship = Ship(ai_settings, screen) bullets = Group() # 创建一个用于子弹的编组 aliens = Group() # 创建一个外星人编组 gf.creat_fleet(ai_settings, screen, aliens, ship) play_button = Button(ai_settings, screen, "PLAY") sb = ScoreBoard(ai_settings, screen, stats) # 开始程序的主循环 while True: # 监视键盘和鼠标事件 gf.check_event(ai_settings, screen, ship, bullets, stats, play_button, aliens, sb) if stats.game_avtive: ship.update(ai_settings, screen, ship, bullets) gf.update_bullets(bullets, aliens, ai_settings, screen, ship, stats, sb) gf.update_aliens(ai_settings, aliens, ship, stats, bullets, screen, sb) # 更新屏幕信息 gf.update_screen(ai_settings, screen, ship, aliens, bullets, play_button, stats, sb)
def run_game(): # 初始化游戏,并创建一个屏幕对象 pygame.init() setting = Settings() screen = pygame.display.set_mode( (setting.screen_width, setting.screen_hight)) # 设置游戏屏幕布局尺寸 pygame.display.set_caption("Alien Invasion's Game") # Title # 创建一艘飞船 ship = Ship(screen, setting) # 创建一个外星人 # alien = Alien(screen, setting) # 创建一个用于存储子弹的编组 bullets = Group() # 创建一个用于存储外星人的编组 aliens = Group() # 创建外星人群 gf.creat_fleet(setting, screen, ship, aliens) # bg_color = (230, 230, 230) # 游戏的主循环 while True: # 监视键盘和鼠标事件 - 关闭时,退出游戏 gf.check_events(setting, screen, ship, bullets) ship.update() # 飞船的位置将在检测到键盘事件后(但在更新屏幕前)更新。 gf.update_bullets(bullets) gf.update_screen(setting, screen, ship, bullets, aliens)
def run_game(): #初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Setting() 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() aliens = Group() stats = GameStats(ai_settings) #创建外星人群 gf.creat_fleet(ai_settings, screen, aliens, ship) #开始主循环 while True: #监视键盘和鼠标事件 gf.check_events(ai_settings, screen, stats, ship, play_button, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, aliens, ship, bullets) gf.update_aliens(ai_settings, screen, ship, aliens, bullets, stats) gf.update_screen(ai_settings, screen, ship, bullets, aliens, stats, 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.creat_fleet(ai_settings,screen,ship,aliens) #创建游戏统计信息实例 stats=GameStats(ai_settings) #创建paly按钮 play_button=Button(ai_settings,screen,"Play") #创建记分牌 sb=Scoreboard(ai_settings,screen,stats) #游戏主循环 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,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") 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, aliens, ship) #这里就是因为alien和ship位置与原函数的位置颠倒就导致出现“Group没有rect属性”的错误报告 while True: gf.check_events(ship, ai_settings, screen, stats, sb, play_button, aliens, bullets) if stats.game_active: ship.update() gf.updat_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, ship, aliens, bullets, play_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') #创建飞船 ship = Ship(ai_settings, screen) #创建子弹编组 bullets = Group() aliens = Group() #创建外星人 #alien = Alien(ai_settings, screen) gf.creat_fleet(ai_settings, screen, aliens) #游戏主循环 while True: #监视键盘鼠标事件 gf.check_events(ai_settings, screen, ship, bullets) #键盘事件监测 ship.update() gf.update_bullets(bullets) #每次循环重新绘制屏幕 gf.update_screen(ai_settings, screen, ship, aliens, bullets)
def run_game(): """" 运行游戏。 """ pygame.init() ai_setting = Settings() pygame.display.set_caption("Alien Invasion") screen =pygame.display.set_mode( (ai_setting.screen_width,ai_setting.screen_height)) #创建游戏物体 play_button = Button(ai_setting, screen, 'Play') stats = GameStats(ai_setting) sb = Scoreboard(ai_setting, screen, stats) ship = Ship(ai_setting, screen) alines = Group() bullets = Group() #临时变量 gf.creat_fleet(ai_setting, screen, alines) #游戏主循环 while True: gf.check_events(ai_setting, screen, alines, bullets, ship, stats, sb, play_button) if stats.game_active: ship.update() gf.update_bullet(ai_setting, screen, alines, bullets, stats, sb) gf.update_alines(ai_setting, stats, sb, screen, ship, alines, bullets) gf.update_screen(ai_setting, screen, stats, ship, alines, bullets, play_button, sb)
def run_game(): #初始化游戏并创建一个屏幕对象 pygame.init() # screen = pygame.display.set_mode((700, 520)) #双括号 参数必须是一个二项的序列,而不是整数 pygame.display.set_caption("Alien Invasion") game_set = Settings() #调用类,自动生成一个设置实例 screen = pygame.display.set_mode( (game_set.screen_width, game_set.screen_hight)) #生成屏幕对象 play_button = Button(screen, "Play") #创建按钮 ship = Ship(game_set, screen) #创建一艘飞船 bullets = Group() #创建一个用于存储子弹的编组Group实例 aliens = Group() #创建一个外星人编组 stats = GameStats(game_set) #创建一个用于存储游戏统计信息的实例 sb = Scoreboard(screen, game_set, stats) #创建记分牌 #创建外星人群 gf.creat_fleet(screen, game_set, ship, aliens) while True: gf.check_events(play_button, stats, aliens, bullets, screen, game_set, ship, sb) if stats.game_active: ship.update() # bullets.update() gf.update_bullets(bullets, aliens, screen, game_set, ship, stats, sb) #当对编组调用update()时,编组将自动对其中的每个sprite(bullet)调用update() gf.update_aliens(game_set, aliens, ship, stats, sb, bullets, screen) gf.update_screen(game_set, screen, ship, aliens, bullets, stats, play_button, sb)
def run_game(): pygame.init() #创建屏幕的尺寸 al_settings=Settings() screen=pygame.display.set_mode((al_settings.screen_width,al_settings.screen_height)) pygame.display.set_caption("Alien Invasion")#标题 play_button = Button(al_settings, screen, "Play") ship=Ship(al_settings,screen); #不可能是一个子弹吧 所以用编组Group 从精灵sprite引入 bullets = Group() #alien = Alien(al_settings,screen) aliens = Group() gf.creat_fleet(al_settings,screen,ship,aliens) stats = GameStats(al_settings) sb = Scoreboard(al_settings, screen, stats) #开始游戏的循环 while True: gf.check_events(al_settings, screen, stats, sb ,play_button, ship, aliens,bullets) if(stats.game_active): ship.update() gf.update_bullets(al_settings, screen, ship, aliens, bullets, stats, sb) gf.update_aliens(al_settings, screen, stats, sb, ship, aliens, bullets) #每次循环时填充颜色 gf.update_screen(al_settings,screen,stats, ship, aliens, bullets, play_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") #Ship实例化 ship=Ship(ai_settings,screen) #创建一个用于存储子弹的编组 bullets=Group() #创建一个外星人编组 aliens=Group() #创建外星人群 gf.creat_fleet(ai_settings,screen,aliens) #开始游戏的主循环 while True: #监视键盘和鼠标事件 gf.check_events(ai_settings,screen,ship,bullets)#这里为什么传入了4个参数? ship.update() gf.update_bullets(bullets) """更新屏幕上的图像,并切换到新屏幕""" gf.update_screen(ai_settings,screen,ship,aliens,bullets)
def run_game(): #初始化游戏并创建一个屏幕对象 pygame.init() setting = Settings() screen = pygame.display.set_mode( (setting.screen_width, setting.screen_height)) pygame.display.set_caption("Alien Invation") #创建一艘飞船的实例 ship = Ship(setting, screen) #创建一个用于存储子弹的编组的实例 bullets = Group() #创建一个外星人的编组的实例 aliens = Group() #创建外形人群 gf.creat_fleet(setting, screen, aliens, ship) #创建统计信息的实例 game_stats = Gamestats(setting) #创建一个按钮 play_button = Button(screen, "play", setting) #创建一个记分牌 scoreboard = Scoreboard(screen, aliens, game_stats) #开始游戏 的主循环 while True: #监视键盘和鼠标事件,不管是否游戏进行 gf.check_events(game_stats, play_button, aliens, bullets, ship, setting, screen) # game_stats, play_button, aliens, bullets, ship, setting, screen 如果是这样就对了 # setting, screen, ship, bullets, game_stats, play_button, aliens 如果是这样就会报错,如下: # if play_button.button_rect.collidepoint(mouse_x, mouse_y): # AttributeError: 'pygame.Surface' object has no attribute 'button_rect' #因此在python中位置参数要一一对应 #以下代码表示游戏进行中,相关位置的更新 if game_stats.game_active: #更新飞船位置 ship.update() #更新子弹位置,删除已经消失的子弹 gf.update_bullets(setting, screen, ship, aliens, bullets, game_stats, scoreboard) #更新外星人的位置 gf.update_aliens(setting, game_stats, aliens, ship, screen, bullets) #更新屏幕上的图画并切换到新屏幕,重新绘制屏幕,飞船,子弹和外星人,不管是否游戏进行 gf.update_screen(setting, screen, ship, bullets, aliens, play_button, game_stats, scoreboard)
def run_game(): #初始化pygame,设置和屏幕对象 pygame.init() ai_settings=Settings() screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") #创建piay按钮 play_button=Button(ai_settings,screen,"play") #创建一个用于存储游戏统计信息的实例 stats=GameStats(ai_settings) sb = Scoreboard(ai_settings,screen,stats) #设置背景色 #bg_color = (230,230,230) #创建一艘飞船 ship=Ship(ai_settings,screen) #创建一个用于存储子弹的数组 bullets=Group() #创建一个外星人编组 aliens=Group() gf.creat_fleet(ai_settings,screen,ship,aliens) #创建一个外星人 #alien=Alien(ai_settings,screen) # 开始游戏的主循环 while True: #监视键盘合鼠标事件 ''' for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit()''' gf.check_events(ai_settings, screen,stats,sb,play_button, ship,aliens, bullets) if stats.game_active: ship.update() ''' # 每次循环时都重新绘制屏幕 screen.fill(ai_settings.bg_color) ship.blitme() # 让最近绘制的屏幕可见 pygame.display.flip()''' 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,play_button)
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Settings() # screen = pygame.display.set_mode((1200,800)) # screen = pygame.display.set_mode(ai_settings.screen_width, # ai_settings.screen_height) # 报错 是因为长宽得再一个元组中 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() # aliens = Alien(ai_settings, screen) # 创建外星人 gf.creat_fleet(ai_settings, screen, aliens, ship) # 创建游戏状态 stats = GameStats(ai_settings) # 创建一个开始按钮 msg = 'Play' button = Button(ai_settings, screen, msg) # 开始游戏的主循环 while True: # 监视键盘和鼠标事件 gf.check_events(ai_settings, screen, stats, ship, aliens, bullets, button) if stats.game_active: # 更新飞船状态 ship.update() # 更新子弹状态 gf.update_bullets(ai_settings, screen, ship, bullets, aliens) # 更新外星人状态 gf.update_aliens(ai_settings, screen, stats, ship, bullets, aliens) # 更新屏幕 gf.up_screen(ai_settings, screen, stats, ship, bullets, aliens, 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") # 创建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) #设置背景色 bg_colar = (230, 230, 230) #开始游戏的主循环 #创建一个外星人 #alien=Ailen(ai_settings,screen) # 创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) # 创建存储游戏统计信息的实例,并创建计分牌 sb = Scoreboard(ai_settings, screen, stats) while True: # 监视键盘和鼠标事件 # gf.check_events(ship) gf.check_events(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, screen, stats, sb, ship, aliens, bullets) # gf.update_screen(ai_settings,screen,ship,alien,bullets) print(len(bullets)) gf.update_screen(ai_settings, screen, stats, sb, 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") #create the play button play_button = Button(ai_settings, screen, "Play") #创建飞船实例 stats = GameStats(ai_settings) #创建飞船 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, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) #print(len(bullets)) #更新屏幕 gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button)