图像不显示

时间:2014-10-25 12:39:09

标签: python pygame filenames

一切正常但图像不显示。只有黑屏。为什么?那是代码:

import pygame

window = pygame.display.set_mode((600, 600))
pygame.display.set_caption("ASSHOLE!")
screen = pygame.Surface((600, 600))
screen.fill((50,50,50))

class Sprite:
    def __init__(self, xpos, ypos, filename):
        self.x=xpos
        self.y=ypos
        self.bitmap=pygame.image.load(filename)
        self.bitmap.set_colorkey((0,0,0))
    def render(self):
        screen.blit(self.bitmap, (self.x,self.y))

laser = Sprite(0, 0, 'laser.png')

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

screen.fill((50,50,50))

laser.render()
window.blit(screen, (0,0))

window.blit(screen, (0, 0))
pygame.display.flip()

不知道为什么会这样。首先决定学习蟒蛇。

2 个答案:

答案 0 :(得分:0)

首先,您不需要这四行:

screen = pygame.Surface((600, 600))
screen.fill((50,50,50))

... 

window.blit(screen, (0,0))

window.blit(screen, (0, 0))

您应该将screen.fill替换为window.fill

然后退出图像时会出现,然后程序退出 如果您希望始终显示图片,则应将screen.filllaser.renderdisplay.flip放在while done循环中(但不要放在for循环中)< / p>

您应该在文件末尾添加pygame.time.wait(500) # Or any other number,这样您按下退出按钮后程序就不会立即退出。

答案 1 :(得分:0)

问题出在“完成”循环下的缩进(双线“window.blit(screen,(0,0))”和“screen.fill((50,50,50))”也被删除了但这不是严重错误)。错误代码:

import pygame

window = pygame.display.set_mode((600, 600))
pygame.display.set_caption("GAME")
screen = pygame.Surface((600, 600))

class Sprite:
    def __init__(self, xpos, ypos, filename):
        self.x=xpos
        self.y=ypos
        self.bitmap=pygame.image.load(filename)
        self.bitmap.set_colorkey((0,0,0))
    def render(self):
        screen.blit(self.bitmap, (self.x,self.y))

laser = Sprite(0, 0, 'laser.png')

done = True
while done:
    window.fill((50,50,50))
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            done = False

    screen.fill((50,50,50))

    laser.render()
    window.blit(screen, (0,0))
    pygame.display.flip()