FR

Réalités Parallèles

Ergonomie et conception de jeu vidéo

Gamemaker Studio Horizontal Shooter - Basics - Prevent the player from leaving the screen

Now, if you test the game, you’ll notice you can easily move around outside the visible part of the screen. For this project, we want players to stay inside the visible area by preventing them from moving beyond the edges.

Note: I renamed the player object to s_player_horizontal since I’m heading towards making a horizontal shooter game in this tutorial, but it’s the same one from the previous tutorial.

In your player object, add a new event called “Step > Step”. The Step event triggers at every frame, which means the game keeps doing whatever actions are in step all the time.

Screenshot

Inside the step event, well add an Execute code block from the control tab, like we did before.

Screenshot

This time we’ll use the following two lines of code.

x = max (x, 0);

x = min (x, room_width);

x = max (x, 0);

That means this line of code will update the player’s position to either itself or the left edge of the room, whichever max() selects as the largest. If the player presses left to move left, and that x would go below 0, then this function tells the game to keep putting the player back inside the screen at its left edge.

x = min (x, room_width);

This is very similar to the first line.

When the player presses right, the player object’s x position will increase until 1024. If it goes above 1024, this second line of code will place it back at the edge of the screen.

Practice

Define the vertical position y so that the player object stays is similarly blocked at the top and bottom edges of the screen.

Tips:

Example solution

Screenshot

Posted by on 2021-04-03. Last updated on 2024-06-07

Articles on similar topics

Game Maker Studio tutorials

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Creating sprites

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Creating a room

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Creating a player character

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Scrolling backgrounds

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Game Maker Studio tutorials

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,