驴友花雕 发表于 2025-9-1 17:35:30

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




驴友花雕 发表于 2025-9-1 17:36:57

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

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

MicroPython实验参考代码

def checkIfWon():
    if len(sprites2) == 1:
      if sprites2 == mp.get_player_sprite(mp.PlayerNumber.ONE):
            game.splash("Player 1 Wins!")
      elif sprites2 == mp.get_player_sprite(mp.PlayerNumber.TWO):
            game.splash("Player 2 Wins!")
      elif sprites2 == mp.get_player_sprite(mp.PlayerNumber.THREE):
            game.splash("Player 3 Wins!")
      else:
            game.splash("Player 4 Wins!")
      game.reset()

def on_life_zero(player2):
    global it
    mp.get_player_sprite(player2).destroy(effects.fire, 500)
    sprites2.remove_at(sprites2.index_of(mp.get_player_sprite(player2)))
    if it == mp.get_player_sprite(player2):
      it.say_text("", 2000, False)
      it = sprites2._pick_random()
      youreIt()
    checkIfWon()
mp.on_life_zero(on_life_zero)

def youreIt():
    it.say_text("It", 2000, False)
    it.set_position(randint(5, 155), randint(15, 115))
    it.set_kind(SpriteKind.enemy)
    info.start_countdown(10)

def on_countdown_end():
    global it
    it.set_kind(SpriteKind.player)
    reduceLife(it)
    it = sprites2._pick_random()
    youreIt()
info.on_countdown_end(on_countdown_end)

def reduceLife(sprite: Sprite):
    if sprite == mp.get_player_sprite(mp.PlayerNumber.ONE):
      mp.change_player_state_by(mp.PlayerNumber.ONE, MultiplayerState.lives, -1)
    elif sprite == mp.get_player_sprite(mp.PlayerNumber.TWO):
      mp.change_player_state_by(mp.PlayerNumber.TWO, MultiplayerState.lives, -1)
    elif sprite == mp.get_player_sprite(mp.PlayerNumber.THREE):
      mp.change_player_state_by(mp.PlayerNumber.THREE, MultiplayerState.lives, -1)
    else:
      mp.change_player_state_by(mp.PlayerNumber.FOUR, MultiplayerState.lives, -1)

def on_on_overlap(sprite2, otherSprite):
    global it
    info.stop_countdown()
    it.say_text("", 2000, False)
    reduceLife(sprite2)
    it = sprite2
    otherSprite.set_kind(SpriteKind.player)
    youreIt()
sprites.on_overlap(SpriteKind.player, SpriteKind.enemy, on_on_overlap)

it: Sprite = None
sprites2: List = []
game.show_long_text("Once all your friends have joined, hit A to continue.",
    DialogLayout.CENTER)
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)
sprites2 = [sprites.create(img("""
            2 2 2 2 2 2 2 2
            2 2 2 1 1 2 2 2
            2 2 2 2 1 2 2 2
            2 2 2 2 1 2 2 2
            2 2 2 2 1 2 2 2
            2 2 2 2 1 2 2 2
            2 2 1 1 1 1 1 2
            2 2 2 2 2 2 2 2
            """),
      SpriteKind.player),
    sprites.create(img("""
            8 8 8 8 8 8 8 8
            8 8 1 1 1 1 8 8
            8 8 1 8 8 1 8 8
            8 8 8 8 8 1 8 8
            8 8 8 8 1 8 8 8
            8 8 8 1 8 8 8 8
            8 8 1 1 1 1 1 8
            8 8 8 8 8 8 8 8
            """),
      SpriteKind.player),
    sprites.create(img("""
            4 4 4 4 4 4 4 4
            4 4 1 1 1 1 4 4
            4 4 1 4 4 1 4 4
            4 4 4 4 4 1 4 4
            4 4 4 4 1 4 4 4
            4 4 1 4 4 1 4 4
            4 4 1 1 1 1 4 4
            4 4 4 4 4 4 4 4
            """),
      SpriteKind.player),
    sprites.create(img("""
            7 7 7 7 7 7 7 7
            7 1 7 7 7 1 7 7
            7 1 7 7 7 1 7 7
            7 1 7 7 7 1 7 7
            7 1 1 1 1 1 7 7
            7 7 7 7 7 1 7 7
            7 7 7 7 7 1 7 7
            7 7 7 7 7 7 7 7
            """),
      SpriteKind.player)]
for value in mp.all_players():
    mp.set_player_sprite(value, sprites2)
    mp.move_with_buttons(value, mp.get_player_sprite(value))
    mp.get_player_sprite(value).set_stay_in_screen(True)
    mp.set_player_state(value, MultiplayerState.lives, 3)
