YCP Logo Lab 26: Bouncing Particles

Due: Friday, Nov 14th by 11:59 PM

Getting started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Download CS200_Lab26.zip. Import it into Visual Studio (File->Import...).

Add your code to the source file called Bounce.c.

Bouncing Particles

In this lab, you will implement a simulation of particles bouncing around in a container. The program is a frame-by-frame animation, like Lab 23 and Assignment 5.

You can run my solution to get an idea of what the program will look like when it runs:


Note that I increased the NUM_PARTICLES constant to 128.

Your task

You will need to add fields to the struct Particle type, and implement the init_particle, render_particle, and update_particle functions.

For each particle, your program will need to keep track of the particle's position, direction/velocity (x and y), and color.

The init_particle function should choose a random initial position, direction, and color for a single particle.

The render_particle function should draw the particle on the screen using its current position and color.

The update_particle function should move the particle one step by its current x and y direction/velocity. If the particle hits an edge of the screen, you should reverse its direction appropriately.


Change the definition of the NUM_PARTICLES constant to change the number of particles simulated.

Use floating point values to keep track of each particle's position and direction/velocity.

The built-in rand() function returns a randomly-chosen non-negative integer value. If you want a random integer in the range 0..N-1, use the following code:

rand() % N

Set each particle's initial x and y position to be random locations on the screen.. For example, set the particle's x coordinate to

rand() % cons_get_screen_width()

Set the initial x and y components of each particle's direction/velocity to be a random double value between -1.5 and 1.5. Here's one way to do it:

((rand() % 3000) - 1500) / 1000.0

To generate a random color, generate a random integer value between 0 and 15:

rand() % 16


When you are done, choose Tools->Submit to submit your completed lab to the server. You will need to enter your Marmoset username and password.

If the Tools->Submit option does not work, create a zip file containing the files in your project, and upload it to the submission server as lab26. The server URL is