简体   繁体   English

Pygame 问题:我的播放器图像不会移动

[英]Pygame problem: My player image won't move

I am creating a game in python with a scrolling background and I can't get my player to move.我正在 python 中创建一个带有滚动背景的游戏,但我无法让我的玩家移动。 Just so you know, we are not allowed to use classes or sprites请注意,我们不允许使用类或精灵

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()

this is what I have tried so far and an error shows up saying " local variable 'x' referenced before assignment" for line 40. Does anyone know any other way for making my player move?这是我到目前为止所尝试的,并且在第 40 行出现了一个错误,显示“在赋值之前引用了局部变量‘x’”。有谁知道让我的玩家移动的其他方法吗?

The position of the player is ( x , y ) rather than ( movex , movey ).播放器的 position 是 ( x , y ) 而不是 ( movex , movey )。 Furthermore there is a ',' missing in the argument list:此外,参数列表中缺少一个“,”:

screen.blit(player (movex, movey))

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

Of course you have to define x and y somewhere before the application loop:当然,您必须在应用程序循环之前的某处定义xy

x = 0
y = 0

while running:
    # [...]

    x += movex
    y += movey

    # [...]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM