如何在python/pygame中显示图像

enr*_*334 7 python pygame

我正在努力学习使用pygame制作基本游戏.我想以.png格式导入和显示图像.到目前为止,我的尝试是:

import pygame
from pygame.locals import*
pygame.image.load('clouds.png')

white = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
screen.fill((white))
running = 1

while running:
    screen.fill((white))

    pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)

Image(clouds.png)与文件位于同一文件夹中.当我尝试运行这个我得到一个错误:

Traceback (most recent call last):
  File "C:\Users\Enrique\Dropbox\gamez.py", line 3, in <module>
    pygame.image.load('clouds.png')
error: Couldn't open clouds.png
Run Code Online (Sandbox Code Playgroud)

rsa*_*xvc 7

干得好.它将图像blit为0,0.你的另一个问题是你的pyimage似乎不是用png支持构建的

import pygame
from pygame.locals import*
img = pygame.image.load('clouds.bmp')

white = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
screen.fill((white))
running = 1

while running:
    screen.fill((white))
    screen.blit(img,(0,0))
    pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)