您好,即使您可能认为存在类似的问题,我的情况也与此完全不同。
我正在尝试从目录中加载图像,并将屏幕尺寸(自动)设置为作为“背景”加载的图像的尺寸。
import pygame
import sys
from pygame.locals import *
image_resources = "C:/Users/user/Desktop/Pygame App/image_resources/"
class load:
def image(self, image):
self.image = image
return (image_resources + image)
def texture(self, texture):
self.texture = texture
return (image_resources + texture)
bg = load().image("bg_solid_black.jpg")
pygame.init()
#screen = pygame.display.set_mode((width,height),0,32)
#background = pygame.image.load(bg).convert()
#width = background.get_width()
#height = background.get_height()
我使用“ load()”类加载的图像设置为变量“ bg”,并且我想使用加载为“ bg”的大小来确定窗口的大小。如果您尝试移动
background = pygame.image.load(bg).convert()
width = background.get_width()
height = background.get_height()
最重要的是:
screen = pygame.display.set_mode((width,height),0,32)
PyGame返回错误,指出未设置显示模式。如果我这样做:
screen = pygame.display.set_mode((width,height),0,32)
background = pygame.image.load(bg).convert()
width = background.get_width()
height = background.get_height()
当然,这不是正确的,因为未定义变量“ width”和“ height”用于“ pygame.display.set_mode()”的使用。
我似乎无法解决这个问题,尽管我通过OO方式解决了,但我似乎无法解决。有什么帮助吗?
谢谢 :)
在convert()
任何表面上使用之前,必须使用初始化屏幕set_mode()
。
您可以加载图像并获取图像的大小,set_mode()
但是convert()
必须在初始化显示后使用它,如下所示:
import pygame
pygame.init()
image = pygame.image.load("file_to_load.jpg")
print(image.get_rect().size) # you can get size
screen = pygame.display.set_mode(image.get_rect().size, 0, 32)
image = image.convert() # now you can convert
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句