Skip to content

Commit f6848e8

Browse files
authored
Add files via upload
1 parent 6efc65d commit f6848e8

File tree

16 files changed

+1144
-0
lines changed

16 files changed

+1144
-0
lines changed
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+
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+
}
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+
}
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+
}
8.45 KB
Loading
4.9 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Games.Snake;
2+
public class snakeAct {
3+
private int x;
4+
private int y;
5+
public int getX() {
6+
return x;
7+
}
8+
public void setX(int x) {
9+
this.x = x;
10+
}
11+
public int getY() {
12+
return y;
13+
}
14+
public void setY(int y) {
15+
this.y = y;
16+
}
17+
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Games.Snake;
2+
import java.awt.Graphics;
3+
import java.awt.Toolkit;
4+
5+
import javax.swing.*;
6+
public class snakeMain extends JFrame {
7+
public snakeMain() {
8+
snakeWin win = new snakeWin();
9+
add(win);
10+
setTitle("Snake");
11+
setIconImage(Toolkit.getDefaultToolkit().getImage(Games.Snake.snakeMain.class.getResource("/Games/Snake/icon/SnakeIcon.png")));
12+
setSize(435,390);
13+
setLocation(200, 200);
14+
setVisible(true);
15+
}
16+
public static void main(String[] args) {
17+
new snakeMain();
18+
}
19+
}

0 commit comments

Comments
 (0)