Java pong game ๐Ÿ“

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I think I mistyped something what's going on guys it's your bro here hope you're doing well and in this video I'm going to teach you guys how we can create a simple game of pong using Java so without further ado let's get into it if you find this video helpful please remember to Like comment and subscribe your support will help keep this channel running alright guys so pong let's do it let's create a new project file new Java project I will call this just pong finish don't create go to your project folder we're going to create a few different classes so file new class I'll call my main class pong game and check public static void bin cuz this is going to contain our main method click finish so what I'm going to do I kind of like to separate a lot of my code into different classes just to help keep things organized and consistent so I'm going to create another class just for the jframe so file new class I'll call this perhaps game frame so game make sure it's uppercase game frame that should be good and then game panel file new class game panel finish what else do we got so we got the paddles so let's make a class called paddles so file new class maybe just paddle singular finish file new class we'll make a class for both finish and then one last one for score file new class score and finish so all of these classes really aren't necessary but that's just a habit of mine I like to separate my code into different classes just because you have like chunks of code for each separate like entity like we have everything related to fall within here everything related to the score to the paddle game panel etc that's just what I like to do but everybody has their own unique coding habits now it's about that time that we import everything that we need from the JRE system library so there's 4 imports will need Java a double to import java.awt event Java dot util and lastly Java X dot swing so some of these classes won't necessarily use all of these what I'm thinking we can do is just copy these and paste it within each of these classes then at the end of the program if there's anything that we don't need we can just take it out from each class but this is just a save time so we're not importing things constantly okay so that should be good then for our imports so let's begin by creating all of the separate methods within each of these individual classes so let's begin with the constructor for our game frame because that's kind of like a special method so with our game frame we're going to create the constructor it's the same name as the class game frame let's create the constructor for our game panel so game panel same thing with our paddle ball and score so let's finish declaring some of these methods so there's no methods we'll need within our pong game class done within our game frame but we do have a bunch of methods to declare within our game at panel class so let's create a method called new ball so whenever we call this method will create a new ball on the screen and then another for new paddles so public avoid new paddles so we can call these methods if we ever want to reset a level or a game we'll want a paint method so we can paint stuff on the screen so public void paint and there is one parameter here graphics and we'll call it G and we'll want a draw method as well so public Boyd draw and this also has one parameter graphics G and we'll want a move method so we'll move things after each iteration of our game loop so public void move and then another method for check collision think I misspelled collision collision and another for run I know there's a lot of methods in this class public void run okay so we're also going to create an inner class so public class and this will be an action listener Al and it's going to extends key adapter and there's two methods within here so public void key pressed key event E and another for key released so key released so let's double check what we have to the game panel we have our constructor game panel a new ball method new paddles paint draw move check collision run an inner class called al short for action listener that contains a key pressed method and key released so if you have all that let's go to our paddle class so there's not as many methods to fill in within our paddle class so let's create a method called key pressed public void key pressed and this takes one argument let's set up a parameter for a key event E and there's also another for key rule least so what we're going to be doing within our game panel with this inner class al we're going to be adding this action listener to our game panel and then we're going to call the appropriate keep pressed method or key release of our paddle so key pressed as well as key released and then we will want a method called set y direction so public void set y direction and this will take one argument an integer named y direction so public void move and then another for draw so public void draw this has a argument or parameter of graphics G and we're good let's move on to the ball class okay so we have set X direction basically with our paddles they're only going to be moving up and down so we're only going to set the y direction we don't have to worry about the x axis but the ball has well it travels on the x axis as well as the y axis so we'll need a set X direction as well as set Y so public void set X direction and this will take an integer value and we'll call it random X direction because when we create a new ball it's going to go in a random direction so we'll set up an instance of the random class to take care of that so set X Direction as well as set y direction and change that here as well int rendom y direction and then a move method so public void move and then a draw method so public void is draw and this has graphics G and then lastly our score class so we only have one method within here well besides our constructor we're only going to draw the score we don't need it to move or anything so public void draw this takes graphics G so moving on with a few of our classes we're going to turn these into subclasses so let's begin with our game frame so next to the declaration let's have this extends the jframe class so we can treat our game frame as a dray frame basically our game panel will extends J panel and it's also going to implements the runnable interface so we can actually have this run on a thread so paddle will extends rectangle same thing for ball extends rectangle and same thing for score extends rectangle so let's head back to our pong game class and we'll start here and then work our way over to the right and declare and instantiate basically most of the things that we'll need throughout the rest of this program so beginning with our pong game class we're going to create an instance of our game frame and the game frame is going to hold a game panel so the game frame is basically the window frame that has the minimize button maximized the X button all that cool stuff so let's create an instance of the game frame so game frame I'll call this frame equals new game frame and that's it for this class let's move on to our game frame and before the constructor we're going to create an instance of the game panel class so game panel will call this panel equals new game panel and then within our game that panel class we have quite a few things to declare within here so we'll create a static final and detered variable called game underscore wit and let's set this to 1000 that's a good size so let me explain some of my reasoning for using these static as well as the final keyword modifiers so the reason that we're making the static is because if for some reason we had in multiple instances of our game panel class they would all share just one variable and they wouldn't each have their own individual game with they'd all be sharing the same one and the reason that we use the final key word is that this prohibits us from accidentally modifying our game with later on in the program also if used the final key word it also makes this run a little bit faster not anything significant but it's considered good practice if you don't really plan on modifying this variable elsewhere and one naming convention with final variables is to make all letters in your variable name all uppercase so it's more easily identifiable and in past videos in this tutorial series we really haven't used the static as well as the final keyword modifiers to often because they weren't really relevant to the topics we were discussing but since we're starting to get into games a little bit more I thought it'd be a good time to reintroduce these so with a few of these variables I'm going to use the static as well as the final keyword modifiers if they apply now let's also do the same thing for our game height so we'll create a static final int called game height now with this game table this game panel I'm going to be copying the dimensions for a standard ping-pong table and it has a ratio of five to nine five feet to nine feet so I'm actually going to copy that ratio so we'll take our game width and multiply this by our ratio five divided by 9 and then I'm going to cast this as an integer so this will give us a corresponding height to our game width and if we were to actually change our game with the game height would adjust accordingly to whatever we assign for our game width so let's also create a dimension so static final dimension and we'll call this screen underscore size equals new dimension and we're going to pass in a width and a height so we're going to pass in our game with as well as our game height okay now let's create the diameter for the ball so static final int ball underscore diameter I think that's how you spell diameter and let's assign this as 20 pixels so the higher number the higher the ball diameter and let's work on the paddle dimensions so static final int paddle underscore width and 25 is good for this and then a paddle height static final int paddle height and maybe 100 that should be good okay so we're going to declare a few instances but not actually initialize them yet so let's begin with a thread since we're implementing the runnable interface so thread game thread and then an image so image and we'll call this image graphics graphics random random paddle paddle one so this will be player 1 paddle paddle 2 ball ball and score score now let's head to our paddle class and there's just two variables to declare so before the constructor will declare an int called ID so this will be either 1 or 2 1 for player 1 or paddle 1 and ID 2 or paddle 2 and then we will also want an int called Y velocity so Y velocity is going to be how fast the paddle is moving up and down when we press a button so let's move to our ball class now and before the constructor this is what we're gonna type an instance of the random class int X velocity how fast it's gonna move on the x axis and int Y velocity how fast this ball is going to move on the y axis and then for our score let's make a static int game underscore with we're actually going to pass in a copy using our constructor and then a static int game underscore hi it an int player 1 so this variable will hold the score the current score of player 1 and another for player 2 and that should be everything we need to declare before the constructors okay guys so let's head back to our pong game so we're pretty much done with this class there's nothing else that we need to do so let's move on to game frame next so the game panel is going to be the canvas on which we're painting and the frame is kind of like the frame around the painting it really doesn't do much it just houses our panel so it kind of contains that minimize button and that maximize button that X button so that's our jane frame basically so the panel is going to be surrounded with the frame and actually just one quick change that I want to make real quick is that I want to finish instantiating this instance within my constructor all right so let's add our game panel to our frame so this dot add panel and let's set a title this set title pong game and we do not want people to resize this so this dot set resizable pulse and let's add a background color so this dot set background color dot black and this dot set default close operation jframe dot exit on close so when we hit that X button it will close out of our application now for this next line we're going to use the pack method of the jframe class and here's a quick description so this causes the window to be sized to fit the preferred size and layout of its subcomponents and in our case what this is going to do once we create our game panel this is kind of like a painting we're going to use the game frame class and it will fit snugly around our game panel so we don't necessarily need to set the size of our J frame it's going to adjust accordingly to accommodate the size of our game panel now for the next line we just need to set visible true so we can actually see this alright so we shouldn't really have anything yet just a little box here and you can see that the background is black so when we use the pack method and we add that to our game frame the window frame is actually going to adjust to fit the size of the game panel and one other trick you can do too is that since this appears in the top left corner we can actually set this to appear in the middle of our window here on our computer so the method for that is set location relative to and then put no so now when you run this it's going to appear in the middle of your screen although we still have to add a game panel to this frame but yeah that is it for our game frame class let's move - game panel no here we are within our game panel class so when we construct our game panel what we want to do exactly so let's begin by calling the new paddles method which we still have to fill in though but we will eventually so when we create our game panel we also want to create some new paddles so we're going to call the new paddles method as well as the new ball method and let's finish instantiating our instance of the score class called score so a score equals new score so one thing that I want to do is pass in our game width as well as our game height variables so I'm going to pass these into the constructor so game width as well as our game pipe so let's finish setting up these constructors well the constructor for the score class so this takes an integer variable and the first one was game width the second one was game height all right so we'll return to this later let's head back to our game panel so with this game panel we want this to be focusable so if we press any Keys it's going to have focus then so it's actually going to read these key presses these keystrokes so this dot set focusable true then we want to add an action listener so this will respond to keystrokes so this dot key listener and we will pass in new al so al is our inner class short for action listener so this extends the key adapter class and this dot set preferred size and we need to pass in a dimension good thing we have a dimension here called screen size so we're going to pass in our screen size and then lastly let's start our thread so game thread equals new thread and we're going to pass in this since we're implementing the runnable interface and then we just need to start this thread so game thread dot start and that is it for our constructor for our game panel hey guys so there's one little mistake that I made that I wanted to bring to your attention so we have this ratio of the height to width of 5 to 9 to match a standard ping-pong table however I forgot that here we're doing integer division so this will round down to 0 so when I ran this program I had a game frame with the width but no height so we should actually place a double value here instead so 5 divided by 9 equals 0.5 repeating so I'm just going to change this from 5 divided by 9 to zero point 5 5 5 5 so that should be good then let's try rerunning this so now we have our game panel that's added to our game frame so this panel will house all of the components for our pong game so we're still within our game panel class let's work on the paint method next so we're going to take our image and we're going to create an image that has the dimensions of the width and the height of our game panel so we're going to use the get with method to retrieve the width of this panel as well as the get height method so then we're going to take our graphics and set this equal to image and we're going to create a graphic so image dot get graphics and then we're going to call the draw method to draw all of the components but we're going to pass in our graphic that we created from our image so we're going to pass in our graphics and then after we draw all of the components we're going to take our graphics G and we're going to draw our image so there's a few things to pass in here not all of these just a few things we're going to pass in our image as well as the coordinates so we want this to start in the top left corner that's where X is zero and Y is zero and we're also going to pass in this RJ panel called game panel alright everyone so let's move on to the run method of the game panel class we're going to create a basic game loop this isn't anything too sophisticated it's just something I would say very basic I'm hoping to get like 60 frames per second or so with this granted though I'm not really a game loop expert so you guys can probably code something better than this but we'll just start with something basic so I actually am copying a game loop for Minecraft I just made a few adjustments so we're going to use a very similar game loop so I'm going to create a long value called last time we're going to set this equal to system dot nano time so this is going to be a long value and then we're going to create a double variable called amount of ticks and we'll set this equal to 60 all right so then we're going to create another double value called NS for nanoseconds and set this to 1 billion so I think that's the right amount of zeros let me double check so that's a thousand that's a hundred thousand million ok so that should be 1 billion divided by ammount of ticks then we're going to create a double value called Delta and set this equal to zero so these are all the variables we'll need to declare to sign so we're going to create a while loop there's a few different ways to do this you could have a boolean value called it running and you can do this while the game is running otherwise if you just want this to be simple you could just say well true I'll just go with the simple version to this and you can always adjust this later on depending on how you want to write your game so while true we're going to take log variable now equals whatever our system dot nano time method is and then we're going to take our delta value plus equals whatever now is minus last time and this will be divided by NS for nanoseconds then last time equals now and if our delta value is greater than or equal to 1 then we're going to first move all of the components and then we're going to check for any collisions so check collision and then repaint everything and then subtract 1 from Delta so I'm just going to do a print line statement just to be sure that everything is working so let me fix that I'm using the shortcut system dot out dot print line test so this should repeatedly print out test ok it looks like the game loop is running then so this isn't the best game loop but it's just something basic for pong so that's everything for the run method of the game panel class ok guys let's head to our new paddles method and we're going to finish instantiating instances of paddle 1 and paddle 2 so within the new paddles method we're going to take paddle 1 and set this equal to a new paddle and we're going to send in some arguments and we'll need to finish setting up the constructor for our paddle class so we're going to send in where we want this paddle to be on the x-axis so for player 1 let's say we want this on the very left so that would be where X is zero so then what if we want this paddle right in the middle of our game frame so what we can do is that we can take our game underscore height divided by two so it's gonna be like right near the middle okay but we'll also want to subtract half of the paddle height so it ends up right in the middle so game height divided by two - paddle underscore height divided by two then we need to state what the paddle width and the paddle height is and send this into the constructor so we're going to send in our constant paddle height and petal width I think it's petal width first so paddle width and then paddle underscore height and then we'll also pass in a unique ID number for this paddle so just 1 for this paddle so let's copy this and do this for paddle two so if we set the x position to be 0 it's going to be on the very left of our window so for this argument we're going to place something else the game underscore with - the paddle with there was gonna be a lot of text here all right so game would - paddle with so we can keep the Y positioning the same but we need to pass in a unique ID number so let's pass in two instead of one okay so let's finish setting up the constructor for this class so we'll head to the paddle class so for this class the first thing we're taking in is an x position Y position width and height and then an ID number so int X into y int paddle with inte paddle height and int ID now within the paddle constructor since this is a subclass of the rectangle superclass we can actually call the super constructor to assign some of these arguments for us so we're going to send the super constructor the x value Y as well as width and height so paddle width and paddle height and then we just need to assign this ID that we receive so this ID equals ID and that's it for our constructor for now so let's go down to the draw method and actually draw these rectangles these paddles so if our ID is equal to 1 this will be player 1 so let's set the color for player 1 so G dot set color and maybe we'll make player 1 blue so color dot blue so if our ID is not one it must be 2 so we don't necessarily need to say if ID is equal to 2 we can just use an else statement because that's more simple so else our ID must be 2 so we're going to set player 2 to a different color and I will pick maybe red so color dot red and then we just need to fill this rectangle so G dot fill rect so we need X Y width and height so X Y width and height all right so let's go to our game at panel class and actually it draw this rectangle so when we go to the paint method we're going to draw all of the graphics that we have so we're going to take our paddle paddle 1 and use the draw function but we need to pass in our graphics G and we'll do the same thing for paddle 2 paddle to draw and let's take a look to see if they appear yeah here's our two paddles paddle one which will be player one and paddle two which will be player two however we would like these paddles to move up and down on the y-axis when we type in certain keystrokes so let's work on that next so let's go to the inner class of the game panel class and we named this AL short for action listener and this extends key adapter so with key pressed let's take paddle one and we're going to call the key pressed method of this class and then let's do the same thing for paddle to paddle to key pressed and let's do something similar for key released when we release a key so paddle one and we'll use key released and then the same thing for paddle to paddle to dot key it released then let's head to our paddle class so now at this point we're going to work within the key pressed method of the paddle class so we're going to create a switch that's going to examine the contents of our ID variable so this is either one or two so case one this is referring to paddle one so if I get key code is equal to key event dot V K underscore let's say W so if somebody types in W on their keyboard it's going to execute this block of code so let's set the Y direction of paddle one so set Y direction and let's pass in maybe negative ten pixels so this is going to move up on the y-axis and let's also call the move function and I'm going to copy this and do the same thing for s so W to move up s to move down and this applies to only paddle one and we need to pass and let's say ten pixels and you know what I'm actually going to change this around let's create a variable called speed and we will pass in negative speed and we'll assign speed to maybe 10 and you can adjust this later I think it'd be easier if we have a variable you can change at the top of this class so we'll just pass in speed I think that would be better okay then let's do this for case two so I'm going to copy this paste it change case 1 to case 2 and let's set this to up on the arrow pads up and down okay that is it for the key pressed method oh we should also brake as well so I'm going to add that so brake as well as here ok now we can actually copy all this and paste it within our key released method so I'm going to copy this paste it and forkie released we're going to change this from speed to zero because otherwise this will just keep on moving forever so zero zero zero zero so make sure 4 key pressed you're passing in a value such as a number or a variable that contains a number so here I just have negative speed and speed and you can want this to move faster or move further you can just upgrade this to a higher number and that is it for key released so let's set the Y direction so all we do here is take our Y velocity and set this equal to Y direction and that's it for this method and then we're going to fill in the move method so we're going to take the Y positioning equals y plus y velocity okay that is everything for this class actually let's take a look to see if this works now let's begin with paddle one which is on the Left I'm holding up W so if you just press it real quick it only moves up by 10 pixels and I'm moving it down however if you hold it down it starts to slide one problem that we're running into though is that this can go off the screen so we'll need to limit that so we'll work on that soon and then let's do this for player 2 otherwise known as paddle 2 so now on the arrow keys I'm pressing up and down cool so we know that that is working now to prevent the paddles from going off the screen we're going to fill in a few things within the check collision method of the game panel class so I'm just going to add a comment that this stops paddles at I should call this window window edges okay so what we'll check is if paddle 1 dot Y is less than or equal to 0 what we'll do is set paddle 1 dot Y to equal 0 and the next thing we'll check is if paddle 1 dot Y is greater than or equal to and this is a little more complicated so this is if you're moving down so game underscore height minus the paddle underscore height and if this is true we're going to take paddle 1 dot Y and set this equal to game height - paddle height and let's do the same thing for paddle - so paddle 2 okay that looks good let's try this again so let's test paddle one I'm trying to go off the screen and it's not letting me it stops right at this border for our frame and let's do the same thing for the bottom side all right so I'm pressing down but I can't move any further so these are the bounds the boundaries of our game and let's try the same thing for paddle - all right that looks good so it looks like our paddles are fully functional so congratulations if you made it this far so let's work on the ball next hold up you're not going anywhere because we forgot to fill in the move method of the game panel class so let's take a closer examination of these paddles so even though these paddles are functioning as they are intended they are somewhat sluggish and slow so we can actually make their movement more smooth and one way we can do that is call each of these individual move methods after each iteration of our game loop and we do that right here after each game loop cycle we're calling the move method of the game panel class so right now let's just call paddle ones move method just to compare the differences between the two now so here's paddle - it's somewhat slow and it's kind of laggy and here is paddle one and it's way more smooth and faster so let's also apply this to paddle - so we'll call paddle to move and they are both silky smooth well at least for Javas swing graphical user interface at least and let's do the same thing for ball because I'll probably forget it if I don't do it now so ball dot move and that is it for the move method of the game panel class for now with the ball dot move method I'm just going to turn this into a comment just so we can run this to test it every once in a while and then when we get to the point where we need this we'll just remove the comment so let's go to the new ball method of the game panel class so what we'll do is that since we already have our random random instance we are going to finish instantiating this so random equals new random and we won't quite need this yet and I'll explain why in just a moment so let's say that we want this ball to start in the very center of our window so right about here so we're going to pass in the coordinates of the ball that we have to our ball constructor so we're going to take ball and ball equals new ball and let's set the X positioning so that would be game underscore width divided by 2 but we also want to subtract the ball diameter so minus ball underscore diameter divided by 2 so this will appear in the middle of the x axis let's do the same thing for the Y axis for now so that would be game underscore height divided by 2 - ball diameter divided by 2 then let's also pass in the ball diameter for the width and the same thing for the height so ball diameter and ball diameter and then end this with a semicolon ok then we need to fill in the constructor for the ball class so let's head there next so we need to set up the parameters for this ball constructor and they need to be the same as when we create this instance of ball so we have the x position the Y position the width and the height however with a perfect circle the width and the height are the same because that would be the diameter so we just passed in ball diameter twice so let's go to the paddle constructor and let's set up the parameters so int X int y int let's say with int pipe and since ball is a subclass of the rectangle superclass we can call these super constructors to assign these arguments for us so we're going to send in X Y the width which is basically our ball diameter and height which is also ball diameter all right so then within the constructor let's set a random direction in which this ball is going to head so we have our own instance of the random class within our ball class and this is also named random so random equals new random and let's create an integer so this is going to be a local variable for the ball constructor so int random X Direction is equal to random dot next int and let's send in two so this is either going to be 0 or 1 if it's 0 let's have this ball go left if it's 1 it's going to go right so after this if random X Direction is equal to 0 we'll have this ball go left so what we'll type is random x-direction and will decrement this by 1 because it's going to begin at 0 so this will just be a negative 1 and then let's set the x-direction so we need to pass in a random x-direction so set X direction and we're going to pass in our random direction okay let's do the same thing with the Y direction so I'm going to copy this paste it change X to Y that can stay the same random y-direction same thing here and we're going to set y direction and pass in random y direction and that should be good so let's go to set X direction so X velocity so we have an X velocity and a Y velocity with our paddles we only had a Y velocity since they only move up and down so X velocity equals the random x-direction that we receive so random x-direction and our Y velocity equals random Y direction alright let's fill in the move method so X plus equals our X velocity and y plus equals our Y velocity and lastly we just need to draw this ball so let's set the color first so g dot set color and let's make this white like a ping-pong ball so color dot white and instead of a rectangle we can actually draw a circle so we're going to use the g dot fill oval method so this will take X Y our height and width so height and width are going to be the same because they are both the ball diameter value which was 20 right here okay and then lastly we just need to draw this ball so let's head to our game panel class and we are going to yeah we can move this ball too so let's remove that comment and go to the draw method and we're going to draw our ball so ball and we're going to call the draw method and pass in our graphics G so let's take a look at what we have so far okay here's our ball it's going kind of slow and it's just going to disappear off the screen so we want to set some limits on the game boundaries for the ball so to bounce the ball off the top and bottom window edges we're going to do this within the check collision method of the game panel class so I'm just going to add a comment that this section of our code of our method will bounce the ball off the top and bottom window edges so what we'll check if the Y positioning of our boss of ball dot y is less than or equal to zero we'll have this if statement do this so ball dot set Y direction and we're going to take our ball dot Y velocity and we're just going to reverse the velocity by adding a minus sign so it's going to go in the opposite direction and let's also do this for if ball dot Y is greater than or equal to game underscore height but we also have to consider the balls diameter because it checks the top left corner that's the y-coordinate so if ball dot y is greater than or equal to game height minus ball diameter and then we're just going to do the same thing where we're going to reverse the Y velocity okay let's try this again so this is moving kind of slow I'm probably going to speed this up a little bit next but you can see that yeah it bounced off the bottom wall and the same thing should apply to the top wall as well but let's speed up this ball a little bit so let's go to the ball class so I wasn't planning on this but let's add one more variable and we'll call this speed actually better yet initial speed that reminds me of initial D so initial speed and we'll multiply our random x-direction when we set the x-direction so we'll multiply random x-direction by our initial speed and let's do this for y but we should probably assign a value to this so let's begin at two because if we did one this would stay the same so this should be a little bit faster now yeah it's about twice as fast as what it was before let's try a higher number like three okay yeah that's a lot faster now I think I'll stick with two to begin with what you can adjust this to whatever number you like like 20 yeah can't even really see that that was too fast for me I'll just stick with two for now so that should be decent what we have to work on next is to check to see if our ball collides with our paddle because right now it'll just go straight through it kind of like so so we'll want to bounce this ball off these paddles when they intersect and since we're using rectangles we can actually use the intersects method which is actually kind of useful for this situation so let's go to the Czech collision method of the game panel class and I'll add a comment that this bounces ball off paddles so we're first going to check to see if our ball is intersecting with paddle one so we're going to do this with an if statement so if ball dot and we're going to use the intersects method since this ball class is a subclass of the rectangle superclass it inherits all the methods and properties of this rectangle class including the intersects method and we'll pass in paddle one so this method is going to compare these two objects to see if there is any collision between the two so if there is a collision we're going to reverse reverse the velocity of the ball so we can actually reassign this so ball dot X velocity and we're going to reassign this width using the math classes absolute value method it's just dot and we're going to pass in balls X velocity so this will be negative and basically all we're doing is turning it into a positive number I suppose you can always multiply this by negative one as well but too late already wrote it so this is totally optional if you want to increase the velocity after it bounces off a player's paddle you can increment that by taking ball dot X velocity and you can increment this by one or a different number I'll just add a note that this is optional for more difficulty you can always leave it out to whatever you want to do all right so let's also check the Y velocity so let's see if our Y velocity of the ball is greater than 0 so if ball dot Y velocity is greater than zero well increment the Y velocity as well for more difficulty so ball dot Y velocity plus plus and this is totally optional too so I'll add a note so else we're going to decrease it so that means if the Y velocity is negative that means it's going upwards so we're just going to increase the upwards velocity so then we're going to set the X direction and the y direction with these new values so ball dot set X direction and we're going to pass in the ball dot X velocity as well as the ball dot set Y direction so ball dot Y velocity all right so let's check this with paddle one so we need to wait for the ball to go left though so we can actually test it okay so we can move the paddle up and down and let's see if it bounces yep and it does and it moves a little bit faster now because we added these two lines of code to increase the X&Y velocity of this ball but this is totally optional if you want to add these two so let's work on paddle two so let's copy this and paste it and we're going to change paddle one to you paddle two and one less change that we need when we set the X direction for this fall we're going to add a negative sign so we're going to flip the X velocity so let's test this so they should bounce off the paddles now yep and it gets a little bit faster too for more difficulty if you want to keep that feature in all right so let's say it goes past the paddle so we want some way for this to score and create a new ball for us so let's work on that next we're going to head back to the check collision method of the game panel class and let's add this maybe at the end so this section is going to give a player one point and creates new paddles and ball so we're going to check to see if the ball dot x value is less than or equal to 0 that means that player to score at a point so that means that this touched the left boundary so let's give player to a point so we have our score class that we need to fill in still but we do have integer variables player 1 and player 2 so score dot player 2 and we'll increment this by one and let's call the new paddles method that we filled in already so that's at the top here somewhere yep right here and then we're also going to create a new ball so that is right here all right and then that is it oh let's also uh system dot out dot print line the current score just so we can be sure that this is working we can test it so we'll take this out later okay let's see if player one squared at all so this is a little different if ball dot X is greater than or equal to game underscore width and we need to subtract the ball underscore diameter but I misspelled diameter there so scored dot player one will be incremented by one and let's also display the score for player one with a simple print line statement just to test this I forgot to add something within the print line statement I'm just going to add some text before this player two plus the score for player two and the same thing for player one player 1 plus score of player one okay so I'm not going to catch this ball so player two scored and they have one point and player 1 scored as well and you can see that when a point is scored it's going to reset the paddles to the middle and it's going to create a new ball for us in a I would say random direction so another thing you can do to this ball always starts in the middle if you want you can actually have this start somewhere randomly on the y-axis so this part is optional if you want some more difficulty so let's head back to where is it the new ball method of the game panel class and we're going to create an instance of the random class within the new vault method so if you want this ball to start randomly on the y-axis right now it begins right in the center so we're going to change the Y positioning within the constructor for our ball object so it looks like that is right here so let's use this instance of the random class so we're going to type in random dot next int and we'll need to set some values so this is going to be on a range of the game underscore height but we also need to subtract the ball diameter so minus ball underscore diameter this is totally optional but I think it would be fun and I keep the spelling diameter alright let's test this so yeah it's no longer appearing right in the middle of the y-axis it's appearing somewhat randomly okay let's head to the scorer class and I believe that is it so with our score constructor we passed in a game width and a game height so let's just assign these to our game with and game high variables of the score class so these are static so what we can type in here is the name of the class instead of the name of this instance so score dot game underscore width is equal to game underscore width but I can never spell ever okay then let's do this for height so score dot game height equals game underscore height all right so let's go to the draw method so let's actually draw the score on the screen so let's set the color G dot set color and maybe we'll make this white so color dot white and let's set a font Shoji set font let's pass in a new font and I will pick the font style of I believe it's pronounced console us I don't know I'm probably mispronouncing that and let's have this font be plain and let's pick a font size so 60 let's also draw a line like right down the middle of this game so that would be G dot draw line and we have a few coordinates to fill in the starting x and y position as well as the ending x and y position so we're going to start this right in the middle of the x axis so we're going to take our game underscore width and divide this by 2 and for the starting y position that will be 0 now for the x position of the ending position this is going to be the same game underscore width divided by 2 and the y position is going to be the game underscore height and let's test this but we forgot to add something so let's go back to the game panel and we forgot to draw the score so let's add that score dot draw so here it is that line that we wanted to draw that divides our game panel into right down the center so lastly let's draw the score maybe in the top around here somewhere so let's head to the score class and I already kind of measured this out so this is what I came up with that was decent so I'm going to take G and use the draw string method so we can pass in a string the x position and the y position so I'm going to and in the string dot value of and we're going to take player 1 so this is going to be the score of player 1 we're turning it into a string we're converting it and then we'll display this we're let's see game underscore width divided by 2 and there's a little bit of an offset so I'm just going to subtract 85 from this because it looked nice in this position and we'll place this where Y is 50 so let's do the same thing for player 2 so player 2 and for the exposition of the second string I'm just going to change this from minus 85 to plus 20 so let's run this and I just want to explain a few things so here are the two scores and you can see right now that they are not quite even from the middle line so what I'm actually planning to make this look a little more arcade-like is that I want a number for the tens digit of the score as well as a single-stage it to be displayed so basically I want this score to proceed with a zero for the tens digit so to make this look a little more rk2 like I'm actually going to take the string value of player 1 and player 2 again and then add a few things so for the first portion of string dot value of player 1 I'm going to divide this by 10 so it's initially going to begin at 0 and for player 1 I'm going to add for the second portion modulus 10 so this will give us the remainder of player 1 modulus 10 and then I'm also going to do this for player 2 so string dot value of player 2 divided by 10 plus the string value of player 2 modulus 10 so let's test this so we have two digits the first is the tens position and the other digit on the is the singles position so I'm just going to fast forward this video until we can actually see this I reached the tenth position here we are it went from zero nine to one zero so we know that this is working fine all right guys well that should be everything so let's run this one last time so that will be the end of this video congratulations by making it this far by the way it was a long video if you would like a copy of all this code I'll post all of this in the comments down below but yeah that's how you can create a simple game of pong using Java hey you yeah I'm talking to you if you learn something new then you can help me help you in 3 easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro you you
Info
Channel: Bro Code
Views: 77,930
Rating: undefined out of 5
Keywords: java pong game, pong game in java, pong game java, java pong, pong in java, Java pong game, Java pong game tutorial, java pong game eclipse, java pong tutorial, java pong code, java, pong, game, tutorial, for beginners, java pong game code eclipse
Id: oLirZqJFKPE
Channel Id: undefined
Length: 67min 43sec (4063 seconds)
Published: Sat Jul 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.