Python 如何阻止pygame中的角色离开屏幕边缘?
我在pygame中创建了一个小程序,玩家可以控制一个围绕屏幕移动的蓝色方块,但我想阻止玩家移动到屏幕边缘。这是我到目前为止的代码,我如何才能做到这一点Python 如何阻止pygame中的角色离开屏幕边缘?,python,pygame,Python,Pygame,我在pygame中创建了一个小程序,玩家可以控制一个围绕屏幕移动的蓝色方块,但我想阻止玩家移动到屏幕边缘。这是我到目前为止的代码,我如何才能做到这一点 import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) done = False x = 30 y = 30 clock = pygame.time.Clock() while not done: for event in pygame
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
is_blue = not is_blue
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]: y -= 5
if pressed[pygame.K_DOWN]: y += 5
if pressed[pygame.K_LEFT]: x -= 5
if pressed[pygame.K_RIGHT]: x += 5
screen.fill((0, 0, 0))
color = (0, 128, 255)
pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
pygame.display.flip()
clock.tick(60)
比如:
if pressed[pygame.K_UP]:
if not (y > maxwidth or y < 0):
y += 5
如果按下[pygame.K_UP]:
如果不是(y>maxwidth或y<0):
y+=5
其他人也是如此。在您的代码中,maxwidth
看起来像是600,但我会把它放在代码的顶部,这样您就不必在不同的地方不断更改它了。这应该行得通
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
如果按下[pygame.K_UP]且y>0:y-=5
如果按下[pygame.K_DOWN]且y<600-60:y+=5
如果按下[pygame.K_LEFT]且x>0:x-=5
如果按下[pygame.K_RIGHT]且x<800-60:x+=5
其中,600和800是屏幕大小,60是矩形的大小,您尝试执行的操作称为边缘检测,如在“检测”中,如果您位于边缘,则希望边缘成为屏幕的边缘。您应该做的是检查您的
x
或y
是否处于边缘,如果处于边缘,请不要再继续
if pressed[pygame.K_UP]:
if 0 < y-5 < 600: #0 and 600 being the edges of your screen, you can use a variable to change it dynamically later one
y -= 5
这将检查正方形的另一侧。注意:对于
x
,您将检查水平极限,在本例中为800
。此外,我们正在检查是否包含-5
,因为我们想知道我们要去哪里,而不是我们在哪里 pygame.Rect
s有一种(和climp_ip
)方法,可用于限制移动区域。因此,创建一个屏幕大小的rect(此处称为screen\u rect
)和一个播放器的rect(player\u rect
),并在每次移动后调用clamp\u ip
方法,使其保持在屏幕区域内
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
BG_COLOR = pg.Color(30, 30, 50)
def main():
clock = pg.time.Clock()
image = pg.Surface((50, 30))
image.fill(pg.Color('dodgerblue'))
pg.draw.rect(image, pg.Color(40, 220, 190), (0, 0, 49, 29), 2)
player_rect = image.get_rect(topleft=(200, 200))
# This pygame.Rect has the dimensions of the screen and
# is used to clamp the player_rect to this area.
screen_rect = screen.get_rect()
speed = 5
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
pressed = pg.key.get_pressed()
if pressed[pg.K_UP]:
player_rect.y -= speed
if pressed[pg.K_DOWN]:
player_rect.y += speed
if pressed[pg.K_LEFT]:
player_rect.x -= speed
if pressed[pg.K_RIGHT]:
player_rect.x += speed
# Clamp the rect to the dimensions of the screen_rect.
player_rect.clamp_ip(screen_rect)
screen.fill(BG_COLOR)
screen.blit(image, player_rect)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()
我将如何调整这一点以适应其他方向的工作?这应该是公认的答案
climp_-ip
是一种非常简单的方法。
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
BG_COLOR = pg.Color(30, 30, 50)
def main():
clock = pg.time.Clock()
image = pg.Surface((50, 30))
image.fill(pg.Color('dodgerblue'))
pg.draw.rect(image, pg.Color(40, 220, 190), (0, 0, 49, 29), 2)
player_rect = image.get_rect(topleft=(200, 200))
# This pygame.Rect has the dimensions of the screen and
# is used to clamp the player_rect to this area.
screen_rect = screen.get_rect()
speed = 5
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
pressed = pg.key.get_pressed()
if pressed[pg.K_UP]:
player_rect.y -= speed
if pressed[pg.K_DOWN]:
player_rect.y += speed
if pressed[pg.K_LEFT]:
player_rect.x -= speed
if pressed[pg.K_RIGHT]:
player_rect.x += speed
# Clamp the rect to the dimensions of the screen_rect.
player_rect.clamp_ip(screen_rect)
screen.fill(BG_COLOR)
screen.blit(image, player_rect)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()