Pygame问题:我的玩家图像无法移动

时间:2020-06-15 19:40:01

标签: python pygame

我正在使用具有滚动背景的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'”时出现错误。有人知道让我的玩家移动的其他方法吗?

1 个答案:

答案 0 :(得分:3)

播放器的位置为(document.querySelector('#submitControl').addEventListener('click', (event) => { event.preventDefault(); // get all checkboxes by its class const checkBoxes = document.querySelectorAll('.checkbox'); let checked = false; // loop over each checkbox to see if its checked and stop looping if true. for (let i = 0; i < checkBoxes.length; i++) { if (checkBoxes[i].checked) { checked = true; break; } } if (checked) { // submit form document.querySelector('form').submit(); } else { // show popup, for example toggle 'error-message--hidden' class that displays and hides the error message or alert() document.querySelector('.error-message').classList.toggle('error-message--hidden'); // window.alert('Please select at least one button.') } }) x),而不是(ymovex)。此外,参数列表中缺少“,”:

movey

screen.blit(player (movex, movey))

当然,您必须在应用程序循环之前的某个位置定义screen.blit(player, (x, y)) x

y
相关问题