且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何阻止 pygame 中的角色离开屏幕边缘?

更新时间:2023-12-01 16:09:46

这应该可行

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

其中 600 和 800 是屏幕尺寸,60 是矩形的尺寸

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