GameMaker Studio 2: Complete Platformer Tutorial (Part 1: Basics)

3 min read 4 hours ago
Published on Feb 13, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to guide beginners through the basics of creating a platformer game using GameMaker Studio 2. It will cover essential elements such as player movement, gravity, and collision detection, all in under 40 lines of code. By following these steps, you'll establish a strong foundation for further game development in this platformer series.

Step 1: Setting Up Your Project

  • Create a New Project

    • Open GameMaker Studio 2.
    • Select "New Project" and choose the "Game" option.
    • Name your project and set the location where you want to save it.
  • Set Up Your Room

    • Right-click on the "Rooms" folder in the Resource Tree.
    • Select "Create Room" and name it "Room1".
    • Adjust the size of the room to your preference (e.g., 800x600).
  • Create Your Player Object

    • Right-click on the "Objects" folder and select "Create Object".
    • Name it "obj_player".
    • Add a sprite for your player by clicking on the sprite option and selecting an existing sprite or creating a new one.

Step 2: Implementing Player Movement

  • Add Movement Code

    • Open "obj_player" and navigate to the "Create Event".
    • Add the following code to set initial variables:
      hspeed = 0; // Horizontal speed
      vspeed = 0; // Vertical speed
      gravity = 0.5; // Gravity value
      jump_speed = -10; // Jump speed
      
  • Control the Player

    • Add a "Step Event" to "obj_player".
    • Insert the following code to handle movement:
      // Horizontal movement
      if (keyboard_check(vk_right)) {
          hspeed = 4; // Move right
      } else if (keyboard_check(vk_left)) {
          hspeed = -4; // Move left
      } else {
          hspeed = 0; // No movement
      }
      
      // Jumping
      if (keyboard_check_pressed(vk_space) && place_meeting(x, y + 1, obj_ground)) {
          vspeed = jump_speed; // Apply jump speed
      }
      
      // Apply gravity
      vspeed += gravity; 
      

Step 3: Adding Collision Detection

  • Create a Ground Object

    • Right-click on the "Objects" folder and select "Create Object".
    • Name it "obj_ground" and add a sprite for the ground.
  • Handle Collisions

    • In the "Step Event" of "obj_player", ensure proper collision handling by adding:
      // Check for collision with ground
      if (place_meeting(x, y + vspeed, obj_ground)) {
          while (!place_meeting(x, y + vspeed, obj_ground)) {
              y += vspeed; // Move player down
          }
          vspeed = 0; // Reset vertical speed
      } else {
          y += vspeed; // Move player normally
      }
      

Conclusion

In this first part of the complete platformer tutorial series, you have set up a basic GameMaker project, implemented player movement and gravity, and added collision detection. These foundational steps are crucial for building more complex game mechanics in future installments.

Next, consider exploring additional features like animations, scoring systems, or enemy behavior to enhance your platformer. Stay tuned for the next parts of the series, which will introduce more advanced concepts and techniques.