2017-05-03 102 views
2

我在网上发现了一个教程(在Youtube上),它向我展示了使用pygame创建游戏的基础知识。 我在与py脚本相同的文件夹中保存了一个png图像。当我运行脚本时,它显示没有错误,但我的图像没有显示在pygame窗口中。请咨询我的Pygame图像未加载

这里的脚本

import pygame,sys 

pygame.init() 
WIDTH,HEIGHT = 640,360 

screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32) 

clock = pygame.time.Clock() 
FPS = 24 
dog_img = pygame.image.load("dog.png") 

#PROCESS 
while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

#PROCESS 
#LOGIC   
#LOGIC     
#DRAW 
screen.blit(dog_img,(0,0)) 

pygame.display.flip() 

#DRAW 
clock.tick(FPS) 
+1

您示例的缩进是否正确? 'screen.blit(dog_img,(0,0))','pygame.display.flip()'和'clock.tick(FPS)'应该在while循环中。 – skrx

+0

你能改正脚本吗..我刚刚开始学习代码.. –

+0

Thanx男人它的工作... –

回答

3

修复缩进。 screen.blit(dog_img, (0, 0)),下面两行应该在while循环的内部(缩进4个空格)。

import pygame,sys 

pygame.init() 
WIDTH, HEIGHT = 640, 360 

screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32) 

clock = pygame.time.Clock() 
FPS = 24 
# Always use `.convert()` or `.convert_alpha()`. It'll improve the performance. 
dog_img = pygame.image.load("dog.png").convert_alpha() 

#PROCESS 
while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

    #LOGIC 
    #DRAW 
    screen.blit(dog_img, (0, 0)) 

    pygame.display.flip() 

    clock.tick(FPS) 
+0

让我知道这是否工作,或如果有更多的问题。 – skrx