How can I stop a character in pygame from leaving the edge of the screen?

前端 未结 4 1470
暗喜
暗喜 2021-01-15 04:22

I have created a small program in pygame where the player controls a blue square moving around the screen, but I want to stop the player from moving past the edge of the scr

4条回答
  •  日久生厌
    2021-01-15 04:38

    This should work

    if pressed[pygame.K_UP] and y > 0: y -= 5
    if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
    if pressed[pygame.K_LEFT] and x > 0: x -= 5
    if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5
    

    Where 600 and 800 is the screen size and 60 the size of your rectangle

提交回复
热议问题