mp.get_player_sprite(mp.PlayerNumber.ONE).set_position(5, 15)
mp.get_player_sprite(mp.PlayerNumber.TWO).set_position(155, 15)
mp.get_player_sprite(mp.PlayerNumber.THREE).set_position(5, 105)
mp.get_player_sprite(mp.PlayerNumber.FOUR).set_position(155, 105)
it = sprites2._pick_random()
youreIt()

驴友花雕 发表于 2025-9-1 17:42:49

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

代码解读

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

1. 游戏初始化
游戏说明
python
game.show_long_text("Once all your friends have joined, hit A to continue.", DialogLayout.CENTER)

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
sprites2 = [sprites.create(img("""..."""), SpriteKind.player),# 玩家1(红色箭头)

    sprites.create(img("""..."""), SpriteKind.player),# 玩家2(橙色数字7?)

    sprites.create(img("""..."""), SpriteKind.player),# 玩家3(绿色数字2?)

    sprites.create(img("""..."""), SpriteKind.player)]# 玩家4(紫色数字4?)
创建4个8x8像素的玩家角色,使用ASCII艺术定义外观

玩家设置
python
for value in mp.all_players():

    mp.set_player_sprite(value, sprites2)# 分配精灵

    mp.move_with_buttons(value, mp.get_player_sprite(value))# 设置按钮控制移动

    mp.get_player_sprite(value).set_stay_in_screen(True)# 限制在屏幕内

    mp.set_player_state(value, MultiplayerState.lives, 3)# 设置3条生命
初始位置设置
python
mp.get_player_sprite(mp.PlayerNumber.ONE).set_position(5, 15)    # 左上角

mp.get_player_sprite(mp.PlayerNumber.TWO).set_position(155, 15)# 右上角

mp.get_player_sprite(mp.PlayerNumber.THREE).set_position(5, 105) # 左下角

mp.get_player_sprite(mp.PlayerNumber.FOUR).set_position(155, 105) # 右下角

2. 游戏核心机制
选择初始"It"
python
it = sprites2._pick_random()# 随机选择一个玩家作为"It"

youreIt()# 启动"It"状态
"It"状态设置函数
python
def youreIt():

    it.say_text("It", 2000, False)# 显示"It"文字2秒

    it.set_position(randint(5, 155), randint(15, 115))# 随机位置

    it.set_kind(SpriteKind.enemy)# 设置为敌人类型

    info.start_countdown(10)# 开始10秒倒计时

3. 碰撞检测系统
python
def on_on_overlap(sprite2, otherSprite):

    global it

    info.stop_countdown()# 停止倒计时

    it.say_text("", 2000, False)# 清除"It"文字

    reduceLife(sprite2)# 被捉到的玩家减命

    it = sprite2# 被捉到的玩家成为新的"It"

    otherSprite.set_kind(SpriteKind.player)# 原"It"变回普通玩家

    youreIt()# 新"It"开始
当普通玩家(SpriteKind.player)与"It"(SpriteKind.enemy)碰撞时触发

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

4. 倒计时结束处理
python
def on_countdown_end():

    global it

    it.set_kind(SpriteKind.player)# "It"变回普通玩家

    reduceLife(it)# "It"失去一条命(因为没捉到人)

    it = sprites2._pick_random()# 随机选择新的"It"

    youreIt()# 新"It"开始
如果"It"在10秒内没有捉到任何人,自己会失去一条命

5. 生命值管理系统
python
def reduceLife(sprite: Sprite):

    if sprite == mp.get_player_sprite(mp.PlayerNumber.ONE):

      mp.change_player_state_by(mp.PlayerNumber.ONE, MultiplayerState.lives, -1)

    # ... 其他玩家类似

6. 生命值为零处理
python
def on_life_zero(player2):

    global it

    mp.get_player_sprite(player2).destroy(effects.fire, 500)# 火焰效果销毁

    sprites2.remove_at(sprites2.index_of(mp.get_player_sprite(player2)))# 从玩家列表中移除

   

    # 如果被淘汰的是当前"It",选择新的"It"

    if it == mp.get_player_sprite(player2):

      it.say_text("", 2000, False)

      it = sprites2._pick_random()

      youreIt()

   

    checkIfWon()# 检查是否有人获胜

7. 胜利条件检查
python
def checkIfWon():

    if len(sprites2) == 1:# 如果只剩一个玩家

      if sprites2 == mp.get_player_sprite(mp.PlayerNumber.ONE):

            game.splash("Player 1 Wins!")

      # ... 其他玩家类似

      game.reset()# 重置游戏

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

驴友花雕 发表于 2025-9-1 17:58:13

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

图形编程参考实验程序



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


驴友花雕 发表于 2025-9-1 18:01:19

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

实验场景记录








页: [1]
查看完整版本: 【花雕动手做】基于Kitronik可编程开发板经典捉人小游戏