Python實現原神抽卡的方法

話不多說,直接貼所有代碼

import random
import sys
import tkinter as tk  # 導入一個第三方庫,用於制作桌面軟件
import tkinter.font as tf
# 數據部分
R = {
    "name": "R",
    "color": "blue",
    "size": "20",
    "font": "微軟雅黑",
    "data": ["冷刃", "黑纓槍", "白纓槍", "翡玉法球", "飛天大禦劍", "暗鐵劍", "旅行劍", "鋼輪弓",
             "吃魚虎刀", "沾染龍血的劍", "以理服人", "異世界行記", "甲級寶鈺", "翡玉法球"],
    "person": []
}
SR = {
    "name": "SR",
    "color": "purple",
    "size": "20",
    "font": "微軟雅黑",
    "data": ["腐殖之劍", "祭禮劍", "西風劍", "試作斬巖", "笛劍", "螭骨劍", "鋼輪弓", "西風獵弓",
             "鋼輪弓", "絕弦", "祭禮弓", "萬國諸海圖譜", "匣裡日月", "千巖古劍", "黑巖緋玉"],
    "person": ["香菱", "菲謝爾", "菲謝爾", "北鬥", "芭芭拉", "北鬥", "凝光", "托馬", "重雲",
              "砂糖", "煙緋", "安柏", "凱亞", "麗莎", "諾艾爾"]
}
SSR = {
    "name": "SSR",
    "color": "yellow",
    "size": "20",
    "font": "微軟雅黑",
    "data": ["天空之卷", "四風原典", "天空之傲", "天空之脊", "風鷹劍", "風鷹劍", "狼的末路"],
    "person": ["迪盧克", "七七", "琴", "莫娜", "刻晴"]
}

ten_count = 0
ninety_count = 0
max_count = 0
person_up = "優菈"
data_up = "松籟響起之時"
ALL = [R, SR, SSR]
tag_id = "0"

# 單抽
def one():
    _res = get()
    count_flush(_res["level"], _res["thing"])
    insert_text(conf=_res["level"], message=_res["thing"])
    text.insert("end", "\n")
    text.see("end")

# 十連抽
def ten():
    text.tag_add('tag', "end")
    text.tag_config('tag', foreground="white")
    text.insert("end", "\nstart\n", 'tag')
    for i in range(10):
        one()
    text.insert("end", f"\nend{ten_count}/{ninety_count}/{max_count}\n", "tag")
    text.see("end")

# 根據抽獎出的物品index獲取物品等級
def found(index):
    for i in ALL:
        if pool[index] in i["person"]:
            return i
        if pool[index] in i["data"]:
            return i

# 每次抽卡後刷新當前計數器
def count_flush(level, thing):
    global ten_count
    global ninety_count
    global max_count
    if level["name"] == "SR":
        ten_count = 0
    if level["name"] == "SSR":
        ninety_count = 0
    if level["name"] == "SSR" and ((thing in person_up) or (thing in data_up)):
        max_count = 0

# 抽卡規則
def get():
    global ten_count
    global ninety_count
    global max_count
    level = None
    ten_count += 1
    ninety_count += 1
    max_count += 1
    if ten_count == 10:
        level = SR
    if ninety_count == 90:
        level = SSR
    if level is SR or level is SSR:
        index = random.randrange(len(level[what]))
        thing = level[what][index]
    if max_count != ninety_count and level is SSR:
        level = SSR
        thing = person_up if what == "person" else data_up
    if max_count == 180:
        level = SSR
        thing = person_up if what == "person" else data_up
    if level is None:
        index = random.randrange(len(pool))
        level = found(index)
        thing = pool[index]
    return {
        "level": level,
        "thing": thing
    }

# 建立一個主窗口 root
root = tk.Tk()
# 設置窗口標題
root.title("原神模擬抽卡器")
# 設置單抽圖片
image_one = tk.PhotoImage(file="單抽圖片.png")
# 設置十連抽圖片
image_ten = tk.PhotoImage(file="十連抽.png")
# 在窗口上創建一個按鈕 button,用於單抽,它依賴於父窗口root
button_one = tk.Button(root, text="單抽", image=image_one, command=one)
button_ten = tk.Button(root, text="十連抽", image=image_ten, command=ten)
# 佈局創建的按鈕,rou代表行,column代表列
button_one.grid(row=0, column=0)
button_ten.grid(row=0, column=1)
# 創建一個文本框,用於打印抽獎日志
text = tk.Text(root, bg="black")
# columnspan代表合並兩列
text.grid(row=1, columnspan=2)

# 添加日志到Text框
def insert_text(message, conf):
    global tag_id
    # 設置字體大小和顏色
    ft = tf.Font(family=conf["font"], size=conf["size"])
    text.tag_add('tag'+tag_id, "end")
    text.tag_config('tag'+tag_id, foreground=conf["color"], font=ft)
    text.insert("end", message + "\n", "tag"+tag_id)
    text.see("end")
    tag_id = str(int(tag_id) + 1)

# mian函數,程序會運行這裡面的東西
if __name__ == '__main__':
    # 修改為武器抽武器池
    what = "角色"
    if what == "角色":
        what = "person"
    if what == "武器":
        what = "data"
    if what not in ["data", "person"]:
        sys.exit(1)
    # 把up角色和武器加入池
    SSR["data"].append(data_up)
    SSR["person"].append(person_up)
    # 合並在一個總池,實現概率,可以通過算法實現,難得弄..
    pool = list()
    for i in range(90):
        pool.extend(R["data"])
    for i in range(10):
        pool.extend(SR[what])
    pool.extend(SSR[what])
    # 運行窗口
    root.mainloop()

運行效果

在這裡插入圖片描述

需要用到的兩張圖片

在這裡插入圖片描述

在這裡插入圖片描述

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: