[英]I'm trying to make a code so when the player attacks the enemy, the enemy's health goes down and doesn't reset
[英]BUG: How to prevent the enemy's health bar to reset?
我目前正在使用 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()
如果有人发现如何修复此错误,我将非常感激,也请不要犹豫更正我的帖子。 非常感谢您的帮助 ! ^^(抱歉可能有英语错误,我是法国学生)
在按钮Spell_Heal
您使用敌人列表self.Heal(dynamicHealthBarCoord)
因此它会更改敌人的值,稍后您会将此更改视为“重置”。
也许你应该使用self.Heal(dynamicManaBarCoord)
来改变玩家的健康状况。
Spell_Heal = tk.Button(mainWindowFrame, text='healIcon',
command=lambda: self.Heal(dynamicManaBarCoord), cursor="hand1", state="disabled")
您可能还需要更改Heal()
self.ennemyDynamicHealthBar
。 可能它应该是self.dynamicManaBar
但我不确定。
def Heal(self, healthCoordList) :
healthCoordList[2], healthCoordList[4] = 214, 195
self.playerInfo.coords(self.dynamicManaBar, healthCoordList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.