我是一名C程序员,但想在python中对我的一些项目进行原型设计,到目前为止还没问题,但我遇到了一个可能很容易出问题的问题: 我有一个内部不起作用的类,当我在主模块中重新创建内部时,一切正常,所以我假设我使用自我或迭代有问题。
CBallSpawner:
from CBullet import Bullet
class BulletSpawner:
def __init__(self, count, position, color, radius, speed):
self.bull = []
self.cnt = count
self.speed = speed
self.freeze = False
self.acc = 0
self.position = position
self.createBullets(color, radius)
def createBullets(self, color, radius):
for i in range(1, self.cnt + 1):
self.bull.append(Bullet(radius, 0, color, i*(360.0/self.cnt), self.speed, self.position))
def speedUp(self, delta):
self.acc += delta
if self.acc > 1000 and not self.freeze:
self.speed += 0.1
for b in self.bull:
b.speed = self.speed
self.acc = 0
def tick(self, delta, w, h, window):
self.speedUp(delta)
for e in self.bull:
e.tick(delta, w, h)
def draw(self, window):
for c in self.bull:
window.draw(c.bullet_object)
对象已创建但仍处于初始位置。当我调试bull中对象的位置时没有变化。如果三角洲足够高,则子弹似乎随机移动。
主要是我打电话
spawner = BulletSpawner(10, sf.Vector2(w/2, h/2), sf.Color.WHITE, 5, 0.16)
spawner.tick(delta, w, h, window)
spawner.draw(window)
子弹类和完全相同的迭代在主模块中正常工作,但由于我将其外包给BulletSpawner类,因此子弹将不再正常移动。谢谢你的建议 E:Bullet的重要部分,它们完全独立工作
class Bullet:
def __init__(self, radius, outline, color, phi, speed, position):
self.phi = 0
self.speed = speed
self.bullet_object = sf.CircleShape()
self.position = sf.Vector2()
self.bounding_pos = sf.Vector2()
self.motion = sf.Vector2()
self.bounding = sf.Rectangle()
self.bullet_object.outline_thickness = outline
self.bullet_object.fill_color = color
self.bullet_object.origin = (radius, radius)
self.bullet_object.radius = radius
self.bounding.size = sf.Vector2(radius*2,radius*2)
self.bounding.position = self.position - (self.bounding.size/2)
self.position = position
self.phi = phi
self.calulateMotion()
def calulateMotion(self):
self.motion.x = math.cos(self.phi * (math.pi / 180))
self.motion.y = math.sin(self.phi * (math.pi / 180))
def tick(self, delta, w, h):
tmp = sf.Vector2(0, 0)
tmp.x = self.position.x + (self.motion.x * (self.speed * delta))
tmp.y = self.position.y + (self.motion.y * (self.speed * delta))
if tmp.x < 0 or tmp.x > w:
self.motion.x = -self.motion.x
self.bullet_object.fill_color = sf.Color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
if tmp.y < 0 or tmp.y > h:
self.motion.y = -self.motion.y
self.bullet_object.fill_color = sf.Color(random.randint(0,255),random.randint(0,255),random.randint(0,255))
self.position.x += self.motion.x * (self.speed * delta)
self.position.y += self.motion.y * (self.speed * delta)
self.bullet_object.position = self.position
self.bounding.position = self.position - (self.bounding.size/2)
编辑:有趣的足够如果我只使用1个子弹创建一个spawner对象:
spawner = BulletSpawner(1, sf.Vector2(w/2, h/2), sf.Color.WHITE, 5, 0.16)
子弹正确移动