Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. package test;
  2.  
  3. public class Coordy {
  4. int x;
  5. int y;
  6. public Coordy(int x, int y) {
  7.     this.x=x;
  8.     this.y=y;
  9. }
  10. }
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. package test;
  34.  
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. import java.util.Random;
  38.  
  39. import javax.swing.JOptionPane;
  40.  
  41. import javafx.animation.KeyFrame;
  42. import javafx.animation.Timeline;
  43. import javafx.application.Application;
  44. import javafx.event.EventHandler;
  45. import javafx.scene.*;
  46. import javafx.scene.layout.Pane;
  47. import javafx.scene.shape.Circle;
  48. import javafx.scene.shape.Rectangle;
  49. import javafx.util.Duration;
  50. import javafx.scene.paint.Color;
  51. import javafx.scene.input.*;
  52. import javafx.stage.Stage;
  53.  
  54. public class Aplikacja extends Application{
  55.    
  56.     private static int pom = 1;
  57.     public static int xS=100;
  58.     public static int yS=280;
  59.     public static int xO;
  60.     public static int yO;
  61.     public static int zmiana = 20;
  62.     public static int dlugosc = 1;
  63.     public static boolean owocek=false;
  64.     public static Circle owoc;
  65.     public static int iloscPkt = 0;
  66.    
  67.  
  68.     public void start(Stage primaryStage) {
  69.        
  70.         Pane root = new Pane();    
  71.         Scene scene = new Scene(root,600,600);
  72.                
  73.         Circle kolo = new Circle(xS, yS, 20.0f);
  74.         kolo.setFill(Color.GREEN);
  75.    
  76.         root.getChildren().add(kolo);  
  77.        
  78.         KeyFrame owoce = new KeyFrame(Duration.millis(500), event-> {
  79.             if(owocek==false) {
  80.             Random ow = new Random();
  81.             xO=(ow.nextInt(30)*20);
  82.             yO=(ow.nextInt(30)*20);
  83.            
  84.             owoc = new Circle(xO,yO,10);
  85.             System.out.println("OWOC" + xO + " " + yO);    
  86.            
  87.             root.getChildren().add(owoc);          
  88.             owocek=true;
  89.             }
  90.         });
  91.        
  92.        
  93.         Map<Integer, Coordy> wspS = new HashMap<Integer, Coordy>();
  94.         wspS.put(dlugosc, new Coordy(xS,yS));
  95.        
  96.        
  97.         KeyFrame test4 = new KeyFrame(Duration.millis(300), event-> {
  98.  
  99.         switch(pom) {                                               //zmiana wsp weza
  100.         case 1:
  101.            
  102.             for(int i = dlugosc; i>1;i--) {        
  103.                 wspS.get(i).x=wspS.get(i-1).x;
  104.                 wspS.get(i).y=wspS.get(i-1).y;
  105.             }
  106.            
  107.             xS+=zmiana;
  108.             kolo.setTranslateX(xS);
  109.            
  110.             wspS.get(1).x=xS;
  111.             wspS.get(1).y=yS;
  112.            
  113.                                     System.out.println("xS" + xS + " " + yS + " " + pom);
  114.             break;
  115.         case 2:
  116.             for(int i = dlugosc; i>1;i--) {        
  117.                 wspS.get(i).x=wspS.get(i-1).x;
  118.                 wspS.get(i).y=wspS.get(i-1).y;
  119.             }
  120.            
  121.             yS+=zmiana;
  122.             kolo.setTranslateY(yS);
  123.            
  124.             wspS.get(1).x=xS;
  125.             wspS.get(1).y=yS;
  126.            
  127.                                     System.out.println("xS" + xS + " " + yS + " " + pom);
  128.             break;
  129.         case 3:
  130.             for(int i = dlugosc; i>1;i--) {        
  131.                 wspS.get(i).x=wspS.get(i-1).x;
  132.                 wspS.get(i).y=wspS.get(i-1).y;
  133.             }
  134.            
  135.             xS-=zmiana;
  136.             kolo.setTranslateX(xS);
  137.            
  138.             wspS.get(1).x=xS;
  139.             wspS.get(1).y=yS;
  140.            
  141.                                     System.out.println("xS" + xS + " " + yS + " " + pom);
  142.             break;
  143.         case 4:
  144.             for(int i = dlugosc; i>1;i--) {        
  145.                 wspS.get(i).x=wspS.get(i-1).x;
  146.                 wspS.get(i).y=wspS.get(i-1).y;
  147.             }
  148.            
  149.             yS-=zmiana;
  150.             kolo.setTranslateY(yS);
  151.            
  152.             wspS.get(1).x=xS;
  153.             wspS.get(1).y=yS;
  154.            
  155.                                     System.out.println("xS" + xS + " " + yS + " " + pom);
  156.             break;     
  157.         }
  158.            
  159.         scene.setOnKeyPressed(new EventHandler<KeyEvent>() {        //klawisz
  160.               @Override
  161.               public void handle(KeyEvent event) {
  162.                 switch (event.getCode()) {      
  163.                 case RIGHT: pom=1; break;
  164.                 case DOWN:  pom=2; break;            
  165.                 case LEFT:  pom=3; break;
  166.                   case UP:  pom=4; break;
  167.                 }
  168.               }    
  169.         });
  170.            
  171.  
  172.         if(wspS.get(dlugosc).x==xO && wspS.get(dlugosc).y==yO) {    //zbieranie owocow
  173.             owocek=false;
  174.             root.getChildren().remove(owoc);
  175.             wspS.put(dlugosc, new Coordy(xS-zmiana,yS-zmiana));
  176.             Circle snake = new Circle(xS-zmiana, yS-zmiana,20);//obliczyc
  177.             root.getChildren().add(snake);
  178.            
  179.             iloscPkt++;
  180.         }
  181.        
  182.         if( xS==600 || xS==0 || yS==600 || yS==0) {        
  183.             Object result = JOptionPane.showInputDialog("Podaj imie: ");
  184.         }
  185.    
  186.         });
  187.        
  188.         Timeline tm = new Timeline();
  189.         Timeline tm1 = new Timeline();
  190.         tm.getKeyFrames().add(test4);
  191.    
  192.         tm1.getKeyFrames().add(owoce);
  193.         tm.setCycleCount(Timeline.INDEFINITE);
  194.         tm.play();
  195.        
  196.         tm1.setCycleCount(Timeline.INDEFINITE);
  197.         tm1.play();
  198.        
  199.        
  200.        
  201.        
  202.         //wspolrzedne ?
  203.        
  204.         Rectangle xxx = new Rectangle(0,0,50,50);
  205.         xxx.setFill(Color.RED);
  206.         root.getChildren().add(xxx);
  207.         Rectangle xxx1 = new Rectangle(580,0,50,50);
  208.         xxx1.setFill(Color.BLUE);
  209.         root.getChildren().add(xxx1);
  210.         Rectangle xxx2 = new Rectangle(0,580,50,50);
  211.         xxx2.setFill(Color.GREEN);
  212.         root.getChildren().add(xxx2);
  213.         Rectangle xxx3 = new Rectangle(580,580,50,50);
  214.         xxx3.setFill(Color.YELLOW);
  215.         root.getChildren().add(xxx3);
  216.        
  217.        
  218.        
  219.        
  220.        
  221.         primaryStage.setTitle("Snake");
  222.         primaryStage.setScene(scene);
  223.         primaryStage.show();   
  224.        
  225.     }
  226.  
  227.     public static void main(String[] args) {
  228.        
  229.        
  230.        
  231.         launch(args);
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement