优化了上版代码,使用了streamlit展示,效果如图所示
以下为代码
import random
import streamlit as st
class Enemy:
def __init__(self, name):
self.hit = 0
self.hit_all = 0
self.time = 0.0
self.flag = True
self.restart = True
self.jihua = 1
self.gap = 0.0
self.name = name
def renew(self):
self.hit = 0
self.hit_all = 0
self.time = 0.0
self.flag = True
self.restart = True
self.jihua = 1
self.gap = 0.0
def do_restart(self, time):
self.restart = True
self.hit = 1
self.hit_all += 1
self.jihua += 1
self.time = time
# print(f"{self.name}在{time}时被击中了,这次攻击是激化")
def enemy_time_hit(self, time=0.0):
if self.flag:
self.time = time
self.flag = False
# print(f"{self.name}在{time}时被击中了,这次攻击是激化")
self.hit += 1
self.hit_all += 1
else:
self.gap = time - self.time
if self.hit == 3 or self.gap >= 2.5:
self.do_restart(time)
else:
self.restart = False
self.hit += 1
self.hit_all += 1
# print(f"{self.name}在{time}时被击中了,这次攻击不是激化")
def jihua_rate(self):
return self.jihua, self.hit_all
def create_sha_sheng_ying(head):
return [head, head+3, head+6, head+9, head+12]
def main():
st.title("八重神子杀生樱激化概率")
st.header("标准激化概率(2.5s / 3hit)")
st.image('yea_miko.png')
gap = float(st.selectbox("杀生樱释放间隔 /s", ['0.25', '0.5', '1']))
times = int(st.selectbox("循环次数", ['10', '100', '500']))
enemy_num = int(st.selectbox("敌人数量", ['1', '2', '3', '5', '8']))
flag = st.button('点击计算')
s = []
enemy = []
for i in range(enemy_num):
enemy.append(Enemy(i))
ssy = create_sha_sheng_ying(0) + create_sha_sheng_ying(gap) + create_sha_sheng_ying(2 * gap)
ssy.sort()
num = []
all = []
for time in range(times):
for k in range(enemy_num):
enemy[k].renew()
for i in range(len(ssy)):
k = random.randint(0, enemy_num-1)
enemy[k].enemy_time_hit(ssy[i])
for j in range(enemy_num):
temp1, temp2 = enemy[j].jihua_rate()
num.append(temp1)
all.append(temp2)
s.append(sum(num) / sum(all))
if flag:
st.header(round(sum(s), 2))
if __name__ == '__main__':
main()
请使用 streamlit run 来启动