我是第一次使用call by sharing,搜索结果但没有答案,我想通过共享call传递参数
下面是我的代码:
#!/usr/bin/python
import sys,os
import pygame
class Setting():
'''how to deliver self.w and self.h into pic'''
pic = pygame.transform.smoothscale(pygame.image.load("pic.png"),(self.w,self.h)) #how to deliver self.w and self.h in here?
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=pygame.RESIZABLE
self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
self.screen_rect=self.screen.get_rect()
self.bkg=Setting.pic.convert()
pygame.display.set_caption("Muhaha")
def game():
pygame.init()
setting=Setting(1200,800)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
setting.screen.blit(setting.bkg,(0,0))
pygame.display.flip()
game()发布于 2020-01-04 21:16:19
在创建class时,即在启动game()函数之前,将计算类级变量。
您应该使pic成为常规的实例成员(即使用self.pic),或者应该将其预初始化为None,并且仅在第一次调用构造函数时才真正对其进行惰性初始化
class Setting:
pic = None
def __init__(self, width, height):
if Setting.pic is None:
# This code will execute only once
Setting.pic = ...
...https://stackoverflow.com/questions/59590988
复制相似问题