使用可移动的字符/图像在 Pygame 中添加边界

我一直在创建一个游戏,其中使用 Keydown 和 Keyup 方法根据玩家输入移动图像。我想添加边界,以便用户无法将图像/字符移出显示器(如果边界被击中,我不希望游戏超过某种东西,只是图像/字符将无法移动到该边界之外)


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

我尝试了几次,包括切换到按键方法,但都失败了。请帮忙,谢谢。


红颜莎娜
浏览 150回答 2
2回答

料青山看我应如是

基本上你想限制玩家的移动。因此,每次您想“移动”玩家时(我猜这是“x_change”/“y_change”),您都需要检查移动后他们是否仍在您的边界内。示例:您的显示器 x 边界是屏幕左侧 0 像素,右侧 500 像素。如果运动的结果在我的范围内,我只允许实际运动。&nbsp;boundary_x_lower = 0&nbsp;boundary_x_upper = 500&nbsp;if event.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.key == pygame.K_LEFT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if boundary_x_lower < (x_change - 4):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # I allow the movement if I'm still above the lower boundary after the move.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x_change -= 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif event.key == pygame.K_RIGHT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if boundary_x_upper > (x_change +4):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# I allow the movement if I'm still below the upper boundary after the move.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x_change += 4PS:当你向右移动时,我对你的代码感到困惑......我习惯于2D游戏,如果你向右移动,你会增加玩家的位置......如果你向左移动则减去。随意调整代码以适合您的项目。基本原理也适用于 y 轴运动:使用 bounding_y_lower 和 _y_upper。如果您还有其他问题,请提问!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python