9浏览
查看: 9|回复: 5

[项目] 【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉

[复制链接]
Kitronik ARCADE 是一款由英国教育科技公司 Kitronik 精心打造的可编程游戏机开发板,专为编程教学与创客实践而设计。该设备原生支持微软的 MakeCode Arcade 平台,用户可通过图形化或 JavaScript 编程方式,轻松创建、下载并运行复古风格的街机游戏。

它集成了彩色 LCD 显示屏、方向控制键、功能按键、蜂鸣器和震动马达等交互组件,提供完整的游戏输入输出体验。无论是初学者进行编程启蒙,还是创客群体开发交互式作品,Kitronik ARCADE 都能作为理想的硬件载体,助力创意实现。

凭借其开源友好、易于上手、兼容性强等特点,该开发板广泛应用于中小学编程课程、创客工作坊、游戏开发教学以及个人项目原型设计,深受教育者与技术爱好者的喜爱。

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图1

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图2

驴友花雕  中级技神
 楼主|

发表于 2 小时前

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉

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

MicroPython实验代码

  1. @namespace
  2. class SpriteKind:
  3.     Mountain = SpriteKind.create()
  4.     Goat = SpriteKind.create()
  5. def drop_goat():
  6.     moving_goat.ay = 300
  7.     moving_goat.vx = 0
  8.     moving_goat.set_flag(SpriteFlag.BOUNCE_ON_WALL, False)
  9. def on_a_pressed():
  10.     drop_goat()
  11. controller.A.on_event(ControllerButtonEvent.PRESSED, on_a_pressed)
  12. # calculate new center of gravity
  13. def will_the_stack_fall_down_():
  14.     global mountain_goats, center_of_gravity
  15.     mountain_goats = sprites.all_of_kind(SpriteKind.Mountain)
  16.     center_of_gravity = 0
  17.     for value in mountain_goats:
  18.         center_of_gravity += value.x
  19.     if center_of_gravity / len(mountain_goats) < bottom_goat.left or center_of_gravity / len(mountain_goats) > bottom_goat.right:
  20.         return True
  21.     return False
  22. def will_the_goat_fall_off(falling_goat: Sprite, mountain_goat: Sprite):
  23.     if falling_goat.x > mountain_goat.right:
  24.         falling_goat.set_velocity(50, -50)
  25.         falling_goat.set_flag(SpriteFlag.GHOST, True)
  26.         falling_goat.set_flag(SpriteFlag.AUTO_DESTROY, True)
  27.         info.change_life_by(-1)
  28.         return True
  29.     elif falling_goat.x < mountain_goat.left:
  30.         falling_goat.set_velocity(-50, 50)
  31.         falling_goat.set_flag(SpriteFlag.GHOST, True)
  32.         falling_goat.set_flag(SpriteFlag.AUTO_DESTROY, True)
  33.         info.change_life_by(-1)
  34.         return True
  35.     else:
  36.         return False
  37. def on_hit_wall(sprite, location):
  38.     if sprite.is_hitting_tile(CollisionDirection.BOTTOM):
  39.         game.over(False)
  40. scene.on_hit_wall(SpriteKind.Goat, on_hit_wall)
  41. def explode_goats():
  42.     global current_goat
  43.     current_goat = bottom_goat
  44.     while current_goat:
  45.         current_goat.set_flag(SpriteFlag.GHOST, True)
  46.         current_goat.set_velocity(randint(-200, 200), randint(-200, 200))
  47.         current_goat = sprites.read_data_sprite(current_goat, "goat on top of me")
  48.         scene.camera_follow_sprite(None)
  49. def on_on_overlap(sprite2, otherSprite):
  50.     global goat_fell_off, top_goat
  51.     sprite2.vy = 0
  52.     sprite2.ay = 0
  53.     sprite2.set_kind(SpriteKind.Mountain)
  54.     goat_fell_off = will_the_goat_fall_off(sprite2, otherSprite)
  55.     if goat_fell_off:
  56.         sprite2.start_effect(effects.fire)
  57.     elif will_the_stack_fall_down_():
  58.         sprites.set_data_sprite(otherSprite, "goat on top of me", sprite2)
  59.         explode_goats()
  60.         pause(1000)
  61.         game.over(False)
  62.     else:
  63.         top_goat = sprite2
  64.         scene.camera_follow_sprite(sprite2)
  65.         sprites.set_data_sprite(otherSprite, "goat on top of me", sprite2)
  66.     make_a_new_goat()
  67. sprites.on_overlap(SpriteKind.Goat, SpriteKind.Mountain, on_on_overlap)
  68. def make_a_new_goat():
  69.     global moving_goat
  70.     moving_goat = sprites.create(goat_species[randint(0, len(goat_species) - 1)],
  71.         SpriteKind.Goat)
  72.     moving_goat.set_position(80, top_goat.y - 30)
  73.     moving_goat.set_flag(SpriteFlag.BOUNCE_ON_WALL, True)
  74.     moving_goat.vx = 40
  75. goat_fell_off = False
  76. current_goat: Sprite = None
  77. center_of_gravity = 0
  78. mountain_goats: List[Sprite] = []
  79. moving_goat: Sprite = None
  80. bottom_goat: Sprite = None
  81. top_goat: Sprite = None
  82. goat_species: List[Image] = []
  83. goat_species = [img("""
  84.         . . . . . . . . . . . . f e e .
  85.         . . . . . . . . . . . . . e e .
  86.         . . . . . . . . . . . . d c d c
  87.         . . . . . . . . . . . . d d d d
  88.         e d d d d d d d d d d d d a d d
  89.         . d d d d d d d d d d d d d a a
  90.         . d d d d d d d d d d d d . . .
  91.         . d d d d d d d d d d d d . . .
  92.         . . d d d d d d d d d d . . . .
  93.         . . . d . d . . d . d . . . . .
  94.         . . . d . d . . d . d . . . . .
  95.         . . . e . e . . e . e . . . . .
  96.         """),
  97.     img("""
  98.         . . . . f f . . . . . . . . . . . . .
  99.         . . d f f d f . . . . . . . . . . . .
  100.         . 4 4 4 4 4 4 4 . . . . . . . . . . .
  101.         . . 1 4 4 1 . . . . . . . . . . . . .
  102.         . 4 4 4 4 4 4 . . . . . . . . . . . .
  103.         . 4 4 4 4 4 4 . . . . . . . . . . . .
  104.         4 4 4 2 4 4 4 4 4 4 4 4 4 4 4 4 4 . .
  105.         . 4 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
  106.         . . 2 . . 4 4 4 4 4 4 4 4 4 4 4 4 4 .
  107.         . . . . . 4 4 4 4 4 4 4 4 4 4 4 4 4 .
  108.         . . . . . 4 2 4 2 2 2 2 2 2 2 2 2 . .
  109.         . . . . . 4 . 4 . . . . . 2 . 4 . . .
  110.         . . . . . 4 . 4 . . . . . 2 . 4 . . .
  111.         . . . . . 4 . 4 . . . . . 2 . 4 . . .
  112.         """),
  113.     img("""
  114.         . . . . . . . . b b d . . d d b . . .
  115.         . . . . . . . . b b b b b b b b . . .
  116.         . . . . . . . . . b b d b b d . . . .
  117.         . . . . . . . . . . b b b b b c . . .
  118.         . b . . . . . . . b b b b b b b . . .
  119.         . b . . . . b b b b b b b b b b . . .
  120.         . c b b b b b b b b b b . . . . . . .
  121.         . c b b b b b b b b b b . . . . . . .
  122.         . . c b b b b b b b b b . . . . . . .
  123.         . . c c b b b b b b b c . . . . . . .
  124.         . . c c c c c c c c c c . . . . . . .
  125.         . . c . b . . . c c c c . . . . . . .
  126.         . . c . b . . . . b . c . . . . . . .
  127.         . . c . b . . . . b . c . . . . . . .
  128.         """),
  129.     img("""
  130.         . . . . . . . . . . . . e . . e . . .
  131.         . . . . . . . . . . . e e e e e e . .
  132.         . . . . . . . . . . e e e e e e e e .
  133.         . . . . . . . . . e e e e c c c e e e
  134.         . . . . . . . . . . . . c c b c . . .
  135.         . . . c c c c c c c c c c f b f c . .
  136.         . . c c b b b b b b b b b f b f . . .
  137.         . . c b b b c c c b b b b b b c . . .
  138.         . c c b b c b b b b b b f b b c . . .
  139.         c c b b c b b b b b b b b f f f . . .
  140.         c b b b b b b b b b b b c c c . . . .
  141.         c c c c c c c c c c c c c . . . . . .
  142.         . . . . c . c . . . c . c . . . . . .
  143.         . . . . c . c . . . c . c . . . . . .
  144.         """),
  145.     img("""
  146.         . . . 1 . . . . . . . . . . . . . .
  147.         . . 1 c c c . . . . . . . . . . . .
  148.         . 1 1 1 1 c c . . . . . . . . . . 1
  149.         1 1 f 1 c 1 c c . . . . . . . . . 1
  150.         1 1 1 c 1 1 c c 1 1 1 1 1 1 1 1 1 1
  151.         1 1 1 c c c c 1 1 1 1 1 1 1 1 1 1 .
  152.         . . . 1 c c 1 1 1 1 1 1 1 1 1 1 1 .
  153.         . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 .
  154.         . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 .
  155.         . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 .
  156.         . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 .
  157.         . . . . 1 . 1 . . . . . . . 1 . 1 .
  158.         . . . . 1 . 1 . . . . . . . 1 . 1 .
  159.         . . . . 1 . 1 . . . . . . . 1 . 1 .
  160.         . . . . 1 . 1 . . . . . . . 1 . 1 .
  161.         . . . . c . c . . . . . . . c . c .
  162.         """),
  163.     img("""
  164.         .........................
  165.         ff.............ff...ff...
  166.         .ff............ffffff....
  167.         ..ff............ff2f2ffff
  168.         ...f............fffffffff
  169.         ...f............ffffff...
  170.         ...ff2f2ffffffffffffff...
  171.         ....f2f22fffffffff.......
  172.         ....f2ff2fffffffff.......
  173.         ....ff22ffffffffff.......
  174.         ....ffffffffffffff.......
  175.         ....fffffffff222ff.......
  176.         ....ffffffff2ff2ff.......
  177.         ....ffffffff22f2ff.......
  178.         ....fffffffff222f........
  179.         .....f...f...f..f........
  180.         .....f...f...f..f........
  181.         .....f...f...f..f........
  182.         """)]
  183. scene.set_background_color(9)
  184. tiles.set_tilemap(tilemap("""
  185.     级别1
  186.     """))
  187. goat = sprites.create(img("""
  188.         . . . . f f . . . . . . . . . . . . .
  189.         . . d f f d f . . . . . . . . . . . .
  190.         . 4 4 4 4 4 4 4 . . . . . . . . . . .
  191.         . . 1 4 4 1 . . . . . . . . . . . . .
  192.         . 4 4 4 4 4 4 . . . . . . . . . . . .
  193.         . 4 4 4 4 4 4 . . . . . . . . . . . .
  194.         4 4 4 2 4 4 4 4 4 4 4 4 4 4 4 4 4 . .
  195.         . 4 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
  196.         . . 2 . . 4 4 4 4 4 4 4 4 4 4 4 4 4 .
  197.         . . . . . 4 4 4 4 4 4 4 4 4 4 4 4 4 .
  198.         . . . . . 4 2 4 2 2 2 2 2 2 2 2 2 . .
  199.         . . . . . 4 . 4 . . . . . 2 . 4 . . .
  200.         . . . . . 4 . 4 . . . . . 2 . 4 . . .
  201.         . . . . . 4 . 4 . . . . . 2 . 4 . . .
  202.         """),
  203.     SpriteKind.Mountain)
  204. goat.ay = 300
  205. goat.set_position(80, 600)
  206. scene.camera_follow_sprite(goat)
  207. top_goat = goat
  208. make_a_new_goat()
  209. how_much_does_a_goat_weigh = 22
  210. info.set_life(3)
  211. bottom_goat = goat
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2 小时前

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉

ARCADE MakeCode 之叠羊罗汉游戏代码解读
这是一个基于ARCADE MakeCode的"叠羊罗汉"平衡游戏,玩家需要控制山羊叠放在一起而不倒塌。解读代码如下:

1. 自定义精灵类型
python
  1. @namespace
  2. class SpriteKind:
  3.     Mountain = SpriteKind.create()  # 已经落下的山羊
  4.     Goat = SpriteKind.create()      # 正在移动的山羊
复制代码


2. 放下山羊函数
python
  1. def drop_goat():
  2.     moving_goat.ay = 300          # 启用重力
  3.     moving_goat.vx = 0            # 停止水平移动
  4.     moving_goat.set_flag(SpriteFlag.BOUNCE_ON_WALL, False)  # 关闭墙壁反弹
  5. def on_a_pressed():
  6.     drop_goat()  # A键按下时放下当前山羊
  7. controller.A.on_event(ControllerButtonEvent.PRESSED, on_a_pressed)
复制代码


3. 平衡检测函数
检查整个堆栈是否会倒塌
python
  1. def will_the_stack_fall_down_():
  2.     global mountain_goats, center_of_gravity
  3.     mountain_goats = sprites.all_of_kind(SpriteKind.Mountain)  # 获取所有已落下的山羊
  4.     center_of_gravity = 0
  5.     for value in mountain_goats:
  6.         center_of_gravity += value.x  # 计算所有山羊的x坐标总和
  7.    
  8.     # 如果重心超出底部山羊的左右边界,则堆栈会倒塌
  9.     if center_of_gravity / len(mountain_goats) < bottom_goat.left or center_of_gravity / len(mountain_goats) > bottom_goat.right:
  10.         return True
  11.     return False
复制代码

检查单个山羊是否会掉落
python
  1. def will_the_goat_fall_off(falling_goat: Sprite, mountain_goat: Sprite):
  2.     if falling_goat.x > mountain_goat.right:  # 山羊超出右侧边界
  3.         falling_goat.set_velocity(50, -50)    # 设置掉落速度和方向
  4.         falling_goat.set_flag(SpriteFlag.GHOST, True)      # 变为幽灵状态(可穿透)
  5.         falling_goat.set_flag(SpriteFlag.AUTO_DESTROY, True)  # 自动销毁
  6.         info.change_life_by(-1)               # 减少生命值
  7.         return True
  8.     elif falling_goat.x < mountain_goat.left:  # 山羊超出左侧边界
  9.         falling_goat.set_velocity(-50, 50)     # 设置掉落速度和方向
  10.         falling_goat.set_flag(SpriteFlag.GHOST, True)
  11.         falling_goat.set_flag(SpriteFlag.AUTO_DESTROY, True)
  12.         info.change_life_by(-1)
  13.         return True
  14.     else:
  15.         return False  # 山羊安全落在平台上
复制代码


4. 碰撞检测
python
  1. def on_hit_wall(sprite, location):
  2.     if sprite.is_hitting_tile(CollisionDirection.BOTTOM):  # 如果山羊碰到底部墙壁
  3.         game.over(False)  # 游戏结束
  4. scene.on_hit_wall(SpriteKind.Goat, on_hit_wall)
复制代码

5. 山羊堆栈**效果
python
  1. def explode_goats():
  2.     global current_goat
  3.     current_goat = bottom_goat  # 从底部山羊开始
  4.     while current_goat:
  5.         current_goat.set_flag(SpriteFlag.GHOST, True)  # 变为幽灵状态
  6.         # 设置随机速度,产生**效果
  7.         current_goat.set_velocity(randint(-200, 200), randint(-200, 200))
  8.         # 获取堆叠在上面的山羊
  9.         current_goat = sprites.read_data_sprite(current_goat, "goat on top of me")
  10.         scene.camera_follow_sprite(None)  # 取消相机跟随
复制代码

6. 山羊重叠事件处理(核心逻辑)
python
  1. def on_on_overlap(sprite2, otherSprite):
  2.     global goat_fell_off, top_goat
  3.     sprite2.vy = 0      # 停止垂直移动
  4.     sprite2.ay = 0      # 取消重力
  5.     sprite2.set_kind(SpriteKind.Mountain)  # 变为已落下的山羊
  6.    
  7.     # 检查山羊是否会掉落
  8.     goat_fell_off = will_the_goat_fall_off(sprite2, otherSprite)
  9.     if goat_fell_off:
  10.         sprite2.start_effect(effects.fire)  # 播放火焰效果
  11.     elif will_the_stack_fall_down_():  # 检查整个堆栈是否会倒塌
  12.         # 记录山羊堆叠关系
  13.         sprites.set_data_sprite(otherSprite, "goat on top of me", sprite2)
  14.         explode_goats()  # **效果
  15.         pause(1000)
  16.         game.over(False)  # 游戏结束
  17.     else:
  18.         top_goat = sprite2  # 更新顶部山羊
  19.         scene.camera_follow_sprite(sprite2)  # 相机跟随新顶部山羊
  20.         # 记录山羊堆叠关系
  21.         sprites.set_data_sprite(otherSprite, "goat on top of me", sprite2)
  22.    
  23.     make_a_new_goat()  # 创建新的山羊
  24. sprites.on_overlap(SpriteKind.Goat, SpriteKind.Mountain, on_on_overlap)
复制代码


7. 创建新山羊函数
python
  1. def make_a_new_goat():
  2.     global moving_goat
  3.     # 随机选择山羊种类
  4.     moving_goat = sprites.create(goat_species[randint(0, len(goat_species) - 1)],
  5.         SpriteKind.Goat)
  6.     moving_goat.set_position(80, top_goat.y - 30)  # 放在顶部山羊上方
  7.     moving_goat.set_flag(SpriteFlag.BOUNCE_ON_WALL, True)  # 开启墙壁反弹
  8.     moving_goat.vx = 40  # 设置水平移动速度
复制代码


8. 山羊种类定义
代码中定义了6种不同样式的山羊图像,增加了游戏的多样性。


9. 游戏初始化
python
  1. scene.set_background_color(9)  # 设置背景颜色
  2. tiles.set_tilemap(tilemap("级别1"))  # 设置地图
  3. # 创建初始山羊
  4. goat = sprites.create(..., SpriteKind.Mountain)
  5. goat.ay = 300  # 启用重力
  6. goat.set_position(80, 600)  # 设置位置
  7. scene.camera_follow_sprite(goat)  # 相机跟随
  8. top_goat = goat  # 设置顶部山羊
  9. make_a_new_goat()  # 创建新山羊
  10. how_much_does_a_goat_weigh = 22  # 山羊重量(可能用于后续扩展)
  11. info.set_life(3)  # 设置初始生命值
  12. bottom_goat = goat  # 设置底部山羊
复制代码

游戏机制总结
控制方式:山羊左右移动,按A键放下

平衡系统:计算堆栈重心,如果超出底部山羊边界则倒塌

生命系统:山羊掉落减少生命值,生命值为0时游戏结束

堆叠关系:使用数据存储记录山羊之间的堆叠关系

视觉效果:掉落的山羊有火焰效果,倒塌时有**效果

相机跟随:相机始终跟随最顶部的山羊

这是一个有趣的物理平衡游戏,玩家需要精确控制放下山羊的时机和位置,以保持堆栈的平衡。游戏通过重心计算和物理效果实现了真实的堆叠体验。

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2 小时前

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉

图形编程参考实验程序

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 1 小时前

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉

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

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图2

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图1

实验场景记录

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图5

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图3

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图4

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 1 小时前

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉

【花雕动手做】基于 Kitronik 可编程开发板之叠羊罗汉图1
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail