Skip to content Skip to sidebar Skip to footer

Colour Changing Bouncing Ball

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 a

Solution 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]

defcreateball():
    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:

defmoveball(x, y, dx, dy, color):
    x, y = x + dx, y + dy
    ifnot radius < x < screensize[0]-radius:
        dx = -dx
        color = random.choice(color_list)
    ifnot 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]

defcreateball():
    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

defmoveball(x, y, dx, dy, color):
    x, y = x + dx, y + dy
    ifnot radius < x < screensize[0]-radius:
        dx = -dx
        color = random.choice(color_list)
    ifnot 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 = Truewhile 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.

Post a Comment for "Colour Changing Bouncing Ball"