SHARE
TWEET

Untitled

a guest Sep 17th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import javax.imageio.ImageIO;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.io.IOException;
  9. import java.lang.invoke.ConstantCallSite;
  10.  
  11. public class GameWindow extends JFrame {
  12.  
  13.  
  14.     private static GameWindow game_window;
  15.     private static long LastFrameTime;
  16.     private static Image background;
  17.     private static Image drop;
  18.     private static Image GameOver;
  19.     private static Image restart;
  20.     private static float drop_left = 200;
  21.     private static float drop_top = 100;
  22.     private static float drop_v = 200;
  23.     private static int score = 0;
  24.     private static boolean end;
  25.     private static float drop_width = 100;
  26.     private static float drop_height = 152;
  27.     private static boolean pause = false;
  28.     private static float drop_speed_save = 0;
  29.  
  30.     private static double mousecordX = 0;
  31.     private static double mousecordY = 0;
  32.  
  33.  
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.  
  37.         background= ImageIO.read(GameWindow.class.getResourceAsStream("background.png"));
  38.         drop = ImageIO.read(GameWindow.class.getResourceAsStream("drop.png")).
  39.                 getScaledInstance (100, 100, Image.SCALE_DEFAULT);
  40.         GameOver = ImageIO.read(GameWindow.class.getResourceAsStream("GameOver.jpg")).
  41.                 getScaledInstance (300, 200, Image.SCALE_DEFAULT);
  42.         restart = ImageIO.read(GameWindow.class.getResourceAsStream("restart.jpg"));
  43.         game_window = new GameWindow();
  44.         game_window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  45.         game_window.setLocation (200, 100);
  46.         game_window.setSize(1200, 850);
  47.         game_window.setResizable(false);
  48.         LastFrameTime=System.nanoTime();
  49.         GameField game_field = new GameField();
  50.         game_field.addMouseListener(new MouseAdapter() {
  51.             @Override
  52.             public void mousePressed(MouseEvent e) {
  53.                 if(e.getButton() == MouseEvent.BUTTON3){
  54.                     if(pause) {
  55.                         pause = false;
  56.                         drop_v = drop_speed_save;
  57.                     }
  58.                     else{
  59.                         drop_speed_save = drop_v;
  60.                         drop_v = 0;
  61.                         mousecordX = MouseInfo.getPointerInfo().getLocation().getX();
  62.                         mousecordY = MouseInfo.getPointerInfo().getLocation().getY();
  63.                         pause = true;
  64.                     }
  65.                 }
  66.                 if (pause) return;
  67.  
  68.                 int x = e.getX();
  69.                 int y = e.getY();
  70.  
  71.                 try {
  72.                     Robot r = new Robot();
  73.                     r.mouseMove((int)mousecordX, (int)mousecordY);
  74.                 }
  75.                 catch (AWTException ee){
  76.  
  77.                 }
  78.  
  79.  
  80.                 float drop_right = drop_left + drop.getWidth(null);
  81.                 float drop_bottom = drop_top + drop.getHeight(null);
  82.                 boolean is_drop = x >= drop_left && x <= drop_right && y >= drop_top && y <= drop_bottom;
  83.                 if (is_drop) {
  84.                     if(!(drop_height<=25 && drop_width <= 50)){
  85.                         drop_width = drop_width - 1;
  86.                         drop_height = drop_height - 2;
  87.                         try{
  88.                             dropResize();
  89.                         }
  90.                         catch (IOException ioe){
  91.  
  92.                         }
  93.  
  94.                     }
  95.  
  96.  
  97.                     drop_top= -100;
  98.                     drop_left =  (int) (Math.random() * (game_field.getWidth() - drop.getWidth(null)));
  99.                     drop_v = drop_v +30;
  100.                     score++;
  101.                     game_window.setTitle("Score: " + score);
  102.                 }
  103.                 if (end) {
  104.                     boolean isRestart = x>=250 && x <= 250 + restart.getWidth(null)
  105.                             && y >= 200 && y <= 200 + restart.getHeight(null);
  106.                     if (isRestart) {
  107.                         end = false;
  108.                         score = 0;
  109.                         game_window.setTitle("Score" + score);
  110.                         drop_top = -100;
  111.                         drop_left = (int)(Math.random()*(game_field.getWidth()-drop.getHeight(null)));
  112.                         drop_v = 200;
  113.                         drop_width = 100;
  114.                         drop_height = 152;
  115.  
  116.  
  117.                     }
  118.                 }
  119.             }
  120.         });
  121.         game_window.add (game_field);
  122.         game_window.setVisible(true);
  123.        }
  124.        private static void dropResize() throws IOException{
  125.            drop = ImageIO.read(GameWindow.class.getResourceAsStream("drop.png")).
  126.                    getScaledInstance ((int) drop_width,(int)drop_height, Image.SCALE_DEFAULT);
  127.        }
  128.  
  129.        private static void onRepaint(Graphics g){
  130.             long currentTime = System.nanoTime();
  131.             float delta_time = (currentTime - LastFrameTime)*0.000000001f;
  132.  
  133.             LastFrameTime = currentTime;
  134.             drop_top=drop_top+drop_v*delta_time;
  135.             g.drawImage(background, 0, 0, null);
  136.             g.drawImage(drop, (int) drop_left,(int) drop_top,null);
  137.             if (drop_top > game_window.getHeight()){
  138.                 g.drawImage(GameOver, 100, 100, null);
  139.                 g.drawImage(restart, 250, 200, null);
  140.                 end = true;
  141.             }
  142.  
  143.        }
  144.        private static class GameField extends JPanel{
  145.         @Override
  146.             protected void paintComponent(Graphics g){
  147.             super.paintComponent(g);
  148.             onRepaint(g);
  149.             repaint();
  150.         }
  151.        }
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top