使用可移动 characters/images 在 Pygame 中添加边界
Adding boundaries in Pygame with movable characters/images
我一直在创建一个游戏,其中图像根据玩家使用 Keydown 和 Keyup 方法输入移动。我想添加边界,这样用户就不能将 image/character 移出显示(如果边界被击中,我不希望游戏结束,只是 image/character 无法移动过去那个边界)
import pygame
pygame.init()#initiate pygame
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
display_width = 1200
display_height = 800
display = pygame.display.set_mode((display_width,display_height))
characterimg_left = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/characterimg_left.png')
characterimg_right = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/characterimg_right.png')
characterimg = characterimg_left
def soldier(x,y):
display.blit(characterimg, (x,y))
x = (display_width * 0.30)
y = (display_height * 0.2)
pygame.display.set_caption('No U')
clock = pygame.time.Clock()#game clock
flip_right = False
x_change = 0
y_change = 0
bg_x = 0
start = True
bg = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/bg.png').convert()
class player:
def __init__(self, x, y):
self.jumping = False
p = player(x, y)
while start:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
start = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change += -4
if flip_right == True:
characterimg = characterimg_left
flip_right = False
x += -150
elif event.key == pygame.K_RIGHT:
x_change += 4
if flip_right == False:
characterimg = characterimg_right
flip_right = True
x += 150
elif event.key == pygame.K_UP:
y_change += -4
elif event.key == pygame.K_DOWN:
y_change += 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change += 4
elif event.key == pygame.K_RIGHT:
x_change += -4
elif event.key == pygame.K_UP:
y_change += 4
elif event.key == pygame.K_DOWN:
y_change += -4
x += x_change
y += y_change
display.fill(white)
soldier(x,y)
pygame.display.update()
clock.tick(120)#fps
pygame.quit()
我尝试了好几次,包括切换到按键方式,但都失败了。请帮忙,谢谢。
基本上你想限制玩家的移动。
所以每次你想要 "move" 玩家(我猜这是 "x_change" / "y_change")你需要检查他们是否仍然在你的边界内移动。
示例:您的显示 x 边界在屏幕左侧为 0 像素,在右侧为 500 像素。我只允许实际移动,如果移动的结果在我的范围内。
boundary_x_lower = 0
boundary_x_upper = 500
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if boundary_x_lower < (x_change - 4):
# I allow the movement if I'm still above the lower boundary after the move.
x_change -= 4
elif event.key == pygame.K_RIGHT:
if boundary_x_upper > (x_change +4):
# I allow the movement if I'm still below the upper boundary after the move.
x_change += 4
PS:我对你的代码感到困惑,因为当你向右移动时你会减去...我习惯了 2D 游戏,如果你向右移动你会增加玩家的位置......如果你向左走,则减去。
请随意调整代码以适合您的项目。基本原理也适用于 y 轴运动:boundary_y_lower & _y_upper。如果您还有其他问题,请提问!
只需将 x
和 y
值限制在 0 和显示宽度和高度之间。
# In the main while loop after the movement.
if x < 0:
x = 0
elif x + image_width > display_width:
x = display_width - image_width
if y < 0:
y = 0
elif y + image_height > display_height:
y = display_height - image_height
我还建议您查看 pygame.Rect
s 的工作原理。你可以用显示器的大小定义一个矩形,
display_rect = display.get_rect()
以及将用作 blit 位置的字符的矩形:
rect = characterimg_left.get_rect(center=(x, y))
然后以这种方式移动并夹紧矩形:
rect.move_ip((x_change, y_change))
rect.clamp_ip(display_rect)
display.fill(white)
# Blit the image at the `rect.topleft` coordinates.
display.blit(characterimg, rect)
我一直在创建一个游戏,其中图像根据玩家使用 Keydown 和 Keyup 方法输入移动。我想添加边界,这样用户就不能将 image/character 移出显示(如果边界被击中,我不希望游戏结束,只是 image/character 无法移动过去那个边界)
import pygame
pygame.init()#initiate pygame
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
display_width = 1200
display_height = 800
display = pygame.display.set_mode((display_width,display_height))
characterimg_left = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/characterimg_left.png')
characterimg_right = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/characterimg_right.png')
characterimg = characterimg_left
def soldier(x,y):
display.blit(characterimg, (x,y))
x = (display_width * 0.30)
y = (display_height * 0.2)
pygame.display.set_caption('No U')
clock = pygame.time.Clock()#game clock
flip_right = False
x_change = 0
y_change = 0
bg_x = 0
start = True
bg = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/bg.png').convert()
class player:
def __init__(self, x, y):
self.jumping = False
p = player(x, y)
while start:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
start = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change += -4
if flip_right == True:
characterimg = characterimg_left
flip_right = False
x += -150
elif event.key == pygame.K_RIGHT:
x_change += 4
if flip_right == False:
characterimg = characterimg_right
flip_right = True
x += 150
elif event.key == pygame.K_UP:
y_change += -4
elif event.key == pygame.K_DOWN:
y_change += 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change += 4
elif event.key == pygame.K_RIGHT:
x_change += -4
elif event.key == pygame.K_UP:
y_change += 4
elif event.key == pygame.K_DOWN:
y_change += -4
x += x_change
y += y_change
display.fill(white)
soldier(x,y)
pygame.display.update()
clock.tick(120)#fps
pygame.quit()
我尝试了好几次,包括切换到按键方式,但都失败了。请帮忙,谢谢。
基本上你想限制玩家的移动。
所以每次你想要 "move" 玩家(我猜这是 "x_change" / "y_change")你需要检查他们是否仍然在你的边界内移动。
示例:您的显示 x 边界在屏幕左侧为 0 像素,在右侧为 500 像素。我只允许实际移动,如果移动的结果在我的范围内。
boundary_x_lower = 0
boundary_x_upper = 500
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if boundary_x_lower < (x_change - 4):
# I allow the movement if I'm still above the lower boundary after the move.
x_change -= 4
elif event.key == pygame.K_RIGHT:
if boundary_x_upper > (x_change +4):
# I allow the movement if I'm still below the upper boundary after the move.
x_change += 4
PS:我对你的代码感到困惑,因为当你向右移动时你会减去...我习惯了 2D 游戏,如果你向右移动你会增加玩家的位置......如果你向左走,则减去。
请随意调整代码以适合您的项目。基本原理也适用于 y 轴运动:boundary_y_lower & _y_upper。如果您还有其他问题,请提问!
只需将 x
和 y
值限制在 0 和显示宽度和高度之间。
# In the main while loop after the movement.
if x < 0:
x = 0
elif x + image_width > display_width:
x = display_width - image_width
if y < 0:
y = 0
elif y + image_height > display_height:
y = display_height - image_height
我还建议您查看 pygame.Rect
s 的工作原理。你可以用显示器的大小定义一个矩形,
display_rect = display.get_rect()
以及将用作 blit 位置的字符的矩形:
rect = characterimg_left.get_rect(center=(x, y))
然后以这种方式移动并夹紧矩形:
rect.move_ip((x_change, y_change))
rect.clamp_ip(display_rect)
display.fill(white)
# Blit the image at the `rect.topleft` coordinates.
display.blit(characterimg, rect)