Advertisement
sergAccount

Untitled

Mar 19th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 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.  
  7. import javax.swing.*;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10.  
  11. /**
  12.  *
  13.  * @author Sergey
  14.  */
  15. public class Pole extends JPanel{
  16.     //
  17.     private Image shapka;
  18.     private Image fon;
  19.     public int x = 400;
  20.     private int slogn;
  21.     private Podar[] gamePodar;    
  22.     private Timer timerUpdate, timerDraw;
  23.  
  24.     public Pole(int slogn){
  25.         this.slogn  = slogn;
  26.         this.shapka = Game.loadImage("c:\\game\\shapka.png");        
  27.         //this.shapka = Game.loadImage("c:\\game\\p0.png");        
  28.         this.fon    = Game.loadImage("c:\\game\\fon.jpg");        
  29.         //
  30.         gamePodar = new Podar[7];
  31.         for(int i = 0; i < gamePodar.length; i++){            
  32.             gamePodar[i] = new Podar(Game.loadImage("c:\\game\\p" + i + ".png"));            
  33.         }
  34.         //
  35.         timerUpdate = new Timer(3000, new ActionListener(){
  36.             @Override
  37.             public void actionPerformed(ActionEvent e){
  38.                 updateStart();
  39.             }                  
  40.         });
  41.         timerUpdate.start();              
  42.         //
  43.         timerDraw = new Timer(50, new ActionListener(){
  44.             @Override
  45.             public void actionPerformed(ActionEvent e){
  46.                 repaint();
  47.             }                  
  48.         });
  49.         timerDraw.start();              
  50.     }
  51.     //
  52.     private void updateStart(){
  53.         //
  54.         int kol = 0;
  55.         for(int i = 0; i < gamePodar.length; i++){
  56.             if(gamePodar[i].act){
  57.                kol++;
  58.             }else if(kol<slogn){
  59.                gamePodar[i].start();
  60.                break;                                
  61.             }                  
  62.         }
  63.     }    
  64.     //
  65.     @Override
  66.     public void paintComponent(Graphics gr){
  67.         //
  68.         super.paintComponent(gr);
  69.         gr.drawImage(fon, 0, 0, null);
  70.         gr.drawImage(shapka, x, 465, null);
  71.         //
  72.         for(int i = 0; i < gamePodar.length; i++){
  73.             gamePodar[i].draw(gr);
  74.             if(gamePodar[i].act){        
  75.                 if(gamePodar[i].y + gamePodar[i].img.getHeight(null) >= 470){                    
  76.                     if(Math.abs(gamePodar[i].x - x) > 75){
  77.                         //
  78.                         gr.setFont(new Font("Arial", Font.BOLD, 24));
  79.                         gr.setColor(Color.red);
  80.                         gr.drawString("Игра закончена!", 300, 300);
  81.                         timerDraw.stop();
  82.                         timerUpdate.stop();
  83.                         break;
  84.                     }else{
  85.                         gamePodar[i].act = false;
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement