By the way I have uploaded this project file to get hub so please feel free to download it from the description section down below.
Let's take a look at the homework. It's fairly simple when you're a game object reaches one side of the screen it should wrap around to the opposite side and it can do it for either direction whether you're going right and up or down and left. Now there is one thing I added that wasn't included in the homework and because I brought up the example of asteroid I decided to mimic the movement behavior where if you type just ever so slightly you continually move in one direction and if you hold on the button you increase by adding acceleration to your speed.
Let's go ahead and take a look at how I did that. So from the other homework assignment where we worked on acceleration for a game object that went ahead and did that. Notice how we have an acceleration variable a maximum speed variable on top of that instead of one speed variable I created two speed variables one for the x position for our game object and the other for the y position of our game object. This is because if we're moving at a constant speed to the right but we're not having a speed going up or down I would like our game object to move exactly to the right until a player input in the opposite direction happens.
And as a matter of fact the only way to affect our game object is if our player inputs something in this case the arrow keys on top of that I created limits to make sure that we have this wrap around effect. And notice that I didn't change much from the last homework assignment. Instead you can see that I've actually commented out code from our last homework assignment to fit the requirements for this homework assignment. In this case are lower limit is just our game height upper limit is zero for our left limit on the x axis we have zero and for the right limit it is our game width.
Nothing's changed in the enter tree position middle is the method I use to position our game object and physics process hasn't changed much either as a matter of fact. All we did was add two extra functions one function to move our hero the second function to check our wrap around. Now when we check our wrap around we're looking for certain things. We're making sure that our game objects y position is not passed the upper limit or pass the lower limit for the lower limit.
We want to basically teleport ourselves only if our game object is basically larger than our game height and then we want to teleport to the opposite side of the screen when our y position is basically a negative number and the same thing for our x position for a game object. When our game object is larger than our game what we want to assign it the zero value. And when our game object is less than zero. So basically a negative number we want to teleport to the opposite side which would just be our game.
Now I did change the functions for moving and the reason for that is because when we move we don't really want to change drastically the direction our game object is going instead. We want to affect our acceleration. So in this case when we move right or left I want to change the acceleration on the x axis. Basically I would just want to change the speed going in the x axis and the same thing for up and down. I want to change the speed on the game object's position for the y axis.
Notice that when I want to go right I add a positive delta value and when I want to go left I pass a negative delta value and the same thing for moving up and down if I want to move up I pass a negative delta value because a negative number to the game objects position means we're heading up because the y axis is inverted for Godot. And of course if I want to move down I just add a positive number to our speed value. In this case a positive delta value for our acceleration on y method.
Now I have three functions that are important when we want to move our game object. In this case our acceleration on the X axis will affect the speed on our x axis and it's basically just our speed formula acceleration multiplied by Delta and then when we want to move our hero we just change the position of the x and y values with their corresponding speed. In this case the x position will be changed based on our x axis speed and our y position will be changed on our y axis speed.
Lastly if you would like to keep the speed from rising above a certain number you'll go ahead and add an if statement checks when this case I went ahead and added. If speed on x axis is less than our max speed then we can go ahead and continually increase arm speed on the x axis. I did the same thing for the y axis. If our speed on the y axis is less than Mach speed we go ahead and increase the speed on the y axis.
Now as you've probably noticed when we started on positioning ending on wraparound effects. Notice how our code started small basically simple solutions to simple problems and we worked our way up to harder problems.
When you are game programming that is exactly how you want to tackle in this case. Basic game concepts
You want to start tackling the easy things first for example the easiest thing in this class would be the positioning. And from there you want to incrementally work your way up to harder issues. The next issue that would be in my opinion the more complicated is the inputs and then working your way around to moving and checking for rep around. On top of that the homework was also structured to tackle different problems because in your game programming journey you're going to realize that some of your early game concepts may have evolved into something different in the previous homework.
We worked on a bounded game. However as you're working along you may decide that you no longer want a bounded game. You may want an unbounded game or a rep around the game and so your code is going to change based on the. In this case the game mechanics that you want and so it is important that you learn how to properly organize your code into functions. In my opinion having things inside of functions that you use inside your virtual method will help you when you want to change game mechanics and so that's why I advocate for beginners to start putting things in functions and to keep raw code outside of virtual methods or at least trying your best.
And of course that again is just a template as you called more you'll realize that sometimes having real code in your virtual methods is better than having them instead of functions. But that's something you will learn along your journey. So to recap sometimes our game ideas start off in one area. In this case a bounded game and as you keep programming along your game mechanics may change. And so again it is important to find ways to organize your code to make changes in game mechanics easier for yourself.
And that would include naming conventions. Well that's all I have for you in this episode. Thank you so much for joining me.
Thank you for subscribing and thank you for clicking the Like button. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have. An amazing day.