如何在类和函数之间移动变量而不更改它们?



我对OOP和pygame相当陌生,所以这些可能是一些愚蠢而基本的问题 - 但是我已经坚持了好几天了,所以任何事情都会有所帮助。

我正在 Gun.shoot(( 中创建一个名为 position3 的变量,我希望这个变量移动到 Bullet.reposition(( 上,因为调用了 Bullet.reposition。然后,我希望 position3 变量移动到 Bullet.update(( 函数,该函数由代码中其他地方的不同进程调用。在整个过程中,position3 变量不应更改,而应保持不变。我已经设法让 position3 变量从 Gun.shoot(( 移动到 Bullet.reposition((,但是我现在无法将其放入 Bullet.update((。帮助!

class Bullet(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((5,5))
self.image.fill(red)
self.rect = self.image.get_rect()
# self.rect.center = (200,200)
self.positionofm = (23,0)
self.pos = vec(300,300)
self.centerlocation = vec(0,0)
self.position3 = vec(0,0)
def update(self):
self.position3 = reposition.position3
print("update",self.position3)
# self.rect.center = self.position3
self.centerlocation = random.randint(200,400),random.randint(200,400)
self.rect.center =(self.centerlocation)


def reposition(self,position3):
print("repositioning")
self.centerlocation = random.randint(200,400),random.randint(200,400)
self.rect.move(position3)
print("regular",position3)
self.position3 = position3
print("First update",self.position3)


class Gun(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30,5), pg.SRCALPHA)
self.image.fill(black)
self.rect = self.image.get_rect()
self.original_image = self.image
self.rect.center = (WIDTH/2 , HEIGHT/2)
self.pos = vec(WIDTH / 2, HEIGHT / 2)
self.offset = vec(20, 0)
self.angle=0
self.position2=vec(0,0)
# self.bullet = Bullet()
def shoot(self):
self.BulletEndPos=vec(0,0)
self.BulletEndPos=vec(pg.mouse.get_pos())
# print(self.BulletEndPos,"akshgdjasgdas")
position2x=self.position2[0]
position2y=self.position2[1]
position3=vec(0,0)
position3=(math.floor(position2x)),(math.floor(position2y))
Bullet.reposition(self, position3)

好吧,您的代码片段已经包含您需要的所有内容,您只需删除该行

self.position3 = reposition.position3

鉴于重新定位不是一个对象,也不会保存一个属性

位置 3 的值已针对重新定位方法上的对象进行了更新,并写入了 Bullet 对象属性中。另一种方法是像这样重写 update((:

def update(self, position3= None):
position_3 = position3 if position3 is not None else self.position3
print("update",position_3)
# self.rect.center = position_3
self.centerlocation = random.randint(200,400),random.randint(200,400)
self.rect.center =(self.centerlocation)

这使您可以更自由地在需要时将 position3 传递到代码中的其他位置,同时保留逻辑。

现在澄清几件事:

  1. 当你编写一个类时,你只是声明了类的整体结构,而不是创建它的任何实例。
  2. self 关键字引用类对象的引用实例,因此您需要创建可以保留这些变量的对象实例。

记住这一点,在你的最后一行方法拍摄中,你什么都不做,没有创建要重新定位和更新的子弹。所以你需要把你的 Gun 类改成这样:

class Gun(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30,5), pg.SRCALPHA)
self.image.fill(black)
self.rect = self.image.get_rect()
self.original_image = self.image
self.rect.center = (WIDTH/2 , HEIGHT/2)
self.pos = vec(WIDTH / 2, HEIGHT / 2)
self.offset = vec(20, 0)
self.angle=0
self.position2=vec(0,0)
self.bullet = Bullet()
def shoot(self):
self.BulletEndPos=vec(0,0)
self.BulletEndPos=vec(pg.mouse.get_pos())
# print(self.BulletEndPos,"akshgdjasgdas")
position2x=self.position2[0]
position2y=self.position2[1]
position3=vec(0,0)
position3=(math.floor(position2x)),(math.floor(position2y))
self.bullet.reposition(self, position3)

OOP有时可能会令人困惑,尤其是在开始时,因此您可以在线尝试其他一些资源(例如 https://www.tutorialspoint.com/python3/python_classes_objects.htm(

最新更新