Advertisement
sergAccount

Untitled

Dec 26th, 2020
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package game;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import java.awt.Graphics;
  11. import java.awt.Image;
  12. import java.awt.Toolkit;
  13. import javax.swing.JPanel;
  14. import javax.swing.Timer;
  15.  
  16. public class Pole extends JPanel{
  17.     // в данной точке с коорд x отображаем шапку
  18.     int shapkaX = 400;
  19.     // в данной точке с коорд y отображаем шапку
  20.     static final int shapkaY = 460;
  21.     // уровень сложности     int slogn;
  22.     Image fon;
  23.     Image shapka;
  24.     // timers
  25.     Timer timerUpdate, timerDraw;        
  26.     // массив подарков
  27.     Podar[] gamePodar;        
  28.     int slogn;
  29.    
  30.     public Pole(int slogn){
  31.         this.slogn  = slogn;
  32.         // загружаем изо из файла и сохр в переменной типа Image
  33.         fon = ImageUtil.loadImage(Game.getFonPath());    
  34.         shapka = ImageUtil.loadImage(Game.getShapkaPath());            
  35.         // создаем массив подарков
  36.         gamePodar = new Podar[7];
  37.         // заполняем массив подарков
  38.         for(int i = 0; i < gamePodar.length; i++){            
  39.             gamePodar[i] = new Podar(ImageUtil.loadImage(Game.getPodarokPath(i)));            
  40.         }        
  41.         timerUpdate = new Timer(4000, (e) -> updateStart());
  42.         timerUpdate.start();              
  43.         // timerDraw
  44.         // repaint() - вызыв метода приводит вызову метода paintComponent
  45.         timerDraw = new Timer(30, (e) -> repaint());
  46.         // запускаем таймер start
  47.         timerDraw.start();
  48.     }
  49.    
  50.     public void updateStart(){
  51.         int kol = 0;
  52.         for(int i = 0; i < gamePodar.length; i++){
  53.             if(gamePodar[i].isAct()){
  54.                kol++;
  55.             }else if(kol<slogn){
  56.                gamePodar[i].start();
  57.                break;                                
  58.             }                  
  59.         }
  60.     }
  61.     //
  62.     public int getShapkaX() {
  63.         return shapkaX;
  64.     }
  65.     public void setShapkaX(int shapkaX) {
  66.         this.shapkaX = shapkaX;
  67.     }
  68.     // методы для сдвига влево/вправо
  69.     public void moveShapkaToL(int shiftX){
  70.         this.shapkaX -= shiftX;
  71.     }
  72.     //
  73.     public void moveShapkaToR(int shiftX){
  74.         this.shapkaX += shiftX;
  75.     }
  76.     // метод paintComponent отвечает за отображение граф информации
  77.     public void paintComponent(Graphics gr){
  78.         // вызов родительского метода paintComponent
  79.         super.paintComponent(gr);
  80.         // отрисовка фона - используем drawImage
  81.         gr.drawImage(fon, 0, 0, null);
  82.         gr.drawImage(shapka, shapkaX, shapkaY, null);        
  83.         // выводим объекты на экране
  84.         for(int i = 0; i < gamePodar.length; i++){
  85.             gamePodar[i].draw(gr); // отрисовка объекта на экране
  86.             if(gamePodar[i].isAct()){        
  87.                 if(gamePodar[i].getY() + gamePodar[i].getImg().getHeight(null) >= 470){                    
  88.                     if(Math.abs(gamePodar[i].getX() - shapkaX) > 75){           // конец игры!!!                    
  89.                         gr.setFont(new Font("Arial", Font.BOLD, 24));
  90.                         gr.setColor(Color.red);
  91.                         gr.drawString("Игра закончена!", 300, 300);
  92.                         timerDraw.stop();
  93.                         timerUpdate.stop();
  94.                         Toolkit.getDefaultToolkit().beep();
  95.                         break;
  96.                     }else{                                                      // подарок пойман - устанавливаем флаг для того, чтобы стереть подарок!
  97.                         gamePodar[i].setAct(false);
  98.                         Toolkit.getDefaultToolkit().beep();
  99.                     }
  100.                 }                
  101.             }
  102.         }
  103.     }    
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement