错误:如何防止敌人的健康状况栏重置?

时间:2019-05-02 23:53:42

标签: python python-3.x tkinter

我目前正在使用python和tkinter开发类似RPG的游戏。 问题在于,在按下“治疗”按钮,然后再按下任意攻击按钮之后,敌人的健康栏会重置。

我已经尝试了解错误的出处,但这并没有结果。我在每一行之后使用print()来验证值,但是这些都没有回答我的问题。

import tkinter as tk 
from tkinter import font
import time 
import threading
import random

class InGameWindow(object) :
    def __init__(self, master) :
        #Window and it's settings
        self.master = master

        windowWidth = self.master.winfo_screenwidth()
        windowHeight = self.master.winfo_screenheight()

        rootCoord = f"800x400+{int((windowWidth/2)-400)}+{int((windowHeight/2)-200)}"

        master.title("A Simple RPG")
        master.geometry(rootCoord)
        master.resizable(width=False, height=False)
        master.overrideredirect(False)
        master.configure(bg="black")

        #Import files as .png etc...    
        levelIcon = tk.PhotoImage(file="playerInfoIcon.png")
        fireballIcon = tk.PhotoImage(file="fireball.png")
        earthquackeIcon = tk.PhotoImage(file="earthquacke.png")
        strokeIcon = tk.PhotoImage(file="stroke.png")
        healIcon = tk.PhotoImage(file="heal.png")
        startIcon = tk.PhotoImage(file="start.png")

        self.levelIcon = levelIcon
        self.fireballIcon = fireballIcon
        self.earthquackeIcon = earthquackeIcon
        self.strokeIcon = strokeIcon
        self.healIcon = healIcon
        self.startIcon = startIcon

        #Frames to create fancy borders and titles
        mainWindowFrame = tk.Frame(master, width=780, height=380, bg="black")
        mainWindowFrame.place(x=10, y=10)

        attackButtonsFrame = tk.LabelFrame(mainWindowFrame, width=420, height=165, bg="black", relief="ridge", bd=3)
        attackButtonsFrame.place(x=300, y=210)

        playerInfoFrame = tk.LabelFrame(mainWindowFrame, width=240, height=365, bg="black", bd=3)
        playerInfoFrame.place(x=10, y=10)

        self.mainWindowFrame = mainWindowFrame

        #Canvas and stuff related to player's info
        playerInfo = tk.Canvas(playerInfoFrame, bg="black", width=225, height=340, bd=0, highlightthickness=0, relief='ridge')
        playerInfo.place(x=0, y=0)

        self.playerInfo = playerInfo

        #Coordinates for .create_figure/image 
        dynamicHealthBarCoord = [14, 16, 214, 16, 195, 34, 14, 34]
        dynamicManaBarCoord = [13, 16+30, 214-100, 16+30, 195-100, 34+30, 13, 34+30]

        self.dynamicHealthBarCoord, self.dynamicManaBarCoord = dynamicHealthBarCoord, dynamicManaBarCoord


        healthBarForground = playerInfo.create_polygon(14, 15, 214, 16, 196, 34, 14, 34, fill="black", outline="black", width=2)
        dynamicHealthBar = playerInfo.create_polygon(dynamicHealthBarCoord, fill="red", width=1)
        healthBarBackground = playerInfo.create_polygon(11, 13, 220, 13, 196, 37, 11, 37, fill="", outline="gray", width=2)

        manaBarForground = playerInfo.create_polygon(14, 15+30, 214-100, 16+30, 196-100, 34+30, 14, 34+30, fill="black", outline="black", width=2)
        dynamicManaBar = playerInfo.create_polygon(dynamicManaBarCoord, fill="blue", width=1)
        manaBarBackground = playerInfo.create_polygon(11, 13+30, 220-99, 13+30, 196-99, 37+30, 11, 37+30, fill="", outline="gray", width=2)

        self.dynamicHealthBar, self.dynamicManaBar = dynamicHealthBar, dynamicManaBar

        #Buttons
        Attack_Stroke = tk.Button(mainWindowFrame, image=self.strokeIcon, command=lambda: self.Stroke(dynamicHealthBarCoord), cursor="hand1", state="disabled")
        Attack_Stroke.place(x=346.67-10, y=242-15)

        Spell_Heal = tk.Button(mainWindowFrame, image=self.healIcon, command=lambda: self.Heal(dynamicHealthBarCoord), cursor="hand1", state="disabled")
        Spell_Heal.place(x=533.33+10, y=242-15)

        Attack_Fireball = tk.Button(mainWindowFrame, image=self.fireballIcon, command=lambda: self.FireBall(dynamicHealthBarCoord, dynamicManaBarCoord), cursor="hand1", state="disabled")
        Attack_Fireball.place(x=346.67-10, y=242+60)

        Attack_Earthquake = tk.Button(mainWindowFrame, image=self.earthquackeIcon, command=lambda: self.EarthQuacke(dynamicHealthBarCoord, dynamicManaBarCoord), cursor="hand1", state="disabled")
        Attack_Earthquake.place(x=533.33+10, y=242+60)

        Enter_The_Dunjon = tk.Button(self.master, image=self.startIcon, command=self.Dunjon, cursor="hand1")
        Enter_The_Dunjon.place(x=440, y=67)

        self.Enter_The_Dunjon = Enter_The_Dunjon

        self.Attack_Stroke, self.Spell_Heal, self.Attack_Fireball, self.Attack_Earthquake =Attack_Stroke, Spell_Heal, Attack_Fireball, Attack_Earthquake

        manaRegenerationThread = threading.Thread(target=lambda: self.manaRegeneration(self.dynamicManaBarCoord))
        manaRegenerationThread.setDaemon(True)
        manaRegenerationThread.start()

        deathEventHandlerThread = threading.Thread(target=lambda: self.deathEventHandler(self.dynamicHealthBarCoord))
        deathEventHandlerThread.setDaemon(True)
        deathEventHandlerThread.start()

        #m = threading.Thread(target=lambda: manaRegeneration(dynamicManaBarCoord))
        #m.start()

    def Stroke(self, healthCoordList) :
        healthCoordList[2], healthCoordList[4] = healthCoordList[2] - 20, healthCoordList[4] - 20
        self.ennemyInfo.coords(self.ennemyDynamicHealthBar, healthCoordList)

    def Heal(self, healthCoordList) :
        healthCoordList[2], healthCoordList[4] = 214, 195
        self.playerInfo.coords(self.dynamicHealthBar, healthCoordList)

    def FireBall(self, healthCoordList, manaCoordList) :
        healthCoordList[2], healthCoordList[4] = healthCoordList[2] - 20, healthCoordList[4] - 20
        self.ennemyInfo.coords(self.ennemyDynamicHealthBar, healthCoordList)

        manaCoordList[2], manaCoordList[4] = manaCoordList[2] - 5, manaCoordList[4] - 5
        self.playerInfo.coords(self.dynamicManaBar, manaCoordList)
        self.master.update()

        i = threading.Thread(target=self.timerFireBall)
        i.setDaemon(True)
        i.start()

    def EarthQuacke(self, healthCoordList, manaCoordList) :
        healthCoordList[2], healthCoordList[4] = healthCoordList[2] - 20, healthCoordList[4] - 20
        self.ennemyInfo.coords(self.ennemyDynamicHealthBar, healthCoordList)

        manaCoordList[2], manaCoordList[4] = manaCoordList[2] - 25, manaCoordList[4] - 25
        self.playerInfo.coords(self.dynamicManaBar, manaCoordList)
        self.master.update()

        i = threading.Thread(target=self.timerEarthQuacke)
        i.setDaemon(True)
        i.start()

    def timerStroke(self) :
        self.Attack_Stroke.configure(state="disabled")
        self.master.update()

        time.sleep(0)

        self.Stroke.configure(state="active")
        self.master.update()

    def timerHeal(self) :
        self.Spell_Heal.configure(state="disabled")
        self.master.update()

        time.sleep(0)

        self.Spell_Heal.configure(stae="active")
        self.master.upgrade()

    def timerFireBall(self) :
        self.Attack_Fireball.configure(state="disabled")
        self.master.update()

        time.sleep(0)

        self.Attack_Fireball.configure(state="active")
        self.master.update()

    def timerEarthQuacke(self) :
        self.Attack_Earthquake.configure(state="disabled")
        self.master.update()

        time.sleep(0)

        self.Attack_Earthquake.configure(state="active")
        self.master.update()

    def manaRegeneration(self, manaCoordList) :
        while True :
            if manaCoordList[2] < 114 :
                manaCoordList[2], manaCoordList[4] = manaCoordList[2] + 1, manaCoordList[4] + 1
                self.playerInfo.coords(self.dynamicManaBar, manaCoordList)
                self.master.update()
                time.sleep(0.90)

            elif manaCoordList[2] >= 114 :
                manaCoordList[2], manaCoordList[4] = manaCoordList[2] + 0, manaCoordList[4] + 0
                self.playerInfo.coords(self.dynamicManaBar, manaCoordList)
                self.master.update()

    def deathEventHandler(self, healthCoordList) :
        while True :
            if healthCoordList[2] < 10 :
                self.master.destroy()

    def Dunjon(self) :
        self.Enter_The_Dunjon.place_forget()

        ennemyInfoFrame = tk.LabelFrame(self.mainWindowFrame, width=420, height=165, bg="black", bd=3)
        ennemyInfoFrame.place(x=300, y=10)

        ennemyInfo = tk.Canvas(ennemyInfoFrame, bg="black", width=415, height=160, bd=0, highlightthickness=0, relief='ridge')
        ennemyInfo.place(x=0, y=0)

        ennemyDynamicHealthBarCoord = [14, 16, 214, 16, 195, 34, 14, 34] 

        ennemyHealthBarForground = ennemyInfo.create_polygon(14, 15, 214, 16, 196, 34, 14, 34, fill="black", outline="black", width=2)
        ennemyDynamicHealthBar = ennemyInfo.create_polygon(ennemyDynamicHealthBarCoord, fill="red", width=1)
        ennemyHealthBarBackground = ennemyInfo.create_polygon(11, 13, 220, 13, 196, 37, 11, 37, fill="", outline="gray", width=2)

        self.ennemyInfo, self.ennemyDynamicHealthBar = ennemyInfo, ennemyDynamicHealthBar

        self.Attack_Stroke.configure(state="active")
        self.Attack_Fireball.configure(state="active")
        self.Attack_Earthquake.configure(state="active")
        self.Spell_Heal.configure(state="active")

root = tk.Tk()
InGameWindow(root)
root.mainloop()

如果有人找到解决该错误的方法,我也将不胜感激,也不要犹豫纠正我的帖子。非常感谢您的帮助 ! ^^(很抱歉我是法国学生,可能会犯英语错误)

1 个答案:

答案 0 :(得分:0)

在按钮Spell_Heal中,您使用敌人列表self.Heal(dynamicHealthBarCoord),因此它会更改敌人的值,稍后您会看到此更改为“重置”。

可能您应该使用self.Heal(dynamicManaBarCoord)来更改玩家的健康状况。

Spell_Heal = tk.Button(mainWindowFrame, text='healIcon', 
       command=lambda: self.Heal(dynamicManaBarCoord), cursor="hand1", state="disabled")

您可能还必须在self.ennemyDynamicHealthBar中更改Heal()。可能应该是self.dynamicManaBar,但我不确定。

def Heal(self, healthCoordList) :
    healthCoordList[2], healthCoordList[4] = 214, 195
    self.playerInfo.coords(self.dynamicManaBar, healthCoordList)