简体   繁体   English

如何设置一个变量等于python中函数返回的值

[英]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.

相关问题 如何将列表的索引设置为等于函数返回值的索引? - How do you set index of a list equal to the index of returned values from a function? 如何检查python中的变量是否相等 - how do you check if variable is equal in python 如何根据 Python 中的用户输入设置变量的值? - How do you set the value of a variable based on user input in Python? 如何在Python3中为变量设置默认值? - How do you set a default value for a variable in Python3? python 将另一个 function 的返回值设置为在 header 中使用的变量 - python set returned value from another function as a variable to use in header 如何调用函数末尾返回的变量? - How do you call a variable that was returned at the end of a function? 将变量设置为等于Python中for循环的返回值 - Setting a variable equal to a returned value from a for loop in Python 如何使用python将函数的返回值放入变量中? - How to put a returned value of a function into a variable using python? 你如何设置一个计时器,一旦完成,增加 python 3.x 中变量的值? - How do you set a timer, that once finished, increments the value of a variable in python 3.x? 将函数返回的值分配给Python中的变量 - Assigning the value returned by a function to a variable in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM