首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Pygame使用call by sharing时传递参数

Python Pygame使用call by sharing时传递参数
EN

Stack Overflow用户
提问于 2020-01-04 21:10:00
回答 1查看 33关注 0票数 1

我是第一次使用call by sharing,搜索结果但没有答案,我想通过共享call传递参数

下面是我的代码:

代码语言:javascript
复制
#!/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()
EN

回答 1

Stack Overflow用户

发布于 2020-01-04 21:16:19

在创建class时,即在启动game()函数之前,将计算类级变量。

您应该使pic成为常规的实例成员(即使用self.pic),或者应该将其预初始化为None,并且仅在第一次调用构造函数时才真正对其进行惰性初始化

代码语言:javascript
复制
class Setting:
    pic = None

    def __init__(self, width, height):
        if Setting.pic is None:
            # This code will execute only once
            Setting.pic = ...
        ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59590988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档