Jump to content
  • Advertisement
Sign in to follow this  

mouse movement in pygame

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, one quick question. With this code for mouse input I get returned a sequence of 0's. (It's in a loop)
if type_of_input == 1:
  move = pygame.mouse.get_rel()
  yrot = yrot + move[0]
  xrot = xrot + move[1]

I obviously don't want that to happen. Just a simple bit of code to change xrot and yrot by the x and y distance traveled by the mouse. I thought it might be events or something, but the code right above it (for key input)...
if type_of_input == 0:
  keystate = pygame.key.get_pressed()
  if keystate[K_LEFT]:
    yrot = yrot + 1.8
  if keystate[K_RIGHT]:
    yrot = yrot - 1.8

...works perfectly fine. Because these two are right next to each other, type_of_input is either 1 or 0 when the program runs (type_of_input never changes). When I set it equal to 0, keys worked, but when I set it to 1, it doesn't. (EDIT by mod - meaningful title added.) (EDIT by Geometrian - Opps, Thanks) [Edited by - Geometrian on April 21, 2007 7:48:12 PM]

Share this post

Link to post
Share on other sites
Are you calling pygame.mouse.get_rel() anywhere else? This function only returns the relative movement since the last time it was called. If you call it again immediately, the result will most likely be (0,0).

Share this post

Link to post
Share on other sites
I placed this function in a loop that is updated for every frame of the game. It is called once per time around the loop. The loop is run through about 20 times per second (hence 20fps) and so the relative mouse position is checked 20 times per second. Since the average fps is 20fps, the function is called every 1/20th of a second.
[edit] 'pygame.mouse.get_focused()' seems to be the equivelent function to 'pygame.key.get_pressed()' except that 'pygame.mouse.get_focused()' returns a bool and I need to see in which direction the mouse is moving.

Share this post

Link to post
Share on other sites
Well, I just tested this function out for myself and as far as I can tell it works fine. get_focused just tells you if the window currently has the mouse focus (which means it is receiving mouse events). If you move the cursor outside the window you'll usually lose focus (unless your application has captured the mouse input). There's no need to check for this in your movement code, but it could be useful if you wanted to know if the window is not receiving mouse events.

Do you have a main event loop (calling something like pygame.event.get())? If you don't have one, that might prevent the system from working (not sure about this).

Share this post

Link to post
Share on other sites
Yes, I've tried that- when I put in that command, my program runs (though the mouse still doesn't work), but refuses to quit with ESCAPE key:
here's the basic code for the program:

#in main function

#type_of_input is loaded from a file.

#main game loop
while 1:
pygame.event.get() #just added this (somehow causes ESCAPE key not to work)
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
draw(type_of_input) #in this function, the mouse problem is happening.

Share this post

Link to post
Share on other sites
pygame.event.get and pygame.event.poll both pull events from the same queue. The difference is that pygame.event.poll only gets at most one event at a time, and pygame.event.get gets all the pending events (optionally filtering to only get events of certain types). My event loop looks like this:

while True:
for event in pygame.event.get():
if event.type == whatever:
do something
if event.type == and so on
blah blah
update game
draw everything
clock.tick(framerate) #or use clock.tick_busy_loop(framerate)

The While True could be replaced with a boolean variable to be set when you want to exit the loop (in this example, I would just return from the function or sys.exit()). I use a pygame.time.clock object to run at a constant framerate. If you don't want a constant framerate then you'd use some other method for your timing.

Share this post

Link to post
Share on other sites
Ok... I think this fixes the quiting problem, and I can see that the next logical step would be to make the mouse move with this, but how do you define a pygame.MOUSEMOTION event? like

for event in pygame.event.get():
if event.type == #what do I put here?
#rotate up
if event.type == #what do I put here?
#rotate down
if event.type == #what do I put here?
#rotate left
if event.type == #what do I put here?
#rotate right

See, I know moving the mouse will make a pygame.MOUSEMOTION event, but how do I distinguish between one moving up, down, left, or right? Hence '#what do I put here?'
[edit: I just tried putting pygame.MOUSEMOTIONUP and pygame.MOUSEMOTIONDOWN there and it says that the module does not have that attribute]

Share this post

Link to post
Share on other sites
I don't really see what you are trying to do without seeing more of your code but if you look at the short program I just wrote you can see that you'd be able to tell which direction the mouse is moving by doing this:

""" moveCircle.py
Creates a blue circle sprite that
the mouse follows around


import pygame

screen = pygame.display.set_mode((640, 480))

class Circle(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.Surface((50, 50))
self.image.fill((255, 255, 255)) # should really use start using names for color
pygame.draw.circle(self.image, (0,0,255), (25,25), 25, 0)
self.rect = self.image.get_rect()

def update(self):
self.rect.center = pygame.mouse.get_pos()
print pygame.mouse.get_rel()

def main():
pygame.display.set_caption("move the circle with the mouse")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255, 255, 255))
screen.blit(background, (0,0))

circle = Circle()
allSprites = pygame.sprite.Group(circle)

# hide mouse
clock = pygame.time.Clock()
keepGoing = True

while keepGoing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False

#allSprites.clear(screen, background)


# return the mouse

if __name__ == "__main__":

Hope this helps or gives you an idea at least;)
p.s. There is no mousemotionup,down,etc so you'll have to create it yourself
Should just be a couple of lines of code you need to add to the code I gave above.

Share this post

Link to post
Share on other sites
Thanks, but in this case I don't really care where the mouse is.
[edit: oh wait... that's not what it's doing, is it.]
Here's what I'm trying to do. I have a 3D airplane, and I want to fly it. The control method is the mouse (since type_of_input == 1), so if I push the mouse forward, the plane rotates down, pull the mouse back and the plane rotates up. So, what I really need is for each frame of the game, something to add the x and y distance traveled by the mouse to the value of two variables ('xrot' and 'yrot').

For example,
If in one frame, the mouse travels two pixels up and 1 pixel left, (xrot = xrot - 1) and (yrot = yrot + 2).
If in the next frame, the mouse moves 1 pixel up and 0 pixels left/right, then (xrot = xrot + 0) and (yrot = yrot + 1).

[edit: I have a hunch that I forgot:
something = pygame.mouse.get_pos()
so it never updated it]
[reedit: No, that didn't seem to fix it]

[Edited by - Geometrian on April 21, 2007 10:15:30 PM]

Share this post

Link to post
Share on other sites
pygame.mouse.get_rel() should really be working; I'm not sure why it would always return (0,0) even when you move the mouse. Have you tried using it with the new event loop? There could have been some weird issue with the event queue in your old loop. Anyway, if you want to try using the event system instead, the events of type MOUSEMOTION have attributes pos, rel, and buttons. You can use the rel property from that.

xrot += event.rel[0]*sensitivity
yrot += event.rel[1]*sensitivity

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!