class Game: def __init__(self): pygame.init() self.ai_settings = Settings() self.screen = pygame.display.set_mode( (self.ai_settings.screen_width, self.ai_settings.screen_height)) pygame.display.set_caption("Super Mario Bros Clone") self.mario_player = Mario(self.ai_settings, self.screen) self.map = Map(self.ai_settings, self.screen, mapfile='Levels/first_level.txt', blockfile='Images/block_tile.png') self.stats = GameStats() self.scoreboard = Scoreboard(self.ai_settings, self.screen, self.stats) self.clock = pygame.time.Clock() self.gameover = Gameover(self.ai_settings, self.screen, self.stats) def play(self): while True: while self.stats.lives > 0: self.mario_player = Mario(self.ai_settings, self.screen) self.map = Map(self.ai_settings, self.screen, mapfile='Levels/first_level.txt', blockfile='Images/block_tile.png') eloop = EventLoop(self.ai_settings, self.screen, self.mario_player, self.map, self.stats, self.scoreboard, finished=False) self.map.create_level() while not eloop.finished: eloop.update_events() eloop.check_input_events() eloop.update_screen() self.clock.tick(60) print(self.clock.get_fps()) self.stats.lives -= 1 self.stats.score = 0 self.stats.reset_stats() if self.stats.win: self.gameover.show_winover() self.stats.final_score = 0 self.stats.win = False else: self.gameover.show_gameover() pygame.time.wait(3000)
def __init__(self): pygame.init() self.ai_settings = Settings() self.screen = pygame.display.set_mode( (self.ai_settings.screen_width, self.ai_settings.screen_height)) pygame.display.set_caption("Super Mario Bros Clone") self.mario_player = Mario(self.ai_settings, self.screen) self.map = Map(self.ai_settings, self.screen, mapfile='Levels/first_level.txt', blockfile='Images/block_tile.png') self.stats = GameStats() self.scoreboard = Scoreboard(self.ai_settings, self.screen, self.stats) self.clock = pygame.time.Clock() self.gameover = Gameover(self.ai_settings, self.screen, self.stats)
def change_game_state(): gs = globals.game_state_label if gs == globals.MAIN_MENU: globals.game_state = Mainmenu() elif gs == globals.RUNNING: globals.game_state = Game() elif gs == globals.GAME_OVER: globals.game_state = Gameover()
def __init__(self): '''初始化游戏并创建游戏资源''' pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption('外星人入侵') # 设置游戏背景颜色 self.bg_color = self.settings.bg_color # 实例化飞船,并将主程序作为对象传入 self.ship = Ship(self) # 实例化子弹编组 self.bullets = pygame.sprite.Group() # 实例化外星人编组 self.aliens = pygame.sprite.Group() self._create_fleet() self.status = GameStatus(self) self.gameover = Gameover(self)
def __init__(self): super(HelloWorld, self).__init__() self.arena = Arena() self.add(self.arena) self.score = cocos.text.Label('30', font_name='Times New Roman', font_size=24, color=define.GOLD) self.score.position = 20, 440 self.add(self.score, 99999) self.gameover = Gameover() self.add(self.gameover, 100000)
def __init__(self): super(HelloWorld, self).__init__() self.arena = Arena() # 创建Arena对象 self.add(self.arena) # 添加Arena对象 self.score = cocos.text.Label('30', font_name='Times New Roman', font_size=24, color=define.GOLD) # 设置得分面板的格式 self.score.position = 20, 440 # 设置得分面板的位置 self.add(self.score, 99999) # 加入得分面板并设置Z轴位置为99999 self.gameover = Gameover() # 创建Gameover对象 self.add(self.gameover, 100000) # 加入Gameover对象
def step(self, char): self.machine.input(char) if (self.next < time()): self.machine.step() self.next = time() + (1.0 / self.FPS) self.machine.draw(self.driver) if (self.machine.nextState): state = self.machine.nextState if (state == "snake"): self.machine = Snake() elif (state == "gameover"): self.machine = Gameover() elif (state == "menu"): self.machine = Menu() elif (state == "quit"): self.machine = False
def end_game(self): self.stream.stop_stream() self.pause_scheduler() # self.unschedule(self.update) self.gameover = Gameover(self) self.add(self.gameover, 100000)
class AlienInvasion: '''管理游戏行为和资源的类''' def __init__(self): '''初始化游戏并创建游戏资源''' pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption('外星人入侵') # 设置游戏背景颜色 self.bg_color = self.settings.bg_color # 实例化飞船,并将主程序作为对象传入 self.ship = Ship(self) # 实例化子弹编组 self.bullets = pygame.sprite.Group() # 实例化外星人编组 self.aliens = pygame.sprite.Group() self._create_fleet() self.status = GameStatus(self) self.gameover = Gameover(self) def run_game(self): '''开始游戏主循环''' while True: self._check_events() self.ship.update() self._update_bullets() self._update_aliens() self._update_screen() def _check_events(self): # 监视键盘和鼠标事件 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) elif event.type == pygame.KEYUP: self._check_keyup_event(event) def _check_keydown_events(self, event): # 监听按下按键事件 if event.key == pygame.K_RIGHT: self.ship.move_right = True elif event.key == pygame.K_LEFT: self.ship.move_left = True elif event.key == pygame.K_q: sys.exit() elif event.key == pygame.K_SPACE: self._fire_bullet() def _check_keyup_event(self, event): # 监听松开按键事件 if event.key == pygame.K_RIGHT: self.ship.move_right = False elif event.key == pygame.K_LEFT: self.ship.move_left = False def _fire_bullet(self): '''创建一颗子弹并将其加入到编组中''' if len(self.bullets) < self.settings.bullet_allowed: new_bullet = Bullet(self) self.bullets.add(new_bullet) def _update_bullets(self): # 更新子弹 self.bullets.update() # 删除消失的子弹 for bullet in self.bullets.copy(): if bullet.rect.bottom <= 0: self.bullets.remove(bullet) self._check_bullet_alien_collisions() # print(len(self.bullets)) def _check_bullet_alien_collisions(self): # 射杀外星人 collosions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True) # 生成新的外星人 if not self.aliens: self.settings.alien_speed += 1 self.bullets.empty() self._create_fleet() def _create_fleet(self): '''创建外星人群''' # 一行可以容纳外星人数量 alien = Alien(self) alien_width, alien_height = alien.rect.size avaliable_space_x = self.settings.screen_width - (2 * alien_width) number_aliens_x = avaliable_space_x // (2 * alien_width) # 可以容纳的外星人行数 ship_height = self.ship.rect.height avaliable_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height) number_rows = avaliable_space_y // (2 * alien_height) # 创建多行外星人 for row_number in range(number_rows): # 创建一行外星人 for alien_number in range(number_aliens_x): self._create_alien(alien_number, row_number) def _check_fleet_edges(self): # 检查外星人是否撞到边缘 for alien in self.aliens.sprites(): if alien.check_edges(): self._change_fleet_direction() break def _change_fleet_direction(self): '''向下移动一格并改变外星人方向''' for alien in self.aliens.sprites(): alien.rect.y += self.settings.fleet_drop_speed self.settings.fleet_direction *= -1 def _create_alien(self, alien_number, row_number): alien = Alien(self) alien_width, alien_height = alien.rect.size alien.x = alien_width + 2 * alien_width * alien_number alien.rect.x = alien.x alien.rect.y = alien.rect.height + 2 * alien_height * row_number self.aliens.add(alien) def _update_aliens(self): self._check_fleet_edges() self.aliens.update() if pygame.sprite.spritecollideany(self.ship, self.aliens): print('ship hit!!!') self._ship_hit() self._check_aliens_bottom() def _check_aliens_bottom(self): # 判断外星人是否到达底部 screen_rect = self.screen.get_rect() for alien in self.aliens.sprites(): if alien.rect.bottom >= screen_rect.bottom: self._ship_hit() break def _ship_hit(self): '''响应飞船碰撞''' if self.status.ships_left > 0: self.status.ships_left -= 1 # 清空外星人和子弹 self.aliens.empty() self.bullets.empty() # 创建新的外星人群 self._create_fleet() self.ship.ship_center() time.sleep(0.5) else: self.status.game_active = False self.screen.fill(self.settings.bg_color) self.ship.blitme() self.gameover.blitme() self.settings.fleet_drop_speed = 0 self.settings.alien_speed = 0 self.ship.move_right = False self.ship.move_left = False pygame.display.flip() def _update_screen(self): # 每次循环重新绘制屏幕 self.screen.fill(self.settings.bg_color) self.ship.blitme() for bullet in self.bullets.sprites(): bullet.draw_bullet() self.aliens.draw(self.screen) # 让绘制的屏幕可见 pygame.display.flip()
from game import Game from state import State from splash_state import SplashScreen from menu import Menu from level_01 import Level_01 from level_02 import Level_02 from level_03 import Level_03 from gameover import Gameover ######################### # Main # # Entry Point # ######################### if __name__ == "__main__": pg.init() screen = pg.display.set_mode((800, 600)) states = { "SPLASH": SplashScreen(), "MENU": Menu(), "LEVEL_01": Level_01(), "LEVEL_02": Level_02(), "LEVEL_03": Level_03(), "GAMEOVER": Gameover() } game = Game(screen, states, "SPLASH") game.run() pg.quit() sys.exit()
pygame.key.set_repeat(75) while 1: area = Area(width - 100, height) tetrino_queue = [] for i in range(5): tetrino_queue.append(randint(0, 6)) tetrino_color = (randint(1, 255), randint(1, 255), randint(1, 255)) tetrino = Tetrino(tetrino_color, tetrino_queue[0]) hud = Hud(width, height) gameOver = Gameover(width, height, area.score) screen.fill((0, 0, 0)) hud.draw_hud(screen) menu = Menu(width, height, (255, 255, 255), (0, 0, 0)) menu.draw_menu(screen) menu.update_menu(screen) pygame.display.flip() core = Core(tetrino, area, menu, screen) core.menu_input() init = 1000 start_time = pygame.time.get_ticks() while menu.singlePlayer and tetrino.game_state: core.check_input()