我正在为repl.it游戏卡纸制作游戏。我试图将图像放在与创建图像的类不同的类中创建的画布上。画布已成功显示文本和带有所需图像的按钮,但是create_image
无法正常工作。
这可能是分辨率问题,但我现在无法检查,图像是1920 x 1080,游戏也是。
我已经尝试过create_image
。
Full program(包括图片)
class game(Application):
"""Where game elements are to be held"""
def __init__(self, players, location, sWidth, sHeight, canvas):
"""Initiate variables & start game"""
#Initiate variables
self.players = players
self.location = location
self.screenWidth = sWidth
self.screenHeight = sHeight
self.Canvas1 = canvas
self.LOCATIONS = Application.LOCATIONS
self.font = Application.font
#Gathering Images
self.map1BG = PhotoImage(file = "polasib.gif")
#Debugging
print("Loading Map", self.location\
, "\nPlayers:", self.players)
self.createLevel(self.location)
def createUI(self, players):
"""Creates the UI that all levels will have"""
self.Canvas1.create_text(self.screenWidth/2, self.screenHeight/16, fill = "white", \
font = (self.font, self.screenWidth//34), text = self.LOCATIONS[self.location - 1])
def createLevel(self, location):
"""Creates the elements of the level"""
if self.location == 1:
#Polasi b
print("Creating Polasi b Level")
self.createUI(self.players)
self.Canvas1.create_image(self.screenWidth/2, self.screenHeight/2, image = self.map1BG, \
anchor = NW)
期望:我希望图像能够加载(并且需要重新对齐)
结果:没有图像出现,但是添加的所有其他内容(作为测试)都有效。
答案 0 :(得分:1)
由于您没有保存对game
实例的引用,因此退出Application.gameScreen()
后将销毁该引用。因此,image
中create_image
的引用将丢失,并且不会显示任何图像。尝试将game
的实例分配给Application
的实例变量,如下所示:
self.running_game = game(self.players, self.mapChosen, self.screenWidth, self.screenHeight, self.Canvas1)