0% found this document useful (0 votes)
8 views6 pages

Search Creators CG LAB Program-06

lab55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Search Creators CG LAB Program-06

lab55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Subject Code:21CSL66

Subject: COMPUTER GRAPHICS AND IMAGE PROCESSING


LABORATORY

Program-06

6. Develop a program to demonstrate Animation effects on simple objects.

Program

import pygame

import random

# Initialize Pygame

pygame.init()

# Set up the display

screen_width = 800

screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))

pygame.display.set_caption("Animation Effects")

# Define colors

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)

RED = (255, 0, 0)

Search Creators…. Page 1


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

# Define object properties

num_objects = 10

objects = []

for _ in range(num_objects):

x = random.randint(50, screen_width - 50)

y = random.randint(50, screen_height - 50)

radius = random.randint(10, 30)

color = random.choice([RED, GREEN, BLUE])

speed_x = random.randint(-5, 5)

speed_y = random.randint(-5, 5)

objects.append({"x": x, "y": y, "radius": radius, "color": color, "speed_x":


speed_x, "speed_y": speed_y})

# Main loop

running = True

clock = pygame.time.Clock()

while running:

# Handle events

for event in pygame.event.get():

if event.type == pygame.QUIT:

Search Creators…. Page 2


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

running = False

# Clear the screen

screen.fill(WHITE)

# Update and draw objects

for obj in objects:

# Move the object

obj["x"] += obj["speed_x"]

obj["y"] += obj["speed_y"]

# Bounce off the edges

if obj["x"] - obj["radius"] < 0 or obj["x"] + obj["radius"] > screen_width:

obj["speed_x"] = -obj["speed_x"]

if obj["y"] - obj["radius"] < 0 or obj["y"] + obj["radius"] > screen_height:

obj["speed_y"] = -obj["speed_y"]

# Draw the object

pygame.draw.circle(screen, obj["color"], (obj["x"], obj["y"]), obj["radius"])

# Update the display

pygame.display.flip()

clock.tick(60) # Limit the frame rate to 60 FPS


Search Creators…. Page 3
21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

# Quit Pygame

pygame.quit()

Output

Search Creators…. Page 4


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Explanation

1. Imports the required modules: pygame for creating the graphical window and
handling events, and random for generating random values.
2. Initializes Pygame and sets up a window with a width of 800 pixels and a
height of 600 pixels.
3. Defines some colors (BLACK, WHITE, RED, GREEN, BLUE) as RGB
tuples.
4. Initializes a list called objects to store the properties of each circle object. The
properties include the x and y coordinates, radius, color, and velocities
(speed_x and speed_y).
5. Generates num_objects (set to 10) with random positions, radii, colors, and
velocities, and appends them to the objects list.
6. Enters the main loop, which runs until the user closes the window.
7. Inside the main loop:
8. Handles the Pygame event queue, checking for the QUIT event to exit the
loop.
9. Clears the screen by filling it with the WHITE color.
10.Iterates over each object in the objects list:
11.Updates the x and y coordinates of the object based on its velocities.
12.Checks if the object has collided with the edges of the screen. If so, it reverses
the corresponding velocity component (x or y) to make the object bounce off
the edge.
13.Draws the object (circle) on the screen using pygame.draw.circle with the
object's color, position, and radius.
14.Updates the display using pygame.display.flip().
15.Limits the frame rate to 60 frames per second (FPS) using clock.tick(60).

Search Creators…. Page 5


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

16.After the main loop ends, the code quits Pygame.

Search Creators…. Page 6

You might also like