This program simulates a dynamic environment where a specified number of cubes, balls, and triangles interact with each other and bounce off the edges of the screen. It uses the Pygame library for rendering graphics and the Pymunk library for handling physics. The unique aspect of this program is how it changes the shapes and colors of the objects upon collision, giving a fascinating visual effect.
In more detail:
- The configuration allows customization of various aspects such as color of the objects, gravity, object speed, size, and number, mass of the cubes, elasticity, friction, screen size, frame rate, etc.
- The
Cube
class is an object-oriented representation of the objects on the screen. Despite its name, it can represent a cube, a ball, or a triangle. It contains data about the object, such as its size, color, and shape type, and methods for its functionality, such asdraw
. - The
handle_collision
function changes the color and shape type of the objects upon collision. A green object changes to white, and vice versa. The shape cycles through cube, triangle, and ball. - The
main
function is the driver function of the program. It initializes Pygame and Pymunk, creates the objects, adds them to the space, and handles the game loop. It also finds the slowest moving object at each frame and changes its color to red. - The objects move in the space with a speed in a random direction, and they bounce off the edges of the screen. When a collision between two objects occurs, their colors and shapes change. The slowest moving object at any frame is always colored red.
- The program renders the scene at each frame, displaying the objects and the count of objects of each color at the top-left corner of the screen.
This code demonstrates how to use Pygame and Pymunk to create a dynamic environment with objects interacting with each other and their environment. It provides a visually engaging way to understand the basic principles of physics and object-oriented programming. It's perfect for those looking to delve into the world of game development or simulation creation.