Python之路,200行Python代码实现飞机大战游戏(建议收藏)

1. 绘制敌机

随机是游戏中一个很重要的元素,不可预测的机制为游戏带来了更丰富的体验。这次我们要在程序中加入随机数,两行代码:

# 导入random库中的randint函数
from random import randint
# 返回一个整数N, a<=N<=b
N = randint(a, b)

这样我们就可以使得敌机每次出现的位置变得不可预测了~(。・ω・。)

跟之前的风格类似,我们把敌机封装成类,主要是为了能够更方便地使用碰撞检测的功能。

 1 # 敌人类
 2 class Enemy(pygame.sprite.Sprite): 3     def \_\_init\_\_(self, enemy\_surface, enemy\_init\_pos):
 4         pygame.sprite.Sprite.\_\_init\_\_(self)            
 5         self.image = enemy\_surface 6         self.rect = self.image.get\_rect() 7         self.rect.topleft = enemy\_init\_pos 8         self.speed = 2
 9 
10     def update(self):
11         self.rect.top += self.speed
12         if self.rect.top > SCREEN\_HEIGHT:
13             self.kill()

依然是超出屏幕区域自动销毁对象,最后就是创建敌人对象并在屏幕上绘制出来:

 1 ...
 2 # enemy1图片 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 3 enemy1\_surface = shoot\_img.subsurface(pygame.Rect(534, 612, 57, 43))
 4 # \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 5 ...
 6 
 7 # 事件循环(main loop)
 8 while True: 9 
10 ...
11 
12     # 产生敌机 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
13     if ticks % 30 == 0:
14         enemy = Enemy(enemy1\_surface, \[randint(0, SCREEN\_WIDTH - enemy1\_surface.get\_width()), -enemy1\_surface.get\_height()\])
15 enemy\_group.add(enemy)
16     # 控制敌机
17 enemy\_group.update()
18     # 绘制敌机
19 enemy\_group.draw(screen)
20     # \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
21         
22     ...

导入图片资源当然是必不可少的啦;我们使用ticks控制敌人产生的频率,每30ticks产生一架新敌机,然后将敌机对象加入一个group,统一操作,每一tick更新一次全体enemy的位置。现在绘制的任务就完成啦~看一下效果:

虽然enemy绘制出来了,但是现在出现了两个问题;第一,子弹无法击中敌人;第二,敌人无法击毁玩家飞机。下面我们先来解决第一个问题。

2. 我们的子弹击穿了敌人的铠甲!

说了那么久,终于说到了“碰撞检测”,游戏中的碰撞检测应用范围很广,不过在pygame中,碰撞检测(collide)的机制其实很简单,就是判断sprite1.rect与sprite2.rect是否重叠。那么在pygame.sprite中,我们可以看到一些函数名中包含collide的函数,这些函数一般是用于检测碰撞的,我们可以大体分为sprite与sprite的碰撞检测,sprite与group的碰撞检测,group与group的碰撞检测。回归问题,子弹是一个group,敌人是一个group,那我们在游戏中检测子弹是否击中了敌人很明显需要用group与group的碰撞检测了~

pygame.sprite.groupcollide()——检测两个group之间所有sprite的碰撞

groupcollide(group1, group2, dokill1, dokill2, collided = None) -> Sprite_dict

group1——精灵组1

group2——精灵组2

dokill1——是否杀死发生碰撞时group1中的精灵对象

dokill2——是否杀死发生碰撞时group2中的精灵对象

collided——可选参数,可自定义一个回调函数,参数为两个精灵对象,用于自定义两个精灵是否发生碰撞,返回bool值;若忽略此参数,则默认碰撞条件为两个精灵的rect发生重叠

返回一个包含所有group1中与group2发生碰撞的精灵字典(dict)

现在,我们只需要在程序中加入两行代码:

...

# 创建击毁敌人组
enemy1\_down\_group = pygame.sprite.Group()

# 事件循环(main loop)
while True:

    ...

    # 检测敌机与子弹的碰撞 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
    enemy1\_down\_group.add(pygame.sprite.groupcollide(enemy1\_group, hero.bullets1, True, True))
      
  ...

创建一个包含被击毁的敌人的group,然后在每一tick中检测一次是否发生碰撞,再将被击毁的敌人加入这个group,方便后续对坠毁敌机的动画渲染;这样第二部分也完成啦~

