1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Ann
  • 11
  • 1

3 Answers3

0

You can do something along the following lines:

balls = [Ball(rand_radius, rand_mass, rand_color) for i in range(10)]
Greg
  • 1,845
  • 2
  • 16
  • 26
0

Use a list for your balls.

balls = []
if count == 1000:
    balls.append(Ball(your_radius,your_mass,your_color))

And later you can reference them in the list by their index:

balls[0].tick()
balls[1].apply_force()
etc.
Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
0

i want to be able to create a new ball using the Ball class i created every 10 seconds

Create an empty list of balls:

balls = []

Further I recommend to use a timer event. Use pygame.time.set_timer() to repeatedly create an USEREVENT. e.g.:

milliseconds_delay = 10000 # 10.0 seconds
ball_event = pygame.USEREVENT + 1
pygame.time.set_timer(ball_event, milliseconds_delay)

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to start at pygame.USEREVENT. In this case pygame.USEREVENT+1 is the event id for the timer event, which spawns the balls.
The timer event can be stopped by passing 0 to the time parameter.

Create a new ball when the event occurs in the event loop:

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

         elif event.type == ball_event:

             radius = random.randint(minRad, maxRad)
             mass   = random.randint(minMass, maxMass)
             color  = random.choice([RED, BLUE, GREEN])
             balls.append(Ball(radius, mass, color))

    for ball in balls:
        ball.tick()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174