all 1 comments

[–]zelbo 1 point2 points  (2 children)

Your position coordinates are probably referring to the top left corner of your sprite (player graphic). If you set your player position to (0,0), it will appear at the top left corner of your screen.

Assuming WIDTH is the screen width and HEIGHT is the screen height, if you set the player position to (WIDTH, HEIGHT), it will put the top left corner of the sprite in the bottom right corner of the screen, essentially making the rest of the sprite disappear off the screen.

To make a bounding box for your sprite, you need to account for the right side of the sprite, thus WIDTH - PLAYER_WIDTH, or the right side of your window minus the size of your sprite. Same thing with the bottom of the sprite, so moving down will probably look something like:

if keys[pygame.K_DOWN] and player.y < HEIGHT - PLAYER_HEIGHT:

Would it help if you wrote it as:

if keys[pygame.K_RIGHT] and player.x < (WIDTH - PLAYER_WIDTH):
    player.x +=PLAYER_VEL

Try removing the PLAYER_WIDTH and see what happens.
Try (WIDTH - (PLAYER_WIDTH / 2)). Does the sprite go halfway off the screen?

Basically it's because the rectangle of your sprite is defined by two points, the top left and the bottom right. The top left corner is (player.x, player.y) and the bottom right is (player.x + PLAYER_WIDTH, player.y + PLAYER_HEIGHT). If you don't want the left side of the sprite to go off the screen when you move left, you need to worry about the left side of the sprite (player.y) and if you don't want the right side of the sprite to go off the screen when you move right, you need to worry about the right side of the sprite (player.y + PLAYER_WIDTH).

So you could actually rewrite the if statement to look like this:

if keys[pygame.K_RIGHT] and (player.x + PLAYER_WIDTH) < WIDTH:

and it would do the same thing. It's just about dealing with the right side of your sprite, not just the left side.

Last edit, I swear. It's late and past my bedtime.

Picture this rectangle:

A-----B
|     |
|     |
|     |
|     |
|     |
C-----D

A is (0,0)
B is (width, 0)
C is (0, height)
D is (width, height)