Python Can';t从函数而不是_init设置self.value__

Python Can';t从函数而不是_init设置self.value__,python,pygame,pycharm,Python,Pygame,Pycharm,我正在pygame中制作一个平台(只是为了好玩,学习新概念)。我试图制作一个滚动背景,当它到达某个点时,我需要重置x值。下面的代码不起作用,pycharm说“这个语句似乎没有效果”。除了\uuu init\uuu之外,我不能从任何函数更改self的值吗?以下是我的资料来源: 课程背景: 定义初始化(自身、图像位置): self.location=pygame.image.load(图像位置) #默认值V 左类: x=0-w/2 y=0 类别权利: x=w/2 y=0 # ^^^^^^^^^^^^

我正在pygame中制作一个平台(只是为了好玩,学习新概念)。我试图制作一个滚动背景,当它到达某个点时,我需要重置x值。下面的代码不起作用,pycharm说“这个语句似乎没有效果”。除了
\uuu init\uuu
之外,我不能从任何函数更改self的值吗?以下是我的资料来源:

课程背景:
定义初始化(自身、图像位置):
self.location=pygame.image.load(图像位置)
#默认值V
左类:
x=0-w/2
y=0
类别权利:
x=w/2
y=0
# ^^^^^^^^^^^^^
def滚动(自):
如果自左x>0-w:
self.left.x-=1
其他:
self.left.x==w
如果自右x>0-w:
self.right.x-=1
其他:
self.right.x==w
DISPLAYSURF.blit(self.location,(self.left.x,self.left.y))
DISPLAYSURF.blit(self.location,(self.right.x,self.right.y))```

w未定义,x,y应为类方法

下面的代码将起作用(假设定义了w)

左类:
x=0-w/2
y=0
类别权利:
x=w/2
y=0
班级背景:
定义初始化(自身、图像位置):
self.location=pygame.image.load(图像位置)
self.left=left()
self.right=right()
def滚动(自):
如果自左x>0-w:
self.left.x-=1
其他:
self.left.x==w
如果自右x>0-w:
self.right.x-=1
其他:
self.right.x==w
DISPLAYSURF.blit(self.location,(self.left.x,self.left.y))
DISPLAYSURF.blit(self.location,(self.right.x,self.right.y))```

使用嵌入类
background.left
background.right
?使
background
实例包含
。left
的方法与使其包含
位置的方法相同。