鱼C论坛

 找回密码
 立即注册
查看: 965|回复: 3

[已解决]如何实现javafx 弹球游戏

[复制链接]
发表于 2022-6-21 11:00:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
目标:1.当球接触到窗口边界回弹 2.当球接触到黑色长方体回弹
已实现:球接触到窗口回弹
未完成:当球接触到黑色长方体回弹
问题:如何让球与长方体接触后回弹,我尝试过了好多种方法但都不理想
以下是代码
package pong;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;


public class Pong extends Application {
        private double x, y, dx, dy;

        @Override
        public void start(Stage stage) {
                Group root = new Group();
                Scene scene = new Scene(root, 900, 700);

                Circle orb = new Circle(10, Color.BLACK);

                x = 450;
                y = 350;
                dx=5;
                dy=5;
               
                orb.relocate(450, 350);
               
               
               
                Rectangle rect = new Rectangle(80,450,Color.BLACK);
               

                rect.relocate(200,250);
               
               
               

                root.getChildren().add(orb);
                root.getChildren().add(rect);

                stage.setTitle("Pong");
                stage.setScene(scene);
                stage.show();

                new AnimationTimer() {
                        @Override
                        public void handle(long now) {
                            x += dx;
                            y += dy;
                           
                            if ( x < 0 || x> scene.getWidth() ){
                                dx = -dx;
                            }
                            if ( y < 0|| y>scene.getHeight()){
                                dy= -dy;
                            }
                           
                        
                            orb.relocate(x, y);
                           
                        }
                }.start();
        }

        public static void main( String[] args ) { launch(args); }
}
最佳答案
2022-6-21 16:22:04
  1. package pong;

  2. import javafx.animation.AnimationTimer;
  3. import javafx.application.Application;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.paint.Color;
  7. import javafx.scene.shape.Rectangle;
  8. import javafx.stage.Stage;
  9. import javafx.scene.shape.Circle;


  10. public class Pong extends Application {
  11.         private double x, y, dx, dy;
  12.         private double rWidth, rHeight, rx, ry;

  13.         @Override
  14.         public void start(Stage stage) {
  15.                 Group root = new Group();
  16.                 Scene scene = new Scene(root, 900, 700);

  17.                 Circle orb = new Circle(10, Color.BLACK);

  18.                 x = 450;
  19.                 y = 350;
  20.                 dx=5;
  21.                 dy=5;
  22.                
  23.                 orb.relocate(450, 350);
  24.                
  25.                
  26.                 rWidth = 80;
  27.                 rHeight = 450;
  28.                 rx = 200;
  29.                 ry = 250;
  30.                 Rectangle rect = new Rectangle(rWidth,rHeight,Color.BLACK);
  31.                

  32.                 rect.relocate(rx,ry);
  33.                
  34.                
  35.                

  36.                 root.getChildren().add(orb);
  37.                 root.getChildren().add(rect);

  38.                 stage.setTitle("Pong");
  39.                 stage.setScene(scene);
  40.                 stage.show();

  41.                 new AnimationTimer() {
  42.                         @Override
  43.                         public void handle(long now) {
  44.                             x += dx;
  45.                             y += dy;
  46.                            
  47.                             if ( x < 0 || x> scene.getWidth() ){
  48.                                 dx = -dx;
  49.                             }
  50.                             if ( y < 0|| y>scene.getHeight()){
  51.                                 dy= -dy;
  52.                             }
  53.                            
  54.                         
  55.                             orb.relocate(x, y);
  56.                            
  57.                         }
  58.                 }.start();
  59.                 new AnimationTimer() {
  60.                     @Override
  61.                     public void handle(long now) {
  62.                         x += dx;
  63.                         y += dy;
  64.                        
  65.                         if (x == rWidth+rx && (y > ry && y < rHeight+rx)){
  66.                             dx = -dx;
  67.                         }
  68.                         if (x == rx && (y > ry && y < rHeight+rx)) {
  69.                                 dx = -dx;
  70.                         }
  71.                         if (y == ry && (x >= rx && x <= rWidth+rx)) {
  72.                                 dy = -dy;
  73.                         }
  74.                         if (y == rHeight+rx && (x >= rx && x <= rWidth+rx)) {
  75.                                 dy = -dy;
  76.                         }
  77.                        
  78.                     
  79.                         orb.relocate(x, y);
  80.                        
  81.                     }
  82.             }.start();
  83.         }

  84.         public static void main( String[] args ) { launch(args); }
  85. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-6-21 16:22:04 | 显示全部楼层    本楼为最佳答案   
  1. package pong;

  2. import javafx.animation.AnimationTimer;
  3. import javafx.application.Application;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.paint.Color;
  7. import javafx.scene.shape.Rectangle;
  8. import javafx.stage.Stage;
  9. import javafx.scene.shape.Circle;


  10. public class Pong extends Application {
  11.         private double x, y, dx, dy;
  12.         private double rWidth, rHeight, rx, ry;

  13.         @Override
  14.         public void start(Stage stage) {
  15.                 Group root = new Group();
  16.                 Scene scene = new Scene(root, 900, 700);

  17.                 Circle orb = new Circle(10, Color.BLACK);

  18.                 x = 450;
  19.                 y = 350;
  20.                 dx=5;
  21.                 dy=5;
  22.                
  23.                 orb.relocate(450, 350);
  24.                
  25.                
  26.                 rWidth = 80;
  27.                 rHeight = 450;
  28.                 rx = 200;
  29.                 ry = 250;
  30.                 Rectangle rect = new Rectangle(rWidth,rHeight,Color.BLACK);
  31.                

  32.                 rect.relocate(rx,ry);
  33.                
  34.                
  35.                

  36.                 root.getChildren().add(orb);
  37.                 root.getChildren().add(rect);

  38.                 stage.setTitle("Pong");
  39.                 stage.setScene(scene);
  40.                 stage.show();

  41.                 new AnimationTimer() {
  42.                         @Override
  43.                         public void handle(long now) {
  44.                             x += dx;
  45.                             y += dy;
  46.                            
  47.                             if ( x < 0 || x> scene.getWidth() ){
  48.                                 dx = -dx;
  49.                             }
  50.                             if ( y < 0|| y>scene.getHeight()){
  51.                                 dy= -dy;
  52.                             }
  53.                            
  54.                         
  55.                             orb.relocate(x, y);
  56.                            
  57.                         }
  58.                 }.start();
  59.                 new AnimationTimer() {
  60.                     @Override
  61.                     public void handle(long now) {
  62.                         x += dx;
  63.                         y += dy;
  64.                        
  65.                         if (x == rWidth+rx && (y > ry && y < rHeight+rx)){
  66.                             dx = -dx;
  67.                         }
  68.                         if (x == rx && (y > ry && y < rHeight+rx)) {
  69.                                 dx = -dx;
  70.                         }
  71.                         if (y == ry && (x >= rx && x <= rWidth+rx)) {
  72.                                 dy = -dy;
  73.                         }
  74.                         if (y == rHeight+rx && (x >= rx && x <= rWidth+rx)) {
  75.                                 dy = -dy;
  76.                         }
  77.                        
  78.                     
  79.                         orb.relocate(x, y);
  80.                        
  81.                     }
  82.             }.start();
  83.         }

  84.         public static void main( String[] args ) { launch(args); }
  85. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-21 23:23:52 | 显示全部楼层
感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-6-21 23:39:14 | 显示全部楼层

但小球在底部会穿过长方体
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-30 20:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表