猛禽奔跑游戏代码解读
这是一个基于ARCADE MakeCode的猛禽奔跑游戏,类似于经典的跑酷游戏。
1. 枚举和自定义精灵类型
python
- class ActionKind(Enum):
-
- Walking = 0
-
- Idle = 1
-
- Jumping = 2
-
- Dead = 3
-
-
-
- @namespace
-
- class SpriteKind:
-
- Ground = SpriteKind.create()
-
- Cloud = SpriteKind.create()
复制代码
定义了猛禽的四种动作状态:行走、空闲、跳跃、死亡
创建了两种自定义精灵类型:地面和云朵
2. 初始化地面
python
- def initGround():
-
- global ground1, ground2
-
- # 创建两个地面精灵
-
- ground1 = sprites.create(...)
-
- ground2 = sprites.create(...)
-
- # 设置位置和速度
-
- ground1.set_position(scene.screen_width() / 2, 120)
-
- ground2.set_position(ground1.x + scene.screen_width(), 120)
-
- ground1.vx = -100 # 向左移动
-
- ground2.vx = -100
-
- ground1.z = 2 # 设置图层
-
- ground2.z = 2
复制代码
创建了两个连续的地面,形成无限循环的效果
地面以恒定速度向左移动
3. 初始化猛禽角色
python
- def initRaptor():
-
- global raptor, run, jump, dead
-
- raptor = sprites.create(...) # 创建猛禽精灵
-
-
-
- # 创建行走动画
-
- run = animation.create_animation(ActionKind.Walking, 100)
-
- run.add_animation_frame(...) # 添加动画帧
-
- run.add_animation_frame(...)
-
-
-
- # 创建跳跃动画
-
- jump = animation.create_animation(ActionKind.Jumping, 200)
-
- jump.add_animation_frame(...)
-
-
-
- # 创建死亡动画
-
- dead = animation.create_animation(ActionKind.Dead, 200)
-
- dead.add_animation_frame(...)
-
-
-
- raptor.z = 3 # 设置图层
-
- raptor.set_position(15, 94) # 设置初始位置
复制代码
创建了猛禽角色和三种动画状态
设置了角色的初始位置和图层
4. 碰撞检测
python
- def on_on_overlap(sprite, otherSprite):
-
- global end
-
- end = 1
-
- animation.set_action(raptor, ActionKind.Dead) # 播放死亡动画
-
- pause(50)
-
- game.over(False, effects.dissolve) # 游戏结束
-
-
-
- sprites.on_overlap(SpriteKind.player, SpriteKind.projectile, on_on_overlap)
复制代码
当猛禽与障碍物碰撞时触发游戏结束
播放死亡动画后显示游戏结束画面
5. 跳跃控制
python
- def on_button_pressed():
-
- if raptor.y == 94 and end == 0: # 确保在地面上且游戏未结束
-
- raptor.vy = -160 # 设置向上的速度
-
- animation.set_action(raptor, ActionKind.Jumping) # 播放跳跃动画
-
-
-
- controller.any_button.on_event(ControllerButtonEvent.PRESSED, on_button_pressed)
复制代码
按下任意按钮时触发跳跃
设置垂直速度并播放跳跃动画
6. 游戏初始化
python
- game.set_dialog_cursor(...) # 设置对话框光标
-
- game.splash("Raptor Run") # 显示游戏标题
-
- scene.set_background_color(1) # 设置背景颜色
-
- initGround() # 初始化地面
-
- initRaptor() # 初始化猛禽
-
- info.set_score(0) # 初始化分数
-
- end = 0 # 游戏结束标志
-
- game.show_long_text("Press any button to jump.", DialogLayout.TOP) # 显示操作提示
复制代码
7. 物理更新
python
- def on_on_update():
-
- if raptor.y < 94: # 如果在空中
-
- raptor.ay = 400 # 应用重力
-
- else:
-
- raptor.ay = 0 # 取消重力
-
- raptor.y = 94 # 固定在地面高度
-
- if end == 0: # 如果游戏未结束
-
- animation.set_action(raptor, ActionKind.Walking) # 播放行走动画
-
-
-
- game.on_update(on_on_update)
复制代码
处理猛禽的重力物理效果
确保猛禽落回地面时恢复行走状态
8. 分数系统
python
- def on_update_interval():
-
- info.change_score_by(1) # 每50毫秒增加1分
-
-
-
- game.on_update_interval(50, on_update_interval)
复制代码
随着游戏进行,分数不断增加
9. 障碍物生成
python
- def on_update_interval2():
-
- global choice, cactus
-
- choice = randint(0, 4) # 随机选择障碍物类型
-
- if choice == 0:
-
- cactus = sprites.create_projectile_from_side(..., ground1.vx, 0)
-
- cactus.y = 94 # 设置在地面高度
-
- cactus.z = 2 # 设置图层
-
- # 其他障碍物类型...
-
-
-
- game.on_update_interval(1000, on_update_interval2)
复制代码
每隔1秒随机生成一种障碍物
障碍物与地面同速向左移动
10. 游戏难度递增
python
- def on_update_interval3():
-
- ground1.vx += -1 # 逐渐加快地面速度
-
- ground2.vx += -1 # 增加游戏难度
-
-
-
- game.on_update_interval(1000, on_update_interval3)
复制代码
随着时间的推移,地面速度逐渐增加
使游戏难度随时间递增
11. 云朵生成
python
- def on_update_interval4():
-
- global cloud
-
- if Math.percent_chance(40): # 40%的概率生成云朵
-
- cloud = sprites.create_projectile_from_side(..., ground1.vx / 4, 0)
-
- cloud.y = randint(20, 60) # 随机高度
-
- cloud.set_kind(SpriteKind.Cloud) # 设置为云朵类型
-
- cloud.z = 1 # 设置图层(在背景但在地面前面)
-
-
-
- game.on_update_interval(1500, on_update_interval4)
复制代码
每隔1.5秒有40%的概率生成云朵
云朵移动速度较慢,营造景深效果
12. 地面循环
python
- def on_forever():
-
- if ground2.x < -1 * (scene.screen_width() / 2):
-
- ground2.x = ground1.x + scene.screen_width() # 重置地面位置
-
-
-
- def on_forever2():
-
- if ground1.x < -1 * (scene.screen_width() / 2):
-
- ground1.x = ground2.x + scene.screen_width() # 重置地面位置
复制代码
确保两个地面精灵无缝循环
当一个地面移出屏幕时,将其重置到另一个地面的右侧。
|