如何在 python/pygame 中显示图像 [英] How to have an image appear in python/pygame

查看:100
本文介绍了如何在 python/pygame 中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to learn to make a basic game using pygame. I want to import and display an image in .png format. so far my attempt has been:

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

图像 (clouds.png) 与文件位于同一文件夹中.当我尝试运行此程序时出现错误:

The Image (clouds.png) is in the same folder as the file. when i try to run this i get an error:

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

推荐答案

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

Here you go. It blits the image to 0,0. Your other problem is that your pyimage doesn't seem to be built with png support

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

这篇关于如何在 python/pygame 中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