代码解读
这是一个经典的"捉人游戏",支持最多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.player_to_index(value)]) # 分配精灵
-
- 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[0] == mp.get_player_sprite(mp.PlayerNumber.ONE):
-
- game.splash("Player 1 Wins!")
-
- # ... 其他玩家类似
-
- game.reset() # 重置游戏
复制代码
游戏机制总结
多人竞技:支持2-4名玩家
生命系统:每个玩家有3条生命
时间压力:"It"有10秒时间捉人,否则自己受罚
角色转换:被捉到的玩家成为新的"It"
淘汰机制:生命值为零的玩家被淘汰
胜利条件:最后剩下的玩家获胜
|