Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图像不显示_Python_Pygame_Filenames - Fatal编程技术网

Python 图像不显示

Python 图像不显示,python,pygame,filenames,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):

不显示除图像外的所有作品。只有黑屏。为什么?这就是代码:

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

我不知道为什么会这样。首先,您决定学习python。

首先,您不需要这四行代码:

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

... 

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

window.blit(screen, (0, 0))
您应该将
屏幕.填充
替换为
窗口.填充

当您退出时,图像将显示,然后程序将退出。
如果你想让图像一直显示,你应该把
屏幕。填充
激光。渲染
显示。完成时在
循环中翻转
(但不在
循环中)


您应该在文件末尾添加
pygame.time.wait(500)或任何其他数字,这样您的程序在按下退出按钮后不会立即退出。

问题在于“while done”循环下的缩进(双位线“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()

不清楚什么不够清楚?你决定让别人帮你学习python?你的游戏标题很贴切。好吧,我像你说的那样扔掉了台词,我有相同的黑屏,退出后我收到消息NameError:name'screen'没有定义。所以,当我在完成时放入screen.fill、laser.render和display.flip时,我得到了相同的结果,但窗口立即关闭。当我放置pygame.Surface((600600))时,没有错误,但仍然是黑屏。我想原因是别的。