MAKING A GAME In 3 Easy Steps Using Love2D & Lua (3/3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
pong really isn't pong without an opponent so let's go ahead and create an ai that will try to defeat us at our own game by now you should be a bit familiar with the process of creating a new file we are going to create one for the ai called ai.lua inside of here we will create a table named ai that will contain all of the variables and functions that it will need we also need to create the three base functions load update and draw these three functions need to be called in main.lua and lastly we need to require ai.lua the ai is going to be very similar to the player we will need to keep track of the x and y positions it will have a width and height as well as a speed variable one thing to keep in mind is that your program is read and executed from the top and then down meaning that if you declare a variable at line 6 but use it at line 5 your program is going to crash this is because the variable has not yet been created when you try to use it i want to use the ai's width when determining the ai's x position due to this we need to make sure that we have declared the width variable before we try to use it in the x variable the ai is going to have the same dimensions as the player so we create a width variable and set it to be equal to 20 and a height variable that we set to be equal to 100 the player paddle is offset by 50 pixels from the left side of the window and we want the ai to mirror that in order for us to set the ai paddle to be 50 pixels offset from the right side of the window we need to set the x variable to be equal to the window width minus the ai's width and minus another 50. we can set the y variable to be equal to the window height divided by 2. the ai paddle has one similarity with the ball they are both not controlled by any input so we need to store the velocity that the ai has but for the ai paddle we only need to make a variable for the y velocity since the pedals can only move along the y-axis i will set it to be equal to 0 so that the ai does not move right off the bat next we need to give the ai a speed variable i will set it to be equal to 500 the same speed that the player paddle has let's go ahead and draw the ai inside of ai draw we are going to once again use the love.graphics.rectangle function we want the rectangle to be filled so we type fill as the first argument followed by the x and y variables finally we need to give it a width and height now we can see that the ai paddle exists and is drawn properly we want the ai paddle to change its y position depending on its current velocity to do this we will create a function and name it move inside of the move function we will set the y position to be equal to whatever the y position was plus the y velocity multiplied by delta time this move function needs to be called in the update function but the ai paddle won't move unless it can actually change the y velocity variable to make the ai able to do this we need to create a new function named acquire target this function will handle the decision making of the ai though it will be very basic if the ball is above the paddle it will set the y velocity to negative speed thus moving up if the ball is below the paddle it will set the y velocity to be equal to the speed variable making it move downwards and finally if the ball is neither above nor below it will set the y velocity to be equal to zero to accomplish this we are going to create an if statement checking if the ball's y position plus height is smaller than the ai's y position if this is true then we will set the y velocity to be equal to negative speed next we will create an elsif statement checking if the ball's y position is greater than the ai's y position plus height finally we can do an else statement to catch the situations when the ball is neither above nor below and when this is the case we simply set the y velocity to be equal to zero remember to call the acquire target function in the update function now the ai is doing a really good job tracing the ball it's just a little bit too good it's actually impossible to beat it currently to make the ai a little bit more mortal and beatable we will make a delay between each decision that it gets to make we will make it only able to call the acquire target function every half a second to accomplish this we need to create two new variables for the ai a timer variable that i will set to be equal to 0 and a rate variable that i will set to be equal to 0.5 inside of the update function we are going to update the timer by typing timer equal to whatever the timer was plus delta time this will make the timer count upwards in real time now we can create an if statement that checks if the timer is greater than the rate if this is the case then we will set the timer back to 0 in order to reset it and call the acquire target function now the acquired target function is not being called every single frame but rather twice per second and as you can see it makes a huge difference in how the ai behaves however it is currently not able to return the ball to us because it lacks collision with the ball so let's go ahead and fix that to make the ai collide with the ball we need to head over to the ball.lua file inside of the collision function we can go ahead and copy the code that we wrote for the player because it is going to function almost identically all we need to do is change player to be ai in the if statement and then change the local variable middle player to middle ai and setting that variable to be equal to the ai.y plus ai.height and finally we need to set the x velocity of the ball to be negative speed because we want the ball to travel back towards the player if we run the game now we can see that the ai is able to deflect the ball back to us now that we can actually play the game we need to handle what happens if the ball hits the left or right side of the window the way we check this is very similar to the code that checks if the ball hits the top or bottom side of the window first we will check if the ai has managed to get the ball past us to do this we make an if statement that checks if the ball.x position is smaller than 0. if this is the case we want to set the ball back to the center of the screen so we set the ball's x position to be equal to the screen width divided by 2 minus half of the ball's width next we set the y position to be equal to half of the screen's height minus half of the ball's height finally we want to set the y velocity of the ball back to 0 and the x velocity to be equal to the speed variable in order to make it go in a straight line towards the ai to handle when the player scores we need to check if the ball's x position plus width is greater than the windows width and if this is the case we will do the exact same thing as when the ai scored resetting the ball back to the center and setting the y velocity to be equal to zero the only difference is that we will set the ball's x velocity to be equal to negative speed so that it will travel towards the player instead that's it you have made pong great job in the next part we are going to polish it up a little bit by giving it some graphical assets and cleaning up the code [Music] you
Info
Channel: DevJeeper
Views: 3,965
Rating: undefined out of 5
Keywords: programming, coding, lua, love2d, game development, PONG, game dev, devlog, how to make games, learn to make games, coding in lua, coding in love2d, programming in lua, programming in love2d, making a game in love2d, how to use love2d
Id: QWoRboCnsuo
Channel Id: undefined
Length: 8min 2sec (482 seconds)
Published: Sun Aug 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.