ARCADE MakeCode 之穿越鸭子游戏代码解读
这是一个类似Flappy Bird的躲避游戏,玩家控制一只鸭子穿越障碍物。
代码结构分析
1. 动作枚举定义
python
- class ActionKind(Enum):
-
- Walking = 0
-
- Idle = 1
-
- Jumping = 2
复制代码
定义了三种动画状态:行走、空闲和跳跃。
2. 自定义精灵类型
python
- @namespace
-
- class SpriteKind:
-
- Gap = SpriteKind.create()
复制代码
创建了一个新的精灵类型"Gap",用于表示障碍物之间的空隙。
3. 碰撞检测事件
python
- def on_on_overlap(sprite, otherSprite):
-
- if otherSprite.right - sprite.left < 2:
-
- info.change_score_by(1)
-
- sprites.on_overlap(SpriteKind.player, SpriteKind.Gap, on_on_overlap)
复制代码
当玩家精灵穿过空隙时增加分数。
python
- def on_on_overlap2(sprite2, otherSprite2):
-
- game.over(False)
-
- sprites.on_overlap(SpriteKind.player, SpriteKind.projectile, on_on_overlap2)
复制代码
当玩家碰到障碍物时游戏结束。
4. 按钮控制事件
python
- def on_button_pressed():
-
- mySprite.vy = -100 # 给鸭子一个向上的速度
-
- animation.set_action(mySprite, ActionKind.Walking) # 设置行走动画
-
- mySprite.start_effect(effects.rings, 300) # 添加环形特效
-
- controller.any_button.on_event(ControllerButtonEvent.PRESSED, on_button_pressed)
复制代码
任何按钮按下时,让鸭子跳跃并播放动画效果。
5. 游戏初始化
python
- # 变量声明
-
- projectile: Sprite = None
-
- gapSprite: Sprite = None
-
- gapImage: Image = None
-
- bottomImage: Image = None
-
- topImage: Image = None
-
- gap = 0
-
- mySprite: Sprite = None
-
-
-
- # 设置背景和初始分数
-
- scene.set_background_color(9)
-
- info.set_score(0)
-
- effects.blizzard.start_screen_effect() # 添加暴风雪屏幕特效
-
-
-
- # 创建玩家精灵(鸭子)
-
- mySprite = sprites.create(img("""
-
- . . . . . . . . . . b 5 b . . .
-
- . . . . . . . . . b 5 b . . . .
-
- . . . . . . . . . b c . . . . .
-
- . . . . . . b b b b b b . . . .
-
- . . . . . b b 5 5 5 5 5 b . . .
-
- . . . . b b 5 d 1 f 5 5 d f . .
-
- . . . . b 5 5 1 f f 5 d 4 c . .
-
- . . . . b 5 5 d f b d d 4 4 . .
-
- b d d d b b d 5 5 5 4 4 4 4 4 b
-
- b b d 5 5 5 b 5 5 4 4 4 4 4 b .
-
- b d c 5 5 5 5 d 5 5 5 5 5 b . .
-
- c d d c d 5 5 b 5 5 5 5 5 5 b .
-
- c b d d c c b 5 5 5 5 5 5 5 b .
-
- . c d d d d d d 5 5 5 5 5 d b .
-
- . . c b d d d d d 5 5 5 b b . .
-
- . . . c c c c c c c c b b . . .
-
- """), SpriteKind.player)
-
-
-
- mySprite.ay = 300 # 设置重力加速度
复制代码
6. 鸭子动画设置
python
- # 创建行走动画
-
- anim = animation.create_animation(ActionKind.Walking, 25)
-
- # 添加6个动画帧
-
- anim.add_animation_frame(img("""..."""))
-
- anim.add_animation_frame(img("""..."""))
-
- anim.add_animation_frame(img("""..."""))
-
- anim.add_animation_frame(img("""..."""))
-
- anim.add_animation_frame(img("""..."""))
-
- anim.add_animation_frame(img("""..."""))
-
-
-
- # 将动画附加到精灵
-
- animation.attach_animation(mySprite, anim)
复制代码
7. 游戏更新事件
python
- def on_on_update():
-
- if mySprite.vy > 0: # 如果鸭子正在下落
-
- animation.set_action(mySprite, ActionKind.Walking) # 设置行走动画
-
-
-
- if mySprite.bottom > 120 or mySprite.top < 0: # 如果鸭子飞出屏幕
-
- game.over(False) # 游戏结束
-
- game.on_update(on_on_update)
复制代码
8. 障碍物生成逻辑
python
- def on_update_interval():
-
- global gap, topImage, bottomImage, gapImage, gapSprite, projectile
-
-
-
- gap = randint(0, 3) # 随机选择4种障碍物配置之一
-
-
-
- # 根据gap值选择不同的上下障碍物图像
-
- if gap == 0:
-
- topImage = img("""...""")
-
- bottomImage = img("""...""")
-
- elif gap == 1:
-
- topImage = img("""...""")
-
- bottomImage = img("""...""")
-
- elif gap == 2:
-
- topImage = img("""...""")
-
- bottomImage = img("""...""")
-
- else:
-
- topImage = img("""...""")
-
- bottomImage = img("""...""")
-
-
-
- # 创建空隙精灵(用于检测通过)
-
- gapImage = image.create(2, scene.screen_height())
-
- gapImage.fill(1)
-
- gapSprite = sprites.create(gapImage, SpriteKind.Gap)
-
- gapSprite.set_flag(SpriteFlag.AUTO_DESTROY, True)
-
- gapSprite.set_flag(SpriteFlag.INVISIBLE, True) # 不可见
-
- gapSprite.left = scene.screen_width() # 从右侧进入
-
- gapSprite.vx = -45 # 向左移动速度
-
-
-
- # 创建顶部障碍物
-
- projectile = sprites.create_projectile_from_side(topImage, -45, 0)
-
- projectile.top = 0
-
-
-
- # 创建底部障碍物
-
- projectile = sprites.create_projectile_from_side(bottomImage, -45, 0)
-
- projectile.bottom = scene.screen_height()
-
-
-
- # 每1500毫秒生成一组新障碍物
-
- game.on_update_interval(1500, on_update_interval)
复制代码
游戏机制解析
控制方式:玩家按下任何按钮使鸭子跳跃
得分机制:当鸭子成功穿过障碍物之间的空隙时得分
失败条件:
鸭子碰到障碍物
鸭子飞出屏幕上下边界
障碍物生成:每隔1.5秒随机生成一组上下障碍物,中间留有可通过的空隙
视觉效果:鸭子有动画效果,跳跃时有环形特效,屏幕有暴风雪效果
这个游戏通过简单的机制创造了有趣的挑战,玩家需要掌握好跳跃时机来穿越不断出现的障碍物。
|