gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:49:05 26 4
gpt4 key购买 nike

我对 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

鉴于重新定位不是对象并且不会保存属性

position3 的值已通过重新定位方法更新为对象,并写入 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 )

关于python - 如何在类和函数之间移动变量而不改变它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com