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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's time to create the ball just like with the player we're going to create a new lua file i'll name it ball.lua first we need to create a table called ball which will contain all of the variables and functions affordable we will also create the three base functions load update and draw these three functions need to be called in main.lua we also need to make sure that we use the require function to make sure that the game understands that bald of lua exists in many ways the ball is similar to the player both have an x and y position as well as a width and height the player and ball will both move so they need a speed variable let's go ahead and create these variables i want the ball to start in the middle of the screen so i will set the x position to be equal to love.graphics.getwith divided by 2 and the y position to be equal to love.graphics.getheight divided by 2. next i will set the width and height to be equal to 20 and the speed to be equal to 200. the difference between the player and the ball is that the player is controlled by input while the ball will move in a direction depending on its last collision in order for us to accomplish this we need to store the velocity of the ball we do this by creating two variables one for velocity on the x-axis which i will name xvel as well as one for velocity on the y-axis which i will call y-val since the ball needs to be moving from the get-go else no paddle will ever reach it we should set the x-variable to be something else than 0. i want the ball to move towards the player at the start setting the x velocity to negative speed will accomplish this now we need to update the ball's x and y position based on the velocity let's go ahead and create a function called move inside we will set the x position to be equal to the x position plus the x velocity multiplied by delta time and the y position to be equal to the y position plus the y velocity times d t we also need to call the move function inside of the update function in order for us to check if our code is actually working we need to go ahead and draw the ball no not like that we need to do this exactly the same way as we did with the player using love.graphics.rectangle in the ball draw function and passing it the appropriate arguments and now we can see the ball moving towards the player though it passes straight through in order for the player to be able to detect the ball we need to add collision detection there are a lot of different ways to calculate collision but since our game only uses different size rectangles we can use something called aabb collision detection here we have a character that has a green rectangle that indicates its hitbox and a rather large bullet that has a red hitbox the bullet has many of the same variables as our player and ball have it has an x and y position as well as a width and height in aabb collision detection we are checking each side of a rectangle against the opposite sides of the other rectangle in order to figure out if the two shapes are intersecting in other words this side checks against this side and this side checks against this side etc the right side of this rectangle won't ever need to check the right side of another rectangle as it just isn't relevant to this collision detection method so it's important that we know how to get each of the sides the left side is simply the x position while the right side is the x position plus the width the top side is the y position and the bottom side is the y position plus height the first thing we need to check is if the right side of the bullet is to the right of the left side of the player if this is not the case then it is impossible for the shapes to be intersecting next we check if the left side of the bullet is to the left of the right side of the player finally we need to do the same thing for the top and bottom sides this is done exactly the same way as with the left and right side except that we change the x to be y and the width to be height as you can see the first and third rows are identical apart from that the same goes for the second and last row since both the player and later the ai opponent need to have collision with the ball we are going to create a function that will check the collision between two shapes and return whether or not they are colliding inside of main.lua i will make a function named check collision we need to be able to pass it to arguments one for each of the shapes that we want to check for collision inside of the parentheses i will add a and b now we just need to write the same if statement that i showed you but instead of a bullet and player we will use a and b so we check that a dot x plus a dot width is greater than b dot x and a dot x is smaller than b dot x plus b dot width and a dot y plus a dot height is greater than b dot y and finally that a dot y is smaller than b dot y plus b dot height if this is the case then a and b are colliding and we will return the boolean value true we do this with the keyword return now we need to make an else statement that will handle when a and b are not colliding then we will return false now that we have the aabb collision function it will be easy to make the ball bounce on the player's paddle go into ball.lua and create a new function named collide inside of this function we are going to create an if statement that uses the check collision function inside of the parentheses we are going to type the two shapes that we want to check for collision in this case we will pass in self meaning the ball and player the function is going to return true when the ball and player are colliding inside of the if statement we are going to define what happens when the ball collides with the player first of all we want the ball to change direction and move towards the right to do this we set the ball x velocity to be the ball speed to make sure that the ball does not just travel back and forth on the same line we need to also modify the y velocity i want the ball to fly upwards when it hits the upper part of the paddle and vice versa to do this we need to compare the middle point of the ball to the middle point of the paddle i am going to store these two points as local variables a local variable is a variable that is only available in the current scope so if it is declared inside an if statement it is only reachable inside that if statement if it is declared at the top of a function it is only accessible inside of the function the main reason to use local variables is that it does not clutter up the global namespace in simpler terms we can use short and specific variable names without having to worry that we override another variable and break the game to create a local variable you simply type the keyword local and then declare your variable like normal i am going to create a local variable named middle ball and set it to be equal to the ball's y position plus half of the ball's height next i will make another local variable and name it middle player which i will set to be equal to the player's y position plus half of the player's height finally i'm going to create a variable named collision position and set it to be equal to middle ball minus middle player now we can use the collision position variable to change the ball's y velocity i will set the ball's y velocity to be equal to the collision position multiplied by 5. the number we set affects how drastically the ball's angle changes a larger number gives the ball larger swings in the y velocity and vice versa now we just need to call the collide function in the update function if we run the game you will see that the ball will move upwards if it hits the top part of the player paddle while it will move downwards if it hits the bottom part however the ball is currently able to leave the window fixing this will be fairly straightforward and very similar to the code that we used to keep the player within the bounds of the window make another if statement inside of the ball's collision function and check if the ball's y position is less than zero if that is the case we want to set the ball's y position to be equal to zero and flip the y velocity we can accomplish this by setting the y velocity to be equal to negative y velocity making a positive number negative well turns it negative while making a negative number negative turns it positive next we can make an elsif statement that checks if the position of the bottom part of the ball is larger than the bottom part of the window if this is the case we will set the ball's y position to be equal to the bottom of the window minus the ball's height we also need to flip the y velocity by setting it to be equal to negative y velocity now the ball is no longer able to leave the window that is it for this video we have now gotten far enough that it makes sense to create an opponent an ai controlled paddle which we will do in the next part [Music] you
Info
Channel: DevJeeper
Views: 5,743
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: 3CRIhC_2wTI
Channel Id: undefined
Length: 9min 31sec (571 seconds)
Published: Fri Jul 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.