《抖音弹幕游戏开发专栏》是优雅草建立的专栏,由优雅草资深开发工程师云桂提供实战教学配对发布有对应的视频教程,以下内容为技术文稿,卓伊凡辅助。
抖音弹幕游戏开发之第11集:礼物触发功能·优雅草云桧·卓伊凡
第11集:礼物触发功能
礼物数据字段
|--------------|--------------|
| 字段 | 说明 |
| giftName | 礼物名称(如"玫瑰花") |
| giftCount | 礼物数量 |
| diamondCount | 礼物单价 |
| name | 送礼用户昵称 |
礼物触发规则设计
|-----|--------|
| 礼物 | 效果 |
| 玫瑰花 | 视角左右摇摆 |
| 爱心 | 连续跳跃 |
| 火箭 | 疯狂旋转 |
添加礼物触发代码
elif msg_type == '礼物':
gift_name = data.get('giftName', '')
gift_count = data.get('giftCount', 0)
name = data.get('name', '')
print(f"🎁 [礼物] {name} 送了 {gift_count} 个 {gift_name}")
# 玫瑰花:视角摇摆
if '玫瑰' in gift_name:
print("触发视角摇摆!")
repeat_count = min(gift_count, 5) # 最多5次
for _ in range(repeat_count):
pyautogui.moveRel(100, 0, duration=0.1)
pyautogui.moveRel(-100, 0, duration=0.1)
print(f"摇摆了{repeat_count}次")
# 爱心:连续跳跃
elif '爱心' in gift_name:
print("触发连续跳跃!")
repeat_count = min(gift_count, 3) # 最多3次
for _ in range(repeat_count):
pyautogui.press('space')
time.sleep(0.3)
print(f"跳跃了{repeat_count}次")
# 火箭:疯狂旋转
elif '火箭' in gift_name:
print("触发疯狂旋转!")
for _ in range(20): # 固定20次
pyautogui.moveRel(100, 0, duration=0.05)
print("旋转完成!")
限制触发次数
使用 min(gift_count, 5) 限制最大次数,避免效果过长。
礼物名称兼容性
不同平台礼物名称可能不同:
vb
`if '玫瑰' in gift_name or 'rose' in gift_name.lower():
# 触发效果`
本集总结
- ✅ 设计礼物触发规则
- ✅ 实现玫瑰花视角摇摆效果
- ✅ 实现爱心连续跳跃效果
- ✅ 实现火箭疯狂旋转效果
- ✅ 添加触发次数限制
下一集:添加冷却时间机制