1

I'm working on a project to make an animation in pygame. The assignment is this:

Add 2 other balls to the screen (3 in total) that will all be different colors, different sizes and start in different directions. They will all bounce around continuously.

Hint: you will need to create all new variables for each ball
i.e. x2,y2, dx2, dy2
and you will need to check each ball individually for hitting a wall and update the vertical and horizontal position of each ball individually.

Level 4 Challenge: when the balls hit the wall and the colors change to make each color randomly change.

Here is my code so far, I am using Python 3.7 on a Mac. The code so far has the first ball bouncing and staying the same color. I can't figure out how to make two more balls and have them change color each time they hit a wall. If someone could please help I literally cannot figure this out.

import pygame
import sys
pygame.init()

screensize = (800,600)
screen = pygame.display.set_mode(screensize,0)
pygame.display.set_caption("Animation Test")

WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,255)
RED = (255,0,0)

screen.fill(WHITE)
pygame.display.update()


x = 100
y = 200
dx = 2
dy = 2
go = True
while go:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            go = False

    screen.fill(WHITE)

    x = x + dx
    y = y + dy
    Colour = BLUE 

    if (x>=775):
            dx = -dx
    elif (x>=775):
        dx = -dx
    Colour = RED
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
ashlee
  • 21
  • 3
  • as in description you need `x2, y2, dx2, dy2` for second ball - and you have to repeate code which uses `x,y,dx,dy` but using `x2, y2, dx2, dy2` . Similar `x3, y3, dx3, dy3` for third ball. And you may need `color2` and `color3` to keep colors for new balls. – furas Feb 20 '20 at 04:09

1 Answers1

1

Create a function (createball) which can crate a random ball. A ball is a tuple of x and y position, the movement vector dx, dy and the color ((x, y, dx, dy, color)). Create a certain number of ball (max_balls) with random positions (random.randint(a, b)) and random colors (random.choice(seq)):

radius = 25
color_list = [GREEN, BLUE, RED]

def createball():
    x = random.randint(radius, screensize[0]-radius)
    y = random.randint(radius, screensize[1]-radius)
    color = random.choice(color_list)
    return x, y, 2, 2, color

Crate the 3 balls and store them to the variables as suggested in the assignment (x2, y2, dx2, dy2):

x, y, dx, dy, color = createball()
x2, y2, dx2, dy2, color2 = createball()
x3, y3, dx3, dy3, color3 = createball()

Create a function (moveball) which can move a ball, change there direction when the hit the bounds and change the color. Move the balls in the application loop:

def moveball(x, y, dx, dy, color):
    x, y = x + dx, y + dy
    if not radius < x < screensize[0]-radius:
        dx = -dx
        color = random.choice(color_list)
    if not radius < y < screensize[1]-radius:
        dy = -dy
        color = random.choice(color_list)
    return x, y, dx, dy, color

while run:

    # [...]

    x, y, dx, dy, color = moveball(x, y, dx, dy, color)
    x2, y2, dx2, dy2, color2 = moveball(x2, y2, dx2, dy2, color2)
    x3, y3, dx3, dy3, color3 = moveball(x3, y3, dx3, dy3, color3)

See the example:

import pygame
import sys
import random

pygame.init()
screensize = (800,600)
screen = pygame.display.set_mode(screensize,0)
pygame.display.set_caption("Animation Test")
clock = pygame.time.Clock()

WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,255)
RED = (255,0,0)

radius = 25
color_list = [GREEN, BLUE, RED]

def createball():
    x = random.randint(radius, screensize[0]-radius)
    y = random.randint(radius, screensize[1]-radius)
    color = random.choice(color_list)
    return x, y, 2, 2, color

def moveball(x, y, dx, dy, color):
    x, y = x + dx, y + dy
    if not radius < x < screensize[0]-radius:
        dx = -dx
        color = random.choice(color_list)
    if not radius < y < screensize[1]-radius:
        dy = -dy
        color = random.choice(color_list)
    return x, y, dx, dy, color

x, y, dx, dy, color = createball()
x2, y2, dx2, dy2, color2 = createball()
x3, y3, dx3, dy3, color3 = createball()

go = True
while go:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            go = False

    x, y, dx, dy, color = moveball(x, y, dx, dy, color)
    x2, y2, dx2, dy2, color2 = moveball(x2, y2, dx2, dy2, color2)
    x3, y3, dx3, dy3, color3 = moveball(x3, y3, dx3, dy3, color3)

    screen.fill(WHITE)
    pygame.draw.circle(screen, color, (x, y), radius)
    pygame.draw.circle(screen, color2, (x2, y2), radius)
    pygame.draw.circle(screen, color3, (x3, y3), radius)
    pygame.display.flip()

A bit more sophisticated approach with a ball class can be found at Use vector2 in pygame.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174