Pygame问题:我的玩家形象无法移动

2024-09-27 07:36:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python创建一个有滚动背景的游戏,我无法让我的玩家移动。正如你所知,我们不允许使用类或精灵

def game():
    running = True
    backgrounX = 0
    button = 0
    movex = 0
    movey = 0
    player = image.load("images/player.gif")
    def drawscene(screen,button,backx):             

        screen.fill((0, 0 , 0))
        background = image.load("images/bc3.png") # Load image
        bc = transform.scale(background, (1000, 700)) # Scale the image
        screen.blit(bc, [backx, 0]) # To show image
        screen.blit(bc, [backx + 1000, 0]) # For background to move

        button_1 = Rect(0, 0, 100, 70)
        draw.rect(screen, (0, 85, 255), button_1)
        draw_text("Exit", font, (0, 0, 0), screen, 25, 25)  



    for e in event.get():
        if e.type == MOUSEBUTTONDOWN:
            if e.button == 1:
                main_menu()     

        # Game loop
    while running:
        for e in event.get():
            if e.type == QUIT:
                quit()
                sys.exit()
                running = False

            if e.type == KEYDOWN:
                if e.key == K_LEFT or e.key == ord('a'):
                    movex = -1
                if e.key == K_RIGHT or e.key == ord('d'):
                    movex = +1
                if e.key == K_UP or e.key == ord('w'):
                    movey = -1

            if e.type == KEYUP:
                if e.key == K_LEFT or e.key == ord('a') or e.key == K_RIGHT or e.key == ord('d'):
                    movex = 0
                if e.key == K_UP or e.key == ord('w'):
                    movey = 0

        x += movex
        y += movey


        drawscene(screen, button, backgrounX)
        screen.blit(player (movex, movey))
        myClock.tick(60)
        backgrounX -= 3 # background scroller speed
        display.update()

这就是我到目前为止所尝试的,第40行出现了一个错误,显示为“赋值前引用的局部变量'x'。有人知道让我的球员移动的其他方法吗


Tags: orkeyimageiftypebuttonscreenrunning
1条回答
网友
1楼 · 发布于 2024-09-27 07:36:03

玩家的位置是(xy),而不是(movexmovey)。此外,参数列表中缺少一个“,”:

screen.blit(player (movex, movey))

screen.blit(player, (x, y))

当然,您必须在应用程序循环之前的某个地方定义xy

x = 0
y = 0

while running:
    # [...]

    x += movex
    y += movey

    # [...]

    screen.blit(player, (x, y))

相关问题 更多 >

    热门问题