这一节我们给游戏增加点额外的奖励,大多数游戏中都会有金币、装备啥的来激励玩家,在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加速器的更多相关文章

  1. pygame系列_原创百度随心听音乐播放器_完整版

    程序名:PyMusic 解释:pygame+music 之前发布了自己写的小程序:百度随心听音乐播放器的一些效果图 你可以去到这里再次看看效果: pygame系列_百度随心听_完美的UI设计 这个程序 ...

  2. pygame系列

    在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,pygame系列偷自http://www.cnblogs.com/hongten/p/hongten_pygame_install. ...

  3. pygame系列_pygame安装

    在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,所以把标题设置为pygame系列 在这篇blog中,主要描述一下我们怎样来安装pygame 可能很多人像我一样,发现了pygame是 ...

  4. pygame系列_小球完全弹性碰撞游戏

    之前做了一个基于python的tkinter的小球完全碰撞游戏: 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名称: 小球完全弹性碰撞游戏规则: 1.游戏初始化的时候,有5个不同 ...

  5. pygame系列_draw游戏画图

    说到画图,pygame提供了一些很有用的方法进行draw画图. ''' pygame.draw.rect - draw a rectangle shape draw a rectangle shape ...

  6. pygame系列_mouse鼠标事件

    pygame.mouse提供了一些方法获取鼠标设备当前的状态 ''' pygame.mouse.get_pressed - get the state of the mouse buttons get ...

  7. pygame系列_font游戏字体

    在pygame游戏开发中,一个友好的UI中,漂亮的字体是少不了的 今天就给大伙带来有关pygame中字体的一些介绍说明 首先我们得判断一下我们的pygame中有没有font这个模块 1 if not ...

  8. pygame系列_游戏窗口显示策略

    在这篇blog中,我将给出一个demo演示: 当我们按下键盘的‘f’键的时候,演示的窗口会切换到全屏显示和默认显示两种显示模式 并且在后台我们可以看到相关的信息输出: 上面给出了一个简单的例子,当然在 ...

  9. pygame系列_游戏中的事件

    先看一下我做的demo: 当玩家按下键盘上的:上,下,左,右键的时候,后台会打印出玩家所按键的数字值,而图形会随之移动 这是客观上面存在的现象. 那么啥是事件呢? 你叫我做出定义,我不知道,我只能举个 ...

随机推荐

  1. [转]JAVA实现SFTP实例

    http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888384.html 最近写的一个JAVA实现SFTP的实例: /** Created ...

  2. Nginx Java 日志切割脚本

    Nginx日志切割脚本: #!/bin/bash ########################################################################### ...

  3. 一起学Hive——使用MSCK命令修复Hive分区

    最近在使用Hive的过程中,在备份数据时,经常会使用cp或mv命令来拷贝数据,将数据拷贝到我们新建备份表的目录下面,如果不是分区表,则上面的操作之后,新建的备份表可以正常使用,但是如果是分区表的,一般 ...

  4. net core体系-web应用程序-4net core2.0大白话带你入门-9asp.net core服务的生命周期

    asp.net core服务的生命周期   Transient:每一次GetService都会创建一个新的实例 Scoped:在同一个Scope内只初始化一个实例 ,可以理解为( 每一个request ...

  5. redis 配置命令

    Redis:是一个key/v  型数据 是nosql的一种 CAP 理论: C:多个数据节点上的数据一致: A:用户发出请求后的有限时间范围内返回结果: P:network partition,网络发 ...

  6. BZOJ1497 [NOI2006]最大获利 网络流 最小割 SAP

    原文链接http://www.cnblogs.com/zhouzhendong/p/8371052.html 题目传送门 - BZOJ1497 题意概括 有n个站要被建立. 建立第i个站的花费为pi. ...

  7. day 51 js-2 函数,对象,正则 (定时器示例)

    本文转载自cnblogs.liwenzhou-----哪吒博客 先来一个定时器让我们看看函数的效果: <script src="/js/jquery-3.2.1.min.js" ...

  8. Python编程基础(一)

    1.Python中的变量赋值不需要类型声明 2.等号(=)用来给变量赋值 3.字符串拼接用  “+”  号 temp=‘123’ print('temp的值是%s'%temp) #整数和字符创的转换, ...

  9. 记录pageHelper分页orderby的坑

    pageHelper的count查询会过滤查询sql中的order by条件! pageHelper分页功能很强大,如果开启count统计方法,在你执行查询条件时会再执行一条selet count(* ...

  10. box-shadow阴影 三面显示

    想弄个只显示三面的阴影效果,网上一搜没有解决根本问题,最后还是在css3演示里面找到方法http://www.css88.com/tool/css3Preview/Box-Shadow.html 我把 ...