我正在尝试学习使用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()
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
答案 0 :(得分: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()
答案 1 :(得分:2)
这是我在游戏中使用的图像处理块:
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()
你可以在任何游戏中复制粘贴它,它会起作用。 <{1}}和os
需要导入游戏,否则无效。
答案 2 :(得分:0)
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( )