Python实现命令非阻塞冷却功能 用于《JoJo的遗产》替身游戏开发
《JoJo的遗产》技能非阻塞冷却实现方案
核心实现思路
- 放弃用
time.sleep()实现冷却的逻辑,改为时间戳差值校验方案,全程不会阻塞主程序运行 - 预先为每个技能绑定固定冷却时长,用字典存储配置
- 新增字典存储每个技能的上次成功使用的时间戳,初始值设为0
- 每次收到玩家技能输入后,先做冷却校验:
- 取当前时间戳,计算和该技能上次使用时间的差值
- 差值大于等于冷却时长:允许释放技能,更新该技能的上次使用时间为当前时间戳
- 差值小于冷却时长:直接提示冷却剩余时间,跳过技能执行逻辑
- 原有时停的5秒阻塞sleep如果需要优化,可以给目标添加「时停Debuff」标记,记录时停结束时间,主循环中判断时间未到则限制目标行动即可,无需阻塞整个程序
改造后代码示例
import random import time dummy=300 # 公用冷却配置,可根据不同替身单独调整 twhv_cooldown = { "e": 1, # barrage 冷却1s "r": 2, # 重攻击冷却2s "h": 8, # 三重Muda冷却8s "t": 5, # 24伤飞刀冷却5s "standofft": 2, # 8伤飞刀冷却2s "y": 7, # 左轮手枪冷却7s "f": 20, # 时停冷却20s "n": 3 # 嘲讽冷却3s } sp_cooldown = { "e": 1, # barrage 冷却1s "r": 2, # 碎颅击冷却2s "standofft": 5, # 左轮冷却5s "t": 6, # 流星指刺冷却6s "y": 10, # 眩晕冷却10s "f": 15, # 单手攻击冷却15s "g": 8, # 飞踢冷却8s "h": 25, # 白金之星时停冷却25s "z": 30, # 反击buff冷却30s "n":3 # 嘲讽冷却3s } while True: standopt=input("Choose between TWHV and SP.\nIf you already chose a Stand, you're choosing again due to the dummy's hospitalisation.").lower() if standopt=="twhv": # 初始化当前替身的技能使用时间记录 last_used = {k:0 for k in twhv_cooldown.keys()} while True: if dummy>=1: Quotes=["I won! You proved every weakness I predicted you had!","Johnny Joestar...you're my ultimate test, and I've accepted this test to become stronger.","Seems my resolve had bested yours, hadn't it?"] BasicMoves=[random.choice(Quotes)] Utility=["Knives Volley (24)","Knives Volley (8)","Pocket Revolver (40)"] Unique=["Triple Muda (90)"] Ultimate=["ZA WARUDO! ORE DAKE NO JIKAN DAZE! (5 second Timestop)"] Action = str(input("Your Stand awaits instructions...(q, e, r, t, standofft, y, h, f, n)")) current_time = time.time() # 退出指令不需要冷却 if Action == "q": print("Your Stand goes back into dormancy...") time.sleep(1.5) break # 校验技能是否在冷却表中 if Action not in twhv_cooldown: print("Please make a correct input.") continue # 冷却校验 if current_time - last_used[Action] < twhv_cooldown[Action]: remain = round(twhv_cooldown[Action] - (current_time - last_used[Action]), 1) print(f"该技能尚未冷却,剩余{remain}秒") continue # 校验通过,执行技能逻辑,更新使用时间 last_used[Action] = current_time if Action=="e": print("Barrage (28)") dummy=dummy-28 print("Your dummy is now on",dummy,"HP.") elif Action=="r": print("Heavy Punch (30)") dummy=dummy-30 print("Your dummy is now on",dummy,"HP.") elif Action=="h": print("Triple Muda (90)") dummy=dummy-90 print("Your dummy is now on",dummy,"HP.") elif Action=="t": print(Utility[0]) dummy=dummy-24 print("Your dummy is now on",dummy,"HP.") elif Action=="standofft": print(Utility[1]) dummy=dummy-8 print("Your dummy is now on",dummy,"HP.") elif Action=="y": print(Utility[2]) dummy=dummy-40 print("Your dummy is now on",dummy,"HP.") elif Action=="f": print(Ultimate[0]) print("The dummy has been timestopped!") # 若要非阻塞时停可替换为debuff标记逻辑 time.sleep(5) elif Action=="n": print(BasicMoves[0]) print("The dummy feels taunted! (This means nothing to a soulless figure.)") else: break elif standopt=="sp": last_used = {k:0 for k in sp_cooldown.keys()} while True: if dummy>=1: Quotes=["Good grief...","Get lost.","I take things to heart, you know!!","You truly are the lowest scum in history..."] BasicMoves=["Barrage (69)","Skull Smash (42)",random.choice(Quotes)] Utility=["Revolver (30)","Ora With the Kicks (80)"] Unique=["Star Finger (35)","Inhale (Stun)","One-Hand Their ### (100)"] Ultimate=["SUTAR PURACHINA! Ore ga toki no tometa. (5 second Timestop)","You pissed me off! (Counter)"] Action = str(input("Your Stand awaits instructions...(q, e, r, t, standofft, y, g, h, z, n)")) current_time = time.time() if Action == "q": print("Your Stand goes back into dormancy...") time.sleep(1.5) break if Action not in sp_cooldown: print("Please make a correct input.") continue if current_time - last_used[Action] < sp_cooldown[Action]: remain = round(sp_cooldown[Action] - (current_time - last_used[Action]), 1) print(f"该技能尚未冷却,剩余{remain}秒") continue last_used[Action] = current_time if Action=="e": print(BasicMoves[0]) dummy=dummy-69 print("Your dummy is now on",dummy,"HP.") elif Action=="r": print(BasicMoves[1]) dummy=dummy-42 print("Your dummy is now on",dummy,"HP.") elif Action=="standofft": print(Utility[0]) dummy=dummy-30 print("Your dummy is now on",dummy,"HP.") elif Action=="t": print(Unique[0]) dummy=dummy-35 print("Your dummy is now on",dummy,"HP.") elif Action=="y": print(Unique[1]) print("The dummy is disoriented!.") elif Action=="f": print(Unique[2]) dummy=dummy-100 print("Your dummy is now on",dummy,"HP.") elif Action=="g": print(Utility[1]) dummy=dummy-80 print("Your dummy is now on",dummy,"HP.") elif Action=="h": print(Ultimate[0]) print("The dummy has been timestopped!") time.sleep(5) elif Action=="z": print(Ultimate[1]) print("*2x ATK & DEF, 1.5x ATKSpd") elif Action=="n": print(BasicMoves[2]) print("The dummy feels taunted! (This means nothing to a soulless figure.)") else: break else: print("An incorrect input was read. Please re-enter your choice.") time.sleep(2) continue
进阶优化方向
如果需要实现冷却结束自动提示、时停状态下依然接受玩家输入等更顺滑的体验,可以引入curses库实现终端非阻塞输入,或改用多线程分离输入监听、逻辑计算、画面渲染三个模块。
内容的提问来源于stack exchange,提问作者Saturdatwal
相关产品推荐
相关产品推荐

