Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- public class Coordy {
- int x;
- int y;
- public Coordy(int x, int y) {
- this.x=x;
- this.y=y;
- }
- }
- package test;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import javax.swing.JOptionPane;
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.event.EventHandler;
- import javafx.scene.*;
- import javafx.scene.layout.Pane;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.util.Duration;
- import javafx.scene.paint.Color;
- import javafx.scene.input.*;
- import javafx.stage.Stage;
- public class Aplikacja extends Application{
- private static int pom = 1;
- public static int xS=100;
- public static int yS=280;
- public static int xO;
- public static int yO;
- public static int zmiana = 20;
- public static int dlugosc = 1;
- public static boolean owocek=false;
- public static Circle owoc;
- public static int iloscPkt = 0;
- public void start(Stage primaryStage) {
- Pane root = new Pane();
- Scene scene = new Scene(root,600,600);
- Circle kolo = new Circle(xS, yS, 20.0f);
- kolo.setFill(Color.GREEN);
- root.getChildren().add(kolo);
- KeyFrame owoce = new KeyFrame(Duration.millis(500), event-> {
- if(owocek==false) {
- Random ow = new Random();
- xO=(ow.nextInt(30)*20);
- yO=(ow.nextInt(30)*20);
- owoc = new Circle(xO,yO,10);
- System.out.println("OWOC" + xO + " " + yO);
- root.getChildren().add(owoc);
- owocek=true;
- }
- });
- Map<Integer, Coordy> wspS = new HashMap<Integer, Coordy>();
- wspS.put(dlugosc, new Coordy(xS,yS));
- KeyFrame test4 = new KeyFrame(Duration.millis(300), event-> {
- switch(pom) { //zmiana wsp weza
- case 1:
- for(int i = dlugosc; i>1;i--) {
- wspS.get(i).x=wspS.get(i-1).x;
- wspS.get(i).y=wspS.get(i-1).y;
- }
- xS+=zmiana;
- kolo.setTranslateX(xS);
- wspS.get(1).x=xS;
- wspS.get(1).y=yS;
- System.out.println("xS" + xS + " " + yS + " " + pom);
- break;
- case 2:
- for(int i = dlugosc; i>1;i--) {
- wspS.get(i).x=wspS.get(i-1).x;
- wspS.get(i).y=wspS.get(i-1).y;
- }
- yS+=zmiana;
- kolo.setTranslateY(yS);
- wspS.get(1).x=xS;
- wspS.get(1).y=yS;
- System.out.println("xS" + xS + " " + yS + " " + pom);
- break;
- case 3:
- for(int i = dlugosc; i>1;i--) {
- wspS.get(i).x=wspS.get(i-1).x;
- wspS.get(i).y=wspS.get(i-1).y;
- }
- xS-=zmiana;
- kolo.setTranslateX(xS);
- wspS.get(1).x=xS;
- wspS.get(1).y=yS;
- System.out.println("xS" + xS + " " + yS + " " + pom);
- break;
- case 4:
- for(int i = dlugosc; i>1;i--) {
- wspS.get(i).x=wspS.get(i-1).x;
- wspS.get(i).y=wspS.get(i-1).y;
- }
- yS-=zmiana;
- kolo.setTranslateY(yS);
- wspS.get(1).x=xS;
- wspS.get(1).y=yS;
- System.out.println("xS" + xS + " " + yS + " " + pom);
- break;
- }
- scene.setOnKeyPressed(new EventHandler<KeyEvent>() { //klawisz
- @Override
- public void handle(KeyEvent event) {
- switch (event.getCode()) {
- case RIGHT: pom=1; break;
- case DOWN: pom=2; break;
- case LEFT: pom=3; break;
- case UP: pom=4; break;
- }
- }
- });
- if(wspS.get(dlugosc).x==xO && wspS.get(dlugosc).y==yO) { //zbieranie owocow
- owocek=false;
- root.getChildren().remove(owoc);
- wspS.put(dlugosc, new Coordy(xS-zmiana,yS-zmiana));
- Circle snake = new Circle(xS-zmiana, yS-zmiana,20);//obliczyc
- root.getChildren().add(snake);
- iloscPkt++;
- }
- if( xS==600 || xS==0 || yS==600 || yS==0) {
- Object result = JOptionPane.showInputDialog("Podaj imie: ");
- }
- });
- Timeline tm = new Timeline();
- Timeline tm1 = new Timeline();
- tm.getKeyFrames().add(test4);
- tm1.getKeyFrames().add(owoce);
- tm.setCycleCount(Timeline.INDEFINITE);
- tm.play();
- tm1.setCycleCount(Timeline.INDEFINITE);
- tm1.play();
- //wspolrzedne ?
- Rectangle xxx = new Rectangle(0,0,50,50);
- xxx.setFill(Color.RED);
- root.getChildren().add(xxx);
- Rectangle xxx1 = new Rectangle(580,0,50,50);
- xxx1.setFill(Color.BLUE);
- root.getChildren().add(xxx1);
- Rectangle xxx2 = new Rectangle(0,580,50,50);
- xxx2.setFill(Color.GREEN);
- root.getChildren().add(xxx2);
- Rectangle xxx3 = new Rectangle(580,580,50,50);
- xxx3.setFill(Color.YELLOW);
- root.getChildren().add(xxx3);
- primaryStage.setTitle("Snake");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement