GameMaker Language Function Examples
Introduction
This document provides example functions for a 2D game implemented in GameMaker
Language (GML), focusing on player movement, shooting mechanics, and enemy behavior
as derived from the provided code snippet.
1 Player Movement Functions
1.1 Player Rotation
Rotates the player sprite to face the mouse cursor.
1 image_angle = point_direction (x , y , mouse_x , mouse_y ) ;
1.2 Player Movement with Collision
Moves the player in four directions (W, A, S, D) with collision detection against walls.
1 player_speed = 5;
2 // Move Up
3 if ( keyboard_check ( ord (" W ") ) ) {
4 if (! place_meeting (x , y - player_speed , wall_obj ) ) {
5 y -= player_speed ;
6 }
7 }
8 // Move Down
9 if ( keyboard_check ( ord (" S ") ) ) {
10 if (! place_meeting (x , y + player_speed , wall_obj ) ) {
11 y += player_speed ;
12 }
13 }
14 // Move Left
15 if ( keyboard_check ( ord (" A ") ) ) {
16 if (! place_meeting ( x - player_speed , y , wall_obj ) ) {
17 x -= player_speed ;
18 }
19 }
20 // Move Right
21 if ( keyboard_check ( ord (" D ") ) ) {
1
22 if (! place_meeting ( x + player_speed , y , wall_obj ) ) {
23 x += player_speed ;
24 }
25 }
2 Player Shooting Functions
2.1 Player Shooting
Creates a bullet when the player shoots, with ammo and cooldown management.
1 can_shoot = true ;
2 ammo = 20;
3 if ( can_shoot && ammo > 0) {
4 i ns t a nc e_ crea t e_ d e p t h (x , y , depth + 1 , playe r_bull et_obj ) ;
5 can_shoot = false ;
6 alarm [0] = 10;
7 ammo -= 1;
8 if ( ammo == 0) {
9 show_message (" Press SPACE to reload ammo ") ;
10 }
11 }
2.2 Drawing Player Stats
Displays player hitpoints and ammo on the screen.
1 draw_self () ;
2 draw_set_font ( Font1 ) ;
3 draw_text (20 , 50 , hitpoints ) ;
4 draw_text (20 , 100 , ammo ) ;
2.3 Player Health Management
Reduces player hitpoints and ends the game when hitpoints reach zero.
1 hitpoints = 100;
2 hitpoints -= 2;
3 if ( hitpoints == 0) {
4 instance_destroy () ;
5 game_end () ;
6 }
3 Enemy Behavior Functions
3.1 Enemy Rotation
Rotates the enemy to face the player when within 250 pixels.
2
1 if ( di st ance_to_obje ct ( player_obj ) < 250) {
2 image_angle = point_direction (x , y , player_obj .x ,
player_obj . y ) ;
3 }
3.2 Enemy Shooting
Enables the enemy to shoot bullets at the player with a cooldown.
1 can_shoot = true ;
2 if ( di st ance_to_obje ct ( player_obj ) < 250) {
3 if ( can_shoot == true ) {
4 in st a nce_ cr e a t e _ d e p t h (x , y , depth , obj_enemy_bullet ) ;
5 can_shoot = false ;
6 alarm [1] = 30;
7 }
8 }
9 alarm [1] = 10;
3.3 Enemy Health Management
Manages enemy hitpoints and destroys the enemy when hitpoints reach zero.
1 hitpoint = 100;
2 hitpoint -= 25;
3 if ( hitpoint == 0) {
4 instance_destroy () ;
5 }