Pygame mouse cursor load/unload

A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any way that I can. Here are the links that contain my code:

Main class: http://pastebin.com/HSQzX6h2
Main file (where the problem lies): http://pastebin.com/67p97RsJ

If the links yield nothing, please let me know ([email protected])
 
I

Ian Kelly

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:

Your mouse motion code draws the paddle in the new position, waits
1/10th of a second, and then draws over it again with the "invisible"
paddle. Thus, approximately 1/10th of a second after you stop moving
the mouse, it disappears.

Mouse motion events are probably not the best way to do this. You can
instead just capture the current position of the mouse on every frame
and use that instead. I replaced your main loop with the following:

paddle_pos = (0, 0)
clock = pygame.time.Clock()

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

# Erase the paddle from the old mouse position.
screen.blit(bpaddle, paddle_pos)
# Redraw the net before the paddle so that the paddle can appear over it.
pygame.draw.line(screen, game.lineColor, game.net1, game.net2,
game.netWidth)
# Get the new mouse position.
paddle_pos = pygame.mouse.get_pos()
# Draw the paddle at the new mouse position.
screen.blit(beeper, paddle_pos)
# Update the screen if it's double-buffered.
pygame.display.update()
# Finally, let the CPU idle until it's time for the next frame.
# 50 here means that it will sleep long enough to achieve 50 FPS.
clock.tick(50)

And I think you will find that this does what you want.

A couple more observations while I'm at it. Generally there is no
need to be calling pygame.display.update() multiple times per frame.
Just draw everything that you need, and then call it once at the end
of the loop, as I have shown above. Also, the shebang line only does
anything if it's the very first line in the file, so it would need to
appear before the module docstring to do anything useful.
 
A

Alex Gardner

Your mouse motion code draws the paddle in the new position, waits

1/10th of a second, and then draws over it again with the "invisible"

paddle. Thus, approximately 1/10th of a second after you stop moving

the mouse, it disappears.



Mouse motion events are probably not the best way to do this. You can

instead just capture the current position of the mouse on every frame

and use that instead. I replaced your main loop with the following:



paddle_pos = (0, 0)

clock = pygame.time.Clock()



while True:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()



# Erase the paddle from the old mouse position.

screen.blit(bpaddle, paddle_pos)

# Redraw the net before the paddle so that the paddle can appear overit.

pygame.draw.line(screen, game.lineColor, game.net1, game.net2,

game.netWidth)

# Get the new mouse position.

paddle_pos = pygame.mouse.get_pos()

# Draw the paddle at the new mouse position.

screen.blit(beeper, paddle_pos)

# Update the screen if it's double-buffered.

pygame.display.update()

# Finally, let the CPU idle until it's time for the next frame.

# 50 here means that it will sleep long enough to achieve 50 FPS.

clock.tick(50)



And I think you will find that this does what you want.



A couple more observations while I'm at it. Generally there is no

need to be calling pygame.display.update() multiple times per frame.

Just draw everything that you need, and then call it once at the end

of the loop, as I have shown above. Also, the shebang line only does

anything if it's the very first line in the file, so it would need to

appear before the module docstring to do anything useful.

Thank you very much, Ian. I understand the code and have learned from it. If I were more knowledgeable in python I wouldn't have had to ask; I am learning as I go with this project. Again, thank you :)
 
A

Alex Gardner

Your mouse motion code draws the paddle in the new position, waits

1/10th of a second, and then draws over it again with the "invisible"

paddle. Thus, approximately 1/10th of a second after you stop moving

the mouse, it disappears.



Mouse motion events are probably not the best way to do this. You can

instead just capture the current position of the mouse on every frame

and use that instead. I replaced your main loop with the following:



paddle_pos = (0, 0)

clock = pygame.time.Clock()



while True:

for event in pygame.event.get():

if event.type == QUIT:

sys.exit()



# Erase the paddle from the old mouse position.

screen.blit(bpaddle, paddle_pos)

# Redraw the net before the paddle so that the paddle can appear overit.

pygame.draw.line(screen, game.lineColor, game.net1, game.net2,

game.netWidth)

# Get the new mouse position.

paddle_pos = pygame.mouse.get_pos()

# Draw the paddle at the new mouse position.

screen.blit(beeper, paddle_pos)

# Update the screen if it's double-buffered.

pygame.display.update()

# Finally, let the CPU idle until it's time for the next frame.

# 50 here means that it will sleep long enough to achieve 50 FPS.

clock.tick(50)



And I think you will find that this does what you want.



A couple more observations while I'm at it. Generally there is no

need to be calling pygame.display.update() multiple times per frame.

