Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 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. 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 = 98;
  26. private static float drop_height = 114;
  27. private static boolean pause = false;
  28. private static float drop_speed_save = 0;
  29. private static int direction=-1;
  30.  
  31. private static double mousecordX = 0;
  32. private static double mousecordY = 0;
  33.  
  34.  
  35.  
  36. public static void main(String[] args) throws IOException {
  37.  
  38. background= ImageIO.read(GameWindow.class.getResourceAsStream("background.jpg"));
  39. drop = ImageIO.read(GameWindow.class.getResourceAsStream("drop.png")).
  40. getScaledInstance (98, 114, Image.SCALE_DEFAULT);
  41. GameOver = ImageIO.read(GameWindow.class.getResourceAsStream("GameOver.png")).
  42. getScaledInstance (300, 200, Image.SCALE_DEFAULT);
  43. restart = ImageIO.read(GameWindow.class.getResourceAsStream("restart.png"));
  44. game_window = new GameWindow();
  45. game_window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  46. game_window.setLocation (200, 100);
  47. game_window.setSize(1000, 700);
  48. game_window.setResizable(false);
  49. LastFrameTime=System.nanoTime();
  50. GameField game_field = new GameField();
  51. game_field.addMouseListener(new MouseAdapter() {
  52. @Override
  53. public void mousePressed(MouseEvent e) {
  54. if(e.getButton() == MouseEvent.BUTTON3){
  55. if(pause) {
  56. pause = false;
  57. drop_v = drop_speed_save;
  58. }
  59. else{
  60. drop_speed_save = drop_v;
  61. drop_v = 0;
  62. mousecordX = MouseInfo.getPointerInfo().getLocation().getX();
  63. mousecordY = MouseInfo.getPointerInfo().getLocation().getY();
  64. pause = true;
  65. }
  66. }
  67. if (pause) return;
  68.  
  69. int x = e.getX();
  70. int y = e.getY();
  71.  
  72. try {
  73. Robot r = new Robot();
  74. r.mouseMove((int)mousecordX, (int)mousecordY);
  75. }
  76. catch (AWTException ee){
  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 - 5;
  86. drop_height = drop_height - 5;
  87. try{
  88. dropResize();
  89. }
  90. catch (IOException ioe){
  91.  
  92. }
  93.  
  94. }
  95.  
  96. drop_top= -100;
  97. drop_left = (int) (Math.random() * (game_field.getWidth() - drop.getWidth(null)));
  98. drop_v = drop_v +30;
  99. score++;
  100. game_window.setTitle("Score: " + score);
  101. }
  102. if (end) {
  103. boolean isRestart = x>=250 && x <= 250 + restart.getWidth(null)
  104. && y >= 200 && y <= 200 + restart.getHeight(null);
  105. if (isRestart) {
  106. end = false;
  107. score = 0;
  108. game_window.setTitle("Score" + score);
  109. drop_top = -100;
  110. drop_left = (int)(Math.random()*(game_field.getWidth()-drop.getHeight(null)));
  111. drop_v = 200;
  112. drop_width = 100;
  113. drop_height = 152;
  114.  
  115.  
  116. }
  117. }
  118. }
  119. });
  120. game_window.add (game_field);
  121. game_window.setVisible(true);
  122. }
  123. private static void dropResize() throws IOException{
  124. drop = ImageIO.read(GameWindow.class.getResourceAsStream("drop.png")).
  125. getScaledInstance ((int) drop_width,(int)drop_height, Image.SCALE_DEFAULT);
  126. }
  127.  
  128. private static int onDirection(){
  129. int rand =(int)(Math.random() *2+1) ;
  130. if(rand == 2) direction=1;
  131. else direction = -1;
  132.  
  133. return direction;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. private static void onRepaint(Graphics g){
  140. long currentTime = System.nanoTime();
  141. float delta_time = (currentTime - LastFrameTime)*0.000000001f;
  142. LastFrameTime = currentTime;
  143. drop_top=drop_top+drop_v*delta_time;
  144. drop_left=drop_left+(direction*drop_v)*delta_time;
  145.  
  146. g.drawImage(background, 0, 0, null);
  147. g.drawImage(drop, (int) drop_left,(int) drop_top,null);
  148. if (drop_top > game_window.getHeight()){
  149. g.drawImage(GameOver, 100, 100, null);
  150. g.drawImage(restart, 250, 200, null);
  151. end = true;
  152. }
  153. if(drop_left<=0.0 || drop_left+drop_width > game_window.getWidth()) {
  154. if(direction == -1) direction=1;
  155. else direction=-1;
  156. }
  157.  
  158. }
  159. private static class GameField extends JPanel{
  160. @Override
  161. protected void paintComponent(Graphics g){
  162. super.paintComponent(g);
  163. onRepaint(g);
  164. repaint();
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement