Let's go ahead and take a look at a solution for the homework. So for the variables that hold the game when the game height sprite with Sprite hired I went ahead and created class properties for them. And on top of that instead of creating functions to handle the assignment of the values I went ahead and just did it on a single line of code. This vastly improves readability and maintain ability versus the solution in the last homework where everything was assigned through a function and also created variables to help with the quality of life.
A little we'll look into that later. The most important thing is the creation of three variables. One for the current speed or game object has an acceleration value and a max speed on top of that. Notice the export keyword. So that way we can edit the code easily through the good dough editor when we want to test different values and how it interacts or rather how it changes the game objects behavior. I also went ahead and created the four functions move right left up down and of course we're compounding to either the x position or the Y position either adding or subtracting based on the direction we would like our game object to head towards 2.
And lastly I do have this functionality called Reset for. And basically it's here because I want to show you how acceleration is acting upon our game object and it's hard to see that when our game object disappears from the screen. Now of course the entry virtual method has the position top center because we would like the game object of all in this case on top of that we use the move down function inside the physics process virtual method which takes in a Delta float and returns back a void and that delta value we pass down to our moved down function.
Now in these two lines of code we have an if statement. Basically if our current speed is less than our max speed we're going to add acceleration. So in this case our current speed starts at the literal float value one hundred. So our object starts with an initial velocity of one hundred pixels per second. We have an acceleration which we've assigned the value three hundred and b max speed variable has a float value of one thousand five hundred. In this case we want to make sure that our current speed does not exceed fifteen hundred.
And that's exactly what these two lines of code do. It makes sure that as long as our current speed is less then our max speed we're going gonna go ahead and calculate for acceleration. Basically we're gonna compound our current speed which then compounds on to our position and as you can see here the function at acceleration which takes in a flow value and doesn't return back anything basically multiplies our acceleration value by Delta and then we add that to our current speed on top of that I also have this basic if statement that will allow our game object to perpetually fall for basically infinity.
I went ahead and commented that out but I'm gonna go ahead and delete that so we can see what happens. And as you can see here are objects started out at an initial velocity of one hundred and over time it starts to speed up until it's reached a speed of about fifteen hundred pixels per second. And that's basically it. So I went ahead and clicked remote hero and if we take a look at the inspector we can see that our current speed is fifteen hundred or max speed is fifteen hundred accelerations three hundred. And so our current speed which started at one hundred is now fifteen hundred.
Now that we took a look at how our game object behaves. Let's take a shallow look at the code inside the physics process for trial method. So as you can see here in this line the line twenty three position that y greater than game height plus sprite height divided by two. Basically every time we run this if statement we're gonna go ahead and calculate this arithmetic operation. Now there are times where we would like to do this. In fact however this is not one of those times.
As you can see here game height plus sprite height divided by two will always return back to you. The same value game height in a sense because the screen never changes will return back case a constant value. The sprite height because our sprite image never changes its pixel height will always return back to you. A specific value and the literal integer 2 is again a constant value and it will never change. And so even though it's okay to write code like this when starting out sometimes we want to do small optimizations and let me show you how we could do that.
So instead of writing that line of code inside the if statement we can for example go ahead and write a new property variable. In this case have sprite height which is sprite height divided by 2. And this line of code basically runs one time only during the existence of our hero G.D. script file and from the half sprite height because we may want to use that in other parts of our code. I went ahead and created another variable called lower limit and in this case that would be our game height plus our half sprite height and instead of running 60 times per second we will get the value one time and so we can go ahead and actually remove this block of code replace it with lower limit.
And because of this two things happened. 1 we're not doing arithmetic operations 60 times a second. And on top of that the readability of our code has vastly improved. Instead of trying to figure out what's happening through the name of our variable we do know that if position that Y is greater than the lower limit then we're going to reset our full. On top of that you can go a little deeper perhaps lower limit is not a descriptive variable name that may cause confusion for you so you can go ahead and feel free to edit and improve upon the variable name.
Now the half sprite height as you'll notice in our code down below you can see sprite height divided by two and every time we call reset for we're gonna do this arithmetic operation which will always return back the same value. And in this case because we call reset ball perhaps once every few seconds we may want to avoid that and so we can go ahead and replace it. And this will give us basically the same result as you can see here. The code hasn't changed at all. It's still stayed the same.
And on top of that we can also go ahead and replace anything that the sprite height divided by two. And we have a few places in our position functions. For example we do have that here as well in the position bottom center as a matter of fact you can do the same thing for the other values as well for example. Notice how our game with divided by two is called twice and came height divided by two is called three times. So basically what's happening here is code duplication as you can see here game height divided by two is code three times.
Game with divided by two is called twice and when you see code duplication like this it's generally good practice to go ahead and create a variable on the class scope. In that case it just be a class property and you can create a variable that holds that value and then you can use it among different functions. And by doing that one you can consolidate. In this case arithmetic operations down inside a variable and two code readability improves because in this line of code game height divided by two.
I still have to think in my head. Well we have a game hide and we're gonna essentially divide that by two. However when I see this line of code here have sprite height I can already think to myself Well we're adding half of the sprite height to whatever it is we're adding it to. Well that's basically it for this episode. Thank you so much for joining me. Thank you for clicking the Like button and thank you for subscribing. If you have any questions or comments please feel free to leave them in the comments. Down below I look forward to seeing you in the next episode.
Have an amazing day.