java自写飞机大战,适合新手练手!

我自己写的飞机大战,这里只有一个主类代码,然后其他的我也上传了,大家需要的话可以一并下载;有不足之处,望大家见谅和指出,,大家可以先点开下面的图片看下界面

 

package 飞机大战;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.RenderingHints.Key;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class WarUI extends Frame{
	public static final int WIDTH=410;
	public static final int HEIGHT=600;
	public ImageIcon img;
	public ImageIcon img2;
	public static WarUI warui;
	public boolean p=true;
	public static List planes = new ArrayList();// 敌人飞机容器
	public static List bullets = new ArrayList();//子弹容器 
	public static List explodes=new ArrayList();//爆炸对象
	public static int score=0;//计分
	
	private Image offScreenImage = null;//用来实现使用双缓冲,画一个缓冲画布
	public  Panle myplane=new Panle(150,500,warui,60,true);
	private Random random=new Random();
	public void lauchFrame() {
		setTitle("飞机大战");
		ImageIcon Icon=new ImageIcon("背景\\Icon.jpg");
		setIconImage(Icon.getImage());
		setBounds(380, 100, WIDTH, HEIGHT);
		// 匿名内部类,短,不涉及将来的扩展
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		img=new ImageIcon("背景\\back.jpg");//接口,getImage方法返回一个Image对象,repaint方法会调用paint方法
		img2=new ImageIcon("背景\\back2.jpg");//接口,getImage方法返回一个Image对象,repaint方法会调用paint方法
		setResizable(false);
		setVisible(true);
		MyKeyListener mkl=new MyKeyListener();
		this.addKeyListener(mkl);
		new Thread(new PaintThread()).start();
	}

	public static void main(String[] args) {
		warui =new WarUI();
		warui.lauchFrame();
		
	}	
	/**
	 	* 消除闪烁,使用双缓冲
	 	* 线程重画更加均匀,更能控制重化的速度。按键重画不能解决子弹自动飞行的问题;
	    * 每次重绘调用repaint方法时,必定会先调用update然后paint方法
	 */

		public void update(Graphics g) {
			if (offScreenImage == null) {
				offScreenImage = this.createImage(WIDTH, HEIGHT);
			}
			// 拿到图片上的画笔
			Graphics gOffScreen = offScreenImage.getGraphics();
			Color c = gOffScreen.getColor();
			gOffScreen.setColor(Color.blue);
			gOffScreen.fillRect(0, 0, WIDTH,HEIGHT);
			gOffScreen.setColor(c);
			paint(gOffScreen);// 画在背后图片上
			g.drawImage(offScreenImage, 0, 0, null);// 画在屏幕上
		}
		/**
		 * 根据线程里面的repaint方法,不断重画面板,飞机,子弹,爆炸效果,等待都是在里面画出来的
		 */
	public void paint(Graphics g){
		//	根据不同的分数层切换背景图片
		if(score>5000){
			g.drawImage(img2.getImage(), 0, 0, warui);
		}else{
			g.drawImage(img.getImage(), 0, 0, warui);
		}
		if(!myplane.isLive()){
			g.setColor(Color.red);
			Font f = g.getFont();
			g.setFont(new Font("宋体",Font.BOLD,60));
			g.drawString("GAME  OVER!!!", 20, 300);
			g.setFont(f);
			g.drawString("复活按B,重新开始按C!!!", 22, 340);
			p=false;
			
		}
		myplane.draw(g);
		myplane.move();
		
		//敌人
		if (planes.size()<3 ) {
			for (int j = 0; j < 3; j++) {
				Panle p = new Panle(false, warui);
				planes.add(p);
			}
		}
		if (planes.size() != 0) {
			for (int i = 0; i < planes.size(); i++) {
				Panle diren = planes.get(i);
				diren.draw(g);
				diren.dmove();
				int r1=random.nextInt(200);
				if(r1==20)
				diren.dfire();
			}
		}
		g.setColor(Color.BLUE);
		g.drawString("子 弹 个  数 :"+bullets.size(), 20,50);
		g.drawString("入侵敌机数:"+planes.size(), 20, 70);
		g.drawString("获得分数:"+score, 20, 90);
		g.setColor(Color.BLACK);
		for(int i=0;i

 

你可能感兴趣的:(数据类型,java项目,基础)