Tutorial 14: Creating a Light Effect in Game Maker


You could demonstrate this effect in a separate room instead of your main playing room. If so, create a button to it on the start screen.

  1. Create a new object called objLight
  2. Add a Create event to this object with the following code:
    /// @description Create a surface the size of the current room
    
    surf = surface_create(room_width, room_height);
    surface_set_target(surf);
    draw_clear_alpha(c_black, 0);
    surface_reset_target();
  3. Add a Step Step event to this light object and paste this code.
    Look out for where the player object and player lazer is referenced in the code below.

    if (surface_exists(surf)) {
    surface_set_target(surf);
    // The following three lines set the 'dark' overlay
    
    draw_set_color(c_black);
    draw_set_alpha(0.8);
    draw_rectangle(0, 0, room_width, room_height, 0);
    gpu_set_blendmode(bm_subtract);
    draw_set_color(c_white);
    
    // Draw circles in the overlay with your different light sources
    // Note that I add randomization to the position and radius to mimic a flicker effect
    //each with statement needs to know what object you are creating the light effect around
    
    with (objPlayer) //light on the player
    draw_circle(x + random_range(-1, 1), y + random_range(-1, 1), 50 + random_range(-1, 1), false);
    
    with (objEnemy1)//light on the enemy
    draw_circle(x + random_range(-1, 1), y + random_range(-1, 1), 50 + random_range(-1, 1), false);
    
    with (objPlayerLazer) //if your player lazer has a different name change this
    //you will need to play with the values inside of here to show your lazer
    draw_rectangle(x-20,y-40,x+20,y+40,false);
    // Reset all of your draw stuff
    gpu_set_blendmode(bm_normal);
    draw_set_alpha(1);
    surface_reset_target();
    
    } else {
    
    surf = surface_create(room_width, room_height);
    surface_set_target(surf);
    draw_clear_alpha(c_black, 0);
    surface_reset_target();
    }
  4. Create a Draw End event on the light object, and copy and paste this code into it.
    if (!surface_exists(surf)) {
    surf = surface_create(room_width, room_height);
    
    } else {
    
    if (view_current == 0) {
    draw_surface(surf, 0, 0);
    
    }
    }
  5. In a Room End event, paste this code:
    // Clean up the surface memory manually
    
    if (surface_exists(surf))
    
    surface_free(surf);
  6. Drag the light object into a room to test it.
  7. If you do not want it to be part of your main game room, create a new room for it, and link to it as a bonus room for the start screen

Challenge:

Add With() conditions to the Step Step event if you want to see your enemy lazers

Similar Posts

One Comment

  1. For the step event, you can replace the “black” for white instead. Personally I find the colour white more suiting for my game due to my background colour being bright.

Leave a Reply

Your email address will not be published. Required fields are marked *