Repository navigation
Expand file tree
/
Copy pathGame.java
More file actions
114 lines (110 loc) · 4.25 KB
/
Copy pathGame.java
File metadata and controls
114 lines (110 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//positions are counted from the center of the ball and paddle
// Game will control the rules of the game
// it extends Pong to get the access to game's elements
// NOTE: semantically Pong is actually a type of game
// but here it is just used to get the Pong's fields
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.lang.Math;
public class Game{
private final double maxAngle = 75;
private Ball pongBall;
private Paddle leftPaddle;
private Paddle rightPaddle;
private int leftScore = 0;
private int rightScore = 0;
public Game(Ball _ball, Paddle _leftP, Paddle _rightP) {
this.pongBall = _ball;
this.leftPaddle = _leftP;
this.rightPaddle = _rightP;
}
private boolean isLeftBounced() {
if( (this.leftPaddle.getPosition().x + Paddle.WIDTH/2 + 10 >= this.pongBall.getPosition().x - Ball.RADIUS) &&
(this.leftPaddle.getPosition().y - Paddle.HEIGHT/2 < this.pongBall.getPosition().y + Ball.RADIUS &&
this.leftPaddle.getPosition().y + Paddle.HEIGHT/2 > this.pongBall.getPosition().y - Ball.RADIUS) ){
return true;
}
else {
return false;
}
}
private boolean isRightBounced() {
if( (this.rightPaddle.getPosition().x - Paddle.WIDTH/2 - 10 <= this.pongBall.getPosition().x + Ball.RADIUS) &&
(this.rightPaddle.getPosition().y - Paddle.HEIGHT/2 < this.pongBall.getPosition().y + Ball.RADIUS &&
this.rightPaddle.getPosition().y + Paddle.HEIGHT/2 > this.pongBall.getPosition().y - Ball.RADIUS) ){
return true;
}
else {
return false;
}
}
private void reflect() {
if(this.pongBall.getPosition().y <= Ball.RADIUS ||
this.pongBall.getPosition().y >= (WindowInfo.WINDOW_HEIGHT - Ball.RADIUS)) {
this.pongBall.setVelocity(new Pair(this.pongBall.getVelocity().x, (-1)*this.pongBall.getVelocity().y));
}
}
//this method is only used if the ball touched the paddle already
private Pair findBouncedVelocity() {
double verticalDistance;
if (this.isLeftBounced()) {
verticalDistance = this.pongBall.getPosition().y - (this.leftPaddle.getPosition().y - Paddle.HEIGHT/2 - Ball.RADIUS);
}
else {
verticalDistance = this.pongBall.getPosition().y - (this.rightPaddle.getPosition().y - Paddle.HEIGHT/2 - Ball.RADIUS);
}
double maxDistance = Paddle.HEIGHT + Ball.RADIUS*2;
double theta = (verticalDistance * this.maxAngle*2 / maxDistance) - this.maxAngle;
double velocityX;
double velocityY = Math.sin(Math.toRadians(theta)) * Ball.INITIAL_SPEED;
if (this.pongBall.getVelocity().x > 0) {
velocityX = (-1)*Math.cos(Math.toRadians(theta)) * Ball.INITIAL_SPEED;
}
else {
velocityX = Math.cos(Math.toRadians(theta)) * Ball.INITIAL_SPEED;
}
System.out.println(theta);
return new Pair(velocityX, velocityY);
}
public void bounce() {
this.reflect();
if(this.isRightBounced() == true || this.isLeftBounced() == true) {
this.pongBall.setVelocity(this.findBouncedVelocity());
}
}
private void scoreUpdate() {
if(this.pongBall.whereDied() == 'l') {
this.rightScore++;
this.pongBall.startOver();
}
if(this.pongBall.whereDied() == 'r') {
this.leftScore++;
this.pongBall.startOver();
}
}
public void drawScore(Graphics g) {
this.scoreUpdate();
if(this.leftScore >= 10) {
g.setColor(new Color(31, 115, 145));
g.fillRect(0, 0, (int) WindowInfo.WINDOW_WIDTH, (int) WindowInfo.WINDOW_HEIGHT);
g.setColor(Color.BLUE);
g.setFont(new Font("Monospaced", Font.PLAIN, 40));
g.drawString("Left player wins", (int)WindowInfo.WINDOW_WIDTH/2 - 100, (int)WindowInfo.WINDOW_HEIGHT/2 - 50);
}
else if(this.rightScore >= 10) {
g.setColor(new Color(31, 115, 145));
g.fillRect(0, 0, (int) WindowInfo.WINDOW_WIDTH, (int) WindowInfo.WINDOW_HEIGHT);
g.setColor(Color.BLUE);
g.setFont(new Font("Monospaced", Font.PLAIN, 40));
g.drawString("Right player wins", (int)WindowInfo.WINDOW_WIDTH/2 - 100, (int)WindowInfo.WINDOW_HEIGHT/2 - 50);
}
else {
g.setColor(new Color(211, 46, 202, 127));
g.fillRect((int)WindowInfo.WINDOW_WIDTH/2 - 75, (int)WindowInfo.WINDOW_HEIGHT - 100, 150, 100);
g.setColor(new Color(66, 244, 215));
g.setFont(new Font("Monospaced", Font.PLAIN, 40));
g.drawString(this.leftScore + " " + this.rightScore, (int)WindowInfo.WINDOW_WIDTH/2 - 35, (int)WindowInfo.WINDOW_HEIGHT - 50);
}
}
}