本次增加的性能是:屏幕渲染绘制游戏分数、生命数、超级炸弹数以及暂停性能
显示如图所示:
所需资源,其中文字资源font无奈上传,莫得方法,能够本人去找一下,也能够下载我下面那个包的连贯
而后本次在代码模块只有还是main.py的变动,如果有人从头看到这一步的话,会发现一共:
main.py:渲染各个组件,运行逻辑,等等,这一步增加了渲染分数、超级炸弹数、生命数、暂停性能
bullet.py:次要是子弹的管制,包含子弹各种属性、以及子弹的重置
enemy.py:敌机类,蕴含敌机的属性、运行、重置等
myplane.py:次要是玩家飞机的管制,包含玩家飞机各种属性、飞机的上下左右挪动,以及飞机的新生
supply.py:次要是补给的管制,包含补给的挪动以及重置,其实写法都差不多
知识点(倡议本人也百度看一下):
1.绘制分数文字,*pygame.font.Font.render()😗在一个新 Surface 对象上绘制文本
pygame.font.Font.render(text, antialias, color, background=None)
a. text:要显示的文字
b. antialias: 为True时文本图像显示更润滑,为False时文本图像显示有锯齿状
c. color:字体色彩
d. background:背景色彩(可选参数),默认为小黑屏
2.paused_rect.collidepoint(event.pos)
buttons – 一个含有三个数字的元组,三个值别离代表左键、中键和右键,1就是按下了。
pos – 就是地位了……
rel – 代表了当初间隔上次产生鼠标事件时的间隔
这里间接放上去main.py的代码,其余的代码就在这一步不放上来了,不然会很多(这不能间接放.py文件就很好受),需要的话能够去后面几步查看,或者去那个包外面下载全副的。
import pygame import sys import traceback from pygame.locals import * from random import * import myplane import enemy import bullet import supply # 初始化 pygame.init() # 设置窗口大小 bg_size = width, height = 400, 700 # 实际上是元组 screen = pygame.display.set_mode(bg_size) # 设置窗口 pygame.display.set_caption("飞机大战") # 窗口题目 # 加载背景图片,对于一般图像的显示成果有没有convert都是一样的,然而 应用 convert 能够转换格局,进步 blit 的速度 background = pygame.image.load("images/background.png").convert() # 设置黑、绿、红、百几种色彩对应值,前面会用到 BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0) WHITE = (255, 255, 255) # 生成敌方小型飞机 def add_small_enemy(small_enemies, enemiesGroup, num): for i in range(num): smallenemy = enemy.SmallEnemy(bg_size) # 精灵组来实现多个图像,很适宜解决精灵列表,有增加,移除,绘制,更新等办法 # Group.sprites 精灵组 # Group.copy 复制 # Group.add 增加 # Group.remove 移除 # Group.has 判断精灵组成员 # Group.update 更新 # Group.draw 位块显示 # Group.clear - 绘制背景 # Group.empty 清空 # 将这一组敌机都增加上小型飞机属性,相当于对立解决,对立赋值 small_enemies.add(smallenemy) enemiesGroup.add(smallenemy) def main(): # 创立时钟对象(能够管制游戏循环频率) clock = pygame.time.Clock() # 生成玩家飞机 me = myplane.MyPlane(bg_size) # 寄存所有敌方飞机,这个飞机组蕴含了小型飞机、中型飞机、大型飞机的各种属性,只有用于解决碰撞 # 当程序中有大量的实体的时候,操作这些实体将会是一件相当麻烦的事 # 应用pygame.sprite.Group()函数能够创立一个精灵组,从而对立治理,这里创立了一个敌机组 enemiesGroup = pygame.sprite.Group() # 生成中央小型飞机,敌方小型飞机也是一个组,进行对立解决 small_enemies = pygame.sprite.Group() add_small_enemy(small_enemies, enemiesGroup, 15) # 生成一般子弹,这里是四颗子弹循环 bullet1s = [] # 标记产生的哪颗子弹 bullet1s_index = 0 # 子弹数目 bullet1_num = 4 for i in range(bullet1_num): # 把玩家飞机的地位发给子弹类 bullet1s.append(bullet.Bullet1(me.rect.midtop)) # 生成增强子弹,这里是八颗子弹循环,左右各四颗 bulletspro = [] # 标记产生的哪颗子弹 bulletspro_index = 0 # 子弹数目 bulletspro_num = 8 # 左右各压入四颗子弹,//2示意的整除,其实用/2也一样 for i in range(bulletspro_num // 2): # 这里(me.rect.centerx - 33, me.rect.centery)是指元组地位,centerx代表x轴,centery代表y轴 bulletspro.append(bullet.Bullet2((me.rect.centerx - 33, me.rect.centery))) bulletspro.append(bullet.Bullet2((me.rect.centerx + 33, me.rect.centery))) # 初始化增强子弹补给,超级炸弹补给 bullet_supply = supply.Bullet_Supply(bg_size) bomb_supply = supply.Bomb_Supply(bg_size) # 设置无敌工夫事件,pygame.USEREVENT代表事件1,pygame.USEREVENT+1代表事件2,以此类推,这里相当于定义了一个事件 invincible_event = pygame.USEREVENT # 设置补给工夫事件 bullet_time_supply = pygame.USEREVENT + 1 # 设置增强子弹定时器事件,即增强子弹buff继续事件 bulletpro_time = pygame.USEREVENT + 2 # 设置定时器,8秒钟发放一次补给 pygame.time.set_timer(bullet_time_supply, 8 * 1000) # 标记是否应用超级子弹 is_double_bullet = False # 玩家三条命 life_num = 3 life_image = pygame.image.load('images/life.png').convert_alpha() life_rect = life_image.get_rect() # 玩家带有超级炸弹数量 bomb_num = 3 # 绘制超级炸弹 bomb_image = pygame.image.load('images/bomb.png').convert_alpha() # 超级炸弹图片地位 bomb_rect = bomb_image.get_rect() # 超级炸弹数量字体 bomb_font = pygame.font.Font('font/font.ttf', 48) # 游戏暂停,默认为非暂停状态 paused = False # 暂停图片 pause_nor_image = pygame.image.load('images/pause_nor.png').convert_alpha() pause_pressed_image = pygame.image.load('images/pause_pressed.png').convert_alpha() # 持续图片 resume_nor_image = pygame.image.load('images/resume_nor.png').convert_alpha() resume_pressed_image = pygame.image.load('images/resume_pressed.png').convert_alpha() # 设置默认图片 paused_image = pause_nor_image # 暂停按钮地位 paused_rect = pause_nor_image.get_rect() paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10 # 管制玩家飞机图片切换,展现突突突的成果 switch_image = True # 切换延时 delay = 100 # 游戏分数 score = 0 # 设定玩家分数字体款式,从一个字体文件创建一个 Font 对象 score_font = pygame.font.Font('font/font.ttf', 36) # 飞机爆炸的图片下标,顺次为小型敌机,中型敌机,大型敌机,玩家飞机的爆炸的图片的下标,切换下标来扭转爆炸图片 e1_destory_index = 0 e2_destory_index = 0 e3_destory_index = 0 me_destory_index = 0 running = True while running: # 获取事件 for event in pygame.event.get(): # 完结事件触发完结操作 if event.type == QUIT: pygame.quit() sys.exit() # 在触发碰撞的时候,写了pygame.time.set_timer(invincible_event, 3*1000) # 意思就是3秒后将会执行invincible_event事件,这里捕捉了invincible_event事件,执行后,将勾销这个计时器,避免循环反复执行,期待下一次触发 elif event.type == invincible_event: # 解除无敌状态 me.invincible = False pygame.time.set_timer(invincible_event, 0) # 触发补给事件 elif event.type == bullet_time_supply: # 随机进行一次判断,如果是True,就发放强化子弹补给 if choice([True, False]): # 发放强化子弹补给 bullet_supply.reset() else: # 发放超级炸弹补给 bomb_supply.reset() # 增强子弹buff到时事件 elif event.type == bulletpro_time: # 子弹切换为一般子弹 is_double_bullet = False # 事件进行循环,期待下一次触发 pygame.time.set_timer(bulletpro_time, 0) # 捕捉按键操作 elif event.type == KEYDOWN: # 如果按下的是空格,触发大招,超级炸弹清空屏幕内的飞机 if event.key == K_SPACE: if bomb_num > 0: bomb_num -= 1 # 判断所有在场的敌机,是否在游戏屏幕内 for ei in enemiesGroup: if ei.rect.bottom > 0: ei.active = False # 鼠标挪动事件 elif event.type == MOUSEMOTION: # 鼠标移入到暂停的矩形框内,检测鼠标是否在矩形里,是则返回True,否则返回False ; pos – 就是鼠标地位 if paused_rect.collidepoint(event.pos): # 如果是暂停状态 if paused: paused_image = resume_pressed_image else: paused_image = pause_pressed_image else: if paused: paused_image = resume_nor_image else: paused_image = pause_nor_image # 鼠标左键点击暂停按钮 elif event.type == MOUSEBUTTONDOWN: # 如果是左键而且鼠标点击地位在暂停框内 if event.button == 1 and paused_rect.collidepoint(event.pos): # 切换暂停状态 paused = not paused # 如果点击暂停按钮后,游戏是暂停状态 if paused: # 绘制暂停按钮图片 paused_image = resume_pressed_image # 暂停补给定时器 pygame.time.set_timer(bullet_time_supply, 0) # 如果点击暂停按钮后,游戏是持续状态 else: # 绘制持续按钮图片 paused_image = pause_pressed_image # 持续补给定时器 pygame.time.set_timer(bullet_time_supply, 8 * 1000) # 检测用户键盘操作,别离为上下左右 key_pressed = pygame.key.get_pressed() if key_pressed[K_w] or key_pressed[K_UP]: me.moveUp() if key_pressed[K_s] or key_pressed[K_DOWN]: me.moveDown() if key_pressed[K_a] or key_pressed[K_LEFT]: me.moveLeft() if key_pressed[K_d] or key_pressed[K_RIGHT]: me.moveRight() # 在屏幕下面绘制背景图像,并指定地位 screen.blit(background, (0, 0)) # 绘制子弹补给、炸弹补给、敌机、玩家飞机等等各种元素 # 未暂停且生命大于0 if paused == False and life_num > 0: # 绘制子弹补给,如果子弹补给状态为真,即触发子弹补给操作 if bullet_supply.active: # 子弹开始静止,并且渲染子弹子弹补给图片 bullet_supply.move() screen.blit(bullet_supply.image, bullet_supply.rect) # 着落过程中如果跟玩家飞机碰撞,阐明玩家飞机拾取到子弹补给 # pygame.sprite.collide_mask:两个精灵之间的像素遮罩检测,接管两个精灵作为参数,返回值是一个bool变量 if pygame.sprite.collide_mask(bullet_supply, me): # 扭转子弹补给状态,包含暗藏以及不能再拾取了 bullet_supply.active = False # 飞机子弹变为增强子弹 is_double_bullet = True # 设置增强子弹buff继续事件,这里继续4s pygame.time.set_timer(bulletpro_time, 4 * 1000) # 绘制超级炸弹补给,如果超级炸弹状态为真,即触发超级炸弹补给操作 if bomb_supply.active: # 超级炸弹补给开始静止,并且渲染补给图片 bomb_supply.move() screen.blit(bomb_supply.image, bomb_supply.rect) # 玩家飞机拾取到补给 if pygame.sprite.collide_mask(bomb_supply, me): # 扭转补给状态,包含暗藏以及不能再拾取了 bomb_supply.active = False # 判断超级炸弹数量,不能大于3 if bomb_num < 3: bomb_num += 1 # 绘制小型敌机,这里是因为下面定义了小型飞机组,飞机组add了小型飞机属性(速度、地位等),这时候地图上就生成了飞机 # 如果这些飞机属于小型敌机类,即一起解决 for ei in small_enemies: # 敌机是活的,未被击毁 if ei.active == True: # 绘制小型敌机,并且敌机开始静止 screen.blit(ei.image, ei.rect) ei.samll_enemy_move() # 小型敌机被捣毁(被玩家击毁或者与玩家碰撞) else: # 这里设置delay % 4是指爆炸画面为4帧(集体猜想),了解为爆炸停留时间,可自行设置 if not (delay % 4): # 用于播放爆炸声音,每一架敌机只有一次 if e1_destory_index == 0: print("播放敌机爆炸声音") # 绘制敌机撞击爆炸画面 screen.blit(ei.destory_images[e1_destory_index], ei.rect) # 切换爆炸图片下标,从而切换爆炸图片 e1_destory_index = (e1_destory_index + 1) % 4 # 经验完一轮爆炸的敌机,能够将其销毁,也能够新生,都是不能不解决,不然会始终爆炸、爆炸 # 这里抉择将其新生 if e1_destory_index == 0: ei.reset() score += 100 # 做碰撞检测,pygame.sprite.spritecollide(sprite,sprite_group,bool):一个组中的所有精灵都会一一地对另外一个单个精灵进行冲突检测,发生冲突的精灵会作为一个列表返回。 # 第一个参数就是单个精灵,第二个参数是精灵组,第三个参数是一个bool值,最初这个参数起了很大的作用。当为True的时候,会删除组中所有抵触的精灵,False的时候不会删除抵触的精灵 # 第四个参数是:两个精灵之间的像素遮罩检测 enemy_collide = pygame.sprite.spritecollide(me, enemiesGroup, False, pygame.sprite.collide_mask) # 碰撞解决,如果不是无敌状态下产生碰撞 if enemy_collide and not me.invincible: # 玩家飞机触发捣毁状态 me.active = False # enemy_collide是一个列表,存储所有跟玩家飞机产生碰撞的敌机,而后把碰撞的敌机状态置为捣毁状态 for ei in enemy_collide: ei.active = False # 绘制玩家飞机,如果飞机为激活状态 if me.active: # 在屏幕上绘制玩家飞机,switch_image为是否切换图片 if switch_image: screen.blit(me.image1, me.rect) # 切换一下航行图片 else: screen.blit(me.image2, me.rect) # 代表飞机受到碰撞,激活爆炸事件 else: if not (delay % 4): # 用于播放爆炸声音,每一架敌机只有一次 if me_destory_index == 0: print("玩家飞机爆炸声音") # 绘制玩家撞击爆炸画面 screen.blit(me.destory_image[me_destory_index], me.rect) # 切换爆炸图片下标,从而切换爆炸图片 me_destory_index = (me_destory_index + 1) % 4 # 爆炸画面播放完之后飞机新生 if me_destory_index == 0: # 生命减一条,如果见到0,会主动跳过上一级循环 life_num -= 1 # 重置状态 me.reset() # 无敌工夫设置为3秒,3秒后,触发无敌工夫事件,pygame.time.set_timer:就是每隔一段时间(这里是3毫秒 * 1000 = 3s),去执行一些动作 pygame.time.set_timer(invincible_event, 3 * 1000) # 每10个单位工夫发射一颗子弹 if not(delay % 10): # 如果是一般子弹 if is_double_bullet == False: bullets = bullet1s # 先定子弹0的地位 bullets[bullet1s_index].reset(me.rect.midtop) # 切换到下一颗子弹 bullet1s_index = (bullet1s_index + 1) % bullet1_num # 如果是超级子弹 else: # 则子弹切换为增强子弹 bullets = bulletspro # 左右增强子弹重置地位,逻辑其实跟一般子弹一样的,只是地位变了 bullets[bulletspro_index].reset((me.rect.centerx - 33, me.rect.centery)) bullets[bulletspro_index + 1].reset((me.rect.centerx + 33, me.rect.centery)) # 切换到下一组子弹 bulletspro_index = (bulletspro_index + 2) % bulletspro_num # 绘制子弹 for bul in bullets: if bul.active: # 子弹如果是激活状态的话,就能够挪动加绘制了 bul.move() screen.blit(bul.image, bul.rect) # 子弹与敌机的碰撞,子弹与敌机组之间的碰撞,失常状况下其实就是1对1的碰撞 enemy_hit = pygame.sprite.spritecollide(bul, enemiesGroup, False, pygame.sprite.collide_mask) # 如果存在被子弹击中的敌机 if enemy_hit: # 击中敌机的子弹先标为未激活状态,下一次循环到这个子弹的时候其实会重置的,又会显示进去 bul.active = False for ei in enemy_hit: ei.active = False # 绘制分数文字,pygame.font.Font.render():在一个新 Surface 对象上绘制文本 # pygame.font.Font.render(text, antialias, color, background=None) # text:要显示的文字 antialias: 为True时文本图像显示更润滑,为False时文本图像显示有锯齿状 # color:字体色彩 background:背景色彩(可选参数),默认为小黑屏 score_text = score_font.render('Score : %s' % str(score), True, WHITE) screen.blit(score_text, (10, 5)) # 绘制超级炸弹图片以及数量显示 bomb_text = bomb_font.render('* %d' % bomb_num, True, WHITE) bomb_text_rect = bomb_text.get_rect() screen.blit(bomb_image, (10, height - 10 - bomb_rect.height)) screen.blit(bomb_text, (bomb_rect.width + 20, height - 5 - bomb_text_rect.height)) # 绘制玩家残余生命数 if life_num > 0: for i in range(life_num): screen.blit(life_image, (width - 10 - (i+1)*life_rect.width, height - 10 - life_rect.height)) delay -= 1 if delay == 0: delay = 100 # 每5帧切换一下航行图片款式 if delay % 5 == 0: switch_image = not switch_image # 绘制暂停按钮图片 screen.blit(paused_image, paused_rect) # 更新整个待显示的 Surface 对象到屏幕上,将内存中的内容显示到屏幕上 pygame.display.flip() # 通过时钟对象指定循环频率,每秒循环60次 # 帧速率是指程序每秒在屏幕山绘制图 clock.tick(60) if __name__ == "__main__": try: main() # 服务失常退出 except SystemExit: print("游戏失常退出!") # pass疏忽谬误并持续往下运行,其实这里以及退出了 pass # 服务呈现其余的异样 except: # 间接将谬误打印进去 traceback.print_exc() pygame.quit()