为什么不赢这项工作? - 通过变量使用.blit

时间:2017-07-30 03:23:05

标签: python-2.7 variables pygame

使用变量来打印某些东西应该在python中工作。为什么我运行代码时不起作用?变量名称为bliting。如果这个问题可以解决,我该怎么办?谢谢。

以下是代码:

import pygame

pygame.init()
window = pygame.display.set_mode((800,600))
image = pygame.image.load("o1.png")
bliting = window.blit(image, (0,0))
gameLoop = True
while gameLoop:
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            gameLoop = False

#why does typing in the variable name "bliting" not blit my image?
    bliting

    window.fill((255,255,255))
    pygame.display.flip()
 pygame.quit()

1 个答案:

答案 0 :(得分:2)

确实如此,问题是你只拍了一次图像,然后屏幕连续充满了白色。试试下面的代码。

import pygame

pygame.init()
window = pygame.display.set_mode((800,600))
image = pygame.image.load("o1.png")
bliting = window.blit(image, (0,0))
gameLoop = True
while gameLoop:
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            gameLoop = False

    bliting

    pygame.display.flip()
 pygame.quit()

请注意我所做的就是删除屏幕填充。虽然这个“有效”,但更好的选择是创建一个功能,使图像闪烁并在循环中调用它。

此外,为了将来参考,请确保您在screen.fill()之后而不是之前是blitting图像。虽然在这种情况下无关紧要,但人们经常会跳过这个错误。

如果您遇到类似问题,请随时告诉我们。)

相关问题