TEMPLE OF LOCKS V1.0 © 2 0 0 9 P A U L K N I C K E R BO C K E R F O R L A N E C O M M U N I T Y C O L L E G E In this game we will expand our look at Game Maker and deal with some of the complexities involved in making moving objects using gravity and physics and also the unique requirements for puzzle games. The game is very similar in function to the NES game Solomon’s Key. This tutorial assumes that the student has already learned the basics of Game Maker b using the Vacuum Marauders tutorials. This game will focus on: Setting and using gravity Setting and using friction Creating a “floor” Jumping 1.LOADING RESOURCES To save some time in later versions, we will load all the resources we need now. All the resources needed should be in the Temple of Locks downloadable resources pack. We will need the following sprites (ensure that all loaded sprites are centered using the button): sprite_player_right – a 40 X 40 player sprite facing right (Transparent :On) sprite_player_left – a 40 X 40 player sprite facing left (Transparent :On) sprite_player_dead – a 40 X 40 player sprite of the player dying (Transparent :On) sprite_brick – a 40 X 40 brick sprite with cracked version (Transparent :Off) sprite_brick_cracked – a 40 X 40 cracked brick sprite (Transparent :Off) sprite_wall - a 40 X 40 brick sprite different from the first (Transparent :Off) sprite_exit_closed – a 40 X 40 closed exit door (Transparent :Off) sprite_exit_open – a 40 X 40 open exit door (Transparent :Off) sprite_lock – a 40 X 40 exit lock (Transparent :On) sprite_enemy – a 40 X 40 enemy (Transparent : On) sprite_congrats – a congratulations image (Transparent : Off) You will need to load the following sounds: sound_music – background music sound_block – sound of block being placed sound_lock – sound of the exit being opened sound_exit – sound of leaving the room sound_death – sound of player dying Add in a font: font_timer – a font of your own choice to use later Finally, add the backgrounds: background_small – a 640 X 480 background background_large – a 4000 X 480 background The final result should look like: 2.MAKING THE ROOM Our game will use multiple rooms that will act as levels, but for now we will just make a simple room that allows us to test our player object’s movement. Create a new room named “room_level1”, make its dimensions 640 X 480, assign it the small background and make both SnapX and SnapY = 20: Now we will make the wall object which will provide the basis of the “floor” on which our player will land. Make a new object “object_wall”, assign it the wall sprite and make sure the Solid box is checked: … and we are done with the wall object. The wall object is somewhat special in the fact that it contains no events or actions, but instead is used only in the way things interact with it. The Solid box is very important because it allows us to determine which things are hard and can be stood on (like the wall object) and which we fall into (like an enemy or trap door). Now that we have our wall we can go back into our first room and place the wall objects to make a floor for the player to stand on and some obstacles for it to jump over. You can place large sections of wall by holding down the <Shift> key while clicking and drag a whole line of wall objects. The end result could be a lot like: The last thing we will do to get the room set up right is add a controller object named “object_controller”. Make an event for Keypress <ESC> and have it activate the End Game action ( ). Also add a Room Start event (in the tab) and put in a Play Sound action ( ) that starts the background music. Finally, place the controller in the room so that you get the icon is present. 3. MAKING THE PLAYER OBEY PHYSICS By default, Game Maker treats all its objects like they are in space. Game Maker provides actions to make the player obey the basics of physics on earth, but getting them to work right is a bit tricky. Make a new object, “object_player” and assign it “sprite_player_right”. Put in a Create event that has a Set Gravity ( Gravity = 0.5. , in the move tab) with Direction = 270 and While setting the Gravity to 0.5 (half normal earth value) is pretty intuitive, the direction value may not be. The value of Direction is the direction, in degrees, that gravity should point - by changing this value you can have gravity point to the top of the screen if you wanted. What may be confusing is how the direction system works. Many people are familiar with determining directional degrees based on a clock-like setup (like when doing map reading) where 0o is at the 12 o’clock and the degrees increase in a clockwise direction, as below: From: http://facweb.bhc.edu/academics/science/harwoodr/Geog101/study/images/Circle.gif Game Maker uses a different system where 0o is the right side of the object and degrees advance counter-clockwise around the circle, like below: From: http://www.mathsisfun.com/geometry/images/degrees-360.gif So when we set Direction = 270 we are actually setting gravity so that it drags the player down to the bottom of the screen. Now place the player somewhere in “room_level1” so that they will fall down onto top of the bricks: If you were to test the game now you would find that the user falls down and through the blocks below and off the screen. Remember: Game Maker make no assumptions about what you want the game to do. If we want the player to stop on top of the blocks, we are going to have to tell it to stop when it hits them. The way in which we make the player appear to “stand” on the wall blocks is by manually stopping the player’s downward movement when it hits a block an turn off gravity. There are 2 types of collisions we can have with blocks, those where the player lands on top of the block, and those where the player hits it from the side or from below. For the purposes of getting the player to stand on the block we are only concerned (for now) with when the player lands on top of the block so the first thing we do is add a Collision with “object_wall” and check if the player is above the object with a Check Object ( , in control tab) conditional with Object = “object_wall”, X = 0, Y = 30 and Relative turned on. This will check to see if there is a “object_wall” directly beneath the player (it being 20 pixels to the bottom of the player object, (0,30) relative is 10 pixels below the player). Inside the conditional blocks, we first turn off the gravity with a Set Gravity ( ) action with Gravity = 0 and Direction = 270. Then do a Speed Vertical ( , in the move tab) action with Vert. Speed = 0 (with relative off), this will stop the downward motion without destroying any sideways motion. Following that, put in a Set Friction ( , in move tab) with Friction = 0.5, this will make any left and right movement subject to the effect of friction by applying loss of momentum to movement. Finally add in a Move to Contact ( , in move tab) with Direction = 270, Maximum = 12, and Against = Solid Objects; because collision detection in Game Maker is imprecise, the event may fire when the player is a few pixels above the object giving the impression of floating – the Move to Contact action fixes this bug by adjusting the position of the object (in any chosen direction) so that it is in direct contact with another object. You should now have: You should now be able to have the game run and have a player standing on top of the wall objects. Now we must also deal with the situation where the player collides with a wall that is not under their feet. In that case we don’t want to turn off gravity or set friction but we do want to stop horizontal movement. For this we will use the Else ( , in the control tab) conditional; Else always comes after the end of the block of another conditional and is executed if the first conditional was not true (e.g. If A is true, do Block 1 - else if A is not true, do Block 2). Else is very useful in situations that have only 2 possible states and each one needs to be handled differently. Else has no parameters (as it depends on the value of the previous conditional) and just like any other conditional it needs blocking underneath. Inside the Else blocking put a Speed Horizontal ( , in move tab) with Hor. Speed = 0. This will eliminate all horizontal speed while still keeping vertical motion (like falling) intact. Final result should be: 4.MOVING THE PLAYER Our player can stand, but not really do much else, so now we will add in some motion. Add a Keyboard <right> event into “object_player” that contains a Test Variable ( ) conditional for Variable = “hspeed”, Value = 6 and Operation = smaller than. “hspeed” is a built in variable for objects in Game Maker like “x” and “y”, it holds the current horizontal speed of the object (positive being right, negative being left); this conditional checks to see if the “hspeed” is less than 6 (the fastest we want the player to go). Inside the conditional put a Speed Horizontal ( , in move tab) with Hor. Speed = 1 and Relative turned on, this will gradually increase speed up to 6 while the <right> key is held down: Repeat the procedure for the Keyboard <left>, but this time make Test Variable ( ) with Variable = “hspeed”, Value = -6 and Operation = larger than, then Speed Horizontal ( with Hor. Speed = -1 and Relative turned on: ) If you test your game now you will have a player that moves back and forth and experiences the drag of friction. Next we need to make the player jump using <space>, so add a Keyboard <Space> event to “object_player”. We only want to perform the jump when the player is standing on a wall (and not in mid-air, for example) so the first thing we will do is a Check Object( ) conditional with Object = “object_wall”, X = 0, Y = 30 and Relative turned on (this is the same conditional we used in setting up the player’s collision with the way so you can just copy it over). Inside the conditional we will remove all friction with a Set Friction ( ) where Friction = 0, this prevents friction from being applied in jumping through the air. Then we turn the gravity back on with a Set Gravity ( ) where Direction = 270 and Gravity = 0.5. Finally we do the actual jumping with a Speed Vertical ( ) action set to Vert. Speed = -7 and Relative off (a negative vertical speed flings the player into the air): If we test our game now we will see a player that can jump and move around the room. OH NO!!! – You will quickly discover a problem with the movement while jumping around the room: if the player walks off the edge of an elevated platform, they will hover in space! This is because we are handling the turning on and off of gravity, and while we turn it on during collision with the wall, we never turn it back on if that collision is no longer happening. The easiest fix to this problem is to continuously check to see if there is still solid ground under our feet. Make a Step event inside of “object_player”, inside this event put a Check Empty ( , in the control tab) conditional with X = 0, Y = 25, Objects = Only Solid and Relative turned on. This is a conditional that will check to see if there is an object with Solid checked in its properties at a specific location (in this case 25 pixels below the object). Inside the conditional’s blocks add a Set Gravity ( add Set Friction ( ) where Friction = 0: with Direction = 270 and Gravity = 0.5. Then This should start the player falling whenever it no longer has a wall beneath it. You will find this movement system still has a lot of little glitches but should be good enough to use while we explore other aspects of game maker in the next versions of the game. 5.MAKING AN EXIT In the first version of our game we will make a very simplistic exit. Create a new object, “object_exit”, and assign it the open exit sprite. Now add in a Collision with “object_player” and have it Destroy Instance ( ) on Other (destroy the player to stop movement), then Play Sound ( ) for “sound_exit”, followed by a Sleep ( , in main2 tab) for 1000 milliseconds (this pauses all action in the game so the sound can finish playing before moving rooms). Finally put in a Next Room action ( the player hits the exit. You should have: ) so that the player moves to the next when Place the exit somewhere in the room that the player can get to it: Now before we test the game we have to avoid a very nasty error by making a second room to go to. Inside this room we will have a single object that handles the displaying of the congratulation sprite and restarts the game. Make a new object called “object_congrats” and give it “sprite_congrats”. In its Create event have it Sleep ( the player to savor the victory), then Display Highscore ( ) for 2000 Milliseconds (allow ) and Restart Game ( ): Now create a new room “room_final”, put “object_congrats” in the middle of it and make sure the room is last in the list of rooms: Now the exit should kick the player to the final room for a victory lap. FINISHING UP Now we should have the following features implemented: Working gravity and friction A jumping player An exit portal that restarts the room
© Copyright 2026 Paperzz