简体   繁体   English

python,pygame图片移动

[英]python, pygame image movement

Hey i'm trying to move an image in python with either arrow keys or WASD but i keep getting an indent error 嘿,我正在尝试使用箭头键或WASD在python中移动图像,但我一直收到缩进错误

File "1stgame.py", line 43 elif event.key == pygame.K_RIGHT: ^ IndentationError: unindent does not match any outer indentation level 文件“ 1stgame.py”,第43行elif event.key == pygame.K_RIGHT:^ IndentationError:unindent与任何外部缩进级别都不匹配

import pygame

#Start pygame

pygame.init()

#Window/Screen/Display

display_x = 1280
display_y = 720
display = pygame.display.set_mode((display_x,display_y))
pygame.display.set_caption('Platforms')

clock = pygame.time.Clock()

#Colors

black = (0,0,0)
green = (1,166,17)

#Images
character = pygame.image.load('character.gif')
def chrctr(x,y):
    display.blit(character,(x,y))
x_c = (display_x  / 2)
y_c = (display_y / 2)

floor_1 = pygame.image.load('wood.jpg')
def floor(x,y):
    display.blit(floor_1,(x,y))
x = (display_x * 0)
y = (display_y * 0.9)

not_dead=True
while not_dead:
    for event in pygame.event.get():
        if (event.type==pygame.QUIT):
            not_dead=False 

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_c = -5
            elif event.key == pygame.K_RIGHT:
                x_c = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_c = 0

    display.fill(green) 
    pygame.draw.rect(display, black, [0, 550, 200, 50])
    pygame.draw.rect(display, black, [0, 450, 200, 50])
    pygame.draw.rect(display, black, [0, 350, 200, 50])
    pygame.draw.rect(display, black, [0, 250, 200, 50])
    pygame.draw.rect(display, black, [0, 150, 200, 50])
    pygame.draw.rect(display, black, [0, 50, 200, 50])
    pygame.draw.rect(display, black, [1080, 550, 200, 50])
    pygame.draw.rect(display, black, [1080, 450, 200, 50])
    pygame.draw.rect(display, black, [1080, 350, 200, 50])
    pygame.draw.rect(display, black, [1080, 250, 200, 50])
    pygame.draw.rect(display, black, [1080, 150, 200, 50])
    pygame.draw.rect(display, black, [1080, 50, 200, 50])

    floor(0,display_y * 0.9)
    floor(236, display_y * 0.9)
    floor(472, display_y * 0.9)
    floor(708, display_y * 0.9)
    floor(944, display_y * 0.9)
    floor(1180, display_y * 0.9)
    chrctr(x_c, y_c)


    pygame.display.update()
    clock.tick(60)
print "Hello"

pygame.quit()

Your code is correct, it ran in my computer without error. 您的代码正确无误,已在我的计算机上正确运行。 Possibly you accidentally indented wrong in your source code. 可能是您不小心在源代码中缩进了错误。 But the one you posted above worked magnificently, though it doesn't work quite as you expected to. 但是您上面发布的内容效果很好,尽管它并没有达到您预期的效果。

And as a side note, I would also suggest you to add the brackets around "hello" for print to make it version compatible print("hello") . 另外,我还建议您在“ hello”周围添加括号以进行打印,以使其与版本兼容print("hello")

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

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