| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3854 人关注过本帖
标题:javaFX的timeline时间轴循环??贪吃蛇
取消只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:1 
javaFX的timeline时间轴循环??贪吃蛇
程序代码:
package application;
    
import java.util.Iterator;
import java.util.LinkedList;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;



public class Main extends Application 
{
    
    @Override
    public void start(Stage stage)
    {
        
        
            Group g = new Group();
            
            Scene scene = new Scene(g,600,600);
            
            //画蛇
            LinkedList<Circle> list = new LinkedList<>();
            for(int i=0;i<5;i++)
            {
                Circle cir = new Circle();
                cir.setCenterX(275-35*i);
                cir.setCenterY(200);
                cir.setRadius(20);
                cir.setFill(Color.rgb(0, 255 ,127));
                list.add(cir);
                
            }
            
                Arc arc = new Arc();
                arc.setFill(Color.rgb(0, 255 ,127) );
                arc.setCenterX(310);
                arc.setCenterY(200);
                arc.setRadiusX(20);
                arc.setRadiusY(20);
                arc.setStartAngle(45);
                arc.setLength(270);
                arc.setType(ArcType.ROUND);
                
                class Move
                {
                    public void A()
                    {

                        System.out.println("A");
                        Timeline timeline=new Timeline();
                        KeyValue ARCxValue = new KeyValue(arc.centerXProperty(),arc.getCenterX()-50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCxValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
                        timeline.play();
                    }
                    public void W()
                    {
                        
                        System.out.println("W");
                        Timeline timeline=new Timeline();
                        //head
                        KeyValue ARCyValue = new KeyValue(arc.centerYProperty(),arc.getCenterY()-50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCyValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
                
        
                        timeline.play();
                        
                        
                    }
                    public void S()
                    {
                        System.out.println("S");
                        Timeline timeline=new Timeline();
                        KeyValue ARCyValue = new KeyValue(arc.centerYProperty(),arc.getCenterY()+50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCyValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
                        
                        
                        timeline.play();

                    }
                    public void D()
                    {
                        System.out.println("D");
                        Timeline timeline=new Timeline();
                        KeyValue ARCxValue = new KeyValue(arc.centerXProperty(),arc.getCenterX()+50);
                        KeyFrame ARCkeyFrame=new KeyFrame(Duration.millis(1000), ARCxValue);
                        timeline.getKeyFrames().add(ARCkeyFrame);
                        
                        //body
                        Iterator<Circle> iterator = list.iterator();
                        Circle cir1 = iterator.next();
                        KeyValue cir1xValue = new KeyValue(cir1.centerXProperty(),arc.getCenterX());
                        KeyValue cir1yValue = new KeyValue(cir1.centerYProperty(),arc.getCenterY());
                        KeyFrame keyFrame1=new KeyFrame(Duration.millis(1000), cir1xValue,cir1yValue);
                        timeline.getKeyFrames().add(keyFrame1);
                        
                        
                        Circle cir3 = iterator.next();
                        KeyValue cir3xValue = new KeyValue(cir3.centerXProperty(),cir1.getCenterX());
                        KeyValue cir3yValue = new KeyValue(cir3.centerYProperty(),cir1.getCenterY());
                        KeyFrame keyFrame2=new KeyFrame(Duration.millis(1000), cir3xValue,cir3yValue);
                        timeline.getKeyFrames().add(keyFrame2);
                        
                        Circle cir4 = iterator.next();
                        KeyValue cir4xValue = new KeyValue(cir4.centerXProperty(),cir3.getCenterX());
                        KeyValue cir4yValue = new KeyValue(cir4.centerYProperty(),cir3.getCenterY());
                        KeyFrame keyFrame3=new KeyFrame(Duration.millis(1000), cir4xValue,cir4yValue);
                        timeline.getKeyFrames().add(keyFrame3);
                        
                        Circle cir5 = iterator.next();
                        KeyValue cir5xValue = new KeyValue(cir5.centerXProperty(),cir4.getCenterX());
                        KeyValue cir5yValue = new KeyValue(cir5.centerYProperty(),cir4.getCenterY());
                        KeyFrame keyFrame4=new KeyFrame(Duration.millis(1000), cir5xValue,cir5yValue);
                        timeline.getKeyFrames().add(keyFrame4);
                        
                        Circle cir6 = iterator.next();
                        KeyValue cir6xValue = new KeyValue(cir6.centerXProperty(),cir5.getCenterX());
                        KeyValue cir6yValue = new KeyValue(cir6.centerYProperty(),cir5.getCenterY());
                        KeyFrame keyFrame5=new KeyFrame(Duration.millis(1000), cir6xValue,cir6yValue);
                        timeline.getKeyFrames().add(keyFrame5);
                        
                        timeline.play();
                    }
                }
                
                //监听键盘事件
            scene.setOnKeyReleased(new EventHandler<KeyEvent>() 
            {
                
                public void handle(KeyEvent event) 
                {
                     Move move = new Move();
                     
                    switch(event.getCode())
                    {
                    case W:
                    {        
                            move.W();
                            break;
                    }
                    case S:
                    {
                        move.S();
                        break;
                    }
                        
                    case A:
                        
                    {
                        move.A();
                    break;
                        
                    }
                    case D:
                    {
                        move.D();
                        break;
                    }
                    }
                    }
                    
                }
              
                
            
               
            
            );
            
            
            
    
            g.getChildren().addAll(list);
            g.getChildren().add(arc);
            
           
            stage.setScene(scene);
            stage.show();
            
            
            
                        
    }
    
    
    public static void main(String[] args) {
        launch(args);
    }
}


做的一个贪吃蛇的小游戏,设置wasd四个按键只能单步实现移动,加while循环似乎不行

求问:怎么实现按下一个键可以一直执行单步的循环移动???
搜索更多相关主题的帖子: javaFX Iterator new add next 
2018-10-01 19:28
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
收藏
得分:0 
回复 3楼 林月儿
月姐姐,你的循环优化做的挺好的。谢谢啦!!
不过这个地方没太懂?

new Timer().schedule(new TimerTask() {
            
            @Override
            public  void run() {
                if(keyCode == null) return;
                DoubleProperty dire = null;
                double arcLoc = 0;
                switch(keyCode)
                {
                case "W":
                case "w":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() - 50;
                    break;
                case "S":
                case "s":
                    dire = arc.centerYProperty();
                    arcLoc = arc.getCenterY() + 50;
                    break;
                case "A":
                case "a":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() - 50;
                    break;
                case "D":
                case "d":
                    dire = arc.centerXProperty();
                    arcLoc = arc.getCenterX() + 50;
                    break;
                }
                Move move = new Move();
                if(dire != null) {
                    move.moveObj(dire, arcLoc);
                }
            }
        }, 10, 1000);
    }


这个是控制什么的??
2018-10-03 12:30
快速回复:javaFX的timeline时间轴循环??贪吃蛇
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024998 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved