0% found this document useful (0 votes)
420 views7 pages

Code For A Java Pong Game GitHub

This document contains code for a Java Pong game including a Ball class and Paddle class. The Ball class contains code for initializing the ball, tracking its position and movement, handling collisions with paddles, and drawing the ball. The Paddle class contains code for initializing the paddles, handling keyboard input, moving the paddles, and drawing them. The ball and paddles are run in separate threads to continuously update their positions.

Uploaded by

zizo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
420 views7 pages

Code For A Java Pong Game GitHub

This document contains code for a Java Pong game including a Ball class and Paddle class. The Ball class contains code for initializing the ball, tracking its position and movement, handling collisions with paddles, and drawing the ball. The Paddle class contains code for initializing the paddles, handling keyboard input, moving the paddles, and drawing them. The ball and paddles are run in separate threads to continuously update their positions.

Uploaded by

zizo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

30/09/2021 14:56 Code for a Java Pong game · GitHub

Instantly share code, notes, and snippets.

hayate891 / Ball.java
Forked from anonymous/Ball.java
Created 5 years ago


Star


Code
Revisions
1
Stars
1
Forks
8

Code for a Java Pong game

Ball.java

1 package pongGame;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import java.awt.Rectangle;
6 import java.util.Random;
7
8
9 public class Ball implements Runnable {
10
11 //global variables
12 int x, y, xDirection, yDirection;
13
14
15 int p1score, p2score;
16
17 Paddle p1 = new Paddle(10, 25, 1);
18 Paddle p2 = new Paddle(485, 25, 2);
19
20 Rectangle ball;
21
22
23 public Ball(int x, int y){
24 p1score = p2score = 0;
25 this.x = x;
26 this.y = y;
27
28 //Set ball moving randomly
29 Random r = new Random();
30 int rXDir = r.nextInt(1);
31 if (rXDir == 0)
32 rXDir--;
33 setXDirection(rXDir);

https://gist.github.com/hayate891/b09670e980ad02737ad6892c98e7e770 1/7
30/09/2021 14:56 Code for a Java Pong game · GitHub

34
35 int rYDir = r.nextInt(1);
36 if (rYDir == 0)
37 rYDir--;
38 setYDirection(rYDir);
39
40 //create "ball"
41 ball = new Rectangle(this.x, this.y, 15, 15);
42 }
43
44 public void setXDirection(int xDir){
45 xDirection = xDir;
46 }
47 public void setYDirection(int yDir){
48 yDirection = yDir;
49 }
50
51 public void draw(Graphics g) {
52 g.setColor(Color.WHITE);
53 g.fillRect(ball.x, ball.y, ball.width, ball.height);
54 }
55
56 public void collision(){
57 if(ball.intersects(p1.paddle))
58 setXDirection(+1);
59 if(ball.intersects(p2.paddle))
60 setXDirection(-1);
61 }
62 public void move() {
63 collision();
64 ball.x += xDirection;
65 ball.y += yDirection;
66 //bounce the ball when it hits the edge of the screen
67 if (ball.x <= 0) {
68 setXDirection(+1);
69 p2score++;
70
71 }
72 if (ball.x >= 485) {
73 setXDirection(-1);
74 p1score++;
75 }
76
77 if (ball.y <= 15) {
78 setYDirection(+1);
79 }
80
81 if (ball.y >= 385) {
82 setYDirection(-1);
83 }
84 }
85

https://gist.github.com/hayate891/b09670e980ad02737ad6892c98e7e770 2/7
30/09/2021 14:56 Code for a Java Pong game · GitHub

86 @Override
87 public void run() {
88 try {
89 while(true) {
90 move();
91 Thread.sleep(8);
92 }
93 }catch(Exception e) { System.err.println(e.getMessage()); }
94
95 }
96
97 }

Paddle.java

1 package pongGame;
2 import java.awt.Color;
3 import java.awt.Graphics;
4 import java.awt.Rectangle;
5 import java.awt.event.KeyEvent;
6
7
8 public class Paddle implements Runnable{
9
10 int x, y, yDirection, id;
11
12 Rectangle paddle;
13
14 public Paddle(int x, int y, int id){
15 this.x = x;
16 this.y = y;
17 this.id = id;
18 paddle = new Rectangle(x, y, 10, 50);
19 }
20
21 public void keyPressed(KeyEvent e) {
22 switch(id) {
23 default:
24 System.out.println("Please enter a Valid ID in paddle contructor");
25 break;
26 case 1:
27 if(e.getKeyCode() == KeyEvent.VK_W) {
28 setYDirection(-1);
29 if(e.getKeyCode() == KeyEvent.VK_S) {
30 setYDirection(1);
31 }
32 break;
33 }
34 case 2:
35 if(e.getKeyCode() == KeyEvent.VK_UP) {
36 setYDirection(-1);
37 }

https://gist.github.com/hayate891/b09670e980ad02737ad6892c98e7e770 3/7
30/09/2021 14:56 Code for a Java Pong game · GitHub

38 if(e.getKeyCode() == KeyEvent.VK_DOWN) {
39 setYDirection(1);
40 }
41 break;
42 }
43 }
44
45 public void keyReleased(KeyEvent e) {
46 switch(id) {
47 default:
48 System.out.println("Please enter a Valid ID in paddle contructor");
49 break;
50 case 1:
51 if(e.getKeyCode() == e.VK_UP) {
52 setYDirection(0);
53 }
54 if(e.getKeyCode() == e.VK_DOWN) {
55 setYDirection(0);
56 }
57 break;
58 case 2:
59
60 if(e.getKeyCode() == e.VK_W) {
61 setYDirection(0);
62 }
63 if(e.getKeyCode() == e.VK_S) {
64 setYDirection(0);
65 }
66 break;
67 }
68 }
69 public void setYDirection(int yDir) {
70 yDirection = yDir;
71 }
72
73 public void move() {
74 paddle.y += yDirection;
75 if (paddle.y <= 15)
76 paddle.y = 15;
77 if (paddle.y >= 340);
78 paddle.y = 340;
79 }
80 public void draw(Graphics g) {
81 switch(id) {
82 default:
83 System.out.println("Please enter a Valid ID in paddle contructor");
84 break;
85 case 1:
86 g.setColor(Color.CYAN);
87 g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
88 break;
89 case 2:

https://gist.github.com/hayate891/b09670e980ad02737ad6892c98e7e770 4/7
30/09/2021 14:56 Code for a Java Pong game · GitHub

90 g.setColor(Color.pink);
91 g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
92 break;
93 }
94 }
95 @Override
96 public void run() {
97 try {
98 while(true) {
99 move();
100 Thread.sleep(7);
101 }
102 } catch(Exception e) { System.err.println(e.getMessage()); }
103 }
104
105
106
107
108 }

Pong.java

1 package pongGame;
2
3
4 import java.awt.*;
5 import java.awt.event.KeyAdapter;
6 import java.awt.event.KeyEvent;
7 import javax.swing.*;
8
9
10 public class Pong extends JFrame {
11
12 //screen size variables.
13 int gWidth = 500;
14 int gHeight = 400;
15 Dimension screenSize = new Dimension(gWidth, gHeight);
16
17 Image dbImage;
18 Graphics dbGraphics;
19
20 //ball object
21 static Ball b = new Ball(250, 200);
22
23
24 //constructor for window
25 public Pong() {
26 this.setTitle("Pong!");
27 this.setSize(screenSize);
28 this.setResizable(false);
29 this.setVisible(true);
30 this.setBackground(Color.DARK_GRAY);

https://gist.github.com/hayate891/b09670e980ad02737ad6892c98e7e770 5/7
30/09/2021 14:56 Code for a Java Pong game · GitHub

31 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32 this.addKeyListener(new AL());
33 }
34
35 public static void main(String[] args) {
36 Pong pg = new Pong();
37
38 //create and start threads.
39 Thread ball = new Thread(b);
40 ball.start();
41 Thread p1 = new Thread(b.p1);
42 Thread p2 = new Thread(b.p2);
43 p2.start();
44 p1.start();
45
46 }
47
48 @Override
49 public void paint(Graphics g) {
50 dbImage = createImage(getWidth(), getHeight());
51 dbGraphics = dbImage.getGraphics();
52 draw(dbGraphics);
53 g.drawImage(dbImage, 0, 0, this);
54 }
55
56 public void draw(Graphics g) {
57 b.draw(g);
58 b.p1.draw(g);
59 b.p2.draw(g);
60
61 g.setColor(Color.WHITE);
62 g.drawString(""+b.p1score, 15, 20);
63 g.drawString(""+b.p2score, 385, 20);
64
65 repaint();
66 }
67
68 public class AL extends KeyAdapter {
69 @Override
70 public void keyPressed(KeyEvent e) {
71 b.p1.keyPressed(e);
72 b.p2.keyPressed(e);
73 }
74 @Override
75 public void keyReleased(KeyEvent e) {
76 b.p1.keyReleased(e);
77 b.p2.keyReleased(e);
78 }
79
80 }
81 }

https://gist.github.com/hayate891/b09670e980ad02737ad6892c98e7e770 6/7
30/09/2021 14:56 Code for a Java Pong game · GitHub


mjstenbe
commented
on 8 Feb 2019

Nice code, but they paddles do not move? Any fix for this?


charlesbastos
commented
on 11 Jun 2020

Paddle.java

line 33 this } move to line 29.

public void move() {

line 77 remove "; "


DerWeiseILL
commented
on 3 Jan

edited

How can i code, that when the Ball hits the side the other gets a point?

https://gist.github.com/hayate891/b09670e980ad02737ad6892c98e7e770 7/7

You might also like