Thursday, March 9, 2017

Collision & Reflection

Collision: when an object makes contact with another object or collides with another object.
Reflection: when an object bounces back to you after it collides with another object.
Pong has been using that with the paddles reflecting the ball towards the other paddle. and when the ball collides with the paddle.

Wednesday, March 1, 2017

Objects Bouncing and Reflecting in Games

if (bouncer_x < 0 || bouncer_x > 640 - 32) {
//flip the x direction
bouncer_dx = -bouncer_dx;
}

if (bouncer_y < 0 || bouncer_y > 480 - 32) {

bouncer_dy = -bouncer_dy;

}

if (bouncer2_x < 0 || bouncer2_x > 640 - 32) {
bouncer2_dx = -bouncer2_dx;

}
if (bouncer2_y < 0 || bouncer2_y > 480 - 32) {
bouncer2_dy = -bouncer2_dy;
}

//really important code!
//move the box in a diagonal
bouncer_x += bouncer_dx;
bouncer_y += bouncer_dy;
bouncer2_x += bouncer2_dx;
bouncer2_y += bouncer2_dy;

//if an event happened, you better redraw
redraw = true;
}

//////////////////////////////////////////////////////////////////
//kill program if the user clicks the exit button
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}

//if you were supposed to redraw but there wasn't an event, don't redraw
if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
}
al_clear_to_color(al_map_rgb(0, 0, 0));

the code tells the object in this case "bouncer" to only bounce after he has hit one of the corners or walls. this basically describes the outer walls and edges of the walls in your "game" with words and out of words a program is created. like the code is describing to the program what to create.