这一节我们给游戏增加点额外的奖励,大多数游戏中都会有金币、装备啥的来激励玩家,在jumpy这个游戏中,我们也可以增加类似的道具:加速器。效果图如下:
档板上会随机出现一些加速器(power up),小兔子碰到它后,会获取额外的跳跃速度,跳得更高,得分自然也更高。实现原理如下:
首先得有一个PowerUp类:
1 # PowerUp 加速器 2 class PowerUp(pg.sprite.Sprite): 3 def __init__(self, game, plat): 4 pg.sprite.Sprite.__init__(self) 5 self.game = game 6 self.plat = plat 7 self.current_frame = 0 8 self.last_update = 0 9 self.images = [self.game.spritesheet.get_image("powerup_empty.png"), 10 self.game.spritesheet.get_image("powerup_jetpack.png")] 11 self.image = random.choice(self.images) 12 self.rect = self.image.get_rect() 13 # 停在档板的中间 14 self.rect.centerx = self.plat.rect.centerx 15 self.rect.bottom = self.plat.rect.top - 5 16 17 def update(self): 18 self.animate() 19 self.rect.bottom = self.plat.rect.top - 5 20 if not self.game.platforms.has(self.plat): 21 self.kill() 22 23 def animate(self): 24 now = pg.time.get_ticks() 25 if now - self.last_update > 200: 26 self.last_update = now 27 self.current_frame += 1 28 self.image = self.images[self.current_frame % len(self.images)] 29 self.rect = self.image.get_rect() 30 self.rect.bottom = self.plat.rect.top - 5 31 self.rect.centerx = self.plat.rect.centerx