我正在尝试创建一个基于回合的游戏图形用户界面,我想一个按钮说,球员攻击,然后将减去一个随机整数之间的两个设定点从另一个标签称为怪物健康。目前,我已经设置为正常的数字,而不是随机的,以更清楚地表明我的问题。你知道吗
我尝试过改变格式,并将当前游戏文件中的代码导入GUI程序
from Tkinter import *
import random
root = Tk()
def phealth(): #playerhealth
(100)
def mhealth(): #monsterhealth
(99)
def pheal(): #playerheal
(rand.randint(10))
def mheal(): #monsterheal
(10)
def pattackf(): #player attack value
(10)
def mattackf(): #monster attack value
(10)
def attackm(): #attack monster
(mhealth-pattackf)
def attackp(): #attack player
(phealth-mattackf)
def healhm(): #heal monster
(mhealth+mheal)
def healp(): #heal player
(phealth+pheal)
def middleClick(event):
mhealth=attackm
print('welcome to monsterland, try not to die')
print('It is time to name your character, what would you like to be called?')
player1= raw_input()
playername = Label(root, text = "PLAYER NAME: " + str.upper(player1), fg="Red")
playername.grid(row=0, column= 2, sticky= "E")
attack = Label(root, text= "Attack Monster")
attack.grid(row=0,column=0, sticky="W")
attackbutton = Button(root, text = 10)
attackbutton.grid(row=0,column=1)
heal = Label(root, text= "Heal Self")
heal.grid(row=1,column=0, sticky="W")
healbutton = Button(root, text = 12)
healbutton.grid(row=1,column=1)
playerhealth = Label(root, text= "Player Health")
playerhealth.grid(row=2,column=0, sticky="W")
phealthdisplay = Label(root,text= 100)
phealthdisplay.grid(row=2,column=1)
monsterhealth = Label(root, text= "Monster Health")
monsterhealth.grid(row=3,column=0, sticky="W")
mhealthdisplay = Label(root, text= 100)
mhealthdisplay.grid(row=3,column=1)
root.geometry('500x500')
#root.configure(background='Red')
root.mainloop()
我想能够从mhealthdisplay中按下player attack和minus,我还想按下player heal和plus heal按钮value来显示player health上面的def函数是我在等式中使用的尝试,但是已经删除了,因为它们不在上面工作,无法演示当前的尝试。非常感谢。你知道吗
像下面这样的函数没有效果。我添加了一些评论
我猜你更想:
现在你只需要确保你的按钮能调用你的函数。代码如下所示:
如果您想摆脱输入语句,这样用户就不必在控制台和gui之间切换,您只需添加:
删除带有
input_raw
的行,并更改带有播放器标签的行,如下所示:还有一个建议: 你好像在用Pyhton2。如果你现在正在学习Python,你应该考虑直接学习Python3。因此,如果您在Python3中也找到了教程,请尝试切换到这个版本,如果没有,您也可以继续使用Python2,但是您可能找不到所有的库,您需要稍后才能找到,因为对Python2的支持正在减少。不幸的是,Python2和Python3有一些更大的区别。例如,将某些内容打印到控制台的方式不同,其他部分也有点不同(如输入、范围和除以整数)。如果你直接在Python3中学习,你就不需要重新学习什么是不同的。你知道吗
相关问题 更多 >
编程相关推荐