【花雕动手做】基于 Kitronik 可编程开发板之玩家接方块
Kitronik ARCADE 是一款由英国教育科技公司 Kitronik 精心打造的可编程游戏机开发板,专为编程教学与创客实践而设计。该设备原生支持微软的 MakeCode Arcade 平台,用户可通过图形化或 JavaScript 编程方式,轻松创建、下载并运行复古风格的街机游戏。它集成了彩色 LCD 显示屏、方向控制键、功能按键、蜂鸣器和震动马达等交互组件,提供完整的游戏输入输出体验。无论是初学者进行编程启蒙,还是创客群体开发交互式作品,Kitronik ARCADE 都能作为理想的硬件载体,助力创意实现。
凭借其开源友好、易于上手、兼容性强等特点,该开发板广泛应用于中小学编程课程、创客工作坊、游戏开发教学以及个人项目原型设计,深受教育者与技术爱好者的喜爱。
【花雕动手做】基于 Kitronik 可编程开发板之玩家接方块
作为学习、练习与尝试,这里创建一个玩家接方块的小游戏。打开网页版:https://arcade.makecode.com/,设置项目名称:玩家接方块
MicroPython实验代码
box: Sprite = None
box_img: Image = None
color = 0
scene.set_background_color(9)
info.set_score(0)
# 创建玩家角色精灵(注意变量名改为 hero)
hero_img = image.create(16, 4)
hero_img.fill(1)
hero = sprites.create(hero_img, 0)
hero.set_position(80, 110)
controller.move_sprite(hero, 100, 0)
# 创建掉落方块
# 每秒生成一个方块
def on_update_interval():
global color, box_img, box
color = randint(2, 15)
box_img = image.create(10, 10)
box_img.fill(color)
box = sprites.create(box_img, 0)
box.set_position(randint(0, 150), 0)
box.set_velocity(0, 40)
def update():
if box.overlaps_with(hero):
box.destroy(effects.confetti, 100)
info.change_score_by(1)
elif box.y > 120:
box.destroy()
info.change_score_by(-1)
game.on_update(update)
game.on_update_interval(1000, on_update_interval)
【花雕动手做】基于 Kitronik 可编程开发板之玩家接方块
代码逐行解读python
scene.set_background_color(9)
设置游戏背景颜色为编号 9,通常是青绿色。你可以换成其他编号试试不同风格。
python
info.set_score(0)
初始化分数为 0。每次接住方块就加分,错过就扣分。
python
hero_img = image.create(16, 4)
hero_img.fill(1)
创建一个 16×4 像素的图像作为玩家角色的外观
fill(1) 表示用颜色编号 1(通常是白色)填满这个图像
python
hero = sprites.create(hero_img)
创建一个精灵对象,使用刚才的图像作为外观
变量名用 hero,避免使用系统保留名 player,防止报错
python
hero.set_position(80, 110)
设置玩家角色的位置:
x = 80 表示水平居中
y = 110 表示靠近屏幕底部
python
controller.move_sprite(hero, 100, 0)
让玩家可以控制角色左右移动
100 是移动速度
0 表示垂直方向不允许移动(只能左右)
python
def create_box():
定义一个函数,每次调用它就会生成一个掉落方块
python
color = randint(2, 15)
随机生成一个颜色编号,范围是 2 到 15,避开黑色和白色,让方块更鲜艳
python
box_img = image.create(10, 10)
box_img.fill(color)
创建一个 10×10 的图像,并用随机颜色填满,作为方块的外观
python
box = sprites.create(box_img)
创建一个精灵对象,使用刚才的图像作为外观
python
box.set_position(randint(0, 150), 0)
设置方块的位置:
x 是随机的,范围在屏幕宽度内
y = 0 表示从屏幕顶部开始掉落
python
box.set_velocity(0, 40)
设置方块的速度:
x = 0 表示不左右移动
y = 40 表示垂直向下掉落
python
def update():
定义一个内部函数,每帧检查这个方块的状态(是否碰到玩家或掉出屏幕)
python
if box.overlaps_with(hero):
box.destroy(effects.confetti, 100)
info.change_score_by(1)
如果方块碰到玩家角色:
销毁方块并播放彩色粒子特效
分数加 1
python
elif box.y > 120:
box.destroy()
info.change_score_by(-1)
如果方块掉出屏幕底部:
销毁方块
分数减 1
python
game.on_update(update)
每帧调用 update(),持续检查这个方块的状态
python
game.on_update_interval(1000, create_box)
每隔 1000 毫秒(1秒)调用一次 create_box(),不断生成新的方块
【花雕动手做】基于 Kitronik 可编程开发板之玩家接方块
图形编程参考实验程序通过模拟器,调试与模拟运行
实验场景记录
页:
[1]