Just draw everything that you need, and then call it once at the end

of the loop, as I have shown above. Also, the shebang line only does

anything if it's the very first line in the file, so it would need to

appear before the module docstring to do anything useful.

Thank you very much, Ian. I understand the code and have learned from it. If I were more knowledgeable in python I wouldn't have had to ask; I am learning as I go with this project. Again, thank you :)
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ



If the links yield nothing, please let me know ([email protected])

I have a new problem :(. I want to restrict the paddle to a certain plane.I have it working but when the paddle reaches the limit it locks up. I have no idea how I can redraw it back in the plane. I am using the code that Ian provided (thank you so much). I was thinking of making an else to redraw the paddle using the same code, but part of me thinks that it would berather sloppy.

if (0,0) <= paddle_pos <= (300,300):
paddle_pos = pygame.mouse.get_pos()
screen.blit(beeper, paddle_pos)
pygame.display.update()
clock.tick(50)
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ



If the links yield nothing, please let me know ([email protected])

I have a new problem that I'm dealing with. I want to restrict the paddle to a certain plane. When the paddle reaches the boundary, it locks up. I am using the code that Ian provided earlier (thank you so much). Here is what I have so far:

if (0,0) <= paddle_pos <= (300,300):
paddle_pos = pygame.mouse.get_pos()
screen.blit(beeper, paddle_pos)
pygame.display.update()
clock.tick(50)

I tried making an else statement and copying Ian's code in it, but I don't think that it would work. I have tried to fix this myself; this is my second GUI program that I have ever made.... please forgive me if I seem needy.
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ


.
If the links yield nothing, please let me know ([email protected])

I followed your advice, but now I have a drawing problem. I simply tried rewriting the paddle with a black one, but its failing. I would like to pester you fine folks one last time. The code is here: http://pastebin.com/gVPPJYWs

I really appreciate your help and am learning from your advice.

Thank you so much, Alex
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ



If the links yield nothing, please let me know ([email protected])

I would like to bother you fine folks one last time! There are drawing problems that I am running into. The paddle keeps on moving but it doesn't rewrite the black paddle. This is a problem because the paddle just keeps leaving a green trail. The code is here: http://pastebin.com/gVPPJYWs

I feel as though I am missing something...
 
I

Ian Kelly

I would like to bother you fine folks one last time! There are drawing problems that I am running into. The paddle keeps on moving but it doesn't rewrite the black paddle. This is a problem because the paddle just keeps leaving a green trail. The code is here: http://pastebin.com/gVPPJYWs

I feel as though I am missing something...

You get the streaking because the first blanking operation only ever
blanks at (0, 0), and the second one blanks at the new paddle
position, not the previous position.

You don't need two separate rects to keep track of where the paddle
is. blank_rect and b_bounds_rect are entirely unnecessary, so get rid
of them.

The line "screen.blit(bpaddle, paddle_pos)" should be replaced with
"screen.blit(bpaddle, paddle_rect)", because paddle_rect is what
you're using to track the paddle location. Since paddle_pos is not
being updated, the former would always draw the blank paddle in the
upper-left corner. This should also be the only place where you're
blanking the paddle, so get rid of the other one.

You also don't need the if statement at all. The clamping operation
already ensures that the paddle is bounded to the region (0, 0, 300,
300).

Once that's gone, you no longer need paddle_pos at all, so you can
delete that as well.
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ



If the links yield nothing, please let me know ([email protected])

Now the cursor isn't moving at all!

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.blit(bpaddle, paddle_rect)
# Draw the net
pygame.draw.line(screen, game.lineColor, game.net1, game.net2, game.netWidth)

paddle_rect.clamp_ip(bounds_rect)
screen.blit(beeper, paddle_rect)

clock.tick(50)

pygame.display.update()
 
I

Ian Kelly

Now the cursor isn't moving at all!

while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()

screen.blit(bpaddle, paddle_rect)
# Draw the net
pygame.draw.line(screen, game.lineColor, game.net1, game.net2, game.netWidth)

paddle_rect.clamp_ip(bounds_rect)
screen.blit(beeper, paddle_rect)

clock.tick(50)

pygame.display.update()

You deleted the line where you update the paddle_rect from the current
mouse position.
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ



If the links yield nothing, please let me know ([email protected])

I added the blank paddle and now the green one is just gone..... I feel like such a newbie ><
 
A

Alex Gardner

I added the blank paddle and now the green one is just gone..... I feel like such a newbie ><


screen.blit(bpaddle, paddle_rect)
 
I

Ian Kelly

I added the blank paddle and now the green one is just gone..... I feel like such a newbie ><


screen.blit(bpaddle, paddle_rect)

We're not psychic, so you'll need to post the current code if you want
any suggestions on how to fix it.
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ



If the links yield nothing, please let me know ([email protected])

My bad! http://pastebin.com/yuvpT7bH
 
I

Ian Kelly


You're still drawing the blank paddle in two different places. One of
those places is immediately after you draw the paddle, which undoes
the work you just did in drawing it. That's why you're not seeing the
paddle.

You're also still not updating the paddle_rect from the current mouse
position, so if you fix the above issue you will find that the paddle
still will not move. You should restore the "paddle_rect.center =
pygame.mouse.get_pos()" line that you deleted prior to the line that
clamps it.

Finally, you currently have the "clock.tick()" call before the
"pygame.display.update()" call, which makes no sense. You're doing
your drawing, asking pygame to sleep for 20 milliseconds, and only
then updating the display. You want to update the display before the
clock.tick() so that the user can actually see the most recent frame
during those 20 milliseconds.
 
A

Alex Gardner

I am in the process of making a pong game in python using the pygame library. My current problem is that when I move the mouse, it turns off as soon as the mouse stops moving. The way I am doing this is by making the default cursor invisible and using .png files as replacements for the cursor. Perhaps my code would best explain my problem. I will take help in any waythat I can. Here are the links that contain my code:



Main class: http://pastebin.com/HSQzX6h2

Main file (where the problem lies): http://pastebin.com/67p97RsJ



If the links yield nothing, please let me know ([email protected])
I tried to append what you told me to. Now it appears that I have a syntaxerror! I checked my indentations and they look fine to me, but I get thiserror:

paddle_pos = pygame.mouse.get_pos()
^
IndentationError: unindent does not match any outer indentation level

I added "paddle_rect.center = pygame.mouse.get_pos()" and removed the double blank paddles. I have no idea if it works though because of the parse error! New code: http://pastebin.com/maqWCdNB
 
M

MRAB

I tried to append what you told me to. Now it appears that I have a syntax error! I checked my indentations and they look fine to me, but I get this error:

paddle_pos = pygame.mouse.get_pos()
^
IndentationError: unindent does not match any outer indentation level

I added "paddle_rect.center = pygame.mouse.get_pos()" and removed the double blank paddles. I have no idea if it works though because of the parse error! New code: http://pastebin.com/maqWCdNB
It's complaining because the preceding 'for' loop is indented more that
that line.
 
I

Ian Kelly

I tried to append what you told me to. Now it appears that I have a syntax error! I checked my indentations and they look fine to me, but I get this error:

paddle_pos = pygame.mouse.get_pos()
^
IndentationError: unindent does not match any outer indentation level

The "for" statement and the statement quoted above should both be at
the same indentation level. If you look at the code, the "for"
statement is indented 7 spaces whereas the other statement is indented
4 spaces. You need to make them equal.
I added "paddle_rect.center = pygame.mouse.get_pos()" and removed the double blank paddles. I have no idea if it works though because of the parse error! New code: http://pastebin.com/maqWCdNB

It won't. You've removed both the position-clamping code and the
clock tick code entirely. Why did you do that? You've also gone from
having two separate lines drawing bpaddle and one line drawing beeper,
to having two separate lines drawing beeper, and not drawing bpaddle
at all.

I think that you need to step back from this and try to understand
better the logical flow that you're trying to implement here. Once
you understand what it is that you're trying to accomplish, then you
can set it down in code. The overall flow should look like this:

while True:
<check for and handle events>
<update the game state>
<draw the updated game state>
<update the screen>
<go to sleep for a while>

Try to implement each of those things as a discrete unit, and think
hard about what operations need to be done and in what order to
accomplish each of those things. It might help you conceptually to
split the first three off into separate functions that each do one
specific piece of the above, and then implement each of those
functions thinking only about what that particular function needs to
do. To provide a concrete example of what I am talking about,
consider replacing your main loop with this:

state = SomeClassRepresentingTheGameState()

while True:
# checks for a QUIT event
process_events(state)

# moves the ball and paddles, updates scores, etc.
update_game(state)

# clears the old state from the display and draws the current state onto it
draw_game(state)

pygame.display.update()
clock.tick(50)

And then separately implement all three of those functions, thinking
only about how each function needs to interact with the state and what
pygame calls it needs to make in order to accomplish its specific
task. Right now the state object would only contain your paddle_rect,
but later you will presumably have more state to add (e.g. the
players' scores, the position and velocity of the ball, etc.), so it
will be useful then to have a single object you can pass around to
contain those.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top