Java pong game 🏓

preview_player
Показать описание
Java pong game tutorial for beginners

#Java #pong #game

Coding boot camps hate him! See how he can teach you to code with this one simple trick...

Bro Code is the self-proclaimed #1 tutorial series on coding in various programming languages and other how-to videos in the known universe.
Рекомендации по теме
Комментарии
Автор


public class PongGame {

public static void main(String[] args) {

GameFrame frame = new GameFrame();

}
}

import java.awt.*;
import javax.swing.*;

public class GameFrame extends JFrame{

GamePanel panel;

GameFrame(){
panel = new GamePanel();
this.add(panel);
this.setTitle("Pong Game");
this.setResizable(false);


this.pack();
this.setVisible(true);

}
}

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GamePanel extends JPanel implements Runnable{

static final int GAME_WIDTH = 1000;
static final int GAME_HEIGHT = (int)(GAME_WIDTH * (0.5555));
static final Dimension SCREEN_SIZE = new Dimension(GAME_WIDTH, GAME_HEIGHT);
static final int BALL_DIAMETER = 20;
static final int PADDLE_WIDTH = 25;
static final int PADDLE_HEIGHT = 100;
Thread gameThread;
Image image;
Graphics graphics;
Random random;
Paddle paddle1;
Paddle paddle2;
Ball ball;
Score score;

GamePanel(){
newPaddles();
newBall();
score = new Score(GAME_WIDTH, GAME_HEIGHT);
this.setFocusable(true);
this.addKeyListener(new AL());


gameThread = new Thread(this);
gameThread.start();
}

public void newBall() {
random = new Random();
ball = new Ball((GAME_WIDTH/2)-(BALL_DIAMETER/2), random.nextInt(GAME_HEIGHT-BALL_DIAMETER), BALL_DIAMETER, BALL_DIAMETER);
}
public void newPaddles() {
paddle1 = new Paddle(0, (GAME_HEIGHT/2)-(PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT, 1);
paddle2 = new Paddle(GAME_WIDTH-PADDLE_WIDTH, (GAME_HEIGHT/2)-(PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT, 2);
}
public void paint(Graphics g) {
image = createImage(getWidth(), getHeight());
graphics = image.getGraphics();
draw(graphics);
g.drawImage(image, 0, 0, this);
}
public void draw(Graphics g) {
paddle1.draw(g);
paddle2.draw(g);
ball.draw(g);
score.draw(g);
// I forgot to add this line of code in the video, it helps with the animation

}
public void move() {
paddle1.move();
paddle2.move();
ball.move();
}
public void checkCollision() {

//bounce ball off top & bottom window edges
if(ball.y <=0) {

}
if(ball.y >= GAME_HEIGHT-BALL_DIAMETER) {

}
//bounce ball off paddles
{
ball.xVelocity = Math.abs(ball.xVelocity);
ball.xVelocity++; //optional for more difficulty
if(ball.yVelocity>0)
ball.yVelocity++; //optional for more difficulty
else
ball.yVelocity--;


}
{
ball.xVelocity = Math.abs(ball.xVelocity);
ball.xVelocity++; //optional for more difficulty
if(ball.yVelocity>0)
ball.yVelocity++; //optional for more difficulty
else
ball.yVelocity--;


}
//stops paddles at window edges
if(paddle1.y<=0)
paddle1.y=0;
if(paddle1.y >= (GAME_HEIGHT-PADDLE_HEIGHT))
paddle1.y = GAME_HEIGHT-PADDLE_HEIGHT;
if(paddle2.y<=0)
paddle2.y=0;
if(paddle2.y >= (GAME_HEIGHT-PADDLE_HEIGHT))
paddle2.y = GAME_HEIGHT-PADDLE_HEIGHT;
//give a player 1 point and creates new paddles & ball
if(ball.x <=0) {
score.player2++;
newPaddles();
newBall();
System.out.println("Player 2: "+score.player2);
}
if(ball.x >= GAME_WIDTH-BALL_DIAMETER) {
score.player1++;
newPaddles();
newBall();
System.out.println("Player 1: "+score.player1);
}
}
public void run() {
//game loop
long lastTime = System.nanoTime();
double amountOfTicks =60.0;
double ns = / amountOfTicks;
double delta = 0;
while(true) {
long now = System.nanoTime();
delta += (now -lastTime)/ns;
lastTime = now;
if(delta >=1) {
move();
checkCollision();
repaint();
delta--;
}
}
}
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e) {
paddle1.keyPressed(e);
paddle2.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
paddle1.keyReleased(e);
paddle2.keyReleased(e);
}
}
}

import java.awt.*;
import java.awt.event.*;

public class Paddle extends Rectangle{

int id;
int yVelocity;
int speed = 10;

Paddle(int x, int y, int PADDLE_WIDTH, int PADDLE_HEIGHT, int id){
super(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
this.id=id;
}

public void keyPressed(KeyEvent e) {
switch(id) {
case 1:
{
setYDirection(-speed);
}
{
setYDirection(speed);
}
break;
case 2:
{
setYDirection(-speed);
}
{
setYDirection(speed);
}
break;
}
}
public void keyReleased(KeyEvent e) {
switch(id) {
case 1:
{
setYDirection(0);
}
{
setYDirection(0);
}
break;
case 2:
{
setYDirection(0);
}
{
setYDirection(0);
}
break;
}
}
public void setYDirection(int yDirection) {
yVelocity = yDirection;
}
public void move() {
y= y + yVelocity;
}
public void draw(Graphics g) {
if(id==1)
g.setColor(Color.blue);
else
g.setColor(Color.red);
g.fillRect(x, y, width, height);
}
}

import java.awt.*;
import java.util.*;

public class Ball extends Rectangle{

Random random;
int xVelocity;
int yVelocity;
int initialSpeed = 2;

Ball(int x, int y, int width, int height){
super(x, y, width, height);
random = new Random();
int randomXDirection = random.nextInt(2);
if(randomXDirection == 0)
randomXDirection--;


int randomYDirection = random.nextInt(2);
if(randomYDirection == 0)
randomYDirection--;


}

public void setXDirection(int randomXDirection) {
xVelocity = randomXDirection;
}
public void setYDirection(int randomYDirection) {
yVelocity = randomYDirection;
}
public void move() {
x += xVelocity;
y += yVelocity;
}
public void draw(Graphics g) {
g.setColor(Color.white);
g.fillOval(x, y, height, width);
}
}

import java.awt.*;

public class Score extends Rectangle{

static int GAME_WIDTH;
static int GAME_HEIGHT;
int player1;
int player2;

Score(int GAME_WIDTH, int GAME_HEIGHT){
Score.GAME_WIDTH = GAME_WIDTH;
Score.GAME_HEIGHT = GAME_HEIGHT;
}
public void draw(Graphics g) {
g.setColor(Color.white);
g.setFont(new Font("Consolas", Font.PLAIN, 60));

g.drawLine(GAME_WIDTH/2, 0, GAME_WIDTH/2, GAME_HEIGHT);

g.drawString(String.valueOf(player1/10)+String.valueOf(player1%10), (GAME_WIDTH/2)-85, 50);
g.drawString(String.valueOf(player2/10)+String.valueOf(player2%10), (GAME_WIDTH/2)+20, 50);
}
}

BroCodez
Автор

Finally finished all 100 videos. Thank you for making this, it really helped me understand some of the slightly more advanced concepts. I'll be returning to the vids whenever I'm stuck!

nikj
Автор

1:02:07
optional details, instead of just drawing a straight line you can use 2d graphics for a dotted or dashed line
as an example, I put:
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.white);
g.setFont(new Font("Consolas", Font.PLAIN, 60));

Stroke dashed = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{25}, 30);
g2d.setStroke(dashed);
g2d.drawLine(GAME_WIDTH/2, 0, GAME_WIDTH/2, GAME_HEIGHT);

g.drawString(String.valueOf(player1/10)+String.valueOf(player1%10), (GAME_WIDTH/2)-85, 50);
g.drawString(String.valueOf(player2/10)+String.valueOf(player2%10), (GAME_WIDTH/2)+20, 50);
}

you helped me get into coding mate thanks a million!

Pation
Автор

Thank you for your video, just 1 hour but I spent very much time to understand apparently what you did in your code, especially in game loop, workflow of a basic game. I think my java skill is better than I before. Have a great day, hope you will release many wonderful video in the future.

vietlequoc
Автор

I saw this video at the beginning of learning Java and with this video I was able to create an interesting and simple game, and it had an important impact on my programming way, I owe you and thank you so much

moxeingames
Автор

i love this channel, whith so many overwhelming information you can focus the essential things without wasting time on futile things. And this is a great and important skill today. After a month of your videos, i can easly write core java code and write a project from scratch. Thanks dude, love you

bsh
Автор

I appreciate you taking the time to explain everything you are doing and why. It really helps me learn instead of me just following along to get a working game.

mindofpaul
Автор

followed the whole way through and I found it very fun! The best part is being able to dissect the code after and understanding every bit in detail. Thank you for the video!

davidbartholomew
Автор

So, I wanted to stop the game when a top score was reached. I am sure that there are better ways to do this, but this works.

In the GamePanel class, I added:

static final int MAX_SCORE = 21;
static boolean gameRun = true;

and I changed: while(true) to while(gameRun)

and then: (I am only including one player's score here)


if(ball.x <= 0) {
score.player2++;
if(score.player2 < MAX_SCORE) {
newPaddles();
newBall();
}
else {
//game over
gameRun = false;
}
}

and yes, it needs polishing, but there you go. Thank you, Bro

stephaniepanah
Автор

So far you explain best game tutorials. You follow some order of creating things that makes it easier to follow. On point. Great for beginners in java after they learn basics and figure out how OOP works. Hope to see more game tutorials.

DomoxD
Автор

One hour worth so much, confidence builder 👌👌👌. 💯❤️

monwil
Автор

Loved the tutorial supper easy to fallow and works perfectly.
Thank you keep up I'm a fan.

rosastrology
Автор

This video is amazing! Easy to understand and follow along!

Robman
Автор

At 22:40 you could have just change (5/9) to (5.0/9.0) and it works just fine.

crackrokmccaib
Автор

Well, I have watched all Java programing language videos in a list (if I didn't miss a single video). Thanks bro, your contents are so high quality and there's no other programing language tutorial youtubers currently known that very underated like this. Your charisma gives me spirit to learn.

I don't have expectation very much to be expert developer yet, because reality is unknown. Therefore I also want to get developer job but I don't know nothing how it works yet, but I'll figure out how to get the job.

Learn programing languages because I'm curious and solving problem that solved in smart way is very satisfying. So because of this, my life impossible to be bored because there are so much to do, like mediation to fresh a mind, learn new skills, playing video games etc that I overwhelmed and sometimes have no time to do those things.

anomalous_guy
Автор

hey! Thank you for this video. I'm learning to code in college and this is very helpful!

keksmampfer
Автор

It's taken a while but I worked through all 100 videos and man it has stepped up my meagre knowledge by leaps and bounds. Thank you for your massive effort in creating all of these - much appreciated. If you're ever in Cape Town please let me take you for a beer! Cheers Bro.

AdrianTregoning
Автор

I loved this so much. Can't wait to do more! I haven't done Java in about 8 years or so, since my first year of college/uni. I found Java really difficult as it was my first coding language. (I really love coding now, favourite is Python). This has given me a love for Java and all the wonderful things it can do.

marlunyu
Автор

video worth watching, I like it.
with a perfect play of the game at the end.

JacobMutuku-mz
Автор

1:06:18 That background music tho. xd
Btw I never watched this awesome Java videos so far.
Thank you!

mr.fakeman