Guest User

Untitled

a guest
Jan 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. import java.util.Random;
  2. import javafx.animation.KeyFrame;
  3. import javafx.animation.Timeline;
  4. import javafx.application.Application;
  5. import javafx.scene.Scene;
  6. import javafx.scene.canvas.Canvas;
  7. import javafx.scene.canvas.GraphicsContext;
  8. import javafx.scene.layout.StackPane;
  9. import javafx.scene.paint.Color;
  10. import javafx.scene.text.Font;
  11. import javafx.scene.text.TextAlignment;
  12. import javafx.stage.Stage;
  13. import javafx.util.Duration;
  14.  
  15. public class Pong extends Application {
  16.  
  17. private static final int width = 800;
  18. private static final int height = 600;
  19. private static final int PLAYER_HEIGHT = 100;
  20. private static final int PLAYER_WIDTH = 15;
  21. private static final double BALL_R = 15;
  22. private int ballYSpeed = 1;
  23. private int ballXSpeed = 1;
  24. private double playerOneYPos = height / 2;
  25. private double playerTwoYPos = height / 2;
  26. private double ballXPos = width / 2;
  27. private double ballYPos = height / 2;
  28. private int scoreP1 = 0;
  29. private int scoreP2 = 0;
  30. private boolean gameStarted;
  31. private int playerOneXPos = 0;
  32. private double playerTwoXPos = width - PLAYER_WIDTH;
  33.  
  34. public void start(Stage stage) throws Exception {
  35. Canvas canvas = new Canvas(width, height);
  36. GraphicsContext gc = canvas.getGraphicsContext2D();
  37. Timeline tl = new Timeline(new KeyFrame(Duration.millis(10), e -> run(gc)));
  38. tl.setCycleCount(Timeline.INDEFINITE);
  39. canvas.setOnMouseMoved(e -> playerOneYPos = e.getY());
  40. canvas.setOnMouseClicked(e -> gameStarted = true);
  41. stage.setScene(new Scene(new StackPane(canvas)));
  42. stage.show();
  43. tl.play();
  44. }
  45.  
  46. private void run(GraphicsContext gc) {
  47. gc.setFill(Color.BLACK);
  48. gc.fillRect(0, 0, width, height);
  49. gc.setFill(Color.WHITE);
  50. gc.setFont(Font.font(25));
  51. if(gameStarted) {
  52. ballXPos+=ballXSpeed;
  53. ballYPos+=ballYSpeed;
  54. if(ballXPos < width - width / 4) {
  55. playerTwoYPos = ballYPos - PLAYER_HEIGHT / 2;
  56. } else {
  57. playerTwoYPos = ballYPos > playerTwoYPos + PLAYER_HEIGHT / 2 ?playerTwoYPos += 1: playerTwoYPos - 1;
  58. }
  59. gc.fillOval(ballXPos, ballYPos, BALL_R, BALL_R);
  60. } else {
  61. gc.setStroke(Color.YELLOW);
  62. gc.setTextAlign(TextAlignment.CENTER);
  63. gc.strokeText("Click to Start", width / 2, height / 2);
  64. ballXPos = width / 2;
  65. ballYPos = height / 2;
  66. ballXSpeed = new Random().nextInt(2) == 0 ? 1: -1;
  67. ballYSpeed = new Random().nextInt(2) == 0 ? 1: -1;
  68. }
  69. if(ballYPos > height || ballYPos < 0) ballYSpeed *=-1;
  70. if(ballXPos < playerOneXPos - PLAYER_WIDTH) {
  71. scoreP2++;
  72. gameStarted = false;
  73. }
  74. if(ballXPos > playerTwoXPos + PLAYER_WIDTH) {
  75. scoreP1++;
  76. gameStarted = false;
  77. }
  78. if( ((ballXPos + BALL_R > playerTwoXPos) && ballYPos >= playerTwoYPos && ballYPos <= playerTwoYPos + PLAYER_HEIGHT) ||
  79. ((ballXPos < playerOneXPos + PLAYER_WIDTH) && ballYPos >= playerOneYPos && ballYPos <= playerOneYPos + PLAYER_HEIGHT)) {
  80. ballYSpeed += 1 * Math.signum(ballYSpeed);
  81. ballXSpeed += 1 * Math.signum(ballXSpeed);
  82. ballXSpeed *= -1;
  83. ballYSpeed *= -1;
  84. }
  85. gc.fillText(scoreP1 + "\t\t\t\t\t\t\t\t" + scoreP2, width / 2, 100);
  86. gc.fillRect(playerTwoXPos, playerTwoYPos, PLAYER_WIDTH, PLAYER_HEIGHT);
  87. gc.fillRect(playerOneXPos, playerOneYPos, PLAYER_WIDTH, PLAYER_HEIGHT);
  88. }
  89.  
  90. }
Add Comment
Please, Sign In to add comment