Skip to content

Commit 3a0a6aa

Browse files
authored
Add files via upload
1 parent a0e012d commit 3a0a6aa

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

src/Games/Pong/Ball.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Games.Pong;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import java.awt.Rectangle;
5+
6+
public class Ball {
7+
private int x, y, xVelocity, yVelocity, diameter;
8+
9+
public Ball(int x, int y, int diameter) {
10+
this.x = x;
11+
this.y = y;
12+
this.diameter = diameter;
13+
this.xVelocity = 2;
14+
this.yVelocity = 2;
15+
}
16+
17+
public void move() {
18+
x += xVelocity;
19+
y += yVelocity;
20+
21+
if (x <= 0 || x >= 780) {
22+
xVelocity = -xVelocity;
23+
}
24+
if (y <= 0 || y >= 580) {
25+
yVelocity = -yVelocity;
26+
}
27+
}
28+
29+
public void draw(Graphics g) {
30+
g.setColor(Color.WHITE);
31+
g.fillOval(x, y, diameter, diameter);
32+
}
33+
34+
public void checkCollision(Paddle paddle) {
35+
if (new Rectangle(x, y, diameter, diameter).intersects(paddle.getBounds())) {
36+
xVelocity = -xVelocity;
37+
}
38+
}
39+
public boolean isLeftOut() {
40+
return x <= 0;
41+
}
42+
public boolean isRightOut() {
43+
return x >= 780;
44+
}
45+
46+
}
47+

src/Games/Pong/GamePanel.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package Games.Pong;
2+
import javax.swing.JPanel;
3+
import java.awt.Graphics;
4+
import java.awt.Color;
5+
import java.awt.event.KeyAdapter;
6+
import java.awt.event.KeyEvent;
7+
import javax.swing.Timer;
8+
import java.awt.event.ActionEvent;
9+
import java.awt.event.ActionListener;
10+
11+
public class GamePanel extends JPanel implements ActionListener {
12+
private Ball ball;
13+
private Paddle leftPaddle;
14+
private Paddle rightPaddle;
15+
private Timer timer;
16+
private int leftScore = 0;
17+
private int rightScore = 0;
18+
19+
public GamePanel() {
20+
setBackground(Color.BLACK);
21+
setFocusable(true);
22+
addKeyListener(new KeyAdapter() {
23+
@Override
24+
public void keyPressed(KeyEvent e) {
25+
leftPaddle.keyPressed(e);
26+
rightPaddle.keyPressed(e);
27+
}
28+
@Override
29+
public void keyReleased(KeyEvent e) {
30+
leftPaddle.keyReleased(e);
31+
rightPaddle.keyReleased(e);
32+
}
33+
});
34+
35+
ball = new Ball(400, 300, 20);
36+
leftPaddle = new Paddle(30, 250, 10, 100, KeyEvent.VK_W, KeyEvent.VK_S);
37+
rightPaddle = new Paddle(760, 250, 10, 100, KeyEvent.VK_UP, KeyEvent.VK_DOWN);
38+
39+
timer = new Timer(5, this);
40+
timer.start();
41+
}
42+
43+
@Override
44+
protected void paintComponent(Graphics g) {
45+
super.paintComponent(g);
46+
ball.draw(g);
47+
leftPaddle.draw(g);
48+
rightPaddle.draw(g);
49+
50+
g.setColor(Color.WHITE);
51+
g.drawString("Score: " + leftScore, 100, 50);
52+
g.drawString("Score: " + rightScore, 600, 50);
53+
}
54+
55+
private void resetBall() {
56+
ball = new Ball(400, 300, 20);
57+
}
58+
59+
@Override
60+
public void actionPerformed(ActionEvent e) {
61+
ball.move();
62+
if (ball.isLeftOut()) {
63+
rightScore++;
64+
resetBall();
65+
} else if (ball.isRightOut()) {
66+
leftScore++;
67+
resetBall();
68+
}
69+
ball.checkCollision(leftPaddle);
70+
ball.checkCollision(rightPaddle);
71+
leftPaddle.move();
72+
rightPaddle.move();
73+
repaint();
74+
}
75+
}

src/Games/Pong/Paddle.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Games.Pong;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import java.awt.Rectangle;
5+
import java.awt.event.KeyEvent;
6+
7+
public class Paddle {
8+
private int x, y, width, height, yVelocity;
9+
private boolean upPressed, downPressed;
10+
private int upKey, downKey;
11+
12+
public Paddle(int x, int y, int width, int height, int upKey, int downKey) {
13+
this.x = x;
14+
this.y = y;
15+
this.width = width;
16+
this.height = height;
17+
this.upKey = upKey;
18+
this.downKey = downKey;
19+
}
20+
21+
public void draw(Graphics g) {
22+
g.setColor(Color.WHITE);
23+
g.fillRect(x, y, width, height);
24+
}
25+
26+
public Rectangle getBounds() {
27+
return new Rectangle(x, y, width, height);
28+
}
29+
30+
public void keyPressed(KeyEvent e) {
31+
if (e.getKeyCode() == upKey) {
32+
upPressed = true;
33+
} else if (e.getKeyCode() == downKey) {
34+
downPressed = true;
35+
}
36+
}
37+
38+
public void keyReleased(KeyEvent e) {
39+
if (e.getKeyCode() == upKey) {
40+
upPressed = false;
41+
} else if (e.getKeyCode() == downKey) {
42+
downPressed = false;
43+
}
44+
}
45+
46+
public void move() {
47+
if (upPressed && y > 0) {
48+
y -= 5;
49+
}
50+
if (downPressed && y < 500) {
51+
y += 5;
52+
}
53+
}
54+
}

src/Games/Pong/PongGame.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Games.Pong;
2+
import java.awt.Toolkit;
3+
4+
import javax.swing.JFrame;
5+
6+
public class PongGame extends JFrame {
7+
public PongGame() {
8+
setTitle("Pong Game");
9+
setSize(800, 640);
10+
setIconImage(Toolkit.getDefaultToolkit().getImage(Games.Pong.PongGame.class.getResource("/Games/Pong/icon/PongIcon.png")));
11+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12+
setLocationRelativeTo(null);
13+
14+
GamePanel panel = new GamePanel();
15+
add(panel);
16+
}
17+
18+
public static void main(String[] args) {
19+
PongGame game = new PongGame();
20+
game.setVisible(true);
21+
}
22+
}

src/Games/Pong/icon/PongIcon.png

8.45 KB
Loading

0 commit comments

Comments
 (0)