如何在python/pygame中显示图像
我正在努力学习使用pygame制作一个基本的游戏。我想导入并显示.png格式的图像。到目前为止,我的尝试是:如何在python/pygame中显示图像,python,pygame,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 ru
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)与文件位于同一文件夹中。当我尝试运行此操作时,我得到一个错误:
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
回溯(最近一次呼叫最后一次):
文件“C:\Users\Enrique\Dropbox\gamez.py”,第3行,在
pygame.image.load('clouds.png')
错误:无法打开clouds.png
给你。它会将图像设置为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()
下面是我在游戏中使用的图像处理块:
import os, sys
...
-snip-
...
def load_image(name, colorkey=None):
fullname = os.path.join('images', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', name
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
你可以复制粘贴在任何游戏,它将工作
os
和sys
需要导入到您的游戏中,否则它将无法工作。您是否尝试过提供图像的绝对路径?假设:“C:\Users\Enrique\Dropbox\clouds.png”是您支持png的python版本吗?您的clouds.png是否命名为clouds.png?/一些大写问题?我应该在哪里添加绝对路径?不,我已经更正了文件名。别担心,我肯定明天会有更多的问题。另外,这里是pygame的一个非常好的介绍:,它应该涵盖您在这里所做的一切-这就是我学习的地方。请注意,您通常应该调用加载图像(pygame.Surface)的orconvert\u alpha
方法,因为这将极大地提高性能,例如img=pygame.image.load('clouds.bmp').convert()
。您好,您是否希望添加您在程序中所做的详细信息和假设,例如分辨率应为460x640、图像的路径是什么、如果源图像的分辨率不同,会发生什么等,这将是一个更完整的答案,便于人们理解和消费。欢迎来到堆栈溢出。
import pygame as pg
pg.init( )
screen = display.set_mode( ( 460 , 640 ) )
def picture( images , x , y , width , height , angle , horizontal_flip , vertical_flip ):
images1 = image.load( images ) # loading image
images2 = transform.scale( images1 , ( width , height ) ) # changing size
images3 = transform.rotate( images2 , angle ) # changing angle 0 - 360 degree
images4 = transform.flip( images3 , horizontal_flip , vertical_flip ) # horizontal or vertical flip - True or False
images4rect = images4.get_rect( )
images4rect.x = x # setting x
images4rect.y = y # setting y
screen.blit( images4 , images4rect ) # image blit
while True:
screen.fill( ( 0 , 0 , 0 ) )
images = "image.png" # image name
x = 100 # x-axis
y = 100 # y-axis
angle = 0 # 0 - 360
horizontal_flip = False # True
vertical_flip = False # True
width = 200 # width of image
height = 150 # height of image
picture( images , x , y , width , height , angle , horizontal_flip , vertical_flip )
pg.display.flip( )