i have looked all over but cannot find how to best achieve my programming objective.
currently designing the basics for a physics engine for pygame.
i want to be able to create a new ball using the Ball class i created every 10 seconds these balls i need to code the actions for but i need a way to create a instance automatically
class code:
class Ball:
def __init__(self, radius, mass, colour):
self.name = self
self.radius = radius
self.colour = colour
self.mass = mass
self.pos = [50, 50]
self.vel = [0, 0]
self.acc = [0, 0]
self.forces = [0, 0]
self.on_ground = False
def get_pos(self):
return self.pos[0], self.pos[1]
def tick(self):
self.forces = [0, GRAVITY]
Ball.apply_force(self)
Ball.update_attributes(self)
pygame.draw.circle(screen, self.colour, (int(self.pos[0]), int(self.pos[1])), self.radius)
def apply_force(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] == 1 and self.on_ground:
self.forces[0] -= 0.2
if keys[pygame.K_RIGHT] == 1 and self.on_ground:
self.forces[0] += 0.2
if keys[pygame.K_SPACE] == 1 and self.on_ground:
self.on_ground = False
self.forces[1] = -20
if self.on_ground:
self.forces[1] = 0
if self.vel[0] > 0:
self.forces[0] -= (abs(self.vel[0])) * 0.1
else:
self.forces[0] += (abs(self.vel[0])) * 0.1
def update_attributes(self):
self.acc = [self.forces[0] / self.mass, self.forces[1] / self.mass]
self.vel = [self.vel[0] + self.acc[0], self.vel[1] + self.acc[1]]
if (self.pos[1] + self.vel[1]) > (300-self.radius) and not self.on_ground:
self.pos[1] = (300 - self.radius)
self.vel[1] = 0
self.on_ground = True
else:
self.pos[1] += self.vel[1]
self.pos[0] += self.vel[0]
while loop:
count = 0
name = ball_1
while running:
pygame.time.delay(10)
count += 1
if count == 1000:
#create instance here called name
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
blueBall.tick()
redBall.tick()
pygame.draw.rect(screen, (0, 200, 0), (0, 300, 1300, 200))
pygame.display.update()
screen.fill((100, 100, 255))
i want to be able to index the names of the balls to be able to access them easily
e.g. ball_1
, ball_2
, ball_3
Is this possible?