def __init__(self): # 初始化 pygame.init() # 设置宽度高度 self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取游戏标题的宽度高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int((self.height - t_height) / 2)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() # 获取开始按钮的宽度高度 self.btn_width, self.btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - self.btn_width) / 2), int(self.height / 2 + self.btn_height + 20)) # 游戏文字对象 self.score_font = pygame.font.SysFont('kaiti', 50) # 加载背景音乐 # pygame.mixer.music.load(constants.BG_MUSIC) # pygame.mixer.music.play(-1) # 设置循环播放 # pygame.mixer.music.set_volume(0.2) # 音量设置 # 设置窗口标题 pygame.display.set_caption('飞机大战') # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=20) # 实例化游戏结果 self.rest = PlayRest() self.clock = pygame.time.Clock() # 上次按的键盘的某一个键 self.key_down = None
class PlaneWar(object): """飞机大战""" READY = 0 # 游戏准备中 PLAYING = 1 # 游戏中 OVER = 2 # 游戏结束 status = READY our_plane = None frame = 0 # 播放帧数 # 为了方便之后的碰撞检测 # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 游戏结果 rest = PlayRest() def __init__(self): # 初始化游戏 pygame.init() self.width, self.height = 480, 852 # 加载游戏屏幕 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置标题 pygame.display.set_caption('PlaneWar') # 背景图片 self.bg = pygame.image.load(constants.BG_IMG) # 结束背景 self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 开始标题的位置设置 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮的位置设置 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() b_width, b_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - b_width) / 2), int(self.height / 2 + b_height)) # 游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # background music pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限循环播放 pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=8) # 设置帧数 self.clock = pygame.time.Clock() self.key_down = None def bind_event(self): """绑定事件""" for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 游戏正在等待中,点击鼠标进入游戏 if self.status == self.READY: self.status = self.PLAYING elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) elif event.type == pygame.KEYDOWN: self.key_down = event.key # 先判断在游戏中,在判断上下左右控制方向 if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_right() # 让飞机一直发射炮 if self.status == self.PLAYING and self.frame % 10 == 0: self.our_plane.shoot() def add_small_enemies(self, num): for i in range(num): plane = SmallEnemyPlane(self.screen, 4) plane.add(self.small_enemies, self.enemies) def run_game(self): """游戏的主循环""" while True: # 1. 设置帧数 self.clock.tick(60) # 计数 self.frame += 1 if self.frame >= 60: self.frame = 0 # 2. 绑定事件 self.bind_event() # 3. 更新游戏状态 硬编码 if self.status == self.READY: # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制开始背景 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) # 重置按键 self.key_down = None self.rest.score = 0 elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制我方飞机 self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update() # 游戏分数 score_text = self.score_font.render( '得分:{}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) # 绘制分数 self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: # 游戏结束背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 1.本次总分 score_text = self.score_font.render( '{}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() score_text_rect.topleft = (int( (self.width - text_w) / 2), int(self.height / 2)) # 绘制分数 self.screen.blit(score_text, score_text_rect) # 2.历史最高分 score_his = self.score_font.render( '{}'.format(self.rest.get_max_core()), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_his, (150, 40)) pygame.display.flip()
class PlaneWar(object): """ 飞机大战 """ # 游戏状态 READY = 0 # 游戏准备中 PLAYING = 1 # 游戏中 PAUSED = 2 # 游戏暂停 OVER = 3 # 游戏结束 status = READY # 0 准备中 1 游戏中 2 游戏暂停 3 游戏结束 # 计数器 frame = 0 # 播放帧数 delay = 100 # 延迟播放计数 kill_enemies_num = 0 # 记录击落敌机数目 # 我方飞机 our_plane = None # 添加敌方飞机(一架飞机可以属于多个精灵组) small_enemies = pygame.sprite.Group() # 小型敌机 medium_enemies = pygame.sprite.Group() # 中型敌机 big_enemies = pygame.sprite.Group() # 大型敌机 enemies = pygame.sprite.Group() # 游戏结果 rest = PlayRest() def __init__(self): # pgame游戏初始化 pygame.init() # 设置屏幕游戏宽度和高度 self.width, self.height = 480, 852 # 加载屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载游戏图标 gameIcon = pygame.image.load(constants.GAME_ICON_IMG) pygame.display.set_icon(gameIcon) # 加载游戏背景图片 self.bg = pygame.image.load(constants.BG_IMG) # 加载游戏结束背景图片 self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 加载游戏标题图片 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) # 设置游戏标题宽度和高度 self.img_game_title_rect = self.img_game_title.get_rect() t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 加载游戏开始按钮图片 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) # 设置游戏开始按钮宽度和高度 self.btn_start_rect = self.btn_start.get_rect() self.btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int((self.width - self.btn_width) / 2), int(self.height / 2 + btn_height)) # 加载暂停按钮图片 self.btn_paused_nor = pygame.image.load( constants.IMG_GAME_PAUSE_NOR_BTN) self.btn_paused = pygame.image.load(constants.IMG_GAME_PAUSE_BTN) # 加载暂停恢复按钮图片 self.btn_resume_nor = pygame.image.load( constants.IMG_GAME_RESUME_NOR_BTN) self.btn_resume = pygame.image.load(constants.IMG_GAME_RESUME_BTN) # 暂停键按钮范围值 self.paused_rect = self.btn_paused_nor.get_rect() self.paused_rect.left, self.paused_rect.top = self.width - self.paused_rect.width - 10, 10 self.paused_image = self.btn_paused_nor self.resume_image = self.btn_resume_nor # 加载暂停标识符 self.img_game_paused = pygame.image.load(constants.IMG_GAME_STOP) # 暂停标识符位置 self.img_game_paused_rect = self.img_game_paused.get_rect() # 游戏文字对象 self.score_font = pygame.font.Font(constants.TEXT_SCORE_FONT, 35) # 加载背景音乐s pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限循环播放 pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=5) # 炸弹补给对象 self.bomb = BombSupply(self.screen, 3) # 炸弹包每隔一定随机出现 self.bomb_appear_time = pygame.USEREVENT pygame.time.set_timer(self.bomb_appear_time, constants.BOMB_APPEAR_TIME) # 血包补给对象 self.hp_supply = HPSupply(self.screen, 3) # 子弹补给对象 self.bullet_supply = BulletsSupply(self.screen, 3) # 炸弹包每隔一定随机出现 self.hp_supply_appear_time = pygame.USEREVENT + 1 pygame.time.set_timer(self.hp_supply_appear_time, constants.HP_APPEAR_TIME) # 设置帧速率 self.clock = pygame.time.Clock() # 上次按的键盘上的某一个键,用于控制飞机 self.key_down = None def bind_event(self): """ 绑定事件 """ # 飞机连续移动键盘事件检测 self.key_down = pygame.key.get_pressed() # 1. 监听事件 for event in pygame.event.get(): # 右上角关闭按钮检测 if event.type == pygame.QUIT: pygame.quit() sys.exit() # 鼠标点击检测 elif event.type == pygame.MOUSEBUTTONDOWN: # 游戏正在准备中,点击才能进入游戏 if self.status == self.READY and self.btn_start_rect.collidepoint( event.pos): # 游戏开始 self.status = self.PLAYING # 重置英雄飞机位置 self.our_plane.reset_pos() # 重置英雄飞机生命值 self.our_plane.hp = constants.OUR_PLANE_HP # 重置本局分数 self.rest.score = 0 elif self.status == self.PLAYING and self.paused_rect.collidepoint( event.pos): # 游戏暂停 self.status = self.PAUSED # 暂停音效 pygame.mixer.music.pause() pygame.mixer.pause() # 游戏音乐暂停 elif self.status == self.PAUSED and self.paused_rect.collidepoint( event.pos): # 游戏继续 self.status = self.PLAYING # 恢复音效 pygame.mixer.music.unpause() pygame.mixer.unpause() elif self.status == self.OVER: self.status = self.READY # 添加敌方飞机 self.add_small_enemies(6) self.add_medium_enemies(2) self.add_big_enemies(1) # 如果游戏结束时补给包在屏幕中,则重置其位置 if self.bomb.active: self.bomb.reset() if self.hp_supply.active: self.hp_supply.reset() # 重置击落敌机数量 self.kill_enemies_num = 0 # 鼠标放置坐标检测 elif event.type == pygame.MOUSEMOTION: if self.paused_rect.collidepoint(event.pos): if self.status == self.PAUSED: self.paused_image = self.btn_resume else: self.paused_image = self.btn_paused else: if self.status == self.PAUSED: self.paused_image = self.btn_resume_nor else: self.paused_image = self.btn_paused_nor # 单次敲击键盘事件监测 elif event.type == pygame.KEYDOWN: if self.status == self.PLAYING: # J键发射子弹 if event.key == pygame.K_j: self.our_plane.shoot() # 空格键发射导弹包 if event.key == pygame.K_SPACE and self.bomb.bomb_num: self.bomb.boom(self) # 炸弹包补给时间检测 elif event.type == self.bomb_appear_time: self.bomb.active = True self.bomb.reset() # 血包补给时间检测 elif event.type == self.hp_supply_appear_time: self.hp_supply.active = True self.hp_supply.reset() def add_small_enemies(self, num): """ 随机产生n架小型敌机 :param num: 飞机产生数量 :return: """ for i in range(num): plane = SmallEnemyPlane(self.screen, 4) plane.add(self.small_enemies, self.enemies) def add_medium_enemies(self, num): """ 随机产生n架中型敌机 :param num: 飞机产生数量 :return: """ for i in range(num): plane = MediumEnemyPlane(self.screen, 2) plane.add(self.medium_enemies, self.enemies) def add_big_enemies(self, num): """ 随机产生n架大型敌机 :param num: 飞机产生数量 :return: """ for i in range(num): plane = BigEnemyPlane(self.screen, 1) plane.add(self.big_enemies, self.enemies) def run_game(self): """ 游戏主循环部分 """ while True: # 设置帧速率 self.clock.tick(60) # 处理计数器 self.frame += 1 self.delay -= 1 if self.frame >= 60: self.frame = 0 if not self.delay: self.delay = 100 # 事件处理 self.bind_event() # 更新游戏的状态 if self.status == self.READY: # 游戏正在准备中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) self.key_down = None elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制暂停按钮 self.screen.blit(self.paused_image, self.paused_rect) # 绘制我方飞机 self.our_plane.update(self) # 绘制敌方飞机 self.small_enemies.update(self) self.medium_enemies.update(self) self.big_enemies.update(self) # 绘制炸弹补给包 self.bomb.update(self) # 绘制血包 self.hp_supply.update(self) # 每击败一定数量敌机,出现子弹补给包 self.bullet_supply.update(self) if self.kill_enemies_num > 0 and not self.kill_enemies_num % constants.BULLET_SUPPLY_APPEAR_NUM: self.bullet_supply.active = True self.bullet_supply.reset() self.bullet_supply.update(self) # 游戏分数 score_text = self.score_font.render( 'Score : {0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.PAUSED: # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制暂停恢复按钮 self.screen.blit(self.paused_image, self.paused_rect) # 绘制暂停标识符 self.screen.blit(self.img_game_paused, ( (self.width - self.img_game_paused_rect.width) / 2, (self.height - self.img_game_paused_rect.height) / 2, )) elif self.status == self.OVER: # 游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 本次总分 score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() # 改变文字的位置 score_text_rect.topleft = (int( (self.width - text_w) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) # 历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_score()), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_his, (150, 40)) pygame.display.flip()
class PlaneWar(object): """飞机大战""" # 游戏状态 READY = 0 PLAYING = 1 OVER = 2 status = READY # 准备中,1游戏中,2游戏结束 our_plane = None frame = 0 # 播放帧数 # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 游戏结果 rest = PlayRest() def __init__(self): # 初始化游戏 pygame.init() self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(BG_IMG) self.bg_over = pygame.image.load(BG_IMG_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 宽度和高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # 游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # 加载背景音乐 # pygame.mixer.music.load(BG_MUSIC) # pygame.mixer.music.play(-1) # 无限循环播放 # pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() # 上次按的键盘上的某一个键,用于控制飞机 self.key_down = None def bind_event(self): """绑定事件""" # 1.监听事件 for events in pygame.event.get(): # 退出游戏 if events.type == pygame.QUIT: pygame.quit() sys.exit() elif events.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏正在准备中,点击才能进入游戏 if self.status == self.READY: self.status = self.PLAYING elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) elif events.type == pygame.KEYDOWN: # 键盘事件 self.key_down = events.key # 游戏正在游戏中,才需要控制键盘 ASWD if self.status == self.PLAYING: if events.key == pygame.K_w or events.key == pygame.K_UP: self.our_plane.move_up() elif events.key == pygame.K_s or events.key == pygame.K_DOWN: self.our_plane.move_down() elif events.key == pygame.K_a or events.key == pygame.K_LEFT: self.our_plane.move_left() elif events.key == pygame.K_d or events.key == pygame.K_RIGHT: self.our_plane.move_right() elif events.key == pygame.K_SPACE: # 发射子弹 self.our_plane.shoot() def add_small_enemies(self, num): """ 飞机的产生数量 num :param num: :return: """ # 随机添加6架小型敌机 for i in range(num): plane = SmallEnemyPlane(self.screen, 3) plane.add(self.small_enemies, self.enemies) def run_game(self): """游戏主循环部分""" while True: # 设置帧速率 self.clock.tick(60) self.frame += 1 if self.frame >= 60: self.frame = 0 # 1.绑定事件 self.bind_event() # 更新游戏的状态 if self.status == self.READY: # 游戏正在准备中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) self.key_down = None elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制飞机 self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update() # 游戏分数 score_text = self.score_font.render( '得分:{0}'.format(self.rest.score), False, TEXT_SCORE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: # 游戏结束 # 游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 1.本次总分 score_text = self.score_font.render( '得分:{0}'.format(self.rest.score), False, TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() # 改变文字的位置 score_text_rect.topleft = (int( (self.width - text_h) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) # 2.历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, TEXT_SCORE_COLOR) self.screen.blit(score_his, (150, 40)) pygame.display.flip()
class PlaneWar(object): """ Aircraft War Game """ # situation of game READY = 0 PLAYING = 1 OVER = 2 status = READY our_plane = None frame = 0 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # result of the game rest = PlayRest() def __init__(self): pygame.init() self.width, self.height = 480, 852 self.screen = pygame.display.set_mode((self.width, self.height)) #name of window pygame.display.set_caption('Aircraft Game') # load background images self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # title self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # start button self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int((self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # font self.score_font = pygame.font.SysFont('test_sans', 32) # load music pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # infinite loop for playing music pygame.mixer.music.set_volume(0.2) # set the volume # our plane self.our_plane = OurPlane(self.screen, speed = 40) self.clock = pygame.time.Clock() # use keyboard to control aircraft self.key_down = None def bind_event(self): # 1. listening event for event in pygame.event.get(): # quit if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # click the mouse to get in the game if self.status == self.READY: self.status = self.PLAYING self.rest.score = 0 elif self.status == self.PLAYING: # click the mouse to fire the bullet self.our_plane.shoot() elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(20) elif event.type == pygame.KEYDOWN: # keyborad self.key_down = event.key # only in the game, need to control the keyboard ASWD if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_right() elif event.key == pygame.K_SPACE: # shoot self.our_plane.shoot() def add_small_enemies(self, num): """ add small enemy aircraft at random :param num: number of aircraft produced :return: """ # add 6 small enemy aircraft at random for i in range(num): plane = SmallEnemyPlane(self.screen, 10) plane.add(self.small_enemies, self.enemies) def run_game(self): """ main part of game """ while True: # 1. set the frame rate self.clock.tick(10) self.frame += 1 if self.frame >= 10: self.frame = 0 # 2. bind event self.bind_event() # 3.update the situation of game if self.status == self.READY: # get ready # setting background self.screen.blit(self.bg, self.bg.get_rect()) # title self.screen.blit(self.img_game_title, self.img_game_title_rect) # start button self.screen.blit(self.btn_start, self.btn_start_rect) self.key_down = None elif self.status == self.PLAYING: # playing # background self.screen.blit(self.bg, self.bg.get_rect()) # plane self.our_plane.update(self) # bullet self.our_plane.bullets.update(self) # enemy plane self.small_enemies.update() # score score_text = self.score_font.render( 'Score: {0}'.format(self.rest.score), False, constants.TEXT_SOCRE_COLOR ) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: # end game # background self.screen.blit(self.bg_over, self.bg_over.get_rect()) # account score # 1. total score score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SOCRE_COLOR ) self.screen.blit(score_text, (350,520)) # 2. the highest score score_his = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, constants.TEXT_SOCRE_COLOR ) self.screen.blit(score_his, (350,300)) pygame.display.flip()
class PlaneWar(object): """飞机大战""" # 游戏状态 0.准备中,1.游戏中,2.游戏结束 READY = 0 PLAYING = 1 OVER = 2 status = READY # 实例化飞机对象 our_plane = None frame = 0 # 播放帧数 # 敌方小飞机精灵组,可以属于多个精灵组,便于碰撞检测 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 游戏结果 rest = PlayRest() def __init__(self): # 初始化游戏 pygame.init() # 指定屏幕高度、宽度 self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 加载游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 标题高度和宽度 self.t_width, self.t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - self.t_width) / 2), int(self.height / 2 - self.t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() self.btn_width, self.btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int((self.width - self.btn_width) / 2), int(self.height / 2 + self.btn_height)) # 游戏文字对象 self.score_font = pygame.font.SysFont('arial', 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) # 无限循环播放 pygame.mixer.music.play(-1) # 设置音量 pygame.mixer.music.set_volume(0.3) # 上次按的键盘 self.key_down = None # 实例化飞机对象 self.our_plane = OurPlane(self.screen, speed=8) self.clock = pygame.time.Clock() def bind_event(self): # 1.监听事件 for event in pygame.event.get(): # 退出游戏 if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏正在准备中才能进入游戏 if self.status == self.READY: self.status = self.PLAYING elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) elif event.type == pygame.KEYDOWN: # 键盘事件 # 游戏正在游戏中,才需要控制键盘 ASWD self.key_down = event.key if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_right() elif event.key == pygame.K_SPACE: # 发射子弹 self.our_plane.shoot() def add_small_enemies(self, num): # 随机添加num架小飞机 for i in range(num): plane = SmallEnemyPlane(self.screen, 4) plane.add(self.small_enemies, self.enemies) def run_game(self): """游戏主循环部分""" while True: # 设置帧速率 self.clock.tick(60) self.frame += 1 if self.frame >= 60: self.frame = 0 # 绑定事件 self.bind_event() # 2.更新游戏的状态 if self.status == self.READY: # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 游戏正在准备中,绘制开始画面,绘制标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) # 重置按键 self.key_down = None elif self.status == self.PLAYING: # 游戏进行中 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制飞机 self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update() # 游戏分数 score_text = self.score_font.render( 'Score:{0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: # 游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 1.本次总分 score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() # 改变文字的位置 score_text_rect.topleft = (int( (self.width - text_h) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) # 2.历史最高分 score_hist = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_hist, (150, 40)) # 刷新屏幕 pygame.display.flip()
class PlaneWar: """飞机大战""" # 游戏状态 READY = 0 # 准备状态 PLAYING = 1 # 游戏中 OVER = 2 # 结束游戏 status = READY our_plane = None frame = 0 # 播发帧数 clock = pygame.time.Clock() # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 游戏结果 rest = PlayRest() def __init__(self): pygame.init() self.width, self.height = 480, 852 # 屏幕对象,设计窗口大小 self.screen = pygame.display.set_mode((self.width, self.height)) # 设计窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 游戏标题的宽和高 self.t_width, self.t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - self.t_width) / 2), int((self.height - self.t_height) / 2 - 40)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START) self.btn_start_rect = self.btn_start.get_rect() self.btn_width, self.btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - self.btn_width) / 2), int((self.height / 2 + self.btn_height))) # 游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限的循环 pygame.mixer.music.set_volume(0.2) # 我放飞机对象 self.our_plane = OurPlane(self.screen, speed=5) self.clock = pygame.time.Clock() # 上次按得键盘上的某一个,用于控制飞机 self.key_down = None def bind_event(self): """绑定事件""" # 1.监听事件 for event in pygame.event.get(): # 退出游戏 if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏正在准备中,点击才能进入游戏 if self.status == self.READY: self.status = self.PLAYING self.rest.score = 0 elif self.status == self.PLAYING: # 点击鼠标发射子弹 self.our_plane.shoot() elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) elif event.type == pygame.KEYDOWN: # 键盘事件 self.key_down = event.key # 游戏中,才需要控制键盘AWSD if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_rige() elif event.key == pygame.K_SPACE: # 发射子弹 self.our_plane.shoot() def add_small_enemies(self, num): """ 随机生成N架小型飞机 :param num: 飞机的生产数量 :return: """ # 随机生成num架小型飞机 for i in range(num): plane = SmallEnemyPlane(self.screen, 5) plane.add(self.small_enemies, self.enemies) def run_game(self): """游戏主循环部分""" while True: # 1.设计帧数 self.clock.tick(60) self.frame += 1 if self.frame >= 60: self.frame = 0 # 2.绑定事件 self.bind_event() # 3.更新游戏状态 if self.status == self.READY: self.screen.blit(self.bg, self.bg.get_rect()) #标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) #开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) self.key_down = None elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制飞机 self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update() # 游戏分数 score_text = self.score_font.render( '得分: {0}'.format(self.rest.score), False, constants.TEXT_SOCRE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: # 游戏结束 # 游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 1. 本次总分 score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SOCRE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() # 改变文字的位置 score_text_rect.topleft = (int( (self.width - text_h) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) # 2. 历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, constants.TEXT_SOCRE_COLOR) self.screen.blit(score_his, (150, 40)) pygame.display.flip()
class PlaneWar(object): """飞机大战""" # 游戏状态 0 准备中 1 游戏中 2 结束 READY = 0 PLAYING = 1 OVER = 2 status = READY # 装载我方飞机 our_plan = None # 帧数计数器 frame = 0 # 敌方飞机精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 分数 rest = PlayRest() def __init__(self): # 初始化游戏 pygame.init() # 屏幕宽高 self.width, self.height = 480, 852 # 打开屏幕 self.screen = pygame.display.set_mode((self.width, self.height)) # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) # 加载结束图片 self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 加载开始标题图片 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 标题的宽高 t_width, t_height = self.img_game_title.get_size() # 设置标题的位置 self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int((self.height - t_height) / 2)) # 加载开始按钮 self.img_game_start_btn = pygame.image.load( constants.IMG_GAME_START_BTN) self.img_game_start_btn_rect = self.img_game_start_btn.get_rect() btn_width, btn_height = self.img_game_start_btn.get_size() # 设置按钮开始按钮位置 self.img_game_start_btn_rect.topleft = (int( (self.width - btn_width) / 2), int( (self.height - btn_height) / 2) + 100) # 游戏文字对象 self.score_font = pygame.font.SysFont('fangsong', 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.set_volume(0.8) pygame.mixer.music.play(-1) # 装载我方飞机 self.our_plan = OurPlane(self.screen, 8) self.clock = pygame.time.Clock() def bind_event(self): """ 绑定事件 """ # 监听事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if self.status == self.READY: self.status = self.PLAYING # 被按下的键 key_pressed = pygame.key.get_pressed() if self.status == self.PLAYING: if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]: self.our_plan.move_up() if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]: self.our_plan.move_down() if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]: self.our_plan.move_left() if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]: self.our_plan.move_right() if key_pressed[pygame.K_SPACE] and (self.frame % 10 == 0): self.our_plan.shoot(10) def add_small_enemies(self, num): # 装载敌方小飞机 for i in range(num): plan = SmallEnemyPlane(self.screen, 2) plan.add(self.small_enemies, self.enemies) def run_game(self): """ 游戏的主循环 """ while True: # 绑定事件 self.bind_event() # 设置帧数率 self.clock.tick(60) self.frame += 1 if self.frame >= 60: self.frame = 0 # 各个状态下 if self.status == self.READY: # 游戏准备中 # 绘制背景图 # 绘制开始按钮 self.screen.blit(self.bg, self.bg.get_rect()) self.screen.blit(self.img_game_title, self.img_game_title_rect) self.screen.blit(self.img_game_start_btn, self.img_game_start_btn_rect) elif self.status == self.PLAYING: # 绘制游戏中的画面 # 绘制背景图片 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制我方飞机 self.our_plan.update(self.frame, self) # 绘制子弹 self.our_plan.bullets.update(self) # 绘制敌方飞机 # print(self.small_enemies) self.small_enemies.update() # 游戏分数 score_text = self.score_font.render( '得分:{0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() score_text_rect.topleft = (20, 50) self.screen.blit(score_text, score_text_rect) elif self.status == self.OVER: # 绘制游戏结束的画面 # 绘制背景图 self.screen.blit(self.bg_over, self.bg.get_rect()) # 统计分数 # 游戏分数 score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() score_text_rect.centerx = int(self.width / 2) score_text_rect.centery = int(self.height / 2) self.screen.blit(score_text, score_text_rect) # 历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, constants.TEXT_SCORE_COLOR) score_his_rect = score_text.get_rect() score_his_rect.topleft = (150, 40) self.screen.blit(score_his, score_his_rect) pygame.display.flip()
class PlaneWar(object): """飞机大战""" READY = 0 # 游戏准备中 PLAYING = 1 # 游戏中 OVER = 2 # 游戏结束 status = READY # 实例化我方飞机 our_plane = None # # 实例化游戏结果 # rest = None # 播放帧数 frame = 0 # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() clock = pygame.time.Clock() def __init__(self): # 初始化 pygame.init() # 设置宽度高度 self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取游戏标题的宽度高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int((self.height - t_height) / 2)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() # 获取开始按钮的宽度高度 self.btn_width, self.btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - self.btn_width) / 2), int(self.height / 2 + self.btn_height + 20)) # 游戏文字对象 self.score_font = pygame.font.SysFont('kaiti', 50) # 加载背景音乐 # pygame.mixer.music.load(constants.BG_MUSIC) # pygame.mixer.music.play(-1) # 设置循环播放 # pygame.mixer.music.set_volume(0.2) # 音量设置 # 设置窗口标题 pygame.display.set_caption('飞机大战') # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=20) # 实例化游戏结果 self.rest = PlayRest() self.clock = pygame.time.Clock() # 上次按的键盘的某一个键 self.key_down = None def bind_event(self): """绑定事件""" # 监听事件 for event in pygame.event.get(): # 退出游戏 if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏正在准备中,点击才能进入游戏 if self.status == self.READY: self.status = self.PLAYING # 游戏结束后,鼠标点击可以继续进行游戏 elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) self.rest.score = 0 elif event.type == pygame.KEYDOWN: # 键盘事件 self.key_down = event.key # 游戏正在进行中,才需要控制键盘 if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_right() elif event.key == pygame.K_SPACE: # 发射子弹 self.our_plane.shoot() def add_small_enemies(self, num): """随机添加N架小型飞机""" for i in range(num): plane = SmallEnemyPlane(self.screen, speed=8) plane.add(self.small_enemies, self.enemies) def run_game(self): """游戏主循环部分""" while True: # 1.设置帧速率 self.clock.tick(60) self.frame += 1 if self.frame >= 60: self.frame = 0 # 2.绑定事件 self.bind_event() # 3.更新游戏的状态 if self.status == self.READY: # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) self.key_down = None elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制飞机 self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update() # 绘制游戏分数 score_text = self.score_font.render( "得分:{}".format(self.rest.score), False, constants.TEXT_SOCRE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: # 游戏结束 # 游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 绘制本次总分数 score_text = self.score_font.render( "{}".format(self.rest.score), False, constants.TEXT_SOCRE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() score_text_rect.topleft = (int( (self.width - text_w) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) # 历史最高分 score_his = self.score_font.render( '{}'.format(self.rest.get_max_score()), False, constants.TEXT_SOCRE_COLOR) self.screen.blit(score_his, (150, 30)) pygame.display.flip()
class PlaneWar(object): """ 飞机大战 控制飞机的整个流程 """ # 设置游戏状态 READY = 0 # 游戏准备中 PLAYING = 1 # 游戏中 OVER = 2 # 游戏结束 status = READY # 0准备中,1 游戏中,2 游戏结束 our_plane = None frame = 0 # 播放帧数 # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 游戏结果 rest = PlayRest() def __init__(self): """ 将所有需要初始化的东西初始化一遍 """ # 忘记函数应该调用什么元素时,应该将pygame官网打开进行查阅 # 初始化游戏 pygame.init() self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 宽度和高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # 游戏文字对象 self.score_font = pygame.font.Font(constants.TEXT_FONT, 35) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限循环播放 pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() # 保存上次按下的键 self.key_down = None def bind_event(self): """ 绑定事件 """ # 1. 监听事件 for event in pygame.event.get(): # 退出游戏 if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏正在准备中,点击才能进入游戏 if self.status == self.READY: self.status = self.PLAYING elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) self.rest.score = 0 elif event.type == pygame.KEYDOWN: # 键盘事件 self.key_down = event.key # 游戏正在游戏中,才需要控制键盘 A S W D if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_right() elif event.key == pygame.K_SPACE: # 发射子弹 self.our_plane.shoot() def add_small_enemies(self, num): """ 随机添加N架小型敌机 :param num: 飞机的产生数量 :return: """ # 随机添加6架小型敌机 for i in range(num): plane = SmallEnemyPlane(self.screen, 4) plane.add(self.small_enemies, self.enemies) def run_game(self): """ 游戏主循环部分 """ while True: # 1. 设置帧速率 self.clock.tick(60) self.frame = (self.frame + 1) % 60 # 2. 绑定事件 self.bind_event() # 3.更新游戏的状态 if self.status == self.READY: # 游戏正在准备中 self.key_down = None # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制飞机 self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update(self) # 游戏分数 score_text = self.score_font.render( '得分:{0}'.format(self.rest.score), False, constants.TEXT_SOCER_COLOR) self.screen.blit(score_text, score_text.get_rect()) # 游戏结束 elif self.status == self.OVER: # 游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 绘制本次总分 score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SOCER_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() # 改变文字的位置 score_text_rect.topleft = (int( (self.width - text_w) / 2), int(self.height / 2) - 30) self.screen.blit(score_text, score_text_rect) # 绘制历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_sore()), False, constants.TEXT_SOCER_COLOR) self.screen.blit(score_his, (150, 38)) pygame.display.flip()
class PlaneWar(object): """飞机大战""" READY = 0 PLAYING = 1 OVER = 2 status = READY our_plane = None frame = 0 #设置帧数 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() #游戏结果 rest = PlayRest() def __init__(self): #初始化游戏 pygame.init() self.width, self.height = 480, 852 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption("飞机大战") # 加载背景 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取图片位置对象 t_width, t_height = self.img_game_title.get_size() # 获取图片宽度和高度 self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height) ) # 设置图片的位置 # 开始按钮, self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height) ) # 设置图片的位置 #游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # # 背景音乐 # pygame.mixer.music.load(constants.BG_MUSIC) # # 循环播放 # pygame.mixer.music.play(-1) # # 音量 # pygame.mixer.music.set_volume(0.2) #我方飞机对象 self.our_plane = OurPlane(self.screen, speed=20) self.clock = pygame.time.Clock() self.key_down = None def bind_event(self): """"绑定事件""" for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(0) elif event.type == pygame.MOUSEBUTTONDOWN: #鼠标点击按下 if self.status == self.READY: self.status = self.PLAYING elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) elif event.type == pygame.KEYDOWN: self.key_down = event.key if self.status == 1: if event.key == pygame.K_w or event.type == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.type == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.type == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.type == pygame.K_RIGHT: self.our_plane.move_right() elif event.key == pygame.K_SPACE: #发射子弹 self.our_plane.shoot() def add_small_enemies(self, num): """ 随机添加num架小飞机 :param num: :return: """ for i in range(num): plane = SmallEnemyPlane(self.screen, 8) plane.add(self.small_enemies, self.enemies) def run_game(self): """游戏主循环""" while True: # 设置帧数率 self.clock.tick(60) self.frame += 1 if self.frame >= 60: # 减少内存 frame = 0 self.bind_event() #更新游戏状态 if self.status == self.READY: # 游戏准备中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 绘制开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) self.key_down = None elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制飞机 self.our_plane.blit_me() self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update() #游戏分数 score_text = self.score_font.render( "得分:{0}".format(self.rest.score), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: #游戏结束 #游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) #分数统计 #绘制总分数,本次 score_text = self.score_font.render( "{0}".format(self.rest.score), False, constants.TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() #改变文字的位置 score_text_rect.topleft = (int( (self.width - text_h) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) #历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_his, (150, 40)) pygame.display.flip()
class PlaneWar(object): ''' 飞机大战 ''' READY = 0 #游戏准备中 PLAYING = 1 #游戏中 OVER = 2 #游戏结束 status = READY # 游戏的状态: 0.准备中 1. 游戏中 3.游戏结束 # 我方飞机 our_plane = None frame = 0 # 播放的帧数 # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() #游戏结果 rest = PlayRest() def __init__(self): #初始化游戏 pygame.init() # 屏幕宽度,高度 self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载 # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # bg_ready = pygame.image.load(constants.BG_IMG_READY) #图片一样不用实现 # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取游戏标题的宽度和高度 t_width, t_height = self.img_game_title.get_size() # 标题摆放的位置 self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() # 获取按钮的宽度和高度 btn_width, btn_height = self.btn_start.get_size() # 开始按钮摆放的位置 self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) #游戏文字对象 self.score_font = pygame.font.Font(constants.FONT_SCORE, 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) # 循环播放音乐 pygame.mixer.music.play(-1) # 设置音量的大小 pygame.mixer.music.set_volume(0.2) #我放飞机实例 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() #上次按的键盘的某一键,用于控制飞机 self.key_down = None def bind_event(self): ''' 绑定事件 ''' # 1.监听事件 for event in pygame.event.get(): if event.type == pygame.QUIT: # 退出事件 pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏在准备中,才能进入 游戏 if self.status == self.READY: self.status = self.PLAYING elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) elif event.type == pygame.KEYDOWN: # 键盘事件 self.key_down = event.key # 游戏正在游戏中才需要控制键盘事件 ASWD if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_right() elif event.key == pygame.K_SPACE: # 发射子弹 self.our_plane.shoot() def add_small_enemies(self, num): ''' 随即添加n架小型敌机 :param num: 飞机产生的数量 :return: ''' # 随机添加六架小型飞机 for i in range(num): plane = SmallEnemyPlane(self.screen, 4) plane.add(self.small_enemies, self.enemies) def run_game(self): ''' 游戏的主循环 ''' #1.设置帧速率 while True: self.clock.tick(60) self.clock.tick(60) self.frame += 1 if self.frame >= 60: self.frame = 0 # 2.绑定事件 self.bind_event() #3.更新游戏的状态: if self.status == self.READY: # 游戏正在准备中 # 3.绘制游戏状态 # 3.1绘制背景图片 self.screen.blit(self.bg, self.bg.get_rect()) #标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) self.key_down = None self.rest.score = 0 elif self.status == self.PLAYING: #游戏进行中 #绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) #绘制飞机 self.our_plane.update(self) #绘制子弹 self.our_plane.bullets.update(self) #绘制敌方飞机 self.small_enemies.update() #绘制游戏分数 score_text = self.score_font.render( '得分:{0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: #游戏结束 #游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) #分数统计 #1.绘制本次总分 score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SCORE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() #改变文字位置 score_text_rect.topleft = (int( (self.width - text_h) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) #2.历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, constants.TEXT_SCORE_COLOR) self.screen.blit(score_his, (150, 40)) # 4.更新屏幕 pygame.display.flip()
class PlaneWar(object): # 游戏状态 READY = 0 # 游戏准备中 PLAYING = 1 # 游戏中 OVER = 2 # 游戏结束 # 0.准备中,1.游戏中,2.游戏结束 status = READY our_plane = None # 播放帧数 frame = 0 # 定义小型飞机精灵组 # 一架飞机可以属于多个精灵组 small_enemies = pygame.sprite.Group() enemies = pygame.sprite.Group() # 游戏结果 rest = PlayRest() def __init__(self): pygame.init() self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption('Ryna的飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMAGE) self.bg_over = pygame.image.load(constants.BG_IMAGE_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 宽度和高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # ryna头像 self.img_ryna = pygame.image.load(constants.IMG_RYNA) self.img_ryna_rect = self.img_ryna.get_rect() r_width, r_height = self.img_ryna.get_size() self.img_ryna_rect.topleft = (int( (self.width - r_width) / 2), int(self.height / 4 - r_height)) # 游戏文字对象 self.score_font = pygame.font.Font(constants.PLAY_FONT, 40) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) pygame.mixer.music.set_volume(0.2) # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() # 上次按的键盘上的某一个键,用于控制飞机 self.key_down = None def bind_event(self): """ 绑定事件 """ # 1.监听事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 鼠标点击进入游戏 # 游戏正在准备中,点击才能进入游戏 if self.status == self.READY: self.status = self.PLAYING elif self.status == self.OVER: self.status = self.READY self.add_small_enemies(6) elif event.type == pygame.KEYDOWN: # 键盘事件 self.key_down = event.key # 游戏正在游戏中,才需要控制键盘 ASWD if self.status == self.PLAYING: if event.key == pygame.K_w or event.key == pygame.K_UP: self.our_plane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.our_plane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: self.our_plane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: self.our_plane.move_right() elif event.key == pygame.K_SPACE: self.our_plane.shoot() def add_small_enemies(self, num): """ 随机添加N架小型敌机 :param num: 飞机数量 :return: """ # 随机添加6架小型敌机 for i in range(num): plane = SmallEnemyPlane(self.screen, 8) plane.add(self.small_enemies, self.enemies) def run_game(self): """ 游戏的主循环部分 """ while True: # 1.设置帧速率 self.clock.tick(60) self.frame += 1 if self.frame >= 60: self.frame = 0 # 2.绑定事件 self.bind_event() # 3.更新游戏状态 if self.status == self.READY: # 游戏正在准备中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 标题 self.screen.blit(self.img_game_title, self.img_game_title_rect) # 开始按钮 self.screen.blit(self.btn_start, self.btn_start_rect) self.screen.blit(self.img_ryna, self.img_ryna_rect) self.key_down = None elif self.status == self.PLAYING: # 游戏进行中 # 绘制背景 self.screen.blit(self.bg, self.bg.get_rect()) # 绘制飞机 self.our_plane.update(self) # 绘制子弹 self.our_plane.bullets.update(self) # 绘制敌方飞机 self.small_enemies.update() # 游戏分数 score_text = self.score_font.render( '得分: {0}'.format(self.rest.score), False, constants.TEXT_SOCRE_COLOR) self.screen.blit(score_text, score_text.get_rect()) elif self.status == self.OVER: # 游戏结束 # 游戏背景 self.screen.blit(self.bg_over, self.bg_over.get_rect()) # 分数统计 # 1. 本次总分 score_text = self.score_font.render( '{0}'.format(self.rest.score), False, constants.TEXT_SOCRE_COLOR) score_text_rect = score_text.get_rect() text_w, text_h = score_text.get_size() # 改变文字的位置 score_text_rect.topleft = (int( (self.width - text_w) / 2), int(self.height / 2)) self.screen.blit(score_text, score_text_rect) # 2. 历史最高分 score_his = self.score_font.render( '{0}'.format(self.rest.get_max_core()), False, constants.TEXT_SOCRE_COLOR) self.screen.blit(score_his, (150, 40)) pygame.display.flip()