2
2
3
3
import javafx .animation .AnimationTimer ;
4
4
import javafx .application .Application ;
5
- import javafx .beans .property .SimpleIntegerProperty ;
6
5
import javafx .event .ActionEvent ;
7
6
import javafx .event .EventHandler ;
8
7
import javafx .geometry .Insets ;
14
13
import javafx .scene .control .Button ;
15
14
import javafx .scene .control .Label ;
16
15
import javafx .scene .input .KeyEvent ;
17
- import javafx .scene .input .MouseEvent ;
18
- import javafx .scene .layout .BorderPane ;
19
- import javafx .scene .layout .HBox ;
20
16
import javafx .scene .layout .VBox ;
21
17
import javafx .scene .paint .Color ;
22
18
import javafx .scene .shape .Line ;
29
25
import java .util .Random ;
30
26
import java .io .InputStream ;
31
27
32
- import static com .sun .corba .se .impl .util .Version .asString ;
33
-
34
28
public class Pinks extends Application {
35
29
36
30
private static final Boolean DEBUG = true ;
@@ -47,6 +41,7 @@ public class Pinks extends Application {
47
41
48
42
private static final double PADDLE_WIDTH = BASE_UNIT ;
49
43
private static final double PADDLE_HEIGHT = BASE_UNIT * 8 ;
44
+ private static final double PC_PADDLE_VELOCITY = BALL_SIZE / 8 ;
50
45
51
46
private static final double MARGIN = 2 * BASE_UNIT + PADDLE_WIDTH ;
52
47
private static final double MARGIN_LEFT = MARGIN ;
@@ -59,8 +54,8 @@ public class Pinks extends Application {
59
54
private Font myLabelFont ;
60
55
61
56
private Random random = new Random (System .currentTimeMillis ());
62
- private double paddleVelocity = BALL_SIZE / 2 ;
63
- private double computerPaddleVelocity = BALL_SIZE / 3 ;
57
+ private double leftPaddleVelocity = PC_PADDLE_VELOCITY ;
58
+ private double rightPaddleVelocity = BALL_SIZE / 2 ;
64
59
65
60
private Sprite ball ;
66
61
private Sprite leftPaddle ;
@@ -105,8 +100,8 @@ public void start(Stage primaryStage) {
105
100
106
101
graphicsContext = canvas .getGraphicsContext2D ();
107
102
108
- leftPaddle = new Sprite (2 * BASE_UNIT , (SCENE_HEIGHT - PADDLE_HEIGHT ) / 2 , PADDLE_WIDTH , PADDLE_HEIGHT , 0 , computerPaddleVelocity );
109
- rightPaddle = new Sprite (SCENE_WIDTH - (2 * BASE_UNIT ) - BALL_SIZE , (SCENE_HEIGHT - PADDLE_HEIGHT ) / 2 , PADDLE_WIDTH , PADDLE_HEIGHT , 0 , paddleVelocity );
103
+ leftPaddle = new Sprite (2 * BASE_UNIT , (SCENE_HEIGHT - PADDLE_HEIGHT ) / 2 , PADDLE_WIDTH , PADDLE_HEIGHT , 0 , leftPaddleVelocity );
104
+ rightPaddle = new Sprite (SCENE_WIDTH - (2 * BASE_UNIT ) - BALL_SIZE , (SCENE_HEIGHT - PADDLE_HEIGHT ) / 2 , PADDLE_WIDTH , PADDLE_HEIGHT , 0 , rightPaddleVelocity );
110
105
111
106
double minVeloX = BALL_SIZE / 2 * 0.85 ;
112
107
double maxVeloX = BALL_SIZE / 2 * 1.1 ;
@@ -166,6 +161,7 @@ public void handle(KeyEvent e) {
166
161
if (!inputKeys .contains (code )) {
167
162
inputKeys .add (code );
168
163
}
164
+ //System.out.println("Key code pressed: " + code);
169
165
}
170
166
}
171
167
}
@@ -190,6 +186,7 @@ public void handle(long arg) {
190
186
191
187
if (leftScore >= WINNING_SCORE || rightScore >= WINNING_SCORE ){
192
188
currentGameState = GameState .RESULT ;
189
+ UpdateLeftPaddleVelocity (PC_PADDLE_VELOCITY );
193
190
}
194
191
195
192
switch (currentGameState ){
@@ -262,14 +259,45 @@ private VBox CreateMenuBox(){
262
259
computerPlayButton .setOnAction (new EventHandler <ActionEvent >() {
263
260
@ Override
264
261
public void handle (ActionEvent e ) {
262
+ menuVBox .setVisible (false );
263
+
264
+ leftScoreLabel .setVisible (true );
265
+ rightScoreLabel .setVisible (true );
266
+
267
+ UpdateLeftPaddleVelocity (PC_PADDLE_VELOCITY );
268
+
265
269
currentGameState = GameState .ONE_PLAYER ;
270
+ inputKeys .clear ();
271
+ CenterPaddles ();
266
272
StartBallFromRightPaddle ();
267
273
}
268
274
});
269
275
270
276
Button playButton = new Button ("2 players" );
271
277
playButton .setMinWidth (width - (2 * BASE_UNIT ));
272
278
playButton .setFont (font );
279
+ playButton .setOnAction (new EventHandler <ActionEvent >() {
280
+ @ Override
281
+ public void handle (ActionEvent event ) {
282
+ menuVBox .setVisible (false );
283
+
284
+ leftScoreLabel .setVisible (true );
285
+ rightScoreLabel .setVisible (true );
286
+
287
+ UpdateLeftPaddleVelocity (rightPaddleVelocity );
288
+
289
+ currentGameState = GameState .TWO_PLAYER ;
290
+ inputKeys .clear ();
291
+ CenterPaddles ();
292
+
293
+ if (random .nextBoolean ()){
294
+ StartBallFromRightPaddle ();
295
+ }
296
+ else {
297
+ StartBallFromRightPaddle ();
298
+ }
299
+ }
300
+ });
273
301
274
302
vBox .getChildren ().addAll (computerPlayButton , playButton );
275
303
@@ -290,13 +318,6 @@ private void ShowMenu(){
290
318
291
319
private void PlayGame (Boolean isInDemoMode ){
292
320
293
- if (!isInDemoMode ){
294
- menuVBox .setVisible (false );
295
-
296
- leftScoreLabel .setVisible (true );
297
- rightScoreLabel .setVisible (true );
298
- }
299
-
300
321
// update the ball position
301
322
ball .updateSpritePosition ();
302
323
@@ -315,14 +336,29 @@ private void PlayGame(Boolean isInDemoMode){
315
336
}
316
337
}
317
338
339
+ if (!isInDemoMode && currentGameState == GameState .TWO_PLAYER ){
340
+ if (inputKeys .contains ("W" )) {
341
+ leftPaddle .setVelY (-(Math .abs (leftPaddle .getVelY ())));
342
+ leftPaddle .updateSpritePosition ();
343
+ }
344
+ }
345
+
346
+ if (!isInDemoMode && currentGameState == GameState .TWO_PLAYER ){
347
+ if (inputKeys .contains ("S" )) {
348
+ leftPaddle .setVelY (Math .abs (leftPaddle .getVelY ()));
349
+ leftPaddle .updateSpritePosition ();
350
+ }
351
+ }
352
+
353
+
318
354
// move the left paddle in demo mode or in 2 player game
319
355
if (isInDemoMode || currentGameState == GameState .ONE_PLAYER ){
320
356
double leftPaddleDiff = (leftPaddle .getY () + (PADDLE_HEIGHT / 2 )) - (ball .getY () + (BALL_SIZE / 2 ));
321
- if (leftPaddleDiff > 0 && leftPaddleDiff > computerPaddleVelocity ) {
357
+ if (leftPaddleDiff > 0 && leftPaddleDiff > leftPaddleVelocity ) {
322
358
leftPaddle .setVelY (-(Math .abs (leftPaddle .getVelY ())));
323
359
leftPaddle .updateSpritePosition ();
324
360
}
325
- else if (leftPaddleDiff < 0 && leftPaddleDiff < -computerPaddleVelocity ) {
361
+ else if (leftPaddleDiff < 0 && leftPaddleDiff < -leftPaddleVelocity ) {
326
362
leftPaddle .setVelY (Math .abs (leftPaddle .getVelY ()));
327
363
leftPaddle .updateSpritePosition ();
328
364
}
@@ -331,11 +367,11 @@ else if (leftPaddleDiff < 0 && leftPaddleDiff < -computerPaddleVelocity) {
331
367
// move the right paddle in demo mode
332
368
if (isInDemoMode ){
333
369
double rightPaddleDiff = (rightPaddle .getY () + (PADDLE_HEIGHT / 2 )) - (ball .getY () + (BALL_SIZE / 2 ));
334
- if (rightPaddleDiff > 0 && rightPaddleDiff > paddleVelocity ) {
370
+ if (rightPaddleDiff > 0 && rightPaddleDiff > rightPaddleVelocity ) {
335
371
rightPaddle .setVelY (-(Math .abs (rightPaddle .getVelY ())));
336
372
rightPaddle .updateSpritePosition ();
337
373
}
338
- else if (rightPaddleDiff < 0 && rightPaddleDiff < -paddleVelocity ) {
374
+ else if (rightPaddleDiff < 0 && rightPaddleDiff < -rightPaddleVelocity ) {
339
375
rightPaddle .setVelY (Math .abs (rightPaddle .getVelY ()));
340
376
rightPaddle .updateSpritePosition ();
341
377
}
@@ -362,6 +398,15 @@ else if (ball.getX() > MARGIN_RIGHT - BALL_SIZE) {
362
398
if (ball .intersects (leftPaddle )) {
363
399
ball .setX (MARGIN_LEFT );
364
400
ball .setVelX (-ball .getVelX ());
401
+ if (currentGameState == GameState .TWO_PLAYER ){
402
+ // if the paddle was still moving opposite direction then reverse the Y angle to give the ball a backward spin
403
+ if (ball .getVelY () > 0 && inputKeys .contains ("W" )) {
404
+ ball .setVelY (-ball .getVelY ());
405
+ }
406
+ else if (ball .getVelY () < 0 && inputKeys .contains ("S" )) {
407
+ ball .setVelY (-ball .getVelY ());
408
+ }
409
+ }
365
410
}
366
411
else if (ball .getX () < MARGIN_LEFT ) {
367
412
StartBallFromRightPaddle ();
@@ -408,7 +453,7 @@ else if (ball.getX() < MARGIN_LEFT) {
408
453
}
409
454
}
410
455
411
- private void StartBallFromLeftPaddle (){
456
+ private void StartBallFromLeftPaddle (){
412
457
ball .setX (MARGIN_LEFT );
413
458
ball .setY (leftPaddle .getY () + (PADDLE_HEIGHT / 2 ) - (BALL_SIZE / 2 ));
414
459
ball .setVelX (Math .abs (ball .getVelX ()));
@@ -421,4 +466,14 @@ private void StartBallFromRightPaddle(){
421
466
ball .setVelX (-(Math .abs (ball .getVelX ())));
422
467
ball .setVelY (ball .getVelY () * randomReverse ());
423
468
}
469
+
470
+ private void CenterPaddles (){
471
+ leftPaddle .setY ((SCENE_HEIGHT - PADDLE_HEIGHT ) / 2 );
472
+ rightPaddle .setY ((SCENE_HEIGHT - PADDLE_HEIGHT ) / 2 );
473
+ }
474
+
475
+ private void UpdateLeftPaddleVelocity (double vel ){
476
+ leftPaddleVelocity = vel ;
477
+ leftPaddle .setVelY (vel );
478
+ }
424
479
}
0 commit comments