主頁 >  其他 > JAVA小游戲 混亂大槍戰

JAVA小游戲 混亂大槍戰

2020-12-16 14:35:32 其他

游戲效果
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

類關系如下
在這里插入圖片描述
不用看sample包和sql包,

BaseObject

package JavaBigJob;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Parent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import java.security.Key;

public class BaseObject extends Parent {
    //為了方便動態系結,用Property型別
    //坐標
    private DoubleProperty xProperty = new SimpleDoubleProperty(0);
    private DoubleProperty yProperty = new SimpleDoubleProperty(0);
    //寬,高
    private DoubleProperty widthProperty = new SimpleDoubleProperty(0);
    private DoubleProperty heightProperty = new SimpleDoubleProperty(0);
    private double speed = 1;//初始速度

    public BaseObject() {

//        this.xProperty.bind(super.xProperty());
    }

    public double getxProperty() {
        return xProperty.get();
    }

    public DoubleProperty xProperty() {
        return xProperty;
    }

    public void setxProperty(double xProperty) {
        this.xProperty.set(xProperty);
    }

    public double getyProperty() {
        return yProperty.get();
    }

    public DoubleProperty yProperty() {
        return yProperty;
    }

    public void setyProperty(double yProperty) {
        this.yProperty.set(yProperty);
    }

    public double getWidthProperty() {
        return widthProperty.get();
    }

    public DoubleProperty widthPropertyProperty() {
        return widthProperty;
    }

    public void setWidthProperty(double widthProperty) {
        this.widthProperty.set(widthProperty);
    }

    public double getHeightProperty() {
        return heightProperty.get();
    }

    public DoubleProperty heightPropertyProperty() {
        return heightProperty;
    }

    public void setHeightProperty(double heightProperty) {
        this.heightProperty.set(heightProperty);
    }

    public double getSpeed(){return this.speed;}
    public void setSpeed(double speed){this.speed=speed;}

    public void moveX(double x){
        System.out.println(getxProperty()+" "+x);
        this.setxProperty(getxProperty()+x);
        System.out.println("moveX");
    }
    public void moveY(double y){
        this.setyProperty(this.getyProperty()+y);
    }

    //判斷是否發生矩形碰撞
    public boolean isImpact(BaseObject baseObject){
        if(getxProperty() + getWidthProperty() > baseObject.getxProperty() && getxProperty() < baseObject.getxProperty() + baseObject.getWidthProperty() && getyProperty() + getHeightProperty() > baseObject.getyProperty() && getyProperty() < baseObject.getyProperty() + baseObject.getHeightProperty()){
            return true;
        }
        return false;
    }

    public interface CanMove {//定義一個介面,子類若能移動則需要繼承此介面
        void onKeyBoardPressed(KeyEvent event);
        void onKeyBoardReleased(KeyEvent event);
        static void add(){
            System.out.println("nb");
        }
    }


}

BaseRectangle

平臺類,設定成不可見,匯入BaseRectangle到平臺集合中,

package JavaBigJob;

import javafx.scene.effect.Bloom;
import javafx.scene.effect.BoxBlur;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;


public class BaseRectangle extends BaseObject {
    private Rectangle r = null;
    private BoxBlur boxBlur = null;
    private Bloom bloom = null;

    private double width;
    private double height;

    public BaseRectangle(double width, double height) {
        this.width = width;
        this.height = height;
        r = new Rectangle();
        //矩形與該類實體化的物件進行動態系結
        r.widthProperty().bindBidirectional(this.widthPropertyProperty());
        r.heightProperty().bindBidirectional(this.heightPropertyProperty());
        r.xProperty().bindBidirectional(this.xProperty());
        r.yProperty().bindBidirectional(this.yProperty());

        //矩形寬度和長度
        r.setWidth(width);
        r.setHeight(height);

        //動態系結后把矩形添加
        this.getChildren().addAll(r);
    }

    public BaseRectangle(double width, double height, double Arc1, double Arc2, Color color) {
        this(width,height);
        //設定圓角角度
        r.setArcWidth(Arc1);
        r.setArcHeight(Arc2);
        r.setFill(color);
        //模糊化
        boxBlur = new BoxBlur();
        boxBlur.setWidth(5);
        boxBlur.setHeight(5);
        //動態化
        bloom = new Bloom();
        bloom.setThreshold(50);
        //上面兩種屬性與舉行系結
        r.setEffect(boxBlur);
        r.setEffect(bloom);
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public void setVisible(Boolean b){
        r.setVisible(b);
        this.setVisible(b);
    }
    //    public void onm ouse(MouseEvent event){
        if (event.getX() >= this.getWidthProperty()/2 && event.getX() <= GameWindow.WIDTH - this.getWidthProperty()/2 && event.getY()<=Window.HEIGHT) {
//            System.out.println(event.getX());
//            System.out.println(event.getY());
//            this.setxProperty(event.getX()/* - this.getWidthProperty()/2*/);
//            this.setyProperty(event.getY()/* - this.getHeightProperty()/2*/);
        }
//    }



}

BaseScene

用來加載各種時間線和物體
(時間線:人物跳躍,人物移動,人物下落,人物與平臺的碰撞判定,子彈飛行,子彈碰撞,
物體:兩個人物,背景,平臺)

package JavaBigJob;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.Collections;


//場景類,繼承了Parent
//加載了 回圈時間軸,和各個物體以及場景

public class BaseScene extends Parent {

    public BaseRectangle br;
    Rectangle rectangleSecen;
    public ArrayList<BaseRectangle> brArrayList = new ArrayList<BaseRectangle>();
    static ArrayList<Rectangle> bulletArrayList = new ArrayList<Rectangle>();

    public static Man man1 =  new Man();;
    public static Man2 man2 = new Man2();

    private int width;//場景的寬度長度,用于邊界判斷,
    private int height;
    static Bullet bullet;
    private ReplaceClothes replaceClothes;
    KeyFrame keyFrame;//堆疊幀keyframe,每一幀要執行的事件,執行后通過timeline重繪
//    Timeline timeline;

    //bg為背景版
    public Rectangle bg;
    public ImageView imageView;

    public BaseScene(int width,int height) {
        this.width = width;
        this.height = height;
        initAll();
    }

    public void initBackGround(){
        imageView = new ImageView(new Image("JavaBigJob/Picture/89.png"));
        imageView.setFitWidth(1111);
        imageView.setFitHeight(800);
        bg = new Rectangle(0,0,GameWindow.WIDTH,GameWindow.HEIGHT);
        bg.setFill(Color.BLACK);
        this.getChildren().add(imageView);
    }

    public void initBullet(){
         bullet = new Bullet();
         getChildren().addAll(bullet);
    }


    public void initMan(){
        man1.setxProperty(700);
        man1.setyProperty(100);
        this.getChildren().add(man1);
    }

    public void initMan2(){
        man2.setxProperty(200);
        man2.setyProperty(100);
        this.getChildren().add(man2);
    }

    public void initReplace(){
        //加載換裝頁面,而不是new一個節點;
        replaceClothes = new ReplaceClothes();
    }

    //加載時間軸
    public void initTimeline(){
        moveTimeline();
        Man.dropOutTimeline(brArrayList);
//        Man.jumpTimeline();
        Man2.dropOutTimeline(brArrayList);

//        Man2.jumpTimeline();
    }

    //加載臨時平臺
    public void initTmpPlat(){
        //臨時平臺
        br = new BaseRectangle(320,5,10,10,Color.YELLOW);
        br.setxProperty(710);
        br.setyProperty(525);
        BaseRectangle br2 = new BaseRectangle(420,5,10,10,Color.BLUE);
        br2.setxProperty(350);
        br2.setyProperty(400);
        BaseRectangle br3 = new BaseRectangle(860,5,10,10,Color.GREEN);
        br3.setxProperty(135);
        br3.setyProperty(173);
        BaseRectangle br4 = new BaseRectangle(875,5,10,10,Color.RED);
        br4.setxProperty(150);
        br4.setyProperty(650);
        BaseRectangle br5 = new BaseRectangle(350,5,10,10,Color.GOLD);
        br5.setxProperty(85);
        br5.setyProperty(525);
        BaseRectangle br6 = new BaseRectangle(350,5,10,10,Color.BLACK);
        br6.setxProperty(693);
        br6.setyProperty(525);
        BaseRectangle br7 = new BaseRectangle(350,5,10,10,Color.GOLD);
        br7.setxProperty(70);
        br7.setyProperty(285);
        BaseRectangle br8 = new BaseRectangle(350,5,10,10,Color.BLACK);
        br8.setxProperty(705);
        br8.setyProperty(285);
        //后續改變下面的方法
        Collections.addAll(brArrayList,br,br2,br3,br4,br5,br6,br7,br8);
//        brArrayList.add(br);
//        brArrayList.add(br2);
        this.getChildren().addAll();
    }

    public void initAll(){
        //加載背景
        this.initBackGround();

        //臨時平臺
        this.initTmpPlat();

        //加載物體


        //加載玩家1
        this.initMan();

        //加載玩家2
        this.initMan2();

        this.initBullet();
        //換裝頁面
        this.initReplace();

        //把已加載的背景與物體 添加到游戲場景中
//        this.getChildren().addAll(bg,man1,man2);

        //加載時間線
        this.initTimeline();

    }

    public void moveTimeline(){
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(-1);//無限回圈
        KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {//重復執行該方法
                BaseScene.man1.move();//左右移動and跳躍,墜落
                BaseScene.man2.move();
            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
    }

    public void loadKeyBoard(){//全域鍵盤方法
        //通過按壓,抬起標記人物四個方向上是否移動,然后通過時間軸不斷重繪來判斷標記,進一步執行移動
        this.getScene().setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                System.out.println("Pressed");
                man1.onKeyBoardPressed(event);
                man2.onKeyBoardPressed(event);
                bullet.onKeyBoardPressed(event);
            }
        });
        getScene().setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                System.out.println("Released");
                man1.onKeyBoardReleased(event);
                man2.onKeyBoardReleased(event);
                bullet.onKeyBoardReleased(event);
            }
        });
    }


}

Bullet

package JavaBigJob;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.effect.Bloom;
import javafx.scene.effect.BoxBlur;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

import java.util.*;

import static JavaBigJob.BaseScene.*;


public class Bullet extends BaseObject implements BaseObject.CanMove{
    //    private Rectangle r = null;
    int i=0,j=0;
      int z=0;
     boolean dirctionClock=false;
     boolean dirctionTwoClok=false;
     boolean isOne = false;
    static Rectangle[] rectangles = new Rectangle[100];
    static boolean select = true;
    static boolean selectTwo = true;
    boolean Out = false;
    private BoxBlur boxBlur = null;
    private Bloom bloom = null;
    static boolean shoutOpen = false;
    static boolean shoutOpenTwo = false;
    private double BulletSpeed;
    static boolean isShow = false;
    Rectangle rectangle = null;
    static Rectangle rectangle1 = null;
    ArrayList<Bullet> list = new ArrayList<Bullet>();
    ArrayList<Bullet> list2 = new ArrayList<>();
    public Bullet() {
        Rectangle rectangle=new Rectangle();
        rectangle.setX(man1.gun.getX()+40);
        rectangle.setY(man1.gun.getY());
        rectangle.setFill(Color.PINK);
        rectangle.setWidth(12);
        rectangle.setHeight(12);
        this.setxProperty(120);
        this.setyProperty(120);
        rectangle.setArcWidth(34);
        rectangle.setArcHeight(56);
        BulletSpeed = 3;
//        模糊化
        boxBlur = new BoxBlur();
        boxBlur.setWidth(5);
        boxBlur.setHeight(5);
        //動態化
        bloom = new Bloom();
        bloom.setThreshold(50);
        //上面兩種屬性與舉行系結
        rectangle.setEffect(boxBlur);
        rectangle.setEffect(bloom);

    }
    public Rectangle HandGunBullet(){
        Rectangle rectangle=new Rectangle();
        rectangle.setX(man2.hand.getX()+40);
        rectangle.setY(man2.hand.getY());
        rectangle.setFill(Color.RED);
        rectangle.setWidth(12);
        rectangle.setHeight(12);
        this.setxProperty(140);
        this.setyProperty(140);
        rectangle.setArcWidth(34);
        rectangle.setArcHeight(56);
        BulletSpeed = 3;
//        模糊化
        boxBlur = new BoxBlur();
        boxBlur.setWidth(6);
        boxBlur.setHeight(6);
        //動態化
        bloom = new Bloom();
        bloom.setThreshold(55);
        //上面兩種屬性與舉行系結
        rectangle.setEffect(boxBlur);
        rectangle.setEffect(bloom);
        return rectangle;
    }
    public void RifleGunBullet(){
        rectangle=new Rectangle();
        rectangle.setFill(Color.BLUE);
        rectangle.setWidth(20);
        rectangle.setHeight(9);
        rectangle.setX(man1.gun.getX()+40);
        rectangle.setY(man1.gun.getY());
        this.setxProperty(140);
        this.setyProperty(150);
        rectangle.setArcWidth(20);
        rectangle.setArcHeight(20);
        BulletSpeed = 3;
//        模糊化
        boxBlur = new BoxBlur();
        boxBlur.setWidth(5);
        boxBlur.setHeight(5);
        //動態化
        bloom = new Bloom();
        bloom.setThreshold(50);
        //上面兩種屬性與舉行系結
        rectangle.setEffect(boxBlur);
        rectangle.setEffect(bloom);


    }
    public Rectangle snipGunBullet(){
        Rectangle rectangle=new Rectangle();
        rectangle.setFill(Color.GOLD);
        rectangle.setWidth(28);
        rectangle.setHeight(7);
        rectangle.setX(man1.gun.getX()+40);
        rectangle.setY(man1.gun.getY());
        this.setxProperty(140);
        this.setyProperty(150);
        rectangle.setArcWidth(20);
        rectangle.setArcHeight(20);
        BulletSpeed = 3;
//        模糊化
        boxBlur = new BoxBlur();
        boxBlur.setWidth(5);
        boxBlur.setHeight(5);
        //動態化
        bloom = new Bloom();
        bloom.setThreshold(50);
        //上面兩種屬性與舉行系結
        rectangle.setEffect(boxBlur);
        rectangle.setEffect(bloom);
        return rectangle;
    }
    public Rectangle ShotGunBullet(){
        Rectangle rectangle = new Rectangle();
        rectangle.setFill(Color.GOLD);
        rectangle.setWidth(15);
        rectangle.setHeight(20);
        rectangle.setX(man2.hand.getX()+40);
        rectangle.setY(man2.hand.getY());

        this.setxProperty(140);
        this.setyProperty(150);
        rectangle.setArcWidth(1);
        rectangle.setArcHeight(32);
        BulletSpeed = 3;
//        模糊化
        boxBlur = new BoxBlur();
        boxBlur.setWidth(5);
        boxBlur.setHeight(5);
        //動態化
        bloom = new Bloom();
        bloom.setThreshold(50);
        //上面兩種屬性與舉行系結
        rectangle.setEffect(boxBlur);
        rectangle.setEffect(bloom);
        return rectangle;
    }
    public static void MoveLeft(Bullet bullet){
        if(shoutOpen == true&&isImpacte(man2,bullet)==false){

//           rectangle.setX(rectangle.getX()-1);
            bullet.rectangle.setX(bullet.rectangle.getX()-1);
//            bullet.moveX(-1);
        }

    }
    public static void MoveRight(Bullet bullet){

        if(shoutOpen == true&&isImpacte(man2,bullet)==false){
//            rectangle.setX(rectangle.getX()+1);
            bullet.rectangle.setX(bullet.rectangle.getX()+1);
        }
    }
    public static void MoveTwoLeft(Bullet bullet){

        if(shoutOpenTwo == true&&isImpacteTwo(man1,bullet)==false){
            bullet.rectangle.setX(bullet.rectangle.getX()-1);
        }
    }
    public static void MoveTwoRight(Bullet bullet){

        if(shoutOpenTwo == true&&isImpacteTwo(man1,bullet)==false){
            bullet.rectangle.setX(bullet.rectangle.getX()+1);
        }
    }
    public  void shot(Bullet bullet){
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(-1);
        KeyFrame keyFrame = new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
             @Override
            public void handle(ActionEvent actionEvent) {
//

                if(bullet.select == false /*&&*/ /*bullet.isOne == false*/){
                    man2.moveX(-4);
                    System.out.println("移動了man2!!!!!!!!!!!!!!!");
                    bullet.select = true;
                }
                if(bullet.rectangle.getX()< 0 || bullet.rectangle.getX() > 1100 || isImpacte(man2,bullet) == true){
                    getChildren().remove(bullet.rectangle);
                    getChildren().remove(bullet);
//                    timeline.stop();
                }
            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
    }

    public  void shotTwo(Bullet bullet){
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(-1);
        KeyFrame keyFrame = new KeyFrame(Duration.millis(0.5), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                if(bullet.selectTwo == false ){
                    man1.moveX(5);
                    System.out.println("!!!!!!!!!!!!!!!移動了man1");
                    bullet.selectTwo = true;
                }
                if(bullet.getxProperty() < 0 || bullet.getxProperty() > 1100 || isImpacteTwo(man1,bullet) == true){
                    getChildren().remove(bullet.rectangle);
                    getChildren().remove(bullet);
//                    timeline.stop();
                }
            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
    }

    public void ShotMove(Bullet bullet){
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(-1);
        KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                if (bullet.dirctionClock) {
                    MoveLeft(bullet);
                }
                else
                    MoveRight(bullet);
            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀

    }

    public void  ShotMoveTwo(Bullet bullet){
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(-1);
        KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                if(bullet.dirctionTwoClok){
                    MoveTwoLeft(bullet);
                }
//                else if(isImpacteN(man2,man1)){
//                    MoveTwoLeft(bullet);
//                }
                else
                    MoveTwoRight(bullet);
            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
    }
    @Override
    public void onKeyBoardPressed(KeyEvent event){
        if(event.getCode()==KeyCode.J){
            shoutOpen=true;
            Bullet bullet = new Bullet();
            list.add(bullet);
            if(Man.direction==0){
                list.get(i).dirctionClock = true;
            }
            else {
                list.get(i).dirctionClock = false;
            }
            list.get(i).rectangle = bullet.snipGunBullet();
            shot(list.get(i));
            ShotMove(list.get(i));
            getChildren().add(list.get(i).rectangle);
            list.remove(0);
        }
        if(event.getCode()==KeyCode.K){
            shoutOpenTwo = true;
            Bullet bullet = new Bullet();
            list2.add(bullet);
            if(Man2.direction==0){
                list2.get(z).dirctionTwoClok = true;
            }
            else {
                list2.get(z).dirctionTwoClok = false;
            }
            list2.get(z).rectangle = bullet.HandGunBullet();
            shotTwo(list2.get(z));
            ShotMoveTwo(list2.get(z));
            getChildren().add(list2.get(z).rectangle);
            list2.remove(0);
        }
    }

    @Override
    public void onKeyBoardReleased(KeyEvent event) {

    }

    public static boolean isImpacte(BaseObject baseObject,Bullet bullet) {
        if(bullet.rectangle.getX()==baseObject.getxProperty()+Man.width&&bullet.rectangle.getY() >= baseObject.getyProperty()-Man.height/2&&bullet.rectangle.getY()<=baseObject.getyProperty()+Man.height){
            bullet.select = false;

            bullet.isOne = true;
            return true;
        }
        return false;
    }
    public static boolean isImpacteTwo(BaseObject baseObject,Bullet bullet) {
        if(bullet.rectangle.getX()==baseObject.getxProperty()-Man.width/2+5&&bullet.rectangle.getY() >= baseObject.getyProperty()-Man.height/2&&bullet.rectangle.getY()<=baseObject.getyProperty()+Man.height){
            bullet.selectTwo = false;
            bullet.isOne = true;System.out.println("$$$$$$$$$$$$$$$$$$$$$$$");
            return true;
        }

        return false;
    }
    public static boolean isImpacteN(BaseObject baseObject,BaseObject baseObject1) {
        if(baseObject.getxProperty()>baseObject1.getxProperty()){
            return true;
        }
        return false;
    }

//    @Override
//    public void run() {
//
//            Timeline timeline;
//            timeline = new Timeline();
//            timeline.setCycleCount(-1);
//            KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
//                @Override
//                public void handle(ActionEvent actionEvent) {
//                    System.out.println("多執行緒}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}");
//                    if(Man.direction==0){
//
//                        MoveLeft(rectangle);
//                    }
//                    else if(Man.direction==1)
//                        MoveRight(rectangle);
//                }
//            });
//            timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
//            timeline.play();//時間軸回圈執行每個堆疊幀
//        }

}

DatabaseUtil

package JavaBigJob;

import java.sql.*;

public class DataBaseUtil {

    //2.通過DriverManager獲取資料庫連接
//    String url = "jdbc:mysql://localhost:3306/label?sever";
    String url = "jdbc:mysql://localhost:3306/label?severTimezone=GMT%2B8";
    String driver = "com.mysql.cj.jdbc.Driver";
    String user = "root";
    String password = "wenchi0124";


    Connection conn = null;
    ResultSet rs = null;
    Statement statement = null;
    PreparedStatement ps = null;

    public DataBaseUtil() {
        try {
            conn = DriverManager.getConnection(url,user,password);
            System.out.println("line success");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void close(){
        try{
            if(rs != null){
                rs.close();
            }
            if(ps != null){
                ps.close();
            }
            if(statement != null){
                statement.close();
            }
            if(conn != null){
                conn.close();
            }
        }
        catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
//            this.close();
        }
    }

    public ResultSet queryExecute(String sql) {
        try {
            Class.forName(driver);
            System.out.println("query_success01");
            statement = conn.createStatement();
            rs = statement.executeQuery(sql);
            return rs;
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        finally {
//            this.close();
        }
    }
    //多載
    public ResultSet queryExecute(String sql,String[] paras){
        try {
            Class.forName(driver);
            System.out.println("query_success02");
//            conn = DriverManager.getConnection(url,user,password);
            ps = conn.prepareStatement(sql);
            for(int i =0;i < paras.length;i++){
                ps.setString(i+1,paras[i]);
            }
            rs = ps.executeQuery();
            return rs;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        finally {
//            this.close();
        }
        return null;
    }

    public boolean updateExecute(String sql,String[] paras){
        boolean b = true;
        try {
            Class.forName(driver);
            System.out.println("update_success03");
//            conn = DriverManager.getConnection(url,user,password);
            ps = conn.prepareStatement(sql);
            for(int i =0;i < paras.length;i++){
                ps.setString(i+1,paras[i]);
            }
            if(ps.executeLargeUpdate() != 1){
                b = false;
            }
        }catch (Exception ex){
            b = false;
            ex.printStackTrace();
        }
        finally {
//            this.close();
        }
        return b;
    }

}

GameWindow

package JavaBigJob;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class GameWindow /*extends Application*/ {

    public static final int WIDTH = 1100;
    public static final int HEIGHT = 700;

    GameWindow(){
        BaseScene bs = new BaseScene(WIDTH,HEIGHT);
        Scene bsScene = new Scene(bs,WIDTH,HEIGHT);
        Stage stage = new Stage();
        stage.setTitle("javaBigWork");
        stage.setScene(bsScene);//添加bs至scene中,下一步通過bs,getScene回傳Scene的實體化物件
        bs.loadKeyBoard();//因為實體化了scene,加載scene的全域鍵盤事件
         stage.show();
    }
}

login.css

給登錄界面加的css樣式


#paneId{
    -fx-background-image: url('Picture/login.png');
    /*-fx-background-image-opacity: 0.5;*/

    -fx-background-color: #0155fd;
}

#labelLogin {
    -fx-text-fill: rgba(238, 116, 116, 0.88);
    -fx-font-size: 10pt;
    -fx-font-family: "Sans-serif";
    -fx-font-weight: 500;
    -fx-font-smoothing-type: inherit;
    /*-fx-font-weight: bold;*/
}

#warn{
    -fx-text-fill: #30ec9a;
    -fx-font-family: "Microsoft Yahei";
    -fx-background-color: gray;
    -fx-font-weight: bold;
    -fx-font-size: 10pt;
    -fx-border-color: #0155fd;
}

#btLogin {
    -fx-text-fill: #2ffd01;
    -fx-font-family: "Microsoft Yahei";
    -fx-text-alignment: center;
    /*-fx-wrap-text: true;*/
    -fx-cell-size: 30pt;
    -fx-font-weight: bold;
    -fx-background-color: linear-gradient(#6169b1, rgba(253, 119, 1, 0.42));
    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
}


#tfLogin {
    -fx-animated: true;
    -fx-border-width: 2pt;
    -fx-border-color: gray;
    /*-fx-border-radius:10px;*/
}

LoginFrame

package JavaBigJob;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginFrame extends Application{
    Label nameLabel = new Label("User Name :");
    Label passwordLabel = new Label("Password  : ");
    Label lack_information = new Label("用戶資訊填寫不全!");
    Label Mismatch_information = new Label("用戶名或密碼不正確");
    Label user_nonentity = new Label("用戶不存在");;
    Label user_exit = new Label("用戶已存在");
    Label sign_successful = new Label("注冊成功!");
    Label login_successful = new Label("登陸成功!");

    HBox user = new HBox();
    HBox password = new HBox();
    TextField tfUser = new TextField();
    PasswordField tfPassword = new PasswordField();
    Button btLogIn = new Button("Log in");
    Button btSignIn = new Button("Sign in");
    HBox h3 = new HBox();//裝按鈕
    VBox pane = new VBox();

    @Override
    public void start(Stage stage) {
        //css
        btLogIn.setId("btLogin");//設定id
        btSignIn.setId("btLogin");
        nameLabel.setId("labelLogin");
        passwordLabel.setId("labelLogin");
        tfUser.setId("tfLogin");
        tfPassword.setId("tfLogin");
        lack_information.setId("warn");
        Mismatch_information.setId("warn");
        user_nonentity.setId("warn");
        user_exit.setId("warn");
        sign_successful.setId("warn");

        user.getChildren().addAll(nameLabel,tfUser);
        user.setAlignment(Pos.CENTER);
        user.setSpacing(20);

        password.getChildren().addAll(passwordLabel,tfPassword);
        password.setAlignment(Pos.CENTER);
        password.setSpacing(20);

        h3.setAlignment(Pos.CENTER);
        btLogIn.setAlignment(Pos.BASELINE_RIGHT);
        btSignIn.setAlignment(Pos.BASELINE_RIGHT);
        h3.getChildren().addAll(btLogIn,btSignIn);
        h3.setSpacing(20);

        pane.setAlignment(Pos.CENTER);
        pane.setSpacing(20);
        pane.getChildren().addAll(user,password,h3);
        Scene scene = new Scene(pane,400,250);
        pane.setId("paneId");
        //添加css樣式
        scene.getStylesheets().add(
                LoginFrame.class.getResource("login.css")
                        .toExternalForm());
        stage.setScene(scene);
        stage.setTitle("Welcome!");
        stage.show();

        btLogIn.setOnAction(e->{
            if(user_exist()==false){
                System.out.println("用戶不存在");
                HBox hBox = new HBox();
                Label label = user_nonentity;
                ImageView image = new ImageView("JavaBigJob/Picture/No.png");
                image.setFitWidth(150);
                image.setFitHeight(120);
                hBox.setAlignment(Pos.CENTER);
                hBox.setSpacing(10);
                hBox.getChildren().addAll(image,label);
                Scene sub_scene = new Scene(hBox,400,200);
                sub_scene.getStylesheets().add(
                        LoginFrame.class.getResource("login.css")
                                .toExternalForm());
                Stage stage1 = new Stage();
                stage1.setScene(sub_scene);
                stage1.setTitle("ERROR");
                stage1.show();
            }
            else if(detection_information()){
                if(user_right()){
                    stage.hide();
                    //登陸成功
                    new GameWindow();
                }
            }
        });

        btSignIn.setOnAction(e->{
            if(user_exist()){
                System.out.println("用戶已存在");
                HBox hBox = new HBox();
                Label label = user_exit;
                ImageView image = new ImageView("JavaBigJob/Picture/No.png");
                image.setFitWidth(150);
                image.setFitHeight(120);
                hBox.setAlignment(Pos.CENTER);
                hBox.setSpacing(10);
                hBox.getChildren().addAll(image,label);
                Scene sub_scene = new Scene(hBox,400,200);
                sub_scene.getStylesheets().add(
                        LoginFrame.class.getResource("login.css")
                                .toExternalForm());
                Stage stage1 = new Stage();
                stage1.setScene(sub_scene);
                stage1.setTitle("ERROR");
                stage1.show();
            }
            else if(detection_information())
            {
                DataBaseUtil db = new DataBaseUtil();
                String sql = "INSERT INTO user (name,passwd) VALUES (?,?)";
                db.updateExecute(sql,new String[]{tfUser.getText(),tfPassword.getText()});
                HBox hBox = new HBox();
                Label label = sign_successful;
                ImageView image = new ImageView("JavaBigJob/Picture/yes.png");
                image.setFitWidth(150);
                image.setFitHeight(120);
                hBox.setAlignment(Pos.CENTER);
                hBox.setSpacing(10);
                hBox.getChildren().addAll(image,label);
                Scene sub_scene = new Scene(hBox,400,200);
                sub_scene.getStylesheets().add(
                        LoginFrame.class.getResource("login.css")
                                .toExternalForm());
                Stage stage1 = new Stage();
                stage1.setScene(sub_scene);
                stage1.show();
            }
        });
    }

    public static void main(String[] args) {
        DataBaseUtil db = new DataBaseUtil();
        launch();
    }

    public Boolean user_exist(){//判斷是否已存在用戶
        DataBaseUtil db = new DataBaseUtil();
        String sql = "select count(*) from user where name = '"+tfUser.getText()+"'";
        try {
            ResultSet rs = db.queryExecute(sql);
            rs.next();
            if(rs.getInt(1)==1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
    public Boolean user_right(){
        DataBaseUtil db = new DataBaseUtil();
        String sql = "select count(*) from user where name = ? and passwd = ?";
        try {
            ResultSet rs = db.queryExecute(sql,new String[]{tfUser.getText(),tfPassword.getText()});
            rs.next();
            if(rs.getInt(1)==1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("用戶名或密碼不正確");
        HBox hBox = new HBox();
        Label label = Mismatch_information;
        ImageView image = new ImageView("JavaBigJob/Picture/No.png");
        image.setFitWidth(150);
        image.setFitHeight(120);
        hBox.setAlignment(Pos.CENTER);
        hBox.setSpacing(10);
        hBox.getChildren().addAll(image,label);
        Scene sub_scene = new Scene(hBox,400,200);
        sub_scene.getStylesheets().add(
                LoginFrame.class.getResource("login.css")
                        .toExternalForm());
        Stage stage1 = new Stage();
        stage1.setScene(sub_scene);
        stage1.setTitle("ERROR");
        stage1.show();
        return false;
    }

    public Boolean detection_information(){//判斷資訊是否填寫完全

        if (tfUser.getText().equals("")||tfPassword.getText().equals("")){
            System.out.println("資訊不全!");
            HBox hBox = new HBox();
            Label label = lack_information;
            ImageView image = new ImageView("JavaBigJob/Picture/No.png");
            image.setFitWidth(150);
            image.setFitHeight(120);
            hBox.setAlignment(Pos.CENTER);
            hBox.setSpacing(10);
            hBox.getChildren().addAll(image,label);
            Scene sub_scene = new Scene(hBox,400,200);
            sub_scene.getStylesheets().add(
                    LoginFrame.class.getResource("login.css")
                            .toExternalForm());
            Stage stage1 = new Stage();
            stage1.setScene(sub_scene);
            stage1.setTitle("ERROR");
            stage1.show();
            return false;//資訊不全
        }
        return true;//資訊全
    }

}


Man

package JavaBigJob;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.effect.Effect;
import javafx.scene.effect.PerspectiveTransform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Duration;

import java.util.ArrayList;

import static JavaBigJob.BaseScene.man1;

public class Man extends BaseObject implements BaseObject.CanMove {

    static double high = 3;//跳躍時的高度
    static boolean isOnPlat = false;
    static int dropSpeed = 1;
    static int times = 0;

    static int height = 68;
    static int width = 42;
    static boolean jump = false;//是否正在跳躍
    static boolean down = false;//掉到下一層
    static boolean PressJump = false;//跳躍(是否正在按壓跳躍鍵)


    boolean moveLeft = false;
//    boolean moveUp = false;
    boolean moveRight = false;
//    boolean moveDown = false;
    static int direction = 0;//0表示人物向左,1表示人物向右

    //頭部不能與this.xProperty直接系結(頭是突出來的,必須改變坐標才能美觀),直接系結無法動態改變頭部的相對位置(不能在add方法中添加變數,變數即使改變,也始終為第一次的值),
    ImageView body = new ImageView("JavaBigJob/Picture/198.png");
    ImageView head = new ImageView("JavaBigJob/Picture/229.png");
    ImageView foot1 = new ImageView("JavaBigJob/Picture/181.png");
    ImageView foot2 = new ImageView("JavaBigJob/Picture/181.png");
    ImageView hand = new ImageView("JavaBigJob/Picture/102.png");
    ImageView gun = new ImageView("JavaBigJob/Picture/456.png");
    ImageView clothes = new ImageView("JavaBigJob/Picture/198.png");

    public Man(){

//        ReplaceClothes replaceClothes = new ReplaceClothes();
//        Image image = replaceClothes.GetImage();
//        clothes = new ImageView(image);
        clothes.xProperty().bind(body.xProperty());
        clothes.yProperty().bind(body.yProperty());
        clothes.setFitHeight(55);
        clothes.setFitWidth(35);
        clothes.scaleXProperty().bind(body.scaleXProperty());

        body.setScaleX(-1);
        //設定主角圖片以及系結圖片在場景中的位置
        body.setFitHeight(55);
        body.setFitWidth(35);
        body.xProperty().bind(this.xProperty());
        body.yProperty().bind(this.yProperty());
        head.setFitWidth(30);
        head.setFitHeight(32);
        head.xProperty().bind(this.xProperty().add(-8));
        head.yProperty().bind(this.yProperty().add(-12));
        hand.xProperty().bind(this.xProperty().add(-4));
        hand.yProperty().bind(this.yProperty().add(20));
        hand.scaleXProperty().bind(body.scaleXProperty());
        hand.rotateProperty().bind(body.yProperty());
        head.scaleXProperty().bind(body.scaleXProperty());//系結翻轉
        head.rotateProperty().bind(body.rotateProperty());//系結角度
        foot1.xProperty().bind(this.xProperty());
        foot1.yProperty().bind(this.yProperty().add(55));
        foot1.rotateProperty().bind(body.rotateProperty());
        foot1.scaleXProperty().bind(body.scaleXProperty());
        foot2.xProperty().bind(this.xProperty().add(15));
        foot2.yProperty().bind(this.yProperty().add(55));
        foot2.rotateProperty().bind(body.rotateProperty());
        foot2.scaleXProperty().bind(body.scaleXProperty());
        gun.xProperty().bind(hand.xProperty().add(-35));
        gun.yProperty().bind(hand.yProperty().add(-4));
        gun.scaleXProperty().bind(hand.scaleXProperty());


        //添加到Man
        getChildren().addAll(body,clothes,head,foot1,foot2,hand,gun);
    }

    public void onKeyBoardPressed(KeyEvent event){
        if(event.getCode() == KeyCode.RIGHT){
            moveRight = true;

        }
        if(event.getCode() == KeyCode.LEFT){
            moveLeft = true;
        }
        if(event.getCode() == KeyCode.UP){
            PressJump = true;
            System.out.println("按壓了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
        if(event.getCode() == KeyCode.DOWN){
            down = true;
        }
    }
    public void onKeyBoardReleased(KeyEvent event){
        if(event.getCode() == KeyCode.RIGHT){
            moveRight = false;
        }
        if(event.getCode() == KeyCode.LEFT){
            moveLeft = false;
        }
        if(event.getCode() == KeyCode.UP){
            PressJump = false;
        }
        if(event.getCode() == KeyCode.DOWN){
            down = false;
        }
    }

    public void move(){
        if(moveLeft == true){
            head.xProperty().bind(this.xProperty().add(-7));
            hand.xProperty().bind(this.xProperty().add(-5));
            gun.xProperty().bind(this.xProperty().add(-38));
            this.direction = 0;//向左
            body.setScaleX(-1);//設定翻轉
            moveX(-this.getSpeed());
        }
        if(PressJump == true && jump == false &&isOnPlat == true){//正在按下跳躍&不在跳躍程序中&在平臺上,進入if后執行跳躍timeline,
            this.jumpTimeline();
            PressJump = false;
            isOnPlat = false;
        }
        if(moveRight == true){
            head.xProperty().bind(this.xProperty().add(14));
            hand.xProperty().bind(this.xProperty().add(25));
            gun.xProperty().bind(this.xProperty().add(23));
            this.direction = 1;//向右
            body.setScaleX(1);
            moveX(this.getSpeed());
        }
        if(down == true && isOnPlat==true){
            moveY(1);
            down = false;
        }
    }



    public static void dropOutTimeline(ArrayList<BaseRectangle> brArrayList){
        System.out.println("---------------");
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(-1);//無限回圈
        KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {//重復執行該方法
//                System.out.println("--------0-------");
//                    System.out.println("---------1------");
                if(!Man.jump){
                    if(ObjectCollections.Man1DropJudge(brArrayList)){
                        System.out.println("碰撞到平臺了!!!!!!!!!");
                        Man.isOnPlat = true;
//                        dropSpeed = 0;
                    }
                    else{
                        Man.isOnPlat = false;
                        BaseScene.man1.moveY(dropSpeed);
                        System.out.println("在墜落!");
                    }
                }

            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
    }

    public void jumpTimeline(){
        jump = true;//
        isOnPlat = false;
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(90);//90
        System.out.println("&&&&&&&執行了jump_timeline!!!!!");
//        high = 3;//初始高度為3
//        times = 0;
        KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                //重復執行該方法
                    times++;
                    System.out.println("&&&&&&&跳躍了!!!!!!!!!!");
                    man1.moveY(-high);

            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
        jump = false;
    }

}

Man2

package JavaBigJob;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Duration;

import java.util.ArrayList;

import static JavaBigJob.BaseScene.man1;
import static JavaBigJob.BaseScene.man2;

public class Man2 extends BaseObject implements BaseObject.CanMove{

    static double high = 3;//跳躍時的高度
    static boolean isOnPlat = false;
    static int dropSpeed = 1;

    static int height = 68;
    static int width = 42;
    static boolean jump = false;//是否正在跳躍
    static boolean down = false;//掉到下一層
    static boolean PressJump = false;//跳躍(是否正在按壓跳躍鍵)


    boolean moveLeft = false;
    //    boolean moveUp = false;
    boolean moveRight = false;
    //    boolean moveDown = false;
    static int direction = 1;//0表示人物向左,1表示人物向右

    //頭部不能與this.xProperty直接系結(頭是突出來的,必須改變坐標才能美觀),直接系結無法動態改變頭部的相對位置(不能在add方法中添加變數,變數即使改變,也始終為第一次的值),

    ImageView body = new ImageView("JavaBigJob/Picture/200.png");
    ImageView head = new ImageView("JavaBigJob/Picture/230.png");
    ImageView foot1 = new ImageView("JavaBigJob/Picture/182.png");
    ImageView foot2 = new ImageView("JavaBigJob/Picture/182.png");
    ImageView hand = new ImageView("JavaBigJob/Picture/104.png");
    ImageView clothes = new ImageView("JavaBigJob/Picture/200.png");

    public Man2(){
        clothes.setFitHeight(55);
        clothes.setFitWidth(35);
        //設定主角圖片以及系結圖片在場景中的位置
        body.setFitHeight(55);
        body.setFitWidth(35);
        body.xProperty().bind(this.xProperty());
        body.yProperty().bind(this.yProperty());
        head.setFitWidth(30);
        head.setFitHeight(32);
        head.xProperty().bind(this.xProperty().add(14));
        head.yProperty().bind(this.yProperty().add(-12));
        head.scaleXProperty().bind(body.scaleXProperty());//系結翻轉
        head.rotateProperty().bind(body.rotateProperty());//系結角度
        hand.xProperty().bind(this.xProperty().add(25));
        hand.yProperty().bind(this.yProperty().add(20));
        hand.scaleXProperty().bind(body.scaleXProperty());
        hand.rotateProperty().bind(body.yProperty());
        foot1.xProperty().bind(this.xProperty());
        foot1.yProperty().bind(this.yProperty().add(55));
        foot1.rotateProperty().bind(body.rotateProperty());
        foot1.scaleXProperty().bind(body.scaleXProperty());
        foot2.xProperty().bind(this.xProperty().add(15));
        foot2.yProperty().bind(this.yProperty().add(55));
        foot2.rotateProperty().bind(body.rotateProperty());
        foot2.scaleXProperty().bind(body.scaleXProperty());

        clothes.xProperty().bind(body.xProperty());
        clothes.yProperty().bind(body.yProperty());
        clothes.scaleXProperty().bind(body.scaleXProperty());//衣服系結
        clothes.rotateProperty().bind(body.rotateProperty());
        //添加到環境
        getChildren().addAll(body,clothes,head,foot1,foot2,hand);
    }

    public void onKeyBoardPressed(KeyEvent event){
        if(event.getCode() == KeyCode.D){
            moveRight = true;
        }
        if(event.getCode() == KeyCode.A){
            moveLeft = true;
        }
        if(event.getCode() == KeyCode.W){
            PressJump = true;
        }
        if(event.getCode() == KeyCode.S){
            down = true;
        }
    }
    public void onKeyBoardReleased(KeyEvent event){
        if(event.getCode() == KeyCode.D){
            moveRight = false;
        }
        if(event.getCode() == KeyCode.A){
            moveLeft = false;
        }
        if(event.getCode() == KeyCode.W){
            PressJump = false;
        }
        if(event.getCode() == KeyCode.S){
            down = false;
        }
    }

    public void move(){
        if(moveLeft == true){
            head.xProperty().bind(this.xProperty().add(-7));
            hand.xProperty().bind(this.xProperty().add(-5));
            this.direction = 0;//向左
            body.setScaleX(-1);//設定翻轉
            moveX(-this.getSpeed());
        }
        if(PressJump == true && jump == false &&isOnPlat == true){
            this.jumpTimeline();
            PressJump = false;
            isOnPlat = false;
        }
        if(moveRight == true){
            head.xProperty().bind(this.xProperty().add(14));
            hand.xProperty().bind(this.xProperty().add(25));
            this.direction = 1;//向右
            body.setScaleX(1);
            moveX(this.getSpeed());
        }
        if(down == true && isOnPlat==true){
            moveY(1);
            down = false;
        }
    }

    public static void dropOutTimeline(ArrayList<BaseRectangle> brArrayList){
        System.out.println("---------------");
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(-1);//無限回圈
        KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {//重復執行該方法
//                System.out.println("--------0-------");
//                    System.out.println("---------1------");
                if(ObjectCollections.Man2DropJudge(brArrayList)&&!Man2.jump){
                    System.out.println("碰撞到平臺了!!!!!!!!!");
                    Man2.isOnPlat = true;
//                        dropSpeed = 0;
                }
                else{
                    Man2.isOnPlat = false;
                    BaseScene.man2.moveY(dropSpeed);
                }

            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
    }

    public void jumpTimeline(){
        jump = true;
        isOnPlat = false;
        Timeline timeline;
        timeline = new Timeline();
        timeline.setCycleCount(90);//無限回圈
        System.out.println("&&&&&&&執行了jump_timeline!!!!!");
        high = 3;//初始高度為3
        KeyFrame keyFrame = new KeyFrame(Duration.millis(4), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                //重復執行該方法
//                if(high >= 0.1 ){
//                    high -= 0.01;
                System.out.println("&&&&&&&跳躍了!!!!!!!!!!");
                man2.moveY(-high);
//                }
            }
        });
        timeline.getKeyFrames().add(keyFrame);//為時間軸添加堆疊幀
        timeline.play();//時間軸回圈執行每個堆疊幀
        jump = false;
    }

}

ObjectCollections

package JavaBigJob;

import java.util.ArrayList;
import java.util.List;

import static JavaBigJob.BaseScene.man1;
import static JavaBigJob.BaseScene.man2;

public class ObjectCollections {

    public static Boolean Man1DropJudge(ArrayList<BaseRectangle> brArrayList){
        if(brArrayList==null){
            System.out.println("平臺集合中沒有元素");
            return false;
        }
        for(BaseRectangle br: brArrayList){
            if(man1.getyProperty()+Man.height==br.getyProperty()&&(man1.getxProperty()<=br.getxProperty()+br.getWidth())&&man1.getxProperty()+Man.width>=br.getxProperty()){
                return true;
            }
//            System.out.println(man1.getHeightProperty()+" "+man1.getxProperty()+" "+br.getxProperty()+" "+br.getxProperty()+br.getWidth());
        }
        return false;
    }

    public static Boolean Man2DropJudge(ArrayList<BaseRectangle> brArrayList){
        if(brArrayList==null){
            System.out.println("平臺集合中沒有元素");
            return false;
        }
        for(BaseRectangle br: brArrayList){
            if(man2.getyProperty()+Man2.height==br.getyProperty()&&(man2.getxProperty()<=br.getxProperty()+br.getWidth())&&man2.getxProperty()+Man2.width>=br.getxProperty()){
                return true;
            }
//            System.out.println(man2.getHeightProperty()+" "+man2.getxProperty()+" "+br.getxProperty()+" "+br.getxProperty()+br.getWidth());
        }
        return false;
    }

}

ReplaceClothes

package JavaBigJob;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Border;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;

public class ReplaceClothes extends Parent {
    Image image = new Image("JavaBigJob/Picture/207.png");
    Image HeadImage = new Image("JavaBigJob/Picture/227.png");
    Image FootImage = new Image("JavaBigJob/Picture/178.png");
    Image HandImage = new Image("JavaBigJob/Picture/670.png");
    Image BodyImage = new Image("JavaBigJob/Picture/195.png");

    Image Man2ClothesImage = new Image("JavaBigJob/Picture/207.png");
    Image Man2headImage = new Image("JavaBigJob/Picture/230.png");
    Image Man2footImage = new Image("JavaBigJob/Picture/182.png");
    Image Man2BodyImage = new Image("JavaBigJob/Picture/200.png");
    Image Man2HandImage = new Image("JavaBigJob/Picture/104.png");

    ImageView imageView;
    ImageView HeadImageView;
    ImageView FootImageView;
    ImageView HandImageView;
    ImageView BodyImageView;

    ImageView Man2ClothesImageView;
    ImageView Man2headImageView;
    ImageView Man2footImageView;
    ImageView Man2BodyImageView;
    ImageView Man2HandImageView;

    int i = 207;
    int HeadCount = 227;
    int FootCount = 178;
    int HandCount = 670;
    int BodyCount = 195;
    int Man2ClothesCount = 207;
    int Man2HeadCount = 227;
    int Man2FootCount = 178;
    int Man2HandCount = 670;
    int Man2BodyCount = 195;

    public void Photo(){//回圈展示衣服圖片
        if(i==225){
            i=207;
        }
        if(i==222){
            i++;
        }
            image = new Image("JavaBigJob/Picture/"+i+".png");
            imageView = new ImageView(image);
                i++;
    }
    public void Man2Photo(){//回圈展示衣服圖片
        if(Man2ClothesCount==225){
            Man2ClothesCount=207;
        }
        if(Man2ClothesCount==222){
            Man2ClothesCount++;
        }
        Man2ClothesImage = new Image("JavaBigJob/Picture/"+Man2ClothesCount+".png");
        Man2ClothesImageView = new ImageView(Man2ClothesImage);
        Man2ClothesCount++;
    }
    public void BodyPhoto(){
        if(BodyCount == 206){
            BodyCount = 195;
        }
        BodyImage = new Image("JavaBigJob/Picture/"+BodyCount+".png");
        BodyImageView = new ImageView(BodyImage);
        BodyCount++;
    }
    public void Man2BodyPhoto(){
        if(Man2BodyCount == 206){
            Man2BodyCount = 195;
        }
        Man2BodyImage = new Image("JavaBigJob/Picture/"+Man2BodyCount+".png");
        Man2BodyImageView = new ImageView(Man2BodyImage);
        Man2BodyCount++;
    }
    public void HeadPhoto(){
        if(HeadCount==238){
            HeadCount = 227;
        }
        HeadImage = new Image("JavaBigJob/Picture/"+HeadCount+".png");
        HeadImageView = new ImageView(HeadImage);
        HeadCount++;
    }
    public void Man2HeadPhoto(){
        if(Man2HeadCount==238){
            Man2HeadCount = 227;
        }
        Man2headImage = new Image("JavaBigJob/Picture/"+Man2HeadCount+".png");
        Man2headImageView = new ImageView(Man2headImage);
        Man2HeadCount++;
    }
    public void FootPhoto(){
        if(FootCount == 189)
            FootCount = 178;
        FootImage = new Image("JavaBigJob/Picture/"+FootCount+".png");
        FootImageView = new ImageView(FootImage);
        FootCount++;
    }
    public void Man2FootPhoto(){
        if(Man2FootCount == 189)
            Man2FootCount = 178;
        Man2footImage = new Image("JavaBigJob/Picture/"+Man2FootCount+".png");
        Man2footImageView = new ImageView(Man2footImage);
        Man2FootCount++;
    }
    public void HandPhoto(){
        if(HandCount == 679){
            HandCount = 670;
        }
        HandImage = new Image("JavaBigJob/Picture/"+HandCount+".png");
        HandImageView = new ImageView(HandImage);
        HandCount++;
    }
    public void Man2HandPhoto(){
        if(Man2HandCount == 679){
            Man2HandCount = 670;
        }
        Man2HandImage = new Image("JavaBigJob/Picture/"+Man2HandCount+".png");
        Man2HandImageView = new ImageView(Man2HandImage);
        Man2HandCount++;
    }
    public ReplaceClothes() {
        Button btnHat = new Button("更換頭部");
        Button btnBody = new Button("更換身體");
        Button btnHand = new Button("更換手部");
        Button btnClothes = new Button("更換衣服");
        Button btnFoot = new Button("更換腳步");
        Button btnOk = new Button("確定");
        Button btnMan2Hat = new Button("更換頭部2");
        Button btnMan2Body = new Button("更換身體2");
        Button btnMan2Hand = new Button("更換手部2");
        Button btnMan2Clothes = new Button("更換衣服2");
        Button btnMan2Foot = new Button("更換腳步2");
        Button btnMan2Ok = new Button("確定2");
        //css

        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(0,0,50,0));//與裝他的容器的間距

        gridPane.setAlignment(Pos.CENTER);

        gridPane.add(btnClothes,3,10);
        gridPane.add(btnBody,1,10);
        gridPane.add(btnOk,2,11);
        gridPane.add(btnHat,0,10);
        gridPane.add(btnHand,2,10);
        gridPane.add(btnFoot,4,10);

        gridPane.add(btnMan2Clothes,3,12);
        gridPane.add(btnMan2Body,2,12);
        gridPane.add(btnMan2Ok,2,13);
        gridPane.add(btnMan2Hat,0,12);
        gridPane.add(btnMan2Hand,1,12);
        gridPane.add(btnMan2Foot,4,12);
        gridPane.setHgap(10);//設定表格單元間的橫向間距
        gridPane.setVgap(20);//設定表格單元間的縱向間距
        gridPane.setId("gridPane");

        btnClothes.setOnAction(e->{
            Photo();//呼叫方法將imageVIEW賦值
            gridPane.add(imageView,1,8); //將圖片顯示到界面上

        });
        btnMan2Clothes.setOnAction(e->{
            Man2Photo();
            gridPane.add(Man2ClothesImageView,2,8);
        });
        btnHand.setOnAction(e->{
            HandPhoto();
           gridPane.add(HandImageView,1,6);
        });
        btnMan2Hand.setOnAction(e->{
            Man2HandPhoto();
            gridPane.add(Man2HandImageView,2,6);
        });
        btnHat.setOnAction(e->{
            HeadPhoto();
            gridPane.add(HeadImageView,1,4);
        });
        btnMan2Hat.setOnAction(e->{
            Man2HeadPhoto();
            gridPane.add(Man2headImageView,2,4);
        });
        btnFoot.setOnAction(e->{
            FootPhoto();
            gridPane.add(FootImageView,1,9);
        });
        btnMan2Foot.setOnAction(e->{
            Man2FootPhoto();
            gridPane.add(Man2footImageView,2,9);
        });
        btnOk.setOnAction(e->{
            BaseScene.man1.clothes.setImage(image); //更換衣服
            BaseScene.man1.head.setImage(HeadImage);
            BaseScene.man1.hand.setImage(HandImage);
            BaseScene.man1.foot1.setImage(FootImage);
            BaseScene.man1.foot2.setImage(FootImage);
            BaseScene.man1.body.setImage(BodyImage);
//            gridPane.add(BaseScene.man1,3,1);
        });
        btnMan2Ok.setOnAction(e->{
            BaseScene.man2.clothes.setImage(Man2ClothesImage); //更換衣服
            BaseScene.man2.head.setImage(Man2headImage);
            BaseScene.man2.hand.setImage(Man2HandImage);
            BaseScene.man2.foot1.setImage(Man2footImage);
            BaseScene.man2.foot2.setImage(Man2footImage);
            BaseScene.man2.body.setImage(Man2BodyImage);
        });
        btnBody.setOnAction(e->{
            BodyPhoto();
            gridPane.add(BodyImageView,1,5);
        });
        btnMan2Body.setOnAction(e->{
            Man2BodyPhoto();
            gridPane.add(Man2BodyImageView,2,5);
        });

        Scene scene = new Scene(gridPane,1000,600);

        scene.getStylesheets().add(ReplaceClothes.class.getResource("replaceClothes.css").toExternalForm());

        Stage primaryStage = new Stage();
        primaryStage.setTitle("換裝");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public Image GetImage(){
        return this.image;
    }
}

換裝頁面由于用的是gridpane所以css樣式不同于正常的節點,我這邊加了沒有顯示,如果想加的話去看官方檔案吧,
在這里插入圖片描述
這邊是jar包,
圖片資源想要的話私信發下郵箱,看緣分發,主要是提供代碼,提供思路,因為演算法實作還蠻難的,
資料庫設計如下,
在這里插入圖片描述
登陸界面流程圖就是用powerdesigner畫的
在這里插入圖片描述
要注意的點其實很多,不過注釋寫的很清楚,多看應該還是可以看懂的,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/235577.html

標籤:其他

上一篇:演算法實作自動掃雷游戲

下一篇:Unity中實作點擊按鈕進行角色切換的具體操作。

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more