Python使用共享调用时传递参数的Pygame

2024-09-29 21:56:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我是第一次使用呼叫分享,搜索结果却没有答案 我想通过共享将参数传递到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()

Tags: andtoinimportselfeventdisplaysys
1条回答
网友
1楼 · 发布于 2024-09-29 21:56:58

在创建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 = ...
        ...

相关问题 更多 >

    热门问题