This project simulates bouncing shapes within a defined boundary. Each shape is assigned a unique piano note that plays when the shape collides with the boundary. The shapes can grow, change color, and vary in speed. The game speed adjusts dynamically based on CPU usage to prevent crashes.
- Features
- Prerequisites
- Installation
- Usage
- Directory Structure
- Configuration
- Scripts
- Contributing
- License
- Simulates multiple shapes (circle, rectangle, polygon) bouncing within a boundary.
- Shapes can grow, change color, and vary in speed.
- Each shape is assigned a unique piano note that plays upon collision with the boundary.
- Supports generating random scenarios with customizable parameters.
- Ability to record the simulation.
- Dynamic game speed adjustment based on CPU usage to prevent crashes.
- Python 3.7+
pydub
librarypsutil
libraryffmpeg
for audio processing
Run
./install
-
Clone the repository:
git clone https://github.com/yourusername/bouncing-shapes-with-sound.git cd bouncing-shapes-with-sound
-
Create a virtual environment and activate it:
python -m venv .venv source .venv/bin/activate
-
Install the required packages:
pip install pydub numpy psutil
-
Install
ffmpeg
:- On macOS:
brew install ffmpeg
- On Ubuntu:
sudo apt-get install ffmpeg
- On Windows:
Download and install
ffmpeg
from ffmpeg.org.
- On macOS:
-
Run with default settings:
./run
-
Run and generate random scenarios:
./run-random
-
Run and generate a specific number of random scenarios:
./run-random 20
-
Run with recording enabled:
./run --record
-
Generate default number of scenarios (10):
./generate-scenarios
-
Generate a specific number of scenarios:
./generate-scenarios 20
ball_bounce/
├── .venv/
├── base.py
├── boundary.py
├── display.py
├── generate-scenarios
├── generate_scenarios.py
├── README.md
├── recorder.py
├── run
├── run-random
├── scenarios.json
├── shape.py
└── sound.py
Each scenario in scenarios.json
defines the boundary and shapes with their respective properties.
{
"scenarios": [
{
"name": "Scenario 1",
"duration": 10,
"rect_x": 100,
"rect_y": 100,
"rect_width": 400,
"rect_height": 300,
"shapes": [
{
"shape": "circle",
"color_change": true,
"size_change": 2.5,
"speed_increase_factor": 1.03,
"growth_rate": 0.05,
"carrying_capacity": 250,
"sides_change": 0,
"initial_size": 30.0,
"initial_speed_x": 2.0,
"initial_speed_y": 3.0
},
{
"shape": "rectangle",
"color_change": false,
"size_change": 1.5,
"speed_increase_factor": 1.02,
"growth_rate": 0.04,
"carrying_capacity": 200,
"sides_change": 0,
"initial_size": 25.0,
"initial_speed_x": 3.0,
"initial_speed_y": 2.5
}
]
}
]
}
shape
: Type of shape (circle
,rectangle
,polygon
).color_change
: Whether the shape changes color upon collision.size_change
: Amount by which the shape's size changes.speed_increase_factor
: Factor by which the shape's speed increases upon collision.growth_rate
: Rate at which the shape grows.carrying_capacity
: Maximum size the shape can grow to.sides_change
: Number of sides to change for polygons.initial_size
: Initial size of the shape.initial_speed_x
: Initial speed in the x-direction.initial_speed_y
: Initial speed in the y-direction.
generate-scenarios
: Generates random scenarios.run
: Runs the main script.run-random
: Generates random scenarios and runs the main script.
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch
. - Make your changes and commit them:
git commit -m 'Add some feature'
. - Push to the branch:
git push origin feature-branch
. - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.