简体   繁体   中英

Python, Pygame Mouse events

How would I assign the variables x, y to the x and y position, respectivly, of a the mouse pointer when it is pressed down (clicked).

Heres what I have thus far:

from pygame import*
init()
SIZE = (width, height)
screen = display.set_mode(SIZE)
position = 0
Running = True
while Running:
    for evnt in event.get():
         if evnt.type == QUIT:
             Running = False 
         elif evnt.type == MOUSEBUTTONDOWN:
             position = evnt.pos

Currently, position is in the form (x position, y position). How would I access the individual elements?

Thanks in advance.

position is a tuple so you can

position = evnt.pos
x = position[0]
y = position[1]

or

x, y = position

or even

x, y = evnt.pos

I have not used pygame in a while nor do i still have it so i can't test this code but I'm pretty sure its correct. first you test if the mouses left button is clicked

The get_pressed function returns three values for each button on a mouse i may be wrong thinking the first value returned is the value for mouse button one.

If the mouse is clicked, we proceed to get the mouses coords and store them.

if pygame.mouse.get_pressed()[0]
    cord = pygame.mouse.get_pressed()

Also, from what i understand pygame does not use az value as the library is only a 2D library so this gives you a X, Y value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM