The first task in my production schedule is to code the player movement and shooting. In my game there will be a basic run and a sprint.
| Basic movement code |
I set up 'xDirection' and 'yDirection' variables. The value assigned to these variables is dependant on the user input into the equation. The 'keyboard_check' function returns 1 if the user is holding the key, and 0 if they are not. This equation will return either 1, 0 or -1, which is then added to the x or y value, and multiplied by the 'playerWalkingSpeed' variable.
If the player is holding D, but not holding A, then the equation would look like this:
xDirection = 1 - 0;
This means that the 'xDirection' will be 1, so then the outcome of this input is:
x = x + (1 * playerWalkingSpeed);
This will make the player move right across the x axis at the speed set in the 'Create' event. If the player wanted to move left, then they would hold the A key:
xDirection = 0 - 1;
This would return -1, so the player would move left along the x axis. The same equation is used for the y axis to move the player up and down.
The first if statement is checking that the shift key is not being held ('!' before a function means 'not'), which means the player will walk, whereas the second if statement is checking that the shift key is being held down, meaning that the player wants to sprint, thus the movement speed multiplier is changed to the 'playerSprintSpeed' variable.
The bottom line of code is simply changing the 'image_angle' so that the character faces the mouse, the way they will be shooting.
This is the movement that this code returns.
![]() |
| My basic game movement... How fun! |
Shooting
The player shooting code is applied to 'objGun'.
| Shooting code |
In the gun's 'Create' event, I set the variable 'firing=false'. The code checks that the player is holding the left mouse button, and that they're not currently firing, and then runs the code, firstly setting 'firing=true' so that the gun won't fire any more bullets until we say so. The next line is setting the alarm, so that the gun only fires twice per second. Then, we create a bullet and assign it the 'myBullet' variable, and proceed to give the bullet a speed, direction and angle. When the alarm triggers, 'firing' is changed back to false, so that we can shoot another bullet.
![]() |
| Pow, pow, pow! |


No comments:
Post a Comment