我再次感到困惑,我从一个名为roll的StringVar()得到一个错误<bound method Player.roll_dice <player.Player object at 0x0000000003569550>
来自player.py文件中的方法
gui.py
try:
from tkinter import *
from player import *
from monster import *
from random import *
except ImportError as err:
print('Import Error',err)
class SimpleGUI:
def __init__(self):
self.window = Tk()
self.window.title('Final Project')
self.window.protocol('WM_DELETE_WINDOW',self.exit)
self.canvas = Canvas(self.window,
width = 245,
height = 550,
bg = 'white')
self.canvas.pack()
self.player = Player()
self.menu = Menu()
self.monster = monster
self.canvas.focus_set()
self.canvas.bind('w', self.w_key)
self.canvas.bind('a', self.a_key)
self.canvas.bind('s', self.s_key)
self.canvas.bind('d', self.d_key)
frame1 = Frame(self.window)
frame1.pack()
self.player_hp = StringVar()
self.player_hp.set(int(self.player.hp))
self.monster_hp = StringVar()
self.monster_hp.set(int(self.monster.e_hp))
Label(frame1, text = "Player HP:").grid(row = 1, column = 1)
ply_hp = Label(frame1, textvariable = self.player_hp).grid(row = 1, column = 2)
Label(frame1, text = "Monster HP:").grid(row = 2, column = 1)
monster_hp = Label(frame1, textvariable = self.monster_hp).grid(row = 2, column = 2)
self.bt_attack = Button(frame1,
text = 'Attack',
command = print('x')).grid(row = 3, column = 0)
self.bt_defend = Button(frame1,
text = 'Defend',
command = print('x')).grid(row = 3, column = 2)
self.dcroll = StringVar()
self.dcroll.set(self.player.roll_dice)
self.bt_roll = Button(frame1,
text = 'Roll Dice',
command = self.player.roll_dice).grid(row = 3, column = 1)
Label(frame1, text = 'YOU ROLLED:').grid(row = 4, column = 1)
roll = Label(frame1, textvariable = self.dcroll).grid(row = 4, column = 2)
self.running = True
self.menu = Menu()
self.bt1 = Button(self.window,
text = 'Start / Reset',
command = self.menu)
self.bt1.pack()
self.mainloop()
self.window.mainloop()
def mainloop(self):
while self.running:
self.canvas.delete(ALL)
#boss square
boss_start = self.canvas.create_rectangle(100,0,150,100, outline = 'black', fill = 'black', width = 2)
#player start square
ply_start = self.canvas.create_rectangle(100,500,150,550, outline = 'black', fill = 'black', width = 2)
#row 9
self.canvas.create_rectangle(0,50,50,100, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,50,100,100, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,50,150,100, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,50,200,100, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,50,250,100, outline = 'black', fill = 'black', width = 2)
#row 8
self.canvas.create_rectangle(0,100,50,150, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,100,100,150, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,100,150,150, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,100,200,150, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,100,250,150, outline = 'black', fill = 'black', width = 2)
#row 7
self.canvas.create_rectangle(0,150,50,200, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,150,100,200, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,150,150,200, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,150,200,200, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,150,250,200, outline = 'black', fill = 'black', width = 2)
#row 6
self.canvas.create_rectangle(0,200,50,250, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,200,100,250, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,200,150,250, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,200,200,250, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,200,250,250, outline = 'black', fill = 'black', width = 2)
#row 5
self.canvas.create_rectangle(0,250,50,300, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,250,100,300, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,250,150,300, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,250,200,300, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,250,250,300, outline = 'black', fill = 'black', width = 2)
#row 4
self.canvas.create_rectangle(0,300,50,350, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,300,100,350, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,300,150,350, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,300,200,350, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,300,250,350, outline = 'black', fill = 'black', width = 2)
#row 3
self.canvas.create_rectangle(0,350,50,400, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,350,100,400, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,350,150,400, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,350,200,400, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,350,250,400, outline = 'black', fill = 'black', width = 2)
#row 2
self.canvas.create_rectangle(0,400,50,450, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,400,100,450, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,400,150,450, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,400,200,450, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,400,250,450, outline = 'black', fill = 'black', width = 2)
#row 1
self.canvas.create_rectangle(0,450,50,500, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(50,450,100,500, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(100,450,150,500, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(150,450,200,500, outline = 'black', fill = 'black', width = 2)
self.canvas.create_rectangle(200,450,250,500, outline = 'black', fill = 'black', width = 2)
self.player.draw(self.canvas)
self.player_hp.set(int(self.player.hp))
self.monster_hp.set(int(self.monster.e_hp))
self.canvas.after(30)
self.canvas.update()
#------Functions------#
def button1(self):
print('working')
def a_key(self, event):
self.player.x -= self.player.speed
if self.player.x < 0:
self.player.x = 0
if self.player.y == 0:
if self.player.x <= 100:
self.player.x = 100
if self.player.y == 500:
if self.player.x <= 100:
self.player.x = 100
def w_key(self, event):
self.player.y -= self.player.speed
if self.player.y < 0:
self.player.y = 0
if self.player.x < 100:
if self.player.y == 0:
self.player.y = 50
if self.player.x > 100:
if self.player.y == 0:
self.player.y = 50
def s_key(self, event):
self.player.y += self.player.speed
if self.player.y > 500:
self.player.y = 500
if self.player.x < 100:
if self.player.y == 500:
self.player.y = 450
if self.player.x > 100:
if self.player.y == 500:
self.player.y = 450
def d_key(self, event):
self.player.x += self.player.speed
if self.player.x > 200:
self.player.x = 200
if self.player.y == 0:
if self.player.x >= 150:
self.player.x = 100
if self.player.y == 500:
if self.player.x >= 150:
self.player.x = 100
def battle(self, monster):
while True:
self.player.attack(monster)
monster.attack(self.player)
self.canvas.after(200)
if self.player.hp <= 0 or monster.e_hp <= 0:
break
def exit(self):
self.running = False
self.window.destroy()
if __name__ == '__main__':
SimpleGUI()
player.py
try:
from random import *
from monster import *
except ImportError as err:
print('Import Error, ', err)
class Player:
def __init__(self, hp = 100, defense = 10, strength = 10):
self.hp = hp
self.defense = defense
self.strength = strength
self.x = 100
self.y = 500
self.speed = 50
self.d1 = randint(1,6)
self.d2 = randint(1,6)
self.roll = (self.d1 + self.d2)
def draw(self, canvas):
canvas.create_oval(self.x,self.y,self.x+50,self.y+50, fill='red')
def roll_dice(self):
self.roll = (self.d1 + self.d2)
self.dcroll = StringVar()
self.dcroll.set(int(self.roll))
def attack(self, other):
attack = self.roll
for x in [7,11,12]:
if attack == x:
hit = monster.e_hp - self.strength
monster.e_hp = hit
break
for x in [2,3,4]:
if attack == x:
player_hit = monster.e_strength - self.hp
self.hp = player_hit
other.e_hp -= self.strength
if __name__ == '__main__':
player = Player()
正如你所看到的,错误“”就在它所说的你所说的位置:。我怎样才能解决这个问题?每次玩家移动时,还有一种简单的方法来调用函数吗?或者,如果x和y值改变函数x上的调用,那么最好写吗?
答案 0 :(得分:0)
请阅读https://stackoverflow.com/help/mcve。你发布的内容太多了,无法阅读。
但我确实看到了以下错误
roll = Label(frame1, textvariable = self.dcroll).grid(row = 4, column = 2)
将roll
绑定到网格调用的结果None
,而不是Label小部件。如果您使用roll
,这必定会导致问题。这是一个常见的初学者错误。代码应该是
roll = Label(frame1, textvariable = self.dcroll)
roll.grid(row = 4, column = 2)
对于ply_hp和monster_hp也是如此。