I'm Coding a Game - part 8

I love when everything works the way it is supposed to! In this installment I added a camera shake effect and moving backgrounds. GameMaker Studio 2 is such a powerful programming language. I feel like I’m barley scratching the surface with it. Getting the camera to shake was relatively easy, and the backgrounds didn’t take much as well. Let’s get started, shall we?
 
Camera Shake:
 
To get the camera to shake I needed to write code in the oCamera object. On the create tab of the object, I added these variables:
The variables created were, shake_lengthshake_magnitudeshake_remain, and buff. So shake_length will be the duration of the shake, or how long it lasts. Shake_magnitude will be how intense it is. The shake_remain variable is used to reduce the magnitude, so that the shake dissipates. Buff stands for buffer. I created this variable to make sure when the camera shakes, it does not shake off the board so the player can see black. It would be like seeing a mic drop onto the screen in a movie. I’m trying to avoid that. Next I wrote some code in the step tab:
 
The first two lines are to get a random number for the X and Y coordinates for the variable, shake_remain. The third line is what diminishes the camera shake, so that it doesn’t go on forever. This line states:
 
shake_remain = max(0, shake_remain-((1/shake_length)*shake_magnitude));
 
So this formula states to take 1/shake_length and times it by the shake_magnitude, and set that value to shake_remain. This will keep returning a smaller number to shake_remain. So every frame the shake is happening, it’s becoming less, and less…etc. Next was to write a script to create a command that I could call upon when I needed a screen shake to happen. I named that script, ScreenShake:

Scripts if you recall, can be thought of as personalized commands. Now when I call ScreenShake anywhere else in the code, it will run this. The final step was to place this script in the gun code so when a player fires, the screen will shake. I opened up the gun object and placed this line of code.

All I had to do was place the script ScreenShake and set the magnitude and length in the gun fire sequence and BOOM, I got camera shake when I fire the gun! Pretty cool, huh? I thought it was pretty cool.

Chester is a man on fire!
 
Backgrounds:
 
Backgrounds are often overlooked when it comes to coding and in game programming. At least it does for me, I tend not to think about it. After doing this tutorial, I started to realize how much depth it adds to a game! The first step was to create or design two separate backgrounds. The first is closest to the character, the second is further away.
 
I named the one closest to me as. “Foreground.”
 
 
and
This one I named, “Background.”
 
 
If you noticed a building is cut in half on both sides. That’s because I’m going to be tiling these backgrounds. Those two sides match up perfectly so it will look seamless.
 
Next, I loaded them in as sprites.

Then I created a new background, which I titled, “Foreground.” Then I loaded and placed the foreground picture on the layer.

Then, I clicked the horizontal tile button and…

Next I did the same process with the other background.

Now, I could of left it like this and had these buildings be a frozen background. There’s nothing wrong with that, but I wanted to get a bit of depth to it. The further an object is, the less it appears to move compared to something close in distance. So I opened up the camera object and wrote this code:
 
 
 
This states that if “Background2 exists move it and half the speed as the camera is moving. The Foreground moves at 1/4 of the speed as the camera. When I run the game we have something that looks like this:
Notice how the ground closest to the viewer moves the fastest?
 
That should do it for this installment. I covered a lot of ground and my brain is starting to feel like mashed potatoes. Until next time!
 
Take it easy, but take it.
 
NickeNDime Out!