Advertisement
Guest User

Untitled

a guest
May 4th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.38 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.animation.AnimationTimer;
  4. import javafx.event.EventHandler;
  5. import javafx.fxml.FXML;
  6. import javafx.scene.canvas.Canvas;
  7. import javafx.scene.canvas.GraphicsContext;
  8. import javafx.scene.control.Label;
  9. import javafx.scene.image.Image;
  10. import javafx.scene.image.ImageView;
  11. import javafx.scene.input.KeyCode;
  12. import javafx.scene.input.MouseEvent;
  13. import javafx.scene.layout.GridPane;
  14. import javafx.scene.paint.Color;
  15.  
  16.  
  17. import java.io.*;
  18. import java.text.SimpleDateFormat;
  19. import java.util.*;
  20.  
  21. /**
  22.  * Created by katrin on 2/29/16.
  23.  */
  24. public class House implements Serializable {
  25.     public static Image t;
  26.  
  27.     private boolean game_stopped = false;
  28.     public KeyProccessing k;
  29.     public ArrayList<DrawItems> items;
  30.     public GraphicsContext gc;
  31.  
  32.     @FXML
  33.     public Label score_l;
  34.     public Label count_cheese;
  35.     public Label time_l;
  36.     public Label fps_label;
  37.     public Label game_over;
  38.     public Label win;
  39.  
  40.     public ImageView pause;
  41.     public ImageView play;
  42.     public ImageView restart;
  43.  
  44.     public GridPane control_p;
  45.     public Canvas canvas;
  46.  
  47.     private int fps = 0;
  48.     private long prevFpsNano = 0;
  49.  
  50.     private SimpleDateFormat sdf;
  51.     private Date curLevelTime;
  52.     private Date finishLevelTime;
  53.  
  54.     private long show_timeD()
  55.     {
  56.         long diff = finishLevelTime.getTime() - curLevelTime.getTime();
  57.         diff = Math.max(diff, 0);
  58.         Date difference = new Date(diff + 75600000);
  59.         if (diff < 11000)
  60.         {
  61.             time_l.setTextFill(Color.RED);
  62.         }
  63.         time_l.setText("Time: " + sdf.format(difference));
  64.         return diff;
  65.     }
  66.  
  67.     private void show_fps()
  68.     {
  69.         fps_label.setText(String.format("fps = %d", fps));
  70.         fps = 0;
  71.         prevFpsNano = System.nanoTime();
  72.     }
  73.  
  74.     private void show_score_l(MouseJerry m)
  75.     {
  76.         score_l.setText(String.format("Score: %d", m.getScore()));
  77.         count_cheese.setText(String.format("Cheese: %d", m.see_cheese(m)));
  78.     }
  79.  
  80.     private void show_lWin() {
  81.         game_over.setVisible(true);
  82.         canvas.setOpacity(0.8);
  83.         game_stopped = true;
  84.     }
  85.  
  86.  
  87.  
  88.     private void stop_pressed(KeyProccessing k)
  89.     {
  90.         HashSet<KeyCode> state = k.retHashSet();
  91.         for (KeyCode key: state)
  92.         {
  93.             if (key == KeyCode.P)
  94.             {
  95.                 game_stopped = game_stopped ^ true;
  96.             }
  97.         }
  98.     }
  99.  
  100.     public void initialize()  throws IOException
  101.     {
  102.         canvas.setFocusTraversable(true);
  103.  
  104.         sdf = new SimpleDateFormat("HH:mm:ss");
  105.  
  106.         t = new Image("/maps/lev1-1.jpg");
  107.         k = new KeyProccessing(canvas);
  108.         items = new ArrayList<DrawItems>();
  109.  
  110.         items.add(new LittleChesse(400, 130));
  111.         items.add(new LittleChesse(185, 300));
  112.         items.add(new Big_cheese(680, 340));
  113.         items.add(new Big_cheese(100, 600));
  114.         items.add(new Big_cheese(1180, 300));
  115.         items.add(new Big_cheese(900, 100));
  116.         items.add(new Big_cheese(1030, 620));
  117.  
  118.         items.add(new BlackOil(640, 120));
  119.         items.add(new BlackOil(110, 400));
  120.         items.add(new BlackOil(450, 300));
  121.         items.add(new BlackOil(1203, 512));
  122.  
  123.         items.add(new Hole(1150, 100));
  124.         items.add(new Clock(1000, 200));
  125.         items.add(new Clock(220, 620));
  126.         items.add(new Flower(270, 100));
  127.  
  128.         items.add(new Mousetrap(600, 600));
  129.         items.add(new MouseJerry(100, 150, k));
  130.  
  131.         curLevelTime = Calendar.getInstance().getTime();
  132.         long level_time = ((MouseJerry) items.get(items.size() - 1)).life_time;
  133.         finishLevelTime = new Date(Calendar.getInstance().getTime().getTime() + level_time * 1000);
  134.  
  135.         items.add(new CatTom(200, 620));
  136.  
  137.         items.add(new VerticalWAlls(210, 210, 7));
  138.         items.add(new VerticalWAlls(770, 200, 5));
  139.         items.add(new VerticalWAlls(1080, 10, 10));
  140.         items.add(new VerticalWAlls(1095, 10, 10));
  141.         items.add(new VerticalWAlls(42, 10, 60));
  142.         items.add(new VerticalWAlls(Constants.MAP_WIDTH - 110, 0 , 60));
  143.  
  144.  
  145.         items.add(new HorizontalWalls(220, 494, 14));
  146.         items.add(new HorizontalWalls(220, 470, 14));
  147.         items.add(new HorizontalWalls(370, 230, 10));
  148.         items.add(new HorizontalWalls(370, 190, 11));
  149.         items.add(new HorizontalWalls(0, Constants.MAP_HEIGHT - 100, 62));
  150.         items.add(new HorizontalWalls(0, 43, 62));
  151.  
  152.         gc = canvas.getGraphicsContext2D();
  153.  
  154.         pause.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
  155.             @Override
  156.             public void handle(MouseEvent event) {
  157.                 game_stopped = game_stopped ^ true;
  158.                 control_p.setVisible(true);
  159.             }
  160.         });
  161.  
  162.         play.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
  163.             @Override
  164.             public void handle(MouseEvent event) {
  165.                 canvas.setOpacity(0.7);
  166.                 game_over.setVisible(false);
  167.                 control_p.setVisible(false);
  168.                 game_stopped = game_stopped ^ true;
  169.             }
  170.         });
  171.  
  172.         restart.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
  173.             @Override
  174.             public void handle(MouseEvent event) {
  175.                 try {
  176.                     canvas.setOpacity(1);
  177.                     win.setVisible(false);
  178.                     game_over.setVisible(false);
  179.                     load();
  180.                     game_stopped = game_stopped ^true;
  181.                     control_p.setVisible(false);
  182.                 } catch (Exception e){ }
  183.             }
  184.         });
  185.  
  186.  
  187.  
  188.         //main loop
  189.         new AnimationTimer() {
  190.             public void handle(long startNanoTime) {
  191.                 make_map(gc);
  192.                 curLevelTime = Calendar.getInstance().getTime();
  193.                 long temp = show_timeD();
  194.                 try {
  195.                     check(k);
  196.                 } catch (IOException e){
  197.                 }
  198.  
  199.                 stop_pressed(k);
  200.  
  201.                 if (temp == 0) {
  202.                     game_stopped = true;
  203.                     game_over.setVisible(true);
  204.                 }
  205.                 if (!game_stopped) {
  206.  
  207.                     Iterator<DrawItems> it = items.iterator();
  208.  
  209.                     while (it.hasNext()) {
  210.                         DrawItems current = it.next();
  211.  
  212.                         if (current instanceof IMoveble) {
  213.                             ((IMoveble) current).Move(items);
  214.                         }
  215.                         if (current instanceof MouseJerry) {
  216.                             if (((MouseJerry) current).isDead == true) {
  217.                                 show_lWin();
  218.                             }
  219.                         }
  220.                     }
  221.  
  222.                     it = items.iterator();
  223.  
  224.                     while (it.hasNext()) {
  225.                         DrawItems current = it.next();
  226.  
  227.                         if (current instanceof IEatable) {
  228.                             if (((IEatable) current).isEaten() == true) {
  229.                                 it.remove();
  230.                             }
  231.                         }
  232.                     }
  233.  
  234.  
  235.                     long currentNanoTime = System.nanoTime();
  236.                     while (System.nanoTime() - startNanoTime < 200000) {
  237.                     }
  238.                 }
  239.  
  240.                 fps++;
  241.                 if (Math.abs(prevFpsNano - System.nanoTime()) >= 1000000000) {
  242.                     show_fps();
  243.                 }
  244.  
  245.                 for (DrawItems obj : items) {
  246.                     if (obj instanceof MouseJerry){
  247.                         show_score_l((MouseJerry) obj);
  248.                     }
  249.                     obj.draw(gc);
  250.                 }
  251.             }
  252.         }.start();
  253.  
  254.  
  255.     }
  256.  
  257.     public void make_map(GraphicsContext gc) {
  258.         gc.drawImage(t, 0, 0, Constants.MAP_WIDTH, Constants.MAP_HEIGHT);
  259.     }
  260.  
  261.  
  262.     public void check(KeyProccessing k) throws IOException
  263.     {
  264.         HashSet<KeyCode> temp = k.retHashSet();
  265.         for (KeyCode i : temp){
  266.             if (i == KeyCode.S) {
  267.                 save();
  268.                 break;
  269.             }
  270.             if(i == KeyCode.L){
  271.                 load();
  272.                 break;
  273.             }
  274.         }
  275.     }
  276.  
  277.     public void save() throws IOException {
  278.         FileOutputStream fos = new FileOutputStream("out.txt");
  279.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  280.         oos.writeObject(items);
  281.         oos.flush();
  282.         oos.close();
  283.         fos.close();
  284.     }
  285.  
  286.     public void load() throws IOException {
  287.         FileInputStream fis = new FileInputStream("out.txt");
  288.         ObjectInputStream oin = new ObjectInputStream(fis);
  289.         try{
  290.             items = (ArrayList<DrawItems>) oin.readObject();
  291.             System.out.print(items);
  292.         } catch (Exception e){
  293.             System.out.println(e);
  294.         }
  295.         for (Object i : items){
  296.             if( i instanceof MouseJerry){
  297.                 ((MouseJerry) i).set_keypr(k);
  298.                 ((MouseJerry) i).setPicture(1);
  299.                     curLevelTime = Calendar.getInstance().getTime();
  300.                     finishLevelTime = new Date(Calendar.getInstance().getTime().getTime() + ((MouseJerry) i).life_time * 1000);
  301.                 break;
  302.             }
  303.         }
  304.         fis.close();
  305.         oin.close();
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement