dimanche 7 juin 2015

How to change a variable in an if statement and have it stay as the number assigned in the statement outside of the statement?

Sorry for the long title but i am really stuck. So i want a red rectangle that is drawn in my main loop to be able to move to the left along the x axis. (I am using a pygame if statement for the keyboard controls). The rectangle only goes to the left by 50 when the left arrow key is pressed (which is fine) but then when the left key isn't being pressed it goes back to its original position. So basically I want to be able to change x in the keyboard controls if statement and for it to stay as the edited number outside the if statement. I dont know if this is possible in any way but if it is please answer. `

x = 500
y = 500

screen.fill(WHITE)

if event.type == pygame.KEYDOWN: 
    if event.key == pygame.K_LEFT:
       x -= 50      

pygame.draw.rect(screen, RED, (x,y,300,300), 0);`

Aucun commentaire:

Enregistrer un commentaire