pygame-KidsCanCode系列jumpy-part15-PowerUp加速器
这一节我们给游戏增加点额外的奖励,大多数游戏中都会有金币、装备啥的来激励玩家,在jumpy这个游戏中,我们也可以增加类似的道具:加速器。效果图如下:

档板上会随机出现一些加速器(power up),小兔子碰到它后,会获取额外的跳跃速度,跳得更高,得分自然也更高。实现原理如下:
首先得有一个PowerUp类:
# PowerUp 加速器
class PowerUp(pg.sprite.Sprite):
def __init__(self, game, plat):
pg.sprite.Sprite.__init__(self)
self.game = game
self.plat = plat
self.current_frame = 0
self.last_update = 0
self.images = [self.game.spritesheet.get_image("powerup_empty.png"),
self.game.spritesheet.get_image("powerup_jetpack.png")]
self.image = random.choice(self.images)
self.rect = self.image.get_rect()
# 停在档板的中间
self.rect.centerx = self.plat.rect.centerx
self.rect.bottom = self.plat.rect.top - 5 def update(self):
self.animate()
self.rect.bottom = self.plat.rect.top - 5
if not self.game.platforms.has(self.plat):
self.kill() def animate(self):
now = pg.time.get_ticks()
if now - self.last_update > 200:
self.last_update = now
self.current_frame += 1
self.image = self.images[self.current_frame % len(self.images)]
self.rect = self.image.get_rect()
self.rect.bottom = self.plat.rect.top - 5
self.rect.centerx = self.plat.rect.centerx
大致就是轮播2个图片,形成动画,并停在档板的中间位置。
然后在Platform类中,随机出现这些加速器:
class Platform(pg.sprite.Sprite):
def __init__(self, game, x, y):
pg.sprite.Sprite.__init__(self)
self.game = game
images = [self.game.spritesheet.get_image("ground_grass_broken.png"),
self.game.spritesheet.get_image("ground_grass_small_broken.png")]
self.image = random.choice(images)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 随机出现power-up加速器
if random.randrange(100) < BOOST_POWER_PERCENT:
power = PowerUp(self.game, self)
self.game.all_sprites.add(power)
self.game.powerups.add(power)
其中常量BOOST_POWER_PERCENT是定义在settings.py中的,代表随机出现的概率。
# power up
BOOST_POWER = 50
BOOST_POWER_PERCENT = 15
另一个常量BOOST_POWER代表小兔子碰到加速器后,获取的额外跳跃速度,下面马上会用到。
main.py的update函数中,加入powerup的碰撞检测:
def update(self):
self.all_sprites.update()
...
if self.player.rect.top < HEIGHT / 4:
self.player.pos.y += max(abs(self.player.vel.y), 2)
for plat in self.platforms:
plat.rect.top += max(abs(self.player.vel.y), 2)
if plat.rect.top > HEIGHT:
plat.kill()
self.score += 10 # 检测power up碰撞
pow_hits = pg.sprite.spritecollide(self.player, self.powerups, True)
for _ in pow_hits:
self.boost_sound.play()
self.player.vel.y = -BOOST_POWER
self.player.jumping = False ...
当然,还可以参考上节的方法,加上音效处理,就不再赘述了。代码可参考:https://github.com/yjmyzz/kids-can-code/tree/master/part_15
pygame-KidsCanCode系列jumpy-part15-PowerUp加速器的更多相关文章
- pygame系列_原创百度随心听音乐播放器_完整版
		程序名:PyMusic 解释:pygame+music 之前发布了自己写的小程序:百度随心听音乐播放器的一些效果图 你可以去到这里再次看看效果: pygame系列_百度随心听_完美的UI设计 这个程序 ... 
- pygame系列
		在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,pygame系列偷自http://www.cnblogs.com/hongten/p/hongten_pygame_install. ... 
- pygame系列_pygame安装
		在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,所以把标题设置为pygame系列 在这篇blog中,主要描述一下我们怎样来安装pygame 可能很多人像我一样,发现了pygame是 ... 
- pygame系列_小球完全弹性碰撞游戏
		之前做了一个基于python的tkinter的小球完全碰撞游戏: 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名称: 小球完全弹性碰撞游戏规则: 1.游戏初始化的时候,有5个不同 ... 
- pygame系列_draw游戏画图
		说到画图,pygame提供了一些很有用的方法进行draw画图. ''' pygame.draw.rect - draw a rectangle shape draw a rectangle shape ... 
- pygame系列_mouse鼠标事件
		pygame.mouse提供了一些方法获取鼠标设备当前的状态 ''' pygame.mouse.get_pressed - get the state of the mouse buttons get ... 
- pygame系列_font游戏字体
		在pygame游戏开发中,一个友好的UI中,漂亮的字体是少不了的 今天就给大伙带来有关pygame中字体的一些介绍说明 首先我们得判断一下我们的pygame中有没有font这个模块 1 if not ... 
- pygame系列_游戏窗口显示策略
		在这篇blog中,我将给出一个demo演示: 当我们按下键盘的‘f’键的时候,演示的窗口会切换到全屏显示和默认显示两种显示模式 并且在后台我们可以看到相关的信息输出: 上面给出了一个简单的例子,当然在 ... 
- pygame系列_游戏中的事件
		先看一下我做的demo: 当玩家按下键盘上的:上,下,左,右键的时候,后台会打印出玩家所按键的数字值,而图形会随之移动 这是客观上面存在的现象. 那么啥是事件呢? 你叫我做出定义,我不知道,我只能举个 ... 
随机推荐
- python从零安装
			一 python 1.安装python https://www.python.org/ 环境变量path添加 ;C:\Python27;C:\Python27\Lib\site-packages;C: ... 
- ssh登陆linux服务器   实际场景讲解  让你管理服务器更安全
			很多时候我们管理linux系统,都谁使用ssh登陆,因为都知道ssh是加密传输的协议的,可以有效保证我们与 服务器之间的数据通信安全.但是我们忽略了一点,但是登陆的时候我们是输入的账号和密码,这一点其 ... 
- Hadoop Avro支持多输入AvroMultipleInputs
			Avro 提供了1.x版本的AvroMultipleInputs,但是不支持2.x API版本,因此修改对应代码,增加对hadoop 2.x API版本的的支持 代码放在https://github. ... 
- ansible基础命令实例
			参考:https://www.cnblogs.com/ilurker/p/6421624.html 1. 使用自定义的hosts 格式: ansible 组机匹配 -i 自定义的hosts - ... 
- NOI2018Day2T1 屠龙勇士 set  扩展欧几里德 中国剩余定理
			原文链接https://www.cnblogs.com/zhouzhendong/p/NOI2018Day2T1.html 题目传送门 - 洛谷P4774 题意 题解 首先我们仔细看一看样例可以发现如 ... 
- ELK安装(windows)
			一.安装JAVA环境 在Oracle官网获取最新版的Java版本,官网:http://www.oracle.com/ 安装完成后,配置JAVA_HOME和JRE_HOME. 二.下载安装ELK htt ... 
- 解决每次调试网页,eclipse总是提示edit source lookup path的问题,我的第一篇小随笔,小激动呢
			如图,很简单,只要把想要debug的项目勾上就行,网页调试时,就会自动去找项目文件位置 
- linux同步Internet时间
			输入ntpdate time.nist.gov同步网络时间 如果未安装:yum install ntpdate 结果:3 Jun 15:42:39 ntpdate[4721]: adjust time ... 
- SVM:SVM之Classification根据已有大量数据集案例,输入已有病例的特征向量实现乳腺癌诊断高准确率预测—Jason niu
			load BreastTissue_data.mat n = randperm(size(matrix,1)); train_matrix = matrix(n(1:80),:); train_lab ... 
- goLand工程结构管理
			goLand工程结构管理 转 https://www.jianshu.com/p/eb7b1fd7179e 开始之前请确保安装好了 go语言环境并配置好了gopath环境变量 1.创建一个新目录并打 ... 
