Sign in to follow this  

pygame menu movement problems.

This topic is 3596 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

I am having trouble finding out how to get this to work correctly. I have it so whenever i press u the menu will open and close. i also have it where up down left and right are supposed to move a ball around the screen. Whenever i press the move keys it only updates whenever i press u. Also whenever i start the program it starts out black instead of blue and only changes to blue after u is pressed.
import sys, os
import pygame
from pygame.locals import *
pygame.init()
size = 640, 480
color = 0,0,255
colorkey = 0,0,0
alphakey = 175
x, y = 0, 0
move_x, move_y = 0, 0
screen = pygame.display.set_mode(size)
image = pygame.image.load('menu.png')
image.convert()
image.set_alpha(alphakey)
ball = pygame.image.load('ball.bmp')
ball.convert()
ball.set_colorkey(colorkey)
menu = False

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_u:
                menu = not menu
                if menu:
                    screen.fill(color)
                    screen.blit(ball,(x,y))
                    screen.blit(image,(0,0))
                else:
                    screen.fill(color)
                    screen.blit(ball,(x,y))
    
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                move_x = -1
            elif event.key == K_RIGHT:
                move_x = +1
            elif event.key == K_UP:
                move_y = -1
            elif event.key == K_DOWN:
                move_y = +1
            
        elif event.type == KEYUP:
            if event.key == K_LEFT:
                move_x = 0
            elif event.key == K_RIGHT:
                move_x = 0
                
            elif event.key == K_UP:
                move_y = 0
            elif event.key == K_DOWN:
                move_y = 0
   
    x+= move_x
    y+= move_y
    pygame.display.flip()
    


Share this post


Link to post
Share on other sites
Quote:
Original post by monp
i also have it where up down left and right are supposed to move a ball around the screen. Whenever i press the move keys it only updates whenever i press u.

You draw the ball in the K_u event handler, not unconditionally every frame.
Quote:

Also whenever i start the program it starts out black instead of blue and only changes to blue after u is pressed.

You call screen.fill(color) in the K_u event handler, not in the initialization.

Share this post


Link to post
Share on other sites
I added screen.blit(ball,(x,y)) just under the x+= move_x, y+= move_y and now it will move rather the menu is open or not, but it leaves a trail of red. also it draws over the menu when it is open, i do not wish to do this.

Share this post


Link to post
Share on other sites
Quote:
Original post by monp
I added screen.blit(ball,(x,y)) just under the x+= move_x, y+= move_y and now it will move rather the menu is open or not, but it leaves a trail of red. also it draws over the menu when it is open, i do not wish to do this.



screen.fill(color)
screen.blit(ball,(x,y))
if menu:
screen.blit(image,(0,0))

This seems the correct rendering sequence: the background, clearing the ball or menu from last frame; the ball; and possibly the menu over the ball.

If you call only

screen.blit(ball,(x,y))

at the end you paint the ball over whatever is left from the previous frames: the menu and the ball in its previous positions.

Compacted and untested:

import sys, os
import pygame
from pygame.locals import *
pygame.init()
size = 640, 480
color = 0,0,255
colorkey = 0,0,0
alphakey = 175
x, y = 0, 0
move_x, move_y = 0, 0
screen = pygame.display.set_mode(size)
image = pygame.image.load('menu.png')
image.convert()
image.set_alpha(alphakey)
ball = pygame.image.load('ball.bmp')
ball.convert()
ball.set_colorkey(colorkey)
menu = False
arrows=set([K_LEFT,K_RIGHT,K_UP,K_DOWN])
speed={K_LEFT:(-1,0),K_RIGHT:(1,0),K_UP:(0,-1),K_DOWN:(0,1)}
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_u:
menu = not menu
elif event.key in arrows:
move_x,move_y=speed[event.key]

elif event.type == KEYUP:
if event.key in arrows:
move_x,move_y=0,0
x+= move_x
y+= move_y
screen.fill(color) #clear
if menu:
screen.blit(image,(0,0))#draw menu
else:
screen.blit(ball,(x,y))#draw updated ball
pygame.display.flip()

Share this post


Link to post
Share on other sites

This topic is 3596 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this