What You’ll Build
An animated program that:- Creates a 2D board/screen
- Draws a ball character (⚾)
- Animates the ball moving across the screen
- Makes the ball bounce off the edges
- Uses velocity to control direction and speed
What You’ll Learn
- Working with 2D slices (slice of slices)
- Understanding velocity and direction in graphics
- Creating smooth animations
- Using buffers for efficient rendering
- Screen manipulation techniques
- Basic physics simulation
Understanding Velocity
Velocity = Speed + Direction In 2D graphics:- X velocity: Controls horizontal movement
vx = 1→ ball moves rightvx = -1→ ball moves left
- Y velocity: Controls vertical movement
vy = 1→ ball moves downvy = -1→ ball moves up
Step-by-Step Approach
Draw the Board
Create a 2D board and draw static content to understand the rendering.Learn: How to create and iterate over 2D slices
Add a Buffer
Instead of printing directly, use a buffer for efficient rendering.Why: Reduces flicker and improves performance
Complete Example
Key Concepts
2D Slices
2D Slices
Buffer vs Direct Printing
Buffer vs Direct Printing
Direct printing (slower, flickers):Using buffer (faster, smooth):
Buffer Reuse
Buffer Reuse
Collision Detection
Collision Detection
Tips & Challenges
Enhancements to Try
- Multiple balls - Track multiple positions and velocities
- Gravity - Make vy increase over time going down
- Trail effect - Leave a fading trail behind the ball
- Different speeds - Use velocities other than 1 and -1
- Obstacles - Add walls or barriers to bounce off
- Colors - Use ANSI color codes for different elements
Practice Exercises
- Find the Bug - Debug a broken version
- Width and Height - Make board size configurable via arguments
- Previous Positions - Draw a trail of previous ball positions
- Single Dimensional - Implement using a 1D slice instead of 2D
- No Slice - Draw without using a board slice at all
Learning Resources
2D Graphics Basics
Crash Course on 2D graphics concepts
Understanding Velocity
Learn about velocity in programming
Next Steps
Retro LED Clock
Another animation project
Slices Guide
Master slices and arrays