/
Game.java
executable file
·207 lines (170 loc) · 7.47 KB
/
Game.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class Game {
// screen size
public static final int X_MAX = 600;
public static final int Y_MAX = 400;
private Paddle humanPaddle;
private Paddle computerPaddle;
private Ball ball;
// score counters
private int humanScore = 0;
private int computerScore = 0;
// Constructor method for the game
public Game() {
humanPaddle = new Paddle(0, 155);
computerPaddle = new Paddle(575, 155);
ball = new Ball(290, 190);
}
// overall screen outlook
public void paint(Graphics g) {
this.ball.paint(g);
this.computerPaddle.paint(g);
this.humanPaddle.paint(g);
Font font = new Font("Arial", Font.BOLD, 15);
g.setFont(font);
g.setColor(Color.cyan);
g.drawString("Human: " + humanScore, 50,380);
g.drawString("Computer: " + computerScore, 450,380);
}
// Following method displays appropriate score of each player on the screen
public void printScore(Graphics g) {
if (this.ball.getX() < 0) { // out of screen (left)
computerScore ++;
} else if (this.ball.getX() > 600) { // out of screen (right)
humanScore ++;
}
g.drawString("Human Player : ", 10, 10);
g.drawString("" + this.humanScore, 30, 10);
g.drawString("Computer : ", 40, 10);
g.drawString("" + this.computerScore, 60, 10);
}
/** Update method that runs every 5 milliseconds
* @param change - Represents the change in the ball's position per 5 milliseconds
*/
public void update(int change){
// computer paddle y-values (top and bottom)
int computerPaddleRange1 = computerPaddle.getY();
int computerPaddleRange2 = computerPaddle.getY() + computerPaddle.getLength();
// human paddle y-values (top and bottom)
int humanPaddleRange1 = humanPaddle.getY();
int humanPaddleRange2 = humanPaddle.getY() + humanPaddle.getLength();
// ball y-values (top and bottom)
int ballRange1 = ball.getY();
int ballRange2 = ball.getY() + ball.getSize();
// checks to see if the ball has hit the computer paddle or not
if ((ball.getX() + ball.getSize() == 575)) {
if ((ballRange1 >= computerPaddleRange1) && (ballRange1 <= computerPaddleRange2)
|| (ballRange2 >= computerPaddleRange1) && (ballRange2 <= computerPaddleRange2)){
ball.setDirectionX(0); // ball moves left
}
// checks to see if the ball has hit the player paddle or not
} else if (ball.getX() == 25) {
if ((ballRange1 >= humanPaddleRange1) && (ballRange1 <= humanPaddleRange2) || (ballRange2 >= humanPaddleRange1) && (ballRange2 <= humanPaddleRange2)) {
ball.setDirectionX(1); // right
}
} else if (ball.getX() == 600) {
ball.setDirectionX(1); //right
humanScore++;
ball.setX(290);
ball.setY(190);
System.out.println("Computer: " + this.computerScore + " Human: " + this.humanScore);
} else if (ball.getX() == -20) {
ball.setDirectionX(0); // left
computerScore ++;
ball.setX(290);
ball.setY(190);
System.out.println("Computer: " + this.computerScore + " Human: " + this.humanScore);
}
// if the ball touches bottom of the compuuter paddle
if (ball.getY() > computerPaddle.getY() + 90 && computerPaddle.getX() < (ball.getX() + 20)) {
ball.setDirectionY(3); // down
// if the ball touches top of the user paddle
} else if (ball.getY() == humanPaddle.getY() + 90 && ball.getX() < (humanPaddle.getX() + 25)) {
ball.setDirectionY(3); // down
// if the ball touches top of the compuuter paddle ...
} else if (ball.getY() + 20 < computerPaddle.getY() && computerPaddle.getX() < (ball.getX() + 20)) {
ball.setDirectionY(2); // up
// if the ball touches borrom of the user paddle
} else if (ball.getY() + 20 == humanPaddle.getY() && ball.getX() < (humanPaddle.getX() + 25)) {
ball.setDirectionY(2); // up
System.out.println("this is 4!");
}
// upon hitting the top and bottom walls...
if (ball.getY() + ball.getSize() >= 400) {
ball.setDirectionY(2); // up
} if (ball.getY() <= 0) {
ball.setDirectionY(3); // down
}
// setting the ball moment
if (this.computerScore < 2 && this.humanScore < 2) {
if(ball.getDirectionX() == 0){ // right
ball.setX(ball.getX() - change);
} else {
ball.setX(ball.getX() + change);
}
if (ball.getDirectionY() == 2) { // up
ball.setY(ball.getY() - change);
} else {
ball.setY(ball.getY() + change);
}
}
/* Controlling the moment og the computer paddle
Computer paddle starts to move when the ball reaches half way point. */
if (this.ball.getX() > 300) {
// if paddle is within the same x-region as the ball ...
if (this.computerPaddle.getY() <= this.ball.getY() + 20 &&
this.ball.getY() <= this.computerPaddle.getY() + 90) {
if (this.ball.getDirectionY() == 2) { // up
this.computerPaddle.movePaddle((-1) * change); // paddle goes up
} else if (this.ball.getDirectionY() == 3) { // down
this.computerPaddle.movePaddle(change); // paddle goes down
}
// ball top, computer paddle bottom ...
} else if (this.computerPaddle.getY() > this.ball.getY() + 20) {
this.computerPaddle.movePaddle((-1)*change); // paddle goes up
System.out.println("!!!!!1");
// ball bottom, computer paddle top ...
} else if (this.computerPaddle.getY() + 90 < this.ball.getY()) {
this.computerPaddle.movePaddle(change); // paddle goes down
System.out.println("!!!!! 2");
}
}
}
/** mutator method for the y-coordinate of the paddle
* @param changeY - delta value to move the paddle in y direction
*/
public void humanPaddleUpdate(int changeY) {
this.humanPaddle.movePaddle(changeY);
}
/** Accessor method for the computer score
* @return computer score
*/
public int getComputerScore() {
return this.computerScore;
}
/** Accessor method for the user score
* @return user score
*/
public int getHumanScore () {
return this.humanScore;
}
/** Mutator method for the computer's score
* @param score - computer's score
*/
public void setComputerScore(int score) {
this.computerScore = score;
}
/** Mutator method for the user's score
* @param score - user's score
*/
public void setHumanScore(int score) {
this.humanScore = score;
}
}