3. 华丽的坠毁(`・ω・´)

现在我们已经实现子弹与敌机的碰撞检测了,但凭空消失是在不咋的,我们来一个华丽一点的爆炸!(°∀°)ノ

首先导入enemy1爆炸的资源图片

1 enemy1\_down\_surface = \[\]
2 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(267, 347, 57, 43)))
3 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(873, 697, 57, 43)))
4 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(267, 296, 57, 43)))
5 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(930, 697, 57, 43)))

然后就是控制爆炸图片切换的速度了,在主循环中加入:

1 for enemy1\_down in enemy1\_down\_group:
2 screen.blit(enemy1\_down\_surface\[enemy1\_down.down\_index\], enemy1\_down.rect)
3         if ticks % (ANIMATE\_CYCLE//2) == 0:
4             if enemy1\_down.down\_index < 3:
5                 enemy1\_down.down\_index += 1
6             else:
7                 enemy1\_down\_group.remove(enemy1\_down)

当index超出图片下标,判断为爆炸效果演示完毕,销毁坠毁的enemy1精灵。

这样爆炸效果就出来啦~~

这一节任务完成!~附上完整代码:

  1 # -\*- coding = utf-8 -\*-
  2 """
  3 @author: Will Wu
  4 """
  5 
  6 import pygame                   # 导入pygame库
  7 from pygame.locals import \*     # 导入pygame库中的一些常量
  8 from sys import exit            # 导入sys库中的exit函数
  9 from random import randint 10 
 11 # 定义窗口的分辨率
 12 SCREEN\_WIDTH = 480
 13 SCREEN\_HEIGHT = 640
 14 
 15 # 子弹类
 16 class Bullet(pygame.sprite.Sprite): 17 
 18     def \_\_init\_\_(self, bullet\_surface, bullet\_init\_pos):
 19         pygame.sprite.Sprite.\_\_init\_\_(self)            
 20         self.image = bullet\_surface 21         self.rect = self.image.get\_rect() 22         self.rect.topleft = bullet\_init\_pos 23         self.speed = 8
 24 
 25     # 控制子弹移动
 26     def update(self): 27         self.rect.top -= self.speed 28         if self.rect.bottom < 0: 29             self.kill()
 30             
 31 
 32 # 玩家类
 33 class Hero(pygame.sprite.Sprite): 34     
 35     def \_\_init\_\_(self, hero\_surface, hero\_init\_pos):
 36         pygame.sprite.Sprite.\_\_init\_\_(self)            
 37         self.image = hero\_surface 38         self.rect = self.image.get\_rect() 39         self.rect.topleft = hero\_init\_pos 40         self.speed = 6
 41 
 42         # 子弹1的Group
 43         self.bullets1 = pygame.sprite.Group() 44 
 45     # 控制射击行为
 46     def single\_shoot(self, bullet1\_surface): 47         bullet1 = Bullet(bullet1\_surface, self.rect.midtop) 48         self.bullets1.add(bullet1)
 49 
 50     # 控制飞机移动
 51     def move(self, offset): 52         x = self.rect.left + offset\[pygame.K\_RIGHT\] - offset\[pygame.K\_LEFT\] 53         y = self.rect.top + offset\[pygame.K\_DOWN\] - offset\[pygame.K\_UP\] 54         if x < 0: 55             self.rect.left = 0 56         elif x > SCREEN\_WIDTH - self.rect.width: 57             self.rect.left = SCREEN\_WIDTH - self.rect.width 58         else:
 59             self.rect.left = x 60             
 61         if y < 0: 62             self.rect.top = 0 63         elif y > SCREEN\_HEIGHT - self.rect.height: 64             self.rect.top = SCREEN\_HEIGHT - self.rect.height 65         else:
 66             self.rect.top = y 67 
 68 # 敌人类
 69 class Enemy(pygame.sprite.Sprite): 70     def \_\_init\_\_(self, enemy\_surface, enemy\_init\_pos):
 71         pygame.sprite.Sprite.\_\_init\_\_(self)            
 72         self.image = enemy\_surface 73         self.rect = self.image.get\_rect() 74         self.rect.topleft = enemy\_init\_pos 75         self.speed = 2
 76 
 77         # 爆炸动画画面索引
 78         self.down\_index = 0 79 
 80     def update(self): 81         self.rect.top += self.speed 82         if self.rect.top > SCREEN\_HEIGHT: 83             self.kill()
 84         
 85 ###########################################################################
 86 
 87 # 定义画面帧率
 88 FRAME\_RATE = 60
 89 
 90 # 定义动画周期(帧数)
 91 ANIMATE\_CYCLE = 30
 92 
 93 ticks = 0 94 clock = pygame.time.Clock() 95 offset = {pygame.K\_LEFT:0, pygame.K\_RIGHT:0, pygame.K\_UP:0, pygame.K\_DOWN:0} 96 
 97           
 98 # 初始化游戏
 99 pygame.init()                   # 初始化pygame
100 screen = pygame.display.set\_mode(\[SCREEN\_WIDTH, SCREEN\_HEIGHT\])     # 初始化窗口
101 pygame.display.set\_caption('This is my first pygame-program')       # 设置窗口标题
102 
103 # 载入背景图
104 background = pygame.image.load('resources/image/background.png')
105 
106 # 载入资源图片
107 shoot\_img = pygame.image.load('resources/image/shoot.png')
108 
109 # 用subsurface剪切读入的图片
110 # Hero图片
111 hero\_surface = \[\]
112 hero\_surface.append(shoot\_img.subsurface(pygame.Rect(0, 99, 102, 126)))
113 hero\_surface.append(shoot\_img.subsurface(pygame.Rect(165, 360, 102, 126)))
114 #hero\_surface.append(shoot\_img.subsurface(pygame.Rect(165, 234, 102, 126)))     
115 #hero\_surface.append(shoot\_img.subsurface(pygame.Rect(330, 624, 102, 126)))
116 #hero\_surface.append(shoot\_img.subsurface(pygame.Rect(330, 498, 102, 126)))
117 #hero\_surface.append(shoot\_img.subsurface(pygame.Rect(432, 624, 102, 126)))
118 hero\_pos = \[200, 500\]
119 
120 # bullet1图片
121 bullet1\_surface = shoot\_img.subsurface(pygame.Rect(1004, 987, 9, 21))
122 
123 # enemy1图片 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
124 enemy1\_surface = shoot\_img.subsurface(pygame.Rect(534, 612, 57, 43))
125 enemy1\_down\_surface = \[\]
126 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(267, 347, 57, 43)))
127 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(873, 697, 57, 43)))
128 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(267, 296, 57, 43)))
129 enemy1\_down\_surface.append(shoot\_img.subsurface(pygame.Rect(930, 697, 57, 43)))
130 # \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
131 
132 # 创建玩家
133 hero = Hero(hero\_surface\[0\], hero\_pos)
134 
135 # 创建敌人组
136 enemy1\_group = pygame.sprite.Group()
137 
138 # 创建击毁敌人组
139 enemy1\_down\_group = pygame.sprite.Group()
140 
141 # 事件循环(main loop)
142 while True:
143 
144     # 控制游戏最大帧率
145 clock.tick(FRAME\_RATE)
146 
147     # 绘制背景
148 screen.blit(background, (0, 0))
149 
150     # 改变飞机图片制造动画
151     if ticks >= ANIMATE\_CYCLE:
152         ticks = 0
153     hero.image = hero\_surface\[ticks//(ANIMATE\_CYCLE//2)\]
154 
155     # 射击
156     if ticks % 10 == 0:
157 hero.single\_shoot(bullet1\_surface)
158     # 控制子弹
159 hero.bullets1.update()
160     # 绘制子弹
161 hero.bullets1.draw(screen)
162 
163     # 产生敌机 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
164     if ticks % 30 == 0:
165         enemy = Enemy(enemy1\_surface, \[randint(0, SCREEN\_WIDTH - enemy1\_surface.get\_width()), -enemy1\_surface.get\_height()\])
166 enemy1\_group.add(enemy)
167     # 控制敌机
168 enemy1\_group.update()
169     # 绘制敌机
170 enemy1\_group.draw(screen)
171     # \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
172 
173     # 检测敌机与子弹的碰撞 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
174 enemy1\_down\_group.add(pygame.sprite.groupcollide(enemy1\_group, hero.bullets1, True, True))
175     
176     for enemy1\_down in enemy1\_down\_group:
177 screen.blit(enemy1\_down\_surface\[enemy1\_down.down\_index\], enemy1\_down.rect)
178         if ticks % (ANIMATE\_CYCLE//2) == 0:
179             if enemy1\_down.down\_index < 3:
180                 enemy1\_down.down\_index += 1
181             else:
182 enemy1\_down\_group.remove(enemy1\_down)
183     # \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
184         
185     # 绘制飞机
186 screen.blit(hero.image, hero.rect)
187     ticks += 1 # python已略去自增运算符
188 
189     # 更新屏幕
190 pygame.display.update()                                         
191     
192     # 处理游戏退出
193     # 从消息队列中循环取
194     for event in pygame.event.get():
195         if event.type == pygame.QUIT:
196 pygame.quit()
197 exit()
198 
199         # ※ Python中没有switch-case 多用字典类型替代
200         # 控制方向       
201         if event.type == pygame.KEYDOWN:
202             if event.key in offset:
203                 offset\[event.key\] = hero.speed
204         elif event.type == pygame.KEYUP:
205             if event.key in offset:
206                 offset\[event.key\] = 0
207 
208     # 移动飞机
209     hero.move(offset)

最后这里给大家免费分享一份Python学习资料,包含了视频、源码、课件,希望能够帮助到那些不满现状,想提示自己却又没用方向的朋友,也可以和我一起来交流呀!

编辑资料、学习路线图、源代码、软件安装包等!【点击这里】领取!
在这里插入图片描述

  • 18
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值