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

前端 未结 4 1443
暗喜
暗喜 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:40

    Something like:

    if pressed[pygame.K_UP]:
        if not (y > maxwidth or y < 0):
            y += 5
    

    And so on for the others. the maxwidth looks like 600 in your code but I'd put it at the top of your code so you dont keep having to change it in different places.

提交回复
热议问题