34浏览
查看: 34|回复: 4

[项目] 【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏

[复制链接]
【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图1

【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图2

驴友花雕  中级技神
 楼主|

发表于 前天 17:36

【花雕动手做】基于Kitronik可编程开发板经典捉人游戏

作为学习、练习与尝试,这里创建一个经典捉人的小游戏。
打开网页版:https://arcade.makecode.com/,设置项目名称:经典捉人

MicroPython实验参考代码

  1. def checkIfWon():
  2.     if len(sprites2) == 1:
  3.         if sprites2[0] == mp.get_player_sprite(mp.PlayerNumber.ONE):
  4.             game.splash("Player 1 Wins!")
  5.         elif sprites2[0] == mp.get_player_sprite(mp.PlayerNumber.TWO):
  6.             game.splash("Player 2 Wins!")
  7.         elif sprites2[0] == mp.get_player_sprite(mp.PlayerNumber.THREE):
  8.             game.splash("Player 3 Wins!")
  9.         else:
  10.             game.splash("Player 4 Wins!")
  11.         game.reset()
  12. def on_life_zero(player2):
  13.     global it
  14.     mp.get_player_sprite(player2).destroy(effects.fire, 500)
  15.     sprites2.remove_at(sprites2.index_of(mp.get_player_sprite(player2)))
  16.     if it == mp.get_player_sprite(player2):
  17.         it.say_text("", 2000, False)
  18.         it = sprites2._pick_random()
  19.         youreIt()
  20.     checkIfWon()
  21. mp.on_life_zero(on_life_zero)
  22. def youreIt():
  23.     it.say_text("It", 2000, False)
  24.     it.set_position(randint(5, 155), randint(15, 115))
  25.     it.set_kind(SpriteKind.enemy)
  26.     info.start_countdown(10)
  27. def on_countdown_end():
  28.     global it
  29.     it.set_kind(SpriteKind.player)
  30.     reduceLife(it)
  31.     it = sprites2._pick_random()
  32.     youreIt()
  33. info.on_countdown_end(on_countdown_end)
  34. def reduceLife(sprite: Sprite):
  35.     if sprite == mp.get_player_sprite(mp.PlayerNumber.ONE):
  36.         mp.change_player_state_by(mp.PlayerNumber.ONE, MultiplayerState.lives, -1)
  37.     elif sprite == mp.get_player_sprite(mp.PlayerNumber.TWO):
  38.         mp.change_player_state_by(mp.PlayerNumber.TWO, MultiplayerState.lives, -1)
  39.     elif sprite == mp.get_player_sprite(mp.PlayerNumber.THREE):
  40.         mp.change_player_state_by(mp.PlayerNumber.THREE, MultiplayerState.lives, -1)
  41.     else:
  42.         mp.change_player_state_by(mp.PlayerNumber.FOUR, MultiplayerState.lives, -1)
  43. def on_on_overlap(sprite2, otherSprite):
  44.     global it
  45.     info.stop_countdown()
  46.     it.say_text("", 2000, False)
  47.     reduceLife(sprite2)
  48.     it = sprite2
  49.     otherSprite.set_kind(SpriteKind.player)
  50.     youreIt()
  51. sprites.on_overlap(SpriteKind.player, SpriteKind.enemy, on_on_overlap)
  52. it: Sprite = None
  53. sprites2: List[Sprite] = []
  54. game.show_long_text("Once all your friends have joined, hit A to continue.",
  55.     DialogLayout.CENTER)
  56. game.show_long_text("When you're it, tag a friend and they will lose a life. Be careful, if time runs out you'll lose a life instead!",
  57.     DialogLayout.CENTER)
  58. sprites2 = [sprites.create(img("""
  59.             2 2 2 2 2 2 2 2
  60.             2 2 2 1 1 2 2 2
  61.             2 2 2 2 1 2 2 2
  62.             2 2 2 2 1 2 2 2
  63.             2 2 2 2 1 2 2 2
  64.             2 2 2 2 1 2 2 2
  65.             2 2 1 1 1 1 1 2
  66.             2 2 2 2 2 2 2 2
  67.             """),
  68.         SpriteKind.player),
  69.     sprites.create(img("""
  70.             8 8 8 8 8 8 8 8
  71.             8 8 1 1 1 1 8 8
  72.             8 8 1 8 8 1 8 8
  73.             8 8 8 8 8 1 8 8
  74.             8 8 8 8 1 8 8 8
  75.             8 8 8 1 8 8 8 8
  76.             8 8 1 1 1 1 1 8
  77.             8 8 8 8 8 8 8 8
  78.             """),
  79.         SpriteKind.player),
  80.     sprites.create(img("""
  81.             4 4 4 4 4 4 4 4
  82.             4 4 1 1 1 1 4 4
  83.             4 4 1 4 4 1 4 4
  84.             4 4 4 4 4 1 4 4
  85.             4 4 4 4 1 4 4 4
  86.             4 4 1 4 4 1 4 4
  87.             4 4 1 1 1 1 4 4
  88.             4 4 4 4 4 4 4 4
  89.             """),
  90.         SpriteKind.player),
  91.     sprites.create(img("""
  92.             7 7 7 7 7 7 7 7
  93.             7 1 7 7 7 1 7 7
  94.             7 1 7 7 7 1 7 7
  95.             7 1 7 7 7 1 7 7
  96.             7 1 1 1 1 1 7 7
  97.             7 7 7 7 7 1 7 7
  98.             7 7 7 7 7 1 7 7
  99.             7 7 7 7 7 7 7 7
  100.             """),
  101.         SpriteKind.player)]
  102. for value in mp.all_players():
  103.     mp.set_player_sprite(value, sprites2[mp.player_to_index(value)])
  104.     mp.move_with_buttons(value, mp.get_player_sprite(value))
  105.     mp.get_player_sprite(value).set_stay_in_screen(True)
  106.     mp.set_player_state(value, MultiplayerState.lives, 3)
  107. mp.get_player_sprite(mp.PlayerNumber.ONE).set_position(5, 15)
  108. mp.get_player_sprite(mp.PlayerNumber.TWO).set_position(155, 15)
  109. mp.get_player_sprite(mp.PlayerNumber.THREE).set_position(5, 105)
  110. mp.get_player_sprite(mp.PlayerNumber.FOUR).set_position(155, 105)
  111. it = sprites2._pick_random()
  112. youreIt()
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 前天 17:42

【花雕动手做】基于Kitronik可编程开发板经典捉人游戏

代码解读

这是一个经典的"捉人游戏",支持最多4名玩家。一名玩家扮演"It"(捉人者),需要在限定时间内捉到其他玩家,否则会受到惩罚。
核心代码解析

1. 游戏初始化
游戏说明
python
  1. game.show_long_text("Once all your friends have joined, hit A to continue.", DialogLayout.CENTER)
  2. game.show_long_text("When you're it, tag a friend and they will lose a life. Be careful, if time runs out you'll lose a life instead!", DialogLayout.CENTER)
复制代码

显示游戏规则:当你是"It"时,捉到朋友会让他们失去一条命;如果时间用完,你自己会失去一条命

玩家角色创建
python
  1. sprites2 = [sprites.create(img("""..."""), SpriteKind.player),  # 玩家1(红色箭头)
  2.     sprites.create(img("""..."""), SpriteKind.player),  # 玩家2(橙色数字7?)
  3.     sprites.create(img("""..."""), SpriteKind.player),  # 玩家3(绿色数字2?)
  4.     sprites.create(img("""..."""), SpriteKind.player)]  # 玩家4(紫色数字4?)
复制代码

创建4个8x8像素的玩家角色,使用ASCII艺术定义外观

玩家设置
python
  1. for value in mp.all_players():
  2.     mp.set_player_sprite(value, sprites2[mp.player_to_index(value)])  # 分配精灵
  3.     mp.move_with_buttons(value, mp.get_player_sprite(value))  # 设置按钮控制移动
  4.     mp.get_player_sprite(value).set_stay_in_screen(True)  # 限制在屏幕内
  5.     mp.set_player_state(value, MultiplayerState.lives, 3)  # 设置3条生命
复制代码

初始位置设置
python
  1. mp.get_player_sprite(mp.PlayerNumber.ONE).set_position(5, 15)    # 左上角
  2. mp.get_player_sprite(mp.PlayerNumber.TWO).set_position(155, 15)  # 右上角
  3. mp.get_player_sprite(mp.PlayerNumber.THREE).set_position(5, 105) # 左下角
  4. mp.get_player_sprite(mp.PlayerNumber.FOUR).set_position(155, 105) # 右下角
复制代码


2. 游戏核心机制
选择初始"It"
python
  1. it = sprites2._pick_random()  # 随机选择一个玩家作为"It"
  2. youreIt()  # 启动"It"状态
复制代码

"It"状态设置函数
python
  1. def youreIt():
  2.     it.say_text("It", 2000, False)  # 显示"It"文字2秒
  3.     it.set_position(randint(5, 155), randint(15, 115))  # 随机位置
  4.     it.set_kind(SpriteKind.enemy)  # 设置为敌人类型
  5.     info.start_countdown(10)  # 开始10秒倒计时
复制代码


3. 碰撞检测系统
python
  1. def on_on_overlap(sprite2, otherSprite):
  2.     global it
  3.     info.stop_countdown()  # 停止倒计时
  4.     it.say_text("", 2000, False)  # 清除"It"文字
  5.     reduceLife(sprite2)  # 被捉到的玩家减命
  6.     it = sprite2  # 被捉到的玩家成为新的"It"
  7.     otherSprite.set_kind(SpriteKind.player)  # 原"It"变回普通玩家
  8.     youreIt()  # 新"It"开始
复制代码

当普通玩家(SpriteKind.player)与"It"(SpriteKind.enemy)碰撞时触发

被捉到的玩家失去一条命并成为新的"It"

4. 倒计时结束处理
python
  1. def on_countdown_end():
  2.     global it
  3.     it.set_kind(SpriteKind.player)  # "It"变回普通玩家
  4.     reduceLife(it)  # "It"失去一条命(因为没捉到人)
  5.     it = sprites2._pick_random()  # 随机选择新的"It"
  6.     youreIt()  # 新"It"开始
复制代码

如果"It"在10秒内没有捉到任何人,自己会失去一条命

5. 生命值管理系统
python
  1. def reduceLife(sprite: Sprite):
  2.     if sprite == mp.get_player_sprite(mp.PlayerNumber.ONE):
  3.         mp.change_player_state_by(mp.PlayerNumber.ONE, MultiplayerState.lives, -1)
  4.     # ... 其他玩家类似
复制代码


6. 生命值为零处理
python
  1. def on_life_zero(player2):
  2.     global it
  3.     mp.get_player_sprite(player2).destroy(effects.fire, 500)  # 火焰效果销毁
  4.     sprites2.remove_at(sprites2.index_of(mp.get_player_sprite(player2)))  # 从玩家列表中移除
  5.    
  6.     # 如果被淘汰的是当前"It",选择新的"It"
  7.     if it == mp.get_player_sprite(player2):
  8.         it.say_text("", 2000, False)
  9.         it = sprites2._pick_random()
  10.         youreIt()
  11.    
  12.     checkIfWon()  # 检查是否有人获胜
复制代码


7. 胜利条件检查
python
  1. def checkIfWon():
  2.     if len(sprites2) == 1:  # 如果只剩一个玩家
  3.         if sprites2[0] == mp.get_player_sprite(mp.PlayerNumber.ONE):
  4.             game.splash("Player 1 Wins!")
  5.         # ... 其他玩家类似
  6.         game.reset()  # 重置游戏
复制代码


游戏机制总结
多人竞技:支持2-4名玩家
生命系统:每个玩家有3条生命
时间压力:"It"有10秒时间捉人,否则自己受罚
角色转换:被捉到的玩家成为新的"It"
淘汰机制:生命值为零的玩家被淘汰
胜利条件:最后剩下的玩家获胜

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 前天 17:58

【花雕动手做】基于Kitronik可编程开发板经典捉人游戏

图形编程参考实验程序

【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图1

通过模拟器,调试与模拟运行

【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图2
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 前天 18:01

【花雕动手做】基于Kitronik可编程开发板经典捉人游戏

实验场景记录

【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图3

【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图1

【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图2

【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏图4
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail