乌龟的 Y 坐标作为方法而不是整数 python
Y co-ordinate of a turtle coming up as a method instead of an integer python
我曾尝试将其更改为全部在一行中,因此 bullet.sety(bullet.ycor()+20
但出现了相同的错误消息。我也试过 bullet.sety(int(y)+20)
但它有一条错误消息说它无法将方法转换为整数。
def shoot_bullet():
stop = "no"
while True:
y = bullet.ycor()
bullet.sety(y+20)
wn.update()
time.sleep(0.5)
if bullet.ycor > 293:
stop = "yes"
if stop == "yes":
break
bullet.goto(main_ship.xcor, main_ship.ycor)
错误信息是
File "C:\Python\spaceinvaders.py", line 46, in shoot_bullet
bullet.sety(y+20)
TypeError: unsupported opperand type for +: 'method' and 'int'#
(它仅被格式化为代码,否则 Stack Overflow 将其标记为错误)
我想你描述的情况发生在第二个循环中 运行。
虽然您没有向我们展示 bullet
的代码,但我怀疑如下:
bullet.goto(main_ship.xcor, main_ship.ycor)
只是将 bullet
对象中的 x
和 y
字段设置为您传递的内容。当你刚刚传递 main_ship.xcor
和 main_ship.ycor
(即方法而不是它们 return)时,这些被放入项目符号并 returned 在 ycor()
打电话。
解决方案:执行bullet.goto(main_ship.xcor(), main_ship.ycor())
以调用这些方法。
顺便说一句,如果不使用 stop = "no"
和 stop = "yes"
,最好使用布尔值(stop = False
和 stop = True
)。
似乎 bullet.ycor()
returns 一个方法而不是某个方法调用的值。
我想您的代码可能如下所示:
class Bullet:
def some_method(self):
...
def ycor(self):
return self.some_method
但应该是
class Bullet:
def some_method(self):
...
def ycor(self):
return self.some_method()
注意括号。
我曾尝试将其更改为全部在一行中,因此 bullet.sety(bullet.ycor()+20
但出现了相同的错误消息。我也试过 bullet.sety(int(y)+20)
但它有一条错误消息说它无法将方法转换为整数。
def shoot_bullet():
stop = "no"
while True:
y = bullet.ycor()
bullet.sety(y+20)
wn.update()
time.sleep(0.5)
if bullet.ycor > 293:
stop = "yes"
if stop == "yes":
break
bullet.goto(main_ship.xcor, main_ship.ycor)
错误信息是
File "C:\Python\spaceinvaders.py", line 46, in shoot_bullet
bullet.sety(y+20)
TypeError: unsupported opperand type for +: 'method' and 'int'#
(它仅被格式化为代码,否则 Stack Overflow 将其标记为错误)
我想你描述的情况发生在第二个循环中 运行。
虽然您没有向我们展示 bullet
的代码,但我怀疑如下:
bullet.goto(main_ship.xcor, main_ship.ycor)
只是将 bullet
对象中的 x
和 y
字段设置为您传递的内容。当你刚刚传递 main_ship.xcor
和 main_ship.ycor
(即方法而不是它们 return)时,这些被放入项目符号并 returned 在 ycor()
打电话。
解决方案:执行bullet.goto(main_ship.xcor(), main_ship.ycor())
以调用这些方法。
顺便说一句,如果不使用 stop = "no"
和 stop = "yes"
,最好使用布尔值(stop = False
和 stop = True
)。
似乎 bullet.ycor()
returns 一个方法而不是某个方法调用的值。
我想您的代码可能如下所示:
class Bullet:
def some_method(self):
...
def ycor(self):
return self.some_method
但应该是
class Bullet:
def some_method(self):
...
def ycor(self):
return self.some_method()
注意括号。