Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  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.  
  10. public class GameWindow extends JFrame {
  11.  
  12.     private static GameWindow game_window;
  13.     private static long last_frame_time;
  14.     private static Image background;
  15.     private static Image game_over;
  16.     private static Image drop;
  17.     private static float drop_left = 200;
  18.     private static float drop_top = -100;
  19.     private static float drop_v = 200;
  20.     private static int score;
  21.  
  22.     public static void main(String[] args) throws IOException {
  23.         background = ImageIO.read(GameWindow.class.getResourceAsStream("background.jpg"));
  24.         game_over = ImageIO.read(GameWindow.class.getResourceAsStream("game_over.png"));
  25.         drop = ImageIO.read(GameWindow.class.getResourceAsStream("drop.png"));
  26.         game_window = new GameWindow();
  27.         game_window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  28.         game_window.setLocation(200, 100);
  29.         game_window.setSize(906, 478);
  30.         game_window.setResizable(false);
  31.         game_window.setTitle("Catch the Mayu! Score: " + score);
  32.         last_frame_time = System.nanoTime();
  33.         final GameField game_field = new GameField();
  34.         game_field.addMouseListener(new MouseAdapter() {
  35.             @Override
  36.             public void mousePressed(MouseEvent e) {
  37.                 int x = e.getX();
  38.                 int y = e.getY();
  39.                 float drop_right = drop_left + drop.getWidth(null);
  40.                 float drop_bottom = drop_top + drop.getHeight(null);
  41.                 boolean is_drop = x >= drop_left && x <= drop_right && y >= drop_top && y <= drop_bottom;
  42.                 if(is_drop) {
  43.                     drop_top = -100;
  44.                     drop_left = (int) (Math.random() * (game_field.getWidth() - drop.getWidth(null)));
  45.                     drop_v = drop_v + 20;
  46.                     score++;
  47.                     game_window.setTitle("Catch the Mayu! Score: " + score);
  48.                 }
  49.             }
  50.         });
  51.         game_window.add(game_field);
  52.         game_window.setVisible(true);
  53.  
  54.     }
  55.  
  56.     private static void onRepaint(Graphics g){
  57.         long current_time = System.nanoTime();
  58.         float delta_time = (current_time - last_frame_time) * 0.000000001f;
  59.         last_frame_time = current_time;
  60.         drop_top = drop_top + drop_v * delta_time;
  61.         g.drawImage(background, 0 , 0, null );
  62.         g.drawImage(drop, (int)drop_left, (int)drop_top, null);
  63.         if(drop_top > game_window.getHeight())
  64.             g.drawImage(game_over, 200, 140, null);
  65.  
  66.     }
  67.  
  68.     private static class GameField extends JPanel{
  69.         @Override
  70.         protected void paintComponent(Graphics g){
  71.             super.paintComponent(g);
  72.             onRepaint(g);
  73.             repaint();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement