驴友花雕 发表于 2025-9-24 17:51:44

【花雕动手做】基于 Kitronik 游戏机开发板之记忆大师



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



驴友花雕 发表于 2025-9-24 17:53:40

【花雕动手做】基于 Kitronik 游戏机开发板之记忆大师

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

MicroPython实验代码

@namespace
class SpriteKind:
    glyph = SpriteKind.create()
    UI = SpriteKind.create()
def InitUI():
    global mySprite, CursorSprite, curX, curY
    mySprite = sprites.create(imageList, SpriteKind.UI)
    mySprite.set_position(60, 90)
    mySprite = sprites.create(imageList, SpriteKind.UI)
    mySprite.set_position(80, 90)
    mySprite = sprites.create(imageList, SpriteKind.UI)
    mySprite.set_position(100, 90)
    mySprite = sprites.create(imageList, SpriteKind.UI)
    mySprite.set_position(60, 108)
    mySprite = sprites.create(imageList, SpriteKind.UI)
    mySprite.set_position(80, 108)
    mySprite = sprites.create(imageList, SpriteKind.UI)
    mySprite.set_position(100, 108)
    CursorSprite = sprites.create(img("""
            1 1 1 1 1 . . . . . . 1 1 1 1 1
            1 . . . . . . . . . . . . . . 1
            1 . . . . . . . . . . . . . . 1
            1 . . . . . . . . . . . . . . 1
            1 . . . . . . . . . . . . . . 1
            1 . . . . . . . . . . . . . . 1
            . . . . . . . . . . . . . . . .
            . . . . . . . . . . . . . . . .
            . . . . . . . . . . . . . . . .
            . . . . . . . . . . . . . . . .
            . . . . . . . . . . . . . . . .
            1 . . . . . . . . . . . . . . 1
            1 . . . . . . . . . . . . . . 1
            1 . . . . . . . . . . . . . . 1
            1 . . . . . . . . . . . . . . 1
            1 1 1 1 1 . . . . . . 1 1 1 1 1
            """),
      SpriteKind.UI)
    CursorSprite.set_position(60, 90)
    curX = 0
    curY = 0
def PlaySequence():
    global CurrentIndex, PlayerTurn
    index2 = 0
    while index2 <= len(CodeSequence) - 1:
      CurrentIndex = index2
      AddToSpriteList()
      pause(200)
      index2 += 1
    pause(600)
    for value2 in spriteList:
      value2.destroy()
    CurrentIndex = 0
    PlayerTurn = True

def on_up_pressed():
    global curY
    curY = max(0, curY - 1)
    UpdateCurPos()
controller.up.on_event(ControllerButtonEvent.PRESSED, on_up_pressed)

def AddToSequence():
    CodeSequence.append(randint(0, 5))

def on_down_pressed():
    global curY
    curY = min(1, curX + 1)
    UpdateCurPos()
controller.down.on_event(ControllerButtonEvent.PRESSED, on_down_pressed)

def InitSounds2():
    global SoundList
    SoundList =

def on_a_pressed():
    global CursorAt, CurrentIndex
    if PlayerTurn == True:
      CursorAt = curX + curY * 3
      if CursorAt == CodeSequence:
            AddToSpriteList()
            info.change_score_by(1)
            CurrentIndex += 1
            if CurrentIndex == len(CodeSequence):
                NextLevel()
      else:
            info.change_life_by(-1)
            music.power_down.play()
controller.A.on_event(ControllerButtonEvent.PRESSED, on_a_pressed)

def on_right_pressed():
    global curX
    curX = min(2, curX + 1)
    UpdateCurPos()
controller.right.on_event(ControllerButtonEvent.PRESSED, on_right_pressed)

def InitImages():
    global imageList
    imageList = [img("""
            . . . . . . . . . . b 5 b . . .
            . . . . . . . . . b 5 b . . . .
            . . . . . . . . . b c . . . . .
            . . . . . . b b b b b b . . . .
            . . . . . b b 5 5 5 5 5 b . . .
            . . . . b b 5 d 1 f 5 5 d f . .
            . . . . b 5 5 1 f f 5 d 4 c . .
            . . . . b 5 5 d f b d d 4 4 . .
            b d d d b b d 5 5 5 4 4 4 4 4 b
            b b d 5 5 5 b 5 5 4 4 4 4 4 b .
            b d c 5 5 5 5 d 5 5 5 5 5 b . .
            c d d c d 5 5 b 5 5 5 5 5 5 b .
            c b d d c c b 5 5 5 5 5 5 5 b .
            . c d d d d d d 5 5 5 5 5 d b .
            . . c b d d d d d 5 5 5 b b . .
            . . . c c c c c c c c b b . . .
            """),
      img("""
            . . . . c c c b b b b b . . . .
            . . c c b 4 4 4 4 4 4 b b b . .
            . c c 4 4 4 4 4 5 4 4 4 4 b c .
            . e 4 4 4 4 4 4 4 4 4 5 4 4 e .
            e b 4 5 4 4 5 4 4 4 4 4 4 4 b c
            e b 4 4 4 4 4 4 4 4 4 4 5 4 4 e
            e b b 4 4 4 4 4 4 4 4 4 4 4 b e
            . e b 4 4 4 4 4 5 4 4 4 4 b e .
            8 7 e e b 4 4 4 4 4 4 b e e 6 8
            8 7 2 e e e e e e e e e e 2 7 8
            e 6 6 2 2 2 2 2 2 2 2 2 2 6 c e
            e c 6 7 6 6 7 7 7 6 6 7 6 c c e
            e b e 8 8 c c 8 8 c c c 8 e b e
            e e b e c c e e e e e c e b e e
            . e e b b 4 4 4 4 4 4 4 4 e e .
            . . . c c c c c e e e e e . . .
            """),
      img("""
            . . . . . . . e c 7 . . . . . .
            . . . . e e e c 7 7 e e . . . .
            . . c e e e e c 7 e 2 2 e e . .
            . c e e e e e c 6 e e 2 2 2 e .
            . c e e e 2 e c c 2 4 5 4 2 e .
            c e e e 2 2 2 2 2 2 4 5 5 2 2 e
            c e e 2 2 2 2 2 2 2 2 4 4 2 2 e
            c e e 2 2 2 2 2 2 2 2 2 2 2 2 e
            c e e 2 2 2 2 2 2 2 2 2 2 2 2 e
            c e e 2 2 2 2 2 2 2 2 2 2 2 2 e
            c e e 2 2 2 2 2 2 2 2 2 2 4 2 e
            . e e e 2 2 2 2 2 2 2 2 2 4 e .
            . 2 e e 2 2 2 2 2 2 2 2 4 2 e .
            . . 2 e e 2 2 2 2 2 4 4 2 e . .
            . . . 2 2 e e 4 4 4 2 e e . . .
            . . . . . 2 2 e e e e . . . . .
            """),
      img("""
            . . . . . . . . . . b b b . . .
            . . . . . . . . b e e 3 3 b . .
            . . . . . . b b e 3 2 e 3 a . .
            . . . . b b 3 3 e 2 2 e 3 3 a .
            . . b b 3 3 3 3 3 e e 3 3 3 a .
            b b 3 3 3 3 3 3 3 3 3 3 3 3 3 a
            b 3 3 3 d d d d 3 3 3 3 3 d d a
            b b b b b b b 3 d d d d d d 3 a
            b d 5 5 5 5 d b b b a a a a a a
            b 3 d d 5 5 5 5 5 5 5 d d d d a
            b 3 3 3 3 3 3 d 5 5 5 d d d d a
            b 3 d 5 5 5 3 3 3 3 3 3 b b b a
            b b b 3 d 5 5 5 5 5 5 5 d d b a
            . . . b b b 3 d 5 5 5 5 d d 3 a
            . . . . . . b b b b 3 d d d b a
            . . . . . . . . . . b b b a a .
            """),
      img("""
            . . . . . 3 3 b 3 3 d d 3 3 . .
            . . . . 3 1 1 d 3 d 1 1 1 1 3 .
            . . . 3 d 1 1 1 d 1 1 1 d 3 1 3
            . . 3 d d 1 1 1 d d 1 1 1 3 3 3
            . 3 1 1 d 1 1 1 1 d d 1 1 b . .
            . 3 1 1 1 d 1 1 1 1 1 d 1 1 3 .
            . b d 1 1 1 d 1 1 1 1 1 1 1 3 .
            . 4 b 1 1 1 1 d d 1 1 1 1 d 3 .
            . 4 4 d 1 1 1 1 1 1 d d d b b .
            . 4 d b d 1 1 1 1 1 1 1 1 3 . .
            4 d d 5 b d 1 1 1 1 1 1 1 3 . .
            4 5 d 5 5 b b d 1 1 1 1 d 3 . .
            4 5 5 d 5 5 d b b b d d 3 . . .
            4 5 5 5 d d d d 4 4 b 3 . . . .
            . 4 5 5 5 4 4 4 . . . . . . . .
            . . 4 4 4 . . . . . . . . . . .
            """),
      img("""
            . . . . . . . . . . . . . . . .
            . . . . . . 6 6 6 6 6 6 6 6 . .
            . . . . . 6 c 6 6 6 6 6 6 9 6 .
            . . . . 6 c c 6 6 6 6 6 6 9 c 6
            . . d 6 9 c c 6 9 9 9 9 9 9 c c
            . d 6 6 9 c b 8 8 8 8 8 8 8 6 c
            . 6 6 6 9 b 8 8 b b b 8 b b 8 6
            . 6 6 6 6 6 8 b b b b 8 b b b 8
            . 6 6 6 6 8 6 6 6 6 6 8 6 6 6 8
            . 6 d d 6 8 f 8 8 8 f 8 8 8 8 8
            . d d 6 8 8 8 f 8 8 f 8 8 8 8 8
            . 8 8 8 8 8 8 8 f f f 8 8 8 8 8
            . 8 8 8 8 f f f 8 8 8 8 f f f f
            . . . 8 f f f f f 8 8 f f f f f
            . . . . f f f f . . . . f f f .
            . . . . . . . . . . . . . . . .
            """)]
def UpdateCurPos():
    CursorSprite.x = 60 + 20 * curX
    CursorSprite.y = 90 + 18 * curY
def InitSequence():
    for index3 in range(3):
      AddToSequence()
def NextLevel():
    global PlayerTurn
    PlayerTurn = False
    pause(500)
    for value3 in spriteList:
      value3.destroy()
    AddToSequence()
    PlaySequence()
def AddToSpriteList():
    global x, y, mySprite
    x = CurrentIndex % 9 * 17
    y = Math.floor(CurrentIndex / 9) * 17
    mySprite = sprites.create(imageList], SpriteKind.glyph)
    spriteList = mySprite
    mySprite.left = x + 5
    mySprite.top = y + 20
    music.play_tone(SoundList],
      music.beat(BeatFraction.HALF))

def on_left_pressed():
    global curX
    curX = max(0, curX - 1)
    UpdateCurPos()
controller.left.on_event(ControllerButtonEvent.PRESSED, on_left_pressed)

def on_on_created(sprite):
    sprite.set_flag(SpriteFlag.GHOST, True)
sprites.on_created(SpriteKind.UI, on_on_created)

y = 0
x = 0
CursorAt = 0
SoundList: List = []
PlayerTurn = False
CurrentIndex = 0
CodeSequence: List = []
curY = 0
curX = 0
CursorSprite: Sprite = None
imageList: List = []
mySprite: Sprite = None
spriteList: List = []
index = 0
value = 0
game.show_long_text("Memorize the sequence and replay it", DialogLayout.BOTTOM)
spriteList = sprites.all_of_kind(SpriteKind.glyph)
info.set_score(0)
info.set_life(3)
InitImages()
InitSounds2()
InitUI()
InitSequence()
PlaySequence()

驴友花雕 发表于 2025-9-24 17:59:10

【花雕动手做】基于 Kitronik 游戏机开发板之记忆大师

这是一个基于ARCADE MakeCode的记忆大师游戏(类似西蒙说)代码。

游戏概述
这是一个记忆训练游戏,玩家需要记住系统展示的图案序列,然后按照相同顺序重复这个序列。

代码结构分析

1. 精灵种类定义
python
class SpriteKind:

    glyph = SpriteKind.create()# 图案精灵

    UI = SpriteKind.create()   # 界面元素

2. 核心游戏机制
游戏流程
系统展示:显示一系列图案序列
玩家记忆:观察并记住序列顺序
玩家输入:使用方向键和A键重复序列
验证判断:检查玩家输入是否正确

3. UI界面系统
初始化UI (InitUI)
python
def InitUI():

    # 创建6个图案按钮(2行3列)

    mySprite = sprites.create(imageList, SpriteKind.UI)

    mySprite.set_position(60, 90)# 第一行第一个

    # ... 创建其他5个按钮

   

    # 创建选择光标

    CursorSprite = sprites.create(光标图片, SpriteKind.UI)

    CursorSprite.set_position(60, 90)# 初始位置
界面布局:
text
← 第一行 (y=90)
← 第二行 (y=108)

光标控制
python
def UpdateCurPos():

    CursorSprite.x = 60 + 20 * curX# 水平间隔20像素

    CursorSprite.y = 90 + 18 * curY# 垂直间隔18像素


4. 图案和音效系统
图案资源 (InitImages)
游戏包含6种精美的像素艺术图案:
龙/怪物图案:复杂的生物设计
圆形生物:对称的卡通形象
火焰/水滴:动态效果图案
恐龙:绿色恐龙造型
机器人:机械风格设计
船只/车辆:交通工具图案

音效系统 (InitSounds2)
python
SoundList = # 6个不同频率的音调

5. 游戏逻辑核心
序列播放 (PlaySequence)
python
def PlaySequence():

    for index in range(len(CodeSequence)):

      CurrentIndex = index

      AddToSpriteList()# 显示当前图案

      pause(200)# 每个图案显示200ms

    # 清空显示,准备玩家输入
玩家输入处理
python
def on_a_pressed():

    if PlayerTurn == True:

      CursorAt = curX + curY * 3# 计算选择的图案索引

      if CursorAt == CodeSequence:# 判断是否正确

            info.change_score_by(1)# 得分

            CurrentIndex += 1

            if CurrentIndex == len(CodeSequence):# 完成当前级别

                NextLevel()

      else:# 错误选择

            info.change_life_by(-1)# 扣生命值

6. 序列管理系统
生成序列
python
def AddToSequence():

    CodeSequence.append(randint(0, 5))# 随机添加0-5的图案索引



def InitSequence():

    for index in range(3):# 初始序列长度为3

      AddToSequence()
下一关卡
python
def NextLevel():

    PlayerTurn = False

    AddToSequence()# 增加序列长度

    PlaySequence()   # 播放新序列

7. 显示系统
图案显示 (AddToSpriteList)
python
def AddToSpriteList():

    x = CurrentIndex % 9 * 17# 水平位置计算

    y = Math.floor(CurrentIndex / 9) * 17# 垂直位置计算

    mySprite = sprites.create(imageList[图案索引], SpriteKind.glyph)

    mySprite.left = x + 5# 设置位置

    mySprite.top = y + 20

    music.play_tone(对应音调, music.beat(BeatFraction.HALF))# 播放音效

驴友花雕 发表于 2025-9-24 18:03:27

【花雕动手做】基于 Kitronik 游戏机开发板之记忆大师

图形编程参考实验程序



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



实验场景记录







驴友花雕 发表于 2025-9-24 18:10:15

【花雕动手做】基于 Kitronik 游戏机开发板之记忆大师


页: [1]
查看完整版本: 【花雕动手做】基于 Kitronik 游戏机开发板之记忆大师