tkinter中的游戏功能(基于回合)

2024-10-04 01:36:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个基于回合的游戏图形用户界面,我想一个按钮说,球员攻击,然后将减去一个随机整数之间的两个设定点从另一个标签称为怪物健康。目前,我已经设置为正常的数字,而不是随机的,以更清楚地表明我的问题。你知道吗

我尝试过改变格式,并将当前游戏文件中的代码导入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函数是我在等式中使用的尝试,但是已经删除了,因为它们不在上面工作,无法演示当前的尝试。非常感谢。你知道吗


Tags: totextdefcolumnrootlabelgridrow
1条回答
网友
1楼 · 发布于 2024-10-04 01:36:07

像下面这样的函数没有效果。我添加了一些评论

def mattackf(): #monster attack value
    (10) 
    # (10) is just the same as if you write 10
    # it creates an int object with value 10
    # and does nothing with it
    # I guess you wanted to assign 10 to a 
    # variable called attackf (see my suggestion)

def attackm(): #attack monster
    (mhealth-pattackf)
# this code subtracts what is named pattackf
# from what is named mhealt
# in your case both are functions
# if you want to execute this code, you have
# to call it like with
# attackm()
# this will rais an error and say, that it can't
# subtract a function from another function
# I guess you wanted to decrease mhealth (see below)

我猜你更想:

# define phealth and mhealth to be the variables 
# of the health value with which you can calculate
# and set them to 100/99, then you would do:
phealth= 100
mhealth= 99

# use a fixed value whenever the heal button for
# the monster is pressed and a random value to
# be used whenever the heal button for the player 
# is used:
mheal=    10

def pheal():    #playerheal
    return rand.randint(1, 10)

# now the functions to apply those heal values:

def healhm(): #heal monster
    # the global statement tells python that
    # you work with the globally defined variables
    # otherwise it would assume, that the
    # variables are only visible in the function
    global mhealth, mheal
    # add the value in mheal to the value of mhealth
    # and store the result in mhealth
    mhealth= mhealth + mheal

def healp(): #heal player
    # again tell python that pheal is a global name
    global phealth
    print 'healing player', phealth
    # like for mheal, but this time we
    # call pheal() and add it's return
    # value to phealth
    phealth= phealth + pheal()

def attackm(): #attack monster
    global mhealth, pattackf
    mhealth= mhealth-pattackf

现在你只需要确保你的按钮能调用你的函数。代码如下所示:

attackbutton = Button(root, text = 10, command=lambda: attackm())
attackbutton.grid(row=0,column=1)

healbutton = Button(root, text = 12, command=lambda:healp())
healbutton.grid(row=1,column=1)

如果您想摆脱输入语句,这样用户就不必在控制台和gui之间切换,您只需添加:

v_pname= tk.StringVar()
v_pname.set('Enter the Name here')

playerentry= Entry(root, textvariable=v_pname)
playerentry.grid(row=0, column= 3, sticky= "E")

删除带有input_raw的行,并更改带有播放器标签的行,如下所示:

playername = Label(root, text = "PLAYER NAME:", fg="Red")
playername.grid(row=0, column= 3, sticky= "W")

还有一个建议: 你好像在用Pyhton2。如果你现在正在学习Python,你应该考虑直接学习Python3。因此,如果您在Python3中也找到了教程,请尝试切换到这个版本,如果没有,您也可以继续使用Python2,但是您可能找不到所有的库,您需要稍后才能找到,因为对Python2的支持正在减少。不幸的是,Python2和Python3有一些更大的区别。例如,将某些内容打印到控制台的方式不同,其他部分也有点不同(如输入、范围和除以整数)。如果你直接在Python3中学习,你就不需要重新学习什么是不同的。你知道吗

相关问题 更多 >