NoNameCracksInc

Java Game

Apr 19th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import java.awt.geom.Line2D;
  6. import java.util.Random;
  7.  
  8. public class Drawing extends JPanel implements KeyListener,Runnable
  9. {
  10.     public static JFrame frame;
  11.     public static JLabel label;
  12.     public static int[] x=new int[20];
  13.     public static int[] y=new int[20];
  14.     public static int a;
  15.     public static Random random;
  16.     public static int p_x=0;
  17.     public static int p_y=0;
  18.     public static int enemy_stop=0;
  19.     public static boolean isin=false;
  20.     public static Thread move;
  21.     public static Rectangle active;
  22.     public static Rectangle[] generated=new Rectangle[20];
  23.     public static boolean ready=false;
  24.     public static int con;
  25.    
  26.     public static void drawframe(int width,int height)
  27.     {      
  28.         frame=new JFrame("The game");
  29.         frame.setLayout(new BorderLayout());
  30.         frame.setSize(width,height);
  31.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.         frame.setVisible(true);
  33.         frame.setResizable(false);
  34.     }
  35.    
  36.     public void paintComponent(Graphics g)
  37.     {
  38.         super.paintComponent(g);
  39.        
  40.         drawenemies(g,5);
  41.         setradius(g);
  42.         setplayer(g);
  43.        
  44.             if(entered()&&isin!=true)
  45.             {
  46.                 isin=true;
  47.                 move=new Thread(new Drawing());
  48.                 move.start();
  49.             }
  50.            
  51.             if(entered()==false&&isin==true)
  52.                 isin=false;
  53.            
  54.                 for(int i=1;i<=a;i++)
  55.                 {
  56.                 Line2D left = new Line2D.Double(x[i], y[i]+5, x[i]-150, y[i]+5);
  57.                 Line2D right = new Line2D.Double(x[i], y[i]+5, x[i]+150, y[i]+5);
  58.                
  59.                 g.setColor(Color.BLUE);
  60.                
  61.                 if(p_y==y[i])
  62.                 {
  63.                     if(p_x>x[i])
  64.                         g.drawLine((int)right.getX1(),(int)right.getY1(),(int)right.getX2(),(int)right.getY2());
  65.                    
  66.                      else
  67.                          g.drawLine((int)left.getX1(),(int)left.getY1(),(int)left.getX2(),(int)left.getY2());
  68.                 }
  69.            }
  70.        
  71.     }
  72.    
  73.     public void drawenemies(Graphics g,int amount)
  74.     {
  75.         random=new Random();
  76.         a=amount;
  77.         enemy_stop=enemy_stop+1;
  78.        
  79.         if(enemy_stop<=1)
  80.         {
  81.             for(int i=1;i<=amount;i++)
  82.             {
  83.                 x[i]=random.nextInt(frame.getWidth());
  84.                 y[i]=random.nextInt(frame.getHeight());
  85.             }
  86.         }  
  87.                
  88.         for(int i=1;i<=amount;i++)
  89.         {  
  90.             g.fillRect(x[i], y[i], 20, 20);
  91.             Rectangle store=new Rectangle(x[i], y[i], 20, 20);
  92.             generated[i]=store;
  93.         }
  94.     }
  95.    
  96.     public void setradius(Graphics g)
  97.     {
  98.         g.setColor(Color.RED);
  99.        
  100.         for(int i=1;i<=a;i++)
  101.         {
  102.             g.drawRect(x[i]-40, y[i]-40, 100, 100);
  103.         }
  104.     }
  105.    
  106.     public void setplayer(Graphics g)
  107.     {
  108.         g.setColor(Color.GREEN);
  109.         g.fillRect(p_x, p_y, 20, 20);
  110.         g.setColor(Color.BLACK);
  111.         g.drawRect(p_x, p_y, 20, 20);
  112.     }
  113.    
  114.     public static void main(String args[])
  115.     {  
  116.         drawframe(500,500);
  117.         Drawing instance=new Drawing();
  118.         frame.add(instance);
  119.         frame.addKeyListener(instance);
  120.        
  121.     }
  122.    
  123.     public boolean entered()
  124.     {
  125.         boolean entered=false;
  126.        
  127.         for(int i=1;i<=a;i++)
  128.         {
  129.             Rectangle q=new Rectangle(x[i]-40, y[i]-40, 100, 100);
  130.             Rectangle s=new Rectangle(p_x, p_y, 20, 20);
  131.            
  132.             if(q.contains(s))
  133.             {
  134.                 entered=true;
  135.                 active=new Rectangle(x[i], y[i], 20, 20);
  136.             }
  137.         }
  138.        
  139.         return entered;
  140.     }
  141.  
  142.     @Override
  143.     public void keyPressed(KeyEvent e)
  144.     {
  145.         switch(e.getKeyCode())
  146.         {
  147.         case 39: p_x=p_x+10;repaint();break;
  148.         case 37: p_x=p_x-10;repaint();break;
  149.         case 38: p_y=p_y-10;repaint();break;
  150.         case 40: p_y=p_y+10;repaint();break;
  151.         }
  152.     }
  153.        
  154.     public void keyReleased(KeyEvent arg0){}
  155.     public void keyTyped(KeyEvent arg0){}
  156.  
  157.     @Override
  158.     public void run()
  159.     {
  160.         try
  161.         {
  162.            
  163.         for(int i=1;i<=a;i++)
  164.         {
  165.             if(active.getX()==generated[i].getX())
  166.             {
  167.                 con=i;
  168.                
  169.                 while(x[i]!=p_x||y[i]!=p_y)
  170.                 {
  171.                     if(x[i]>p_x)
  172.                         x[i]=x[i]-1;
  173.                     else
  174.                         x[i]=x[i]+1;
  175.                    
  176.                     if(y[i]>p_y)
  177.                         y[i]=y[i]-1;
  178.                     else
  179.                         y[i]=y[i]+1;
  180.                    
  181.                     Thread.sleep(100);
  182.                     frame.repaint();
  183.                 }
  184.             }
  185.         }
  186.     }
  187.     catch(InterruptedException e){}
  188.   }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment