Game 4: Breakout

Breakout Instructions

Bounce Code

1
///Bounce against walls
2
3
//Back up ball before collision
4
x=xprevious;
5
y=yprevious;
6
var corner = true;
7
8
//Bounce for horizontal collision
9
if(place_meeting(x + hspeed, y, other))
10
{
11
hspeed = -hspeed;
12
corner = false;
13
}
14
15
//Bounce for vertical collision
16
if(place_meeting(x, y + vspeed, other))
17
{
18
vspeed = -vspeed;
19
corner = false;
20
}
21
22
//deal with corner case
23
if (corner == true)
24
{
25
hspeed = -hspeed;
26
vspeed = -vspeed;
27
}
28
Copied!

Pause Code - in obj_pause (create event)

1
// Create surface, draw everything to it, deactivate all other objects
2
3
surf = surface_create(room_width, room_height);
4
// creates a surface which is the same height and width as the room.
5
// A surface is basically a clear drawing surface on the screen.
6
7
surface_set_target(surf);
8
// Sets aside room in memory for surface and begins drawing on it
9
10
draw_clear_alpha (c_red, .5);
11
// Clears surface of buffer noise and sets color and alpha
12
// (0= fully transparent, 1 = fully opaque, .5=50% transparent)
13
14
surface_reset_target();
15
// Tells GM to stop drawing on the surface and
16
// allows drawing directly on the screen again (and thus the draw GUI below)
17
18
instance_deactivate_object (obj_ball);
19
// Deactivates the ball without destroying it.
20
21
visible = true;
22
//This object is visible.
23
//Adding the command is not strictly necessary since we already set the object to visible.
24
Copied!

Pause Code - In obj_pause (Draw GUI event)

1
draw_set_font(fnt_pause);
2
// GML commands, also called functions, are set up in categories.
3
// Draw_ gives you access to draw functions.
4
// Instance_ functions give you access to things dealing with instances of objects.
5
// Draw_set_ commands establish parameters for your draw actions.
6
// This one changes your draw font to your pause font (set it up, if you haven’t).
7
8
draw_set_color(c_black);
9
// This one sets the color.
10
// typing c_ will give you a list of different color options.
11
12
draw_set_halign(fa_center);
13
// Sets the horizontal alignment of the font to center.
14
15
draw_surface(surf, 0, 0);
16
// This function draws the surface over the room.
17
// The surface that it draws is called surf (which is what we called it in the create event).
18
// It draws it starting at 0x and 0y in the room.
19
20
draw_text(room_width/2, room_height/2, "Game Paused: Press Space to Continue");
21
// This draws text. It looks for (x,y,string) which means the programmer
22
// decides where the it goes. room_width / 2 is right in the center of the room.
23
// A string is a series of text in “”. The text is in black and centered because of the above
24
// draw_set_ functions.
25
26
draw_set_halign(fa_left);
27
// Since the text of the scoreboard is aligned to the left, we need to reset the alignment.
28
Copied!

Pause Code - In obj_pause (Key Press - Space)

1
/// Unpause, clear surface, re-activate all objects.
2
3
surface_free(surf);
4
// Free up the memory from the surface.
5
// Essentially this deletes the surface.
6
// By the way, we are starting code now because there are no commands in
7
// drag and drop programming that deal with surfaces.
8
9
instance_activate_all();
10
// Activate all the objects again
11
// We could have also used instance_activate_object (obj_ball);
12
// since that is the only thing we deactivated in the create event.
13
14
instance_destroy(self);
15
// Destroy the pause object.
16
17
// As you look at this code, remember that instance_ functions are used
18
// to control instances of objects in the room. As we get further along, we
19
// will use more code blocks, so it is important to be aware of the general
20
// language rules because you won’t always know what the exact command
21
// will be for an operation.
22
23
// In the code block autocomplete will help you find the right one. If you know
24
// you are dealing with an instance, you can start typing “instance_” and will be
25
// given a list of possible things which could finish that command.
26
Copied!