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.
Inside the step event, well add an Execute code block from the control tab, like we did before.
This time we’ll use the following two lines of code.
x = max (x, 0);
x = min (x, room_width);
x = max (x, 0);
x
is once again the variable that stores the horizontal position of your object, in this case it’s the player object.=
means we’re assigning a new value to this variable-
max()
is a function that will select the highest value among the ones inserted as parameters. A parameter is a piece of information that you give a function to tell it what it’s supposed to do. They’re different for each function. max() takes as many parameters as you want, as long as they’re numbers. It will look at the value of each parameter –and will choose the one with the highest value. x
inside themax()
function is the current position of the player object.0
is the left most position of the edge of the room.
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.
min()
does the opposite of max: this function will select the smallest value out of those given as parameters.room_width
gives the position of the far edge of the room. In my prototype, the room is 1024 pixels wide.
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:
- Game maker stores the position of the player object with its x coordinate for the horizontal position, and y coordinate for its vertical position.
- room_width indicates how wide the room is. How do you think the height of the room is called?
Example solution
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,