java坦克

鄙人花了一个下午写了一个小坦克,空格发射炮弹,敌方坦克(速度快的那辆)炮弹会变轨;
不过程序有bug,容易死锁,而且未给坦克之间加边界判断;
今天感觉不是很好,写到这就行了,我要看lucene3.5了,不在这废话了。
效果请看图。
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.lang.Math;
public class Pete extends JFrame
{
	public enemytank enemy;
	public enemytank1 enemy1;
	public ammo ammo1;
	public ammoo ammoo1;
	public myammoo myammoo1;
	public int i=0;
    PetePanel pete = new PetePanel();
	public Pete() 
	{
		super("Tank Battle");
        setSize(512, 430);
		setResizable(false);
		enemy=new enemytank(pete);
		enemy1=new enemytank1(pete);
		ammo1=new ammo(pete);
		ammoo1=new ammoo(pete);
		myammoo1=new myammoo(pete);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//添加窗口关闭
        JPanel pane = new JPanel();
        pane.setLayout(new GridLayout(1, 1, 15, 15));
        pane.add(pete);
        this.addKeyListener(new KeyAdapter(){//得到焦点,放frame容易获取焦点。
            @Override
            public void keyPressed(KeyEvent e)
			{
                System.out.println("ppppp");
                if (e.getKeyCode() == KeyEvent.VK_LEFT&&pete.x>0)//向左
				{
                    pete.current = 1;
                    pete.x =pete.x - 10;
					repaint();
                }
				else if (e.getKeyCode() == KeyEvent.VK_RIGHT&&pete.x<460)//向右
				{
                    pete.current = 0;
                    pete.x = pete.x + 10;
					repaint();
                } 
				else if (e.getKeyCode() == KeyEvent.VK_UP&&pete.y>0)//向上
				{
                    pete.current = 2;
                    pete.y = pete.y - 10;
					repaint();
                } 
				else if (e.getKeyCode() == KeyEvent.VK_DOWN&&pete.y<360) //向下
				{
                    pete.current = 3;
                    pete.y = pete.y + 10;
					repaint();
                }
				else if (e.getKeyCode() == KeyEvent.VK_SPACE) //fire
				{
					//i=i+1;
					//if(i%2==0)
                    pete.live=1;
					//else pete.live=0;
					System.out.println(pete.live);
                }
            }

            public void keyTyped(KeyEvent e)
			{
                System.out.println("kkkkk");
            }

            public void keyReleased(KeyEvent e) 
			{
                System.out.println("rrrrrr");
            }
        });
        setContentPane(pane);//建立联系与pane
		enemy.start();
		enemy1.start();
		ammo1.start();
		ammoo1.start();
		myammoo1.start();
        this.setVisible(true);//窗口可见
        this.requestFocus();//具体办法可以手设焦点。如果焦点有问题,就优先想到它
	}
	public static void main(String[] arguments)
	{
        Pete Penguin = new Pete();
        PetePanel a = new PetePanel();
    }
}

class PetePanel extends JPanel{
    Image petePics[] = new Image[4];
	Image petePics1[] = new Image[4];
	Image petePics2[] = new Image[4];
	Image petePics3[] = new Image[4];
	Image petePics4[] = new Image[4];
	Image petePics5[] = new Image[4];
    Image back;
    int current = 0;
    int x = 100;
    int y = 100;
	int current1 = 0;
    int x1 = 300;
    int y1 = 300;
	int current2 = 0;
    int x2 = 400;
    int y2 = 50;
	int current3 = 0;
    int x3 = -20;
    int y3 = -20;
	int current4 = 0;
    int x4 = -20;
    int y4 = -20;
	int current5 = 0;
    int x5 = -20;
    int y5 = -20;
	int live=1;
	PetePanel() 
	{
        super();
        String peteSrc[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
		String peteSrc1[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
		String peteSrc2[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
		String peteSrc3[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
		String peteSrc4[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
		String peteSrc5[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
        Toolkit kit = Toolkit.getDefaultToolkit();
        for (int i = 0; i < petePics.length; i++) 
		{
            petePics[i] = kit.getImage(peteSrc[i]);
            petePics1[i] = kit.getImage(peteSrc1[i]);
			petePics2[i] = kit.getImage(peteSrc2[i]);
			petePics3[i] = kit.getImage(peteSrc3[i]);
			petePics4[i] = kit.getImage(peteSrc4[i]);
			petePics5[i] = kit.getImage(peteSrc5[i]);
        }
        back = kit.getImage("30.jpg");
    }

    public void paint(Graphics comp) //创建绘图函数
	{
        Graphics2D comp2D = (Graphics2D) comp;
        if (petePics[current] != null)
			 comp2D.drawImage(back,0,0,this);
            comp2D.drawImage(petePics[current], x, y, this);
			comp2D.drawImage(petePics1[current1], x1, y1, this);
			comp2D.drawImage(petePics2[current2], x2, y2, this);
			comp2D.drawImage(petePics3[current3], x3, y3, this);
			comp2D.drawImage(petePics4[current4], x4, y4, this);
			comp2D.drawImage(petePics5[current5], x5, y5, this);
    }
}
class enemytank extends Thread
{
	PetePanel pete ;
	Random rand = new Random();
	public enemytank(PetePanel pete)
	{
		this.pete=pete;
	}
	public void run()//重载run()函数
    {
	    while(true)//线程中的无限循环
        {
            try
            {
                sleep(10); //线程休眠
            }
            catch(InterruptedException e){}
			int i=rand.nextInt(1200);
			if(i>0&&i<300&&pete.x1>30)
			{
				 pete.current1 = 1;
				 pete.x1 =pete.x1 - 10;
				 pete.repaint();
				  try
            {
                sleep(300); //线程休眠
            }
            catch(InterruptedException e){}
				 pete.x1 =pete.x1 - 10;
				 pete.repaint();
				  try
            {
                sleep(300); //线程休眠
            }
            catch(InterruptedException e){}
				 pete.x1 =pete.x1 - 10;
				 pete.repaint();
			}
			else if(i>300&&i<600&&pete.x1<430)
			{
				  pete.current1 = 0;
				  pete.x1 = pete.x1 + 10;
				  pete.repaint();
				   try
            {
                sleep(500); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.x1 = pete.x1 + 10;
				  pete.repaint();
				   try
            {
                sleep(300); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.x1 = pete.x1 + 10;
				  pete.repaint();
			} 
			else if(i>600&&i<900&&pete.y1<330)
			{
				  pete.current1 = 3;
				  pete.y1 = pete.y1 + 10;
				  pete.repaint();
				   try
            {
                sleep(300); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y1 = pete.y1 + 10;
				  pete.repaint();
				   try
            {
                sleep(300); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y1 = pete.y1 + 10;
				  pete.repaint();
			} 
			else if(i>900&&i<1200&&pete.y1>30)
			{
				  pete.current1 = 2;
				  pete.y1 = pete.y1 - 10;
				  pete.repaint();
				   try
            {
                sleep(300); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y1 = pete.y1 - 10;
				  pete.repaint();
				   try
            {
                sleep(300); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y1 = pete.y1 - 10;
				  pete.repaint();
			} 
		}
	}
}
class enemytank1 extends Thread
{
	PetePanel pete ;
	Random rand = new Random();
	public enemytank1(PetePanel pete)
	{
		this.pete=pete;
	}
	public void run()//重载run()函数
    {
	    while(true)//线程中的无限循环
        {
            try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
			int i=rand.nextInt(1200);
			if(i>0&&i<300&&pete.x2>50)
			{
				 pete.current2 = 1;
				 pete.x2 =pete.x2 - 10;
				 pete.repaint();
				  try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				 pete.x2 =pete.x2 - 10;
				 pete.repaint();
				  try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				 pete.x2 =pete.x2 - 10;
				 pete.repaint();
			}
			else if(i>300&&i<600&&pete.x2<400)
			{
				  pete.current2 = 0;
				  pete.x2 = pete.x2 + 10;
				  pete.repaint();
				   try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.x2 = pete.x2 + 10;
				  pete.repaint();
				   try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.x2 = pete.x2 + 10;
				  pete.repaint();
			} 
			else if(i>600&&i<900&&pete.y2<330)
			{
				  pete.current2 = 3;
				  pete.y2 = pete.y2 + 10;
				  pete.repaint();
				   try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y2 = pete.y2 + 10;
				  pete.repaint();
				   try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y2 = pete.y2 + 10;
				  pete.repaint();
			} 
			else if(i>900&&i<1200&&pete.y2>50)
			{
				  pete.current2 = 2;
				  pete.y2 = pete.y2 - 10;
				  pete.repaint();
				   try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y2 = pete.y2 - 10;
				  pete.repaint();
				   try
            {
                sleep(100); //线程休眠
            }
            catch(InterruptedException e){}
				  pete.y2 = pete.y2 - 10;
				  pete.repaint();
			} 
		}
	}
}
class ammo extends Thread 
{
	PetePanel pete ;
	public ammo(PetePanel pete)
	{
		this.pete=pete;
	}
	public void run()
	{
		while(true)
		{
			 try
					  {
                          sleep(1000); //线程休眠
                      }
                      catch(InterruptedException e){}
			if(pete.current1==0)
			{
				pete.current3=0;
				pete.x3=pete.x1+10;
				pete.y3=pete.y1+10;
				for(int i=0;i<500;i++)
				{
					  pete.x3=pete.x3+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current1==1)
			{
				pete.current3=1;
				pete.x3=pete.x1+10;
				pete.y3=pete.y1+10;
				for(int i=0;i<500;i++)
				{
					  pete.x3=pete.x3-1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current1==2)
			{
				pete.current3=2;
				pete.x3=pete.x1+10;
				pete.y3=pete.y1+10;
				for(int i=0;i<500;i++)
				{
					  pete.y3=pete.y3-1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current1==3)
			{
				pete.current3=3;
				pete.x3=pete.x1+10;
				pete.y3=pete.y1+10;
				for(int i=0;i<500;i++)
				{
					  pete.y3=pete.y3+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			 pete.x3=0-20;
			 pete.y3=0-20;
		}
	}
}
class ammoo extends Thread 
{
	PetePanel pete ;
	public ammoo(PetePanel pete)
	{
		this.pete=pete;
	}
	public void run()
	{
		while(true)
		{
			 try
					  {
                          sleep(1000); //线程休眠
                      }
                      catch(InterruptedException e){}
			if(pete.current2==0)
			{
				pete.current4=0;
				pete.x4=pete.x2+10;
				pete.y4=pete.y2+10;
				for(int i=0;i<500;i++)
				{
					  pete.x4=pete.x4+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current2==1)
			{
				pete.current4=1;
				pete.x4=pete.x2+10;
				pete.y4=pete.y2+10;
				for(int i=0;i<500;i++)
				{
					  pete.x4=pete.x4-1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current2==2)
			{
				pete.current4=2;
				pete.x4=pete.x2+10;
				pete.y4=pete.y2+10;
				for(int i=0;i<500;i++)
				{
					  pete.y4=pete.y4-1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current2==3)
			{
				pete.current4=3;
				pete.x4=pete.x2+10;
				pete.y4=pete.y2+10;
				for(int i=0;i<500;i++)
				{
					  pete.y4=pete.y4+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			 pete.x4=0-20;
			 pete.y4=0-20;
		}
	}
}
class myammoo extends Thread
{
	PetePanel pete ;
	int i=1;
	public myammoo(PetePanel pete)
	{
		this.pete=pete;
	}
	public void run()
	{
		for(;true;){
			while(pete.live==1)
		{
			if(pete.current==0)
			{
				System.out.println("Thread is live");
				pete.current5=0;
				pete.x5=pete.x+10;
				pete.y5=pete.y+10;
				for(int i=0;i<500;i++)
				{
					  pete.x5=pete.x5+1;
					   pete.y4=pete.y4+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current==1)
			{
				pete.current5=1;
				pete.x5=pete.x+10;
				pete.y5=pete.y+10;
				for(int i=0;i<500;i++)
				{
					  pete.x5=pete.x5-1;
					   pete.y4=pete.y4+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current==2)
			{
				pete.current5=2;
				pete.x5=pete.x+10;
				pete.y5=pete.y+10;
				for(int i=0;i<500;i++)
				{
					  pete.y5=pete.y5-1;
					   pete.y4=pete.y4+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			else if(pete.current==3)
			{
				pete.current5=3;
				pete.x5=pete.x+10;
				pete.y5=pete.y+10;
				for(int i=0;i<500;i++)
				{
					  pete.y5=pete.y5+1;
					   pete.y4=pete.y4+1;
					  try
					  {
                          sleep(1); //线程休眠
                      }
                      catch(InterruptedException e){}
					  pete.repaint();
				}
			}
			 pete.x5=0-20;
			 pete.y5=0-20;
			 try
					  {
                          sleep(1000); //线程休眠 
                      }
                      catch(InterruptedException e){}
					  pete.live=0;
		}
                int a=95566;
                 System.out.println("ammo"+a);
                try{
                    sleep(500);
                 }
                 catch(Interruption e)
                  {}
		
		System.out.println("ammo"+a);
	}}
}

java坦克_第1张图片

你可能感兴趣的:(java,thread,image,String,Random,Class)