Prevent the PYGAME window from closing

Plasma8 Posted messages 5 Registration date   Status Member Last intervention   -  
 Anonymous user -
Hello,
So here it is; I'm working on a "game" with pygame for school, I started it two weeks ago and I'm going slowly. But now I'm stuck D: Let me explain, it's a kind of Space Invaders for those who know it, and I have my ship all nice at the bottom of the screen, I can move it but I can't manage to prevent it from going off-screen.
I've tried several things like simply preventing it from moving when it reaches the edge coordinates of the window, but with what I've done at the beginning of the program I haven't managed to do anything (except to make it move with the mouse but only in one direction which was not intended at all ;))

I'm pasting here a few lines of code (the most important ones from the main version that works well, not the testing movement version ...) to show you ;D

surfaceW = 1000 #Window size
surfaceH = 800 #Window size
vaisseauW = 50 #To be revised
vaisseauH = 66 #To be revised
def vaisseau(x,y,image):
surface.blit(image, (x,y))
def principale():
x = 400 #To be revised
y = 600 #To be revised
if event.type == pygame.KEYDOWN : #MOVEMENT
if event.key == pygame.K_LEFT :
x_mouvement = -1
if event.key == pygame.K_RIGHT :
x_mouvement = +1
if event.type == pygame.KEYUP :
x_mouvement = +0
x+=x_mouvement

So if someone can help me :)

PS: just in case (Yes sir, the student you are currently questioning in front of you, the one you are checking hasn't copied anything, it's me :) (Ludovic and Jordan)

1 answer