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

[项目] 【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏

[复制链接]
【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图1

Kitronik ARCADE 使用 Microsoft MakeCode 平台,具有以下优势:
图形化编程界面:适合初学者,支持拖拽式编程。
即时模拟器:可以实时测试游戏效果。
硬件兼容性:可部署到 Kitronik ARCADE 设备,实现实体游戏体验。
支持 Python/JavaScript:便于进阶学习。


【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图3

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图2

驴友花雕  中级技神
 楼主|

发表于 昨天 17:24

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏

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

MicroPython实验代码

  1. @namespace
  2. class SpriteKind:
  3.     Ball = SpriteKind.create()
  4. bouncers: List[Sprite] = []
  5. balls: List[Image] = []
  6. catcher: Sprite = None
  7. deadball: Sprite = None
  8. sitting = 0
  9. playing = False
  10. game.splash("Bouncer Bucket", "A = 1 ball, B = 10 balls")
  11. balls.append(img("""
  12.     . . 7 7 7 7 . .
  13.     . 7 7 7 7 7 7 .
  14.     7 7 7 7 7 7 7 7
  15.     7 7 7 7 7 7 7 7
  16.     7 7 7 7 7 7 7 7
  17.     7 7 7 7 7 7 7 7
  18.     . 7 7 7 7 7 7 .
  19.     . . 7 7 7 7 . .
  20.     """))
  21. balls.append(img("""
  22.     . . 2 2 2 2 . .
  23.     . 2 2 2 2 2 2 .
  24.     2 2 2 2 2 2 2 2
  25.     2 2 2 2 2 2 2 2
  26.     2 2 2 2 2 2 2 2
  27.     2 2 2 2 2 2 2 2
  28.     . 2 2 2 2 2 2 .
  29.     . . 2 2 2 2 . .
  30.     """))
  31. balls.append(img("""
  32.     . . 4 4 4 4 . .
  33.     . 4 4 4 4 4 4 .
  34.     4 4 4 4 4 4 4 4
  35.     4 4 4 4 4 4 4 4
  36.     4 4 4 4 4 4 4 4
  37.     4 4 4 4 4 4 4 4
  38.     . 4 4 4 4 4 4 .
  39.     . . 4 4 4 4 . .
  40.     """))
  41. catcher = sprites.create(img("""
  42.         . . . . . . . . . . . . . . . .
  43.         . . . . . . . . . . . . . . . .
  44.         1 . . . . . . . . . . . . . . 1
  45.         f e e . . . . . . . . . . e e f
  46.         1 f e e . . . . . . . . e e f 1
  47.         1 1 f e e e e e e e e e e f 1 1
  48.         . 1 e f e e e e e e e e f e 1 .
  49.         . 1 e f e e e e e e e e f e 1 .
  50.         . 1 e e f f e e e e f f e e 1 .
  51.         . 1 e e e e f f f f e e e e 1 .
  52.         . 1 1 e e e e e e e e e e 1 1 .
  53.         . . 1 e e e e e e e e e e 1 . .
  54.         . . 1 e e e e e e e e e e 1 . .
  55.         . . 1 1 e e e e e e e e 1 1 . .
  56.         . . . 1 e e e e e e e e 1 . . .
  57.         . . . 1 1 1 1 1 1 1 1 1 1 . . .
  58.         """),
  59.     SpriteKind.player)
  60. catcher.bottom = scene.screen_height() - 1
  61. catcher.set_stay_in_screen(True)
  62. info.set_score(0)
  63. def on_on_overlap(sprite, otherSprite):
  64.     if sprite.x > otherSprite.x - 2 and sprite.x < otherSprite.x + 2:
  65.         if sitting > 300:
  66.             otherSprite.say("nope", 200)
  67.             sprite.vy = sprite.vy * -2
  68.         else:
  69.             normalScore = sprite.vx
  70.             if normalScore < 0:
  71.                 normalScore = normalScore * -1
  72.             otherSprite.say("" + str(normalScore), 200)
  73.             info.set_score(info.score() + normalScore)
  74.             sprite.destroy()
  75.     elif sprite.x <= otherSprite.x:
  76.         sprite.vx = sprite.vx * -2
  77.     else:
  78.         sprite.vx = sprite.vx * 2
  79. sprites.on_overlap(SpriteKind.Ball, SpriteKind.player, on_on_overlap)
  80. def on_countdown_end():
  81.     global playing
  82.     playing = False
  83.     game.over(False)
  84. info.on_countdown_end(on_countdown_end)
  85. def on_on_destroyed(sprite2):
  86.     global deadball
  87.     j = 0
  88.     while j <= len(bouncers) - 1:
  89.         if bouncers[j] == sprite2:
  90.             deadball = bouncers.remove_at(j)
  91.         j += 1
  92.     makeBouncer()
  93. sprites.on_destroyed(SpriteKind.Ball, on_on_destroyed)
  94. def on_a_pressed():
  95.     global playing
  96.     if not playing:
  97.         playing = True
  98.         makeBouncer()
  99.         info.start_countdown(60)
  100. controller.A.on_event(ControllerButtonEvent.PRESSED, on_a_pressed)
  101. def on_b_pressed():
  102.     global playing
  103.     if not playing:
  104.         playing = True
  105.         for i in range(10):
  106.             makeBouncer()
  107.         info.start_countdown(30)
  108. controller.B.on_event(ControllerButtonEvent.PRESSED, on_b_pressed)
  109. def makeBouncer():
  110.     ballChoice = randint(0, 2)
  111.     ballsCount = bouncers.unshift(sprites.create(balls[ballChoice], SpriteKind.Ball))
  112.     bouncers[0].set_flag(SpriteFlag.AUTO_DESTROY, True)
  113.     bouncers[0].x = randint(0, scene.screen_width() / 4)
  114.     bouncers[0].y = randint(0, scene.screen_height() / 3)
  115.     bouncers[0].vx = 10 + ballChoice * 10
  116.     bouncers[0].ay = 100
  117. def on_on_update():
  118.     global sitting
  119.     moveX = controller.dx()
  120.     if moveX != 0:
  121.         sitting = 0
  122.         catcher.x += moveX
  123. game.on_update(on_on_update)
  124. def on_update_interval():
  125.     global sitting
  126.     for bouncer in bouncers:
  127.         if bouncer.bottom >= scene.screen_height() and bouncer.vy > 0:
  128.             bouncer.vy = bouncer.vy * -1
  129.             bouncer.ay = bouncer.ay + 20
  130.     sitting += 1
  131. game.on_update_interval(10, on_update_interval)
复制代码



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 17:29

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏

这段 Arcade MakeCode 的《弹跳桶游戏》使用 MicroPython 编写,是一个反应类得分游戏,玩家控制一个“桶”接住弹跳球以获得分数。玩家通过按下 A 或 B 键启动游戏,控制底部的“桶”左右移动,接住从上方弹跳下来的球。每接住一个球,根据其水平速度获得相应分数。游戏有时间限制,球也会不断反弹。

代码结构详解

1、精灵种类定义
python
  1. @namespace
  2. class SpriteKind:
  3.     Ball = SpriteKind.create()
复制代码

创建一个新的精灵种类 Ball,用于标记弹跳球。

2、球的图像与列表初始化
python
  1. balls: List[Image] = []
  2. balls.append(img("""..."""))  # 三种不同颜色的球
复制代码

创建三种不同颜色的球图像并存入列表,供后续随机选择。

3、玩家桶设置
python
  1. catcher = sprites.create(img("""..."""), SpriteKind.player)
  2. catcher.bottom = scene.screen_height() - 1
  3. catcher.set_stay_in_screen(True)
复制代码

创建玩家控制的“桶”精灵,放置在屏幕底部。
限制其不离开屏幕。

4、得分机制与碰撞处理
python
  1. def on_on_overlap(sprite, otherSprite):
  2.     if sprite.x > otherSprite.x - 2 and sprite.x < otherSprite.x + 2:
  3.         if sitting > 300:
  4.             otherSprite.say("nope", 200)
  5.             sprite.vy = sprite.vy * -2
  6.         else:
  7.             normalScore = abs(sprite.vx)
  8.             otherSprite.say(str(normalScore), 200)
  9.             info.set_score(info.score() + normalScore)
  10.             sprite.destroy()
  11.     elif sprite.x <= otherSprite.x:
  12.         sprite.vx = sprite.vx * -2
  13.     else:
  14.         sprite.vx = sprite.vx * 2
复制代码

当球与桶重叠时:
如果桶长时间未移动(sitting > 300),球弹回并显示“nope”。
否则,根据球的水平速度 vx 计算得分。
球被销毁,得分显示在桶上方。
如果碰撞位置偏左或偏右,球会反弹或加速。

5、 倒计时结束处理
python
  1. def on_countdown_end():
  2.     playing = False
  3.     game.over(False)
复制代码

游戏时间结束后,游戏失败。

6、球销毁后自动补球
python
  1. def on_on_destroyed(sprite2):
  2.     ...
  3.     makeBouncer()
复制代码

当球被销毁时,从列表中移除,并补充一个新球。

7、控制器启动游戏
python
  1. def on_a_pressed():
  2.     makeBouncer()
  3.     info.start_countdown(60)
  4. def on_b_pressed():
  5.     for i in range(10):
  6.         makeBouncer()
  7.     info.start_countdown(30)
复制代码

按 A 键:生成 1 个球,游戏时间 60 秒。
按 B 键:生成 10 个球,游戏时间 30 秒。

8、生成弹跳球函数
python
  1. def makeBouncer():
  2.     ballChoice = randint(0, 2)
  3.     bouncer = sprites.create(balls[ballChoice], SpriteKind.Ball)
  4.     bouncer.set_flag(SpriteFlag.AUTO_DESTROY, True)
  5.     bouncer.x = randint(0, scene.screen_width() / 4)
  6.     bouncer.y = randint(0, scene.screen_height() / 3)
  7.     bouncer.vx = 10 + ballChoice * 10
  8.     bouncer.ay = 100
复制代码

随机选择一种球图像。
设置初始位置、速度和重力加速度。

9、玩家移动与静止检测
python
  1. def on_on_update():
  2.     moveX = controller.dx()
  3.     if moveX != 0:
  4.         sitting = 0
  5.         catcher.x += moveX
复制代码

玩家通过方向键移动桶。
如果桶移动了,sitting 重置为 0。

10、球反弹逻辑与静止计数
python
  1. def on_update_interval():
  2.     for bouncer in bouncers:
  3.         if bouncer.bottom >= scene.screen_height() and bouncer.vy > 0:
  4.             bouncer.vy = bouncer.vy * -1
  5.             bouncer.ay += 20
  6.     sitting += 1
复制代码

每 10 毫秒检查球是否触底并反弹。
增加重力加速度使球更难接。
增加 sitting 计数,用于判断桶是否长时间未动。

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 17:41

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏

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

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图2

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图1

实验场景记录

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图3

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图4

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图5

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图6

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 17:43

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏

【花雕动手做】基于 Kitronik 游戏机开发板之弹跳桶游戏图1
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail