Перемщение фигуры - Java

Узнай цену своей работы

Формулировка задачи:

Пишу игру пинг понг и столкнулся с проблемой джижения ракетки. Как её решить?Вот мой вариант:
Листинг программы
  1. import javafx.application.Application;
  2. import javafx.scene.shape.*;
  3. import javafx.scene.Parent;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.layout.Pane;
  7. import javafx.animation.KeyFrame;
  8. import javafx.animation.Timeline;
  9. import javafx.util.Duration;
  10. import javafx.scene.input.KeyCode;
  11. public class PingPong extends Application {
  12. Ball ball = new Ball(100, 300);
  13. Player rect = new Player();
  14. @Override
  15. public void start(Stage primaryStage) {
  16. Pane pane = new Pane();
  17. pane.getChildren().addAll(rect, ball);
  18. //----------------------------------------------------------------------
  19. rect.setOnKeyPressed(e -> {
  20. if (e.getCode() == KeyCode.ENTER)
  21. pane.getChildren().clear();
  22. rect.moveUp();
  23. pane.getChildren().addAll(rect, ball);
  24. });
  25. //-----------------------------------------------------------------------
  26. Scene scene = new Scene(pane, 400, 400);
  27. primaryStage.setScene(scene);
  28. primaryStage.show();
  29. }
  30. public class Player extends Rectangle {
  31. private int step = 1;
  32. public Player() {
  33. super(10, 50, 30, 200);
  34. }
  35. public void moveUp() {
  36. rect.setY(rect.getY() + step);
  37. }
  38. }
  39. public class Ball extends Circle {
  40. final static int RADIUS = 15;
  41. private int dx = 1, dy = 1;
  42. private Timeline animation;
  43. public Ball(int x, int y) {
  44. super(x, y,RADIUS);
  45. animation = new Timeline(new KeyFrame(Duration.millis(5), e -> moveBall()));
  46. animation.setCycleCount(Timeline.INDEFINITE);
  47. animation.play();
  48. }
  49.  
  50. protected void moveBall() {
  51. if(ball.getCenterX() < ball.getRadius() || ball.getCenterX() > 400 - ball.getRadius())
  52. dx *=-1;
  53. if(ball.getCenterY() < ball.getRadius() || ball.getCenterY() > 400 - ball.getRadius())
  54. dy *=-1;
  55. if(collideX() && collideY()) {
  56. dx *=-1;
  57. // dy *=-1;
  58. }
  59. ball.setCenterX(ball.getCenterX() + ball.dx);
  60. ball.setCenterY(ball.getCenterY() + ball.dy);
  61. }
  62. public boolean collideX() {
  63. if(ball.getCenterX() - RADIUS < rect.getX() + rect.getWidth() && ball.getCenterX() > rect.getX())
  64. return true;
  65. return false;
  66. }
  67. public boolean collideY() {
  68. if(ball.getCenterY() - RADIUS < rect.getY() + rect.getHeight() && ball.getCenterY() > rect.getY())
  69. return true;
  70. return false;
  71. }
  72. }
  73. }

Решение задачи: «Перемщение фигуры»

textual
Листинг программы
  1. scene.setOnKeyPressed(e -> {
  2.     if (e.getCode() == KeyCode.ENTER)
  3.         pane.getChildren().clear();
  4.         rect.moveUp();
  5.         pane.getChildren().addAll(rect, ball);
  6. });

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут