Coding the Player
Learn (20 minutes)
-
The first thing we are going to do is create our Player object.
-
Right click on where it says “Objects” in the resources list and select “Create Object”.
-
Name the object “obj_player1”.
- Click on where it says “Variable Definitions”, this will bring up the variable definitions window. Go ahead and add the following variables.
-
ang will be the angle that we give our projectile when shooting it.
- pow will be the power that will be applied to our projectile.
- click_x_start and click_y_start is the point from our mouse that we use to get the angle and power of our projectile.
-
Click “Add Event” and add the Draw > Draw event. This event will be responsible for drawing our player as well as our aiming reticle.
-
Within the event drag in a Draw Self code block and drag it into the coding area.
-
Underneath the draw self code we need to drag in an “If Mouse Pressed” code block.
- Button: mb_left | Not: Unchecked
-
Then within the if statement we need to drag in a “Assign Variable Code” block. You can hit the plus sign to assign more than one variable at a time.
- Name: click_x_start | Value: mouse_x
- Name: click_y_start | Value: mouse_y
-
-
Underneath the If Mouse Pressed we need to add an “If Mouse Down” code block.
- Button: mb_left | Not: Unchecked
-
Within this if statement we need to add a “Draw Line” code block.
-
X: click_x_start | Y: click_y_start | X2: mouse_x | Y2: mouse_y
-
Next under the Draw Line code block add and “Assign Variable” code block.
- Name: ang | Value: point_direction(mouse_x, mouse_y, click_x_start, click_y_start)
- Name: ang | Value: point_distance(mouse_x, mouse_y, click_x_start, click_y_start)
-
To save time you can always copy and paste the value, the only difference being the point_direction and point_distance part.
- The function point_direction returns an angle that is calculated by the points provided.
- The function point_distance returns the distance between the two points provided.
-
Under the assign variable code block drag in two “Draw Value” code blocks.
- Caption: “Angle ” + string(ang) | Value: Leave Blank | X: mouse_x | Y: mouse_y-65
- Caption: “Power ” + string(pow/10) | Value: Leave Blank | X: mouse_x | Y: mouse_y-50
-
-
Lastly we just need to add two code blocks. Under the if mouse down code block we add the “If Mouse Released” code block.
- Button: mb_left | Not: Unchecked
-
Within that if statement we add an “Execute Code” code block.
- var inst = instance_create_depth(x,y, depth-1, obj_projectile)
- inst.direction = ang;
-
inst.speed = pow/30;
- The code we typed in is responsible for creating our projectile object. We then assign it’s direction to our ang variable and it’s speed to our pow variable.
- And that is it for our player object for now. However, you might notice that within our execute code block there is a yellow warning sign. And that is because we have not created obj_projectile yet. So let’s go ahead and do that next!
Coding the Projectile
Learn (10 minutes)
-
So let’s first start off by creating an object and naming it “obj_projectile”.
- Then let’s add a new variable definition.
- gravity will act as the force pushing down on the projectile.
-
Next go ahead and add the Draw>Draw event.
- Within this event we are going to be doing two things. The first is drawing our projectile and making it rotate through the air. The second is removing the object when it exceeds the length of the room.
-
So in the draw event let’s first add a “If Variable” code block.
- Variable: speed | Not: Checked | Is: Equal | Value: 0
-
Within the if statement we add a “Set Instance Variable” code block
- Variable: Image Rotation | Value: point_direction(x, y, x+hspeed, y+vspeed)
-
And then underneath the if statement we add a “Draw Sprite Transformed” code block.
-
Sprite: sprite_index | Frame: image_index | X: x | Y: y | Scale X: 1 | Scale Y: 1 | Rotation: image_angle | Colour: $FFFFFFFF
-
Next underneath the draw sprite transformed code block, we add an “If Variable” code block.
- Variable: x | Not: unchecked | Is: Less | Value: 0
-
Within the if statement we add a “Destroy Instance” code block.
-
Next we are going to do the exact same thing. So you can go ahead and copy the two previous code blocks and paste them under the if statement. The only difference being is that we are going to change what is in the new “If Variable” code block.
-
Variable: x | Not: unchecked | Is: Greater| Value: room_width
- So that is it for our projectile! However we still have one major issue and that is we won’t be able to see anything since our objects have no sprites.
Creating the Player/Projectile Sprite
Learn (<5 minutes)
-
The first thing we need to do is create two new sprite objects. Right click where it says “Sprites” in the resources bar and select “Create Sprite”.
-
Name the first sprite “spr_player1” and the second sprite “spr_projectile”. For both we need to set the origin to middle center.
- Try to keep the player’s sprite somewhere between 64x64 and 128x128 pixels in size. While the projectile should be 64x64 pixels in size.
Drawing the Player/Projectile Sprite
Practice (15 minutes)
- Have the students create their player and projectile object sprites.
- Have the students also adjust the collision masks for each of the sprites.
Testing the Game
Learn (<5 minutes)
- Open up room0 and place our player object somewhere in the room.
- Hit the run button or press F5 to run our game.
- So as you can see when we press and hold the left mouse button we draw a line that shows us our angle and power of the throw. When we release the left mouse button, we create a projectile object that gets thrown off screen. And with that lesson 1 is complete.