[英]How do you set a variable equal to the value returned by a function in python
I'm trying to design a system that will allow me to move sprites to the cursor position when clicked, similar to RuneScape, where the player will move to the position of the mouse click on the screen. 我正在尝试设计一个系统,使我可以在单击时将精灵移动到光标位置,类似于RuneScape,在该系统中播放器将移动到屏幕上鼠标单击的位置。 Here is my code below:
这是我的代码如下:
for event in pygame.event.get ():
if event.type == QUIT:
pygame.quit ()
sys.exit (0)
elif event.type == KEYDOWN:
if event.key == K_UP:
Shrin_y -= 5
elif event.key == K_LEFT:
Shrin_x -= 5
elif event.key == K_RIGHT:
Shrin_x += 5
elif event.key == K_DOWN:
Shrin_y += 5
elif event.type == MOUSEBUTTONDOWN:
(Shrin_x, Shrin_y) = pygame.mouse.get_pos
print pygame.mouse.get_pos
Surface.blit (Shrin, (Shrin_x, Shrin_y))
pygame.display.update ()
Note: Shrin is just the name of the sprite; 注意:Shrin只是精灵的名称; if it pleases you, just replace all instances of Shrin in the code with 'sprite'.
如果您满意,只需将代码中所有Shrin实例替换为'sprite'。
So how do you store the value RETURNED by pygame.mouse.get_pos
in (Shrin_x, Shrin_y)
? 那么,如何将
pygame.mouse.get_pos
的值存储在(Shrin_x, Shrin_y)
?
(Shrin_x, Shrin_y) = pygame.mouse.get_pos
This line should be giving you an error, because it is trying to unpack a single value (a function or method object) to two variables. 该行应该给您一个错误,因为它试图将单个值(一个函数或方法对象)解压缩为两个变量。 What you want is:
您想要的是:
(Shrin_x, Shrin_y) = pygame.mouse.get_pos()
The difference is that the ()
is the operator which calls the callable object. 区别在于
()
是调用可调用对象的运算符。
You need to call pygame.mouse.get_pos
. 您需要调用
pygame.mouse.get_pos
。 The way you have it in your code you are just looking at the method. 您在代码中拥有它的方式只是在查看方法。 So change just change one line:
因此更改只需更改一行:
(Shrin_x, Shrin_y) = pygame.mouse.get_pos()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.