Skip to content

Commit e71ae97

Browse files
author
Tanel Vari
committed
2 player game works! yay
1 parent e26bca5 commit e71ae97

File tree

1 file changed

+77
-22
lines changed

1 file changed

+77
-22
lines changed

src/Pinks.java

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import javafx.animation.AnimationTimer;
44
import javafx.application.Application;
5-
import javafx.beans.property.SimpleIntegerProperty;
65
import javafx.event.ActionEvent;
76
import javafx.event.EventHandler;
87
import javafx.geometry.Insets;
@@ -14,9 +13,6 @@
1413
import javafx.scene.control.Button;
1514
import javafx.scene.control.Label;
1615
import javafx.scene.input.KeyEvent;
17-
import javafx.scene.input.MouseEvent;
18-
import javafx.scene.layout.BorderPane;
19-
import javafx.scene.layout.HBox;
2016
import javafx.scene.layout.VBox;
2117
import javafx.scene.paint.Color;
2218
import javafx.scene.shape.Line;
@@ -29,8 +25,6 @@
2925
import java.util.Random;
3026
import java.io.InputStream;
3127

32-
import static com.sun.corba.se.impl.util.Version.asString;
33-
3428
public class Pinks extends Application {
3529

3630
private static final Boolean DEBUG = true;
@@ -47,6 +41,7 @@ public class Pinks extends Application {
4741

4842
private static final double PADDLE_WIDTH = BASE_UNIT;
4943
private static final double PADDLE_HEIGHT = BASE_UNIT * 8;
44+
private static final double PC_PADDLE_VELOCITY = BALL_SIZE / 8;
5045

5146
private static final double MARGIN = 2 * BASE_UNIT + PADDLE_WIDTH;
5247
private static final double MARGIN_LEFT = MARGIN;
@@ -59,8 +54,8 @@ public class Pinks extends Application {
5954
private Font myLabelFont;
6055

6156
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;
6459

6560
private Sprite ball;
6661
private Sprite leftPaddle;
@@ -105,8 +100,8 @@ public void start(Stage primaryStage) {
105100

106101
graphicsContext = canvas.getGraphicsContext2D();
107102

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);
110105

111106
double minVeloX = BALL_SIZE / 2 * 0.85;
112107
double maxVeloX = BALL_SIZE / 2 * 1.1;
@@ -166,6 +161,7 @@ public void handle(KeyEvent e) {
166161
if (!inputKeys.contains(code)) {
167162
inputKeys.add(code);
168163
}
164+
//System.out.println("Key code pressed: " + code);
169165
}
170166
}
171167
}
@@ -190,6 +186,7 @@ public void handle(long arg) {
190186

191187
if (leftScore >= WINNING_SCORE || rightScore >= WINNING_SCORE){
192188
currentGameState = GameState.RESULT;
189+
UpdateLeftPaddleVelocity(PC_PADDLE_VELOCITY);
193190
}
194191

195192
switch (currentGameState){
@@ -262,14 +259,45 @@ private VBox CreateMenuBox(){
262259
computerPlayButton.setOnAction(new EventHandler<ActionEvent>() {
263260
@Override
264261
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+
265269
currentGameState = GameState.ONE_PLAYER;
270+
inputKeys.clear();
271+
CenterPaddles();
266272
StartBallFromRightPaddle();
267273
}
268274
});
269275

270276
Button playButton = new Button("2 players");
271277
playButton.setMinWidth(width - (2 * BASE_UNIT));
272278
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+
});
273301

274302
vBox.getChildren().addAll(computerPlayButton, playButton);
275303

@@ -290,13 +318,6 @@ private void ShowMenu(){
290318

291319
private void PlayGame(Boolean isInDemoMode){
292320

293-
if (!isInDemoMode){
294-
menuVBox.setVisible(false);
295-
296-
leftScoreLabel.setVisible(true);
297-
rightScoreLabel.setVisible(true);
298-
}
299-
300321
// update the ball position
301322
ball.updateSpritePosition();
302323

@@ -315,14 +336,29 @@ private void PlayGame(Boolean isInDemoMode){
315336
}
316337
}
317338

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+
318354
// move the left paddle in demo mode or in 2 player game
319355
if (isInDemoMode || currentGameState == GameState.ONE_PLAYER){
320356
double leftPaddleDiff = (leftPaddle.getY() + (PADDLE_HEIGHT / 2)) - (ball.getY() + (BALL_SIZE / 2));
321-
if (leftPaddleDiff > 0 && leftPaddleDiff > computerPaddleVelocity) {
357+
if (leftPaddleDiff > 0 && leftPaddleDiff > leftPaddleVelocity) {
322358
leftPaddle.setVelY(-(Math.abs(leftPaddle.getVelY())));
323359
leftPaddle.updateSpritePosition();
324360
}
325-
else if (leftPaddleDiff < 0 && leftPaddleDiff < -computerPaddleVelocity) {
361+
else if (leftPaddleDiff < 0 && leftPaddleDiff < -leftPaddleVelocity) {
326362
leftPaddle.setVelY(Math.abs(leftPaddle.getVelY()));
327363
leftPaddle.updateSpritePosition();
328364
}
@@ -331,11 +367,11 @@ else if (leftPaddleDiff < 0 && leftPaddleDiff < -computerPaddleVelocity) {
331367
// move the right paddle in demo mode
332368
if (isInDemoMode){
333369
double rightPaddleDiff = (rightPaddle.getY() + (PADDLE_HEIGHT / 2)) - (ball.getY() + (BALL_SIZE / 2));
334-
if (rightPaddleDiff > 0 && rightPaddleDiff > paddleVelocity) {
370+
if (rightPaddleDiff > 0 && rightPaddleDiff > rightPaddleVelocity) {
335371
rightPaddle.setVelY(-(Math.abs(rightPaddle.getVelY())));
336372
rightPaddle.updateSpritePosition();
337373
}
338-
else if (rightPaddleDiff < 0 && rightPaddleDiff < -paddleVelocity) {
374+
else if (rightPaddleDiff < 0 && rightPaddleDiff < -rightPaddleVelocity) {
339375
rightPaddle.setVelY(Math.abs(rightPaddle.getVelY()));
340376
rightPaddle.updateSpritePosition();
341377
}
@@ -362,6 +398,15 @@ else if (ball.getX() > MARGIN_RIGHT - BALL_SIZE) {
362398
if (ball.intersects(leftPaddle)) {
363399
ball.setX(MARGIN_LEFT);
364400
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+
}
365410
}
366411
else if (ball.getX() < MARGIN_LEFT) {
367412
StartBallFromRightPaddle();
@@ -408,7 +453,7 @@ else if (ball.getX() < MARGIN_LEFT) {
408453
}
409454
}
410455

411-
private void StartBallFromLeftPaddle(){
456+
private void StartBallFromLeftPaddle(){
412457
ball.setX(MARGIN_LEFT);
413458
ball.setY(leftPaddle.getY() + (PADDLE_HEIGHT / 2) - (BALL_SIZE / 2));
414459
ball.setVelX(Math.abs(ball.getVelX()));
@@ -421,4 +466,14 @@ private void StartBallFromRightPaddle(){
421466
ball.setVelX(-(Math.abs(ball.getVelX())));
422467
ball.setVelY(ball.getVelY() * randomReverse());
423468
}
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+
}
424479
}

0 commit comments

Comments
 (0)