飞机大战特殊简洁版

FlyingObject类

package com.cetc.shoot;

import java.awt.image.BufferedImage;

public abstract class FlyingObject {
	
	protected int x; //x坐标
	protected int y; //y坐标
	protected int width;  //宽
	protected int height; //高
	protected BufferedImage image; //图片
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public BufferedImage getImage() {
		return image;
	}
	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public abstract void step();
}

Airplane

package com.cetc.shoot;

public class Airplane extends FlyingObject implements Enemy{


	private int yspeed = 2; //走步的步数
	
	/** 重写getScore() */
	public int getScore(){
		return 0;
	}
	
	/** 构造方法 */
	public Airplane(){
		image = ShootGame.airplane; //图片
		width = image.getWidth();   //宽
		height = image.getHeight(); //高
		x = (int) (Math.random()*(ShootGame.WIDTH-this.width));
		y = -this.height; //y:负的敌机的高
		//x=100;
		//y=100;

	}

	@Override
	public void step() {
		y+=yspeed;
		
	}
	

}

Bee类

package com.cetc.shoot;

import java.util.Random;

/** 小蜜蜂: 是飞行物,也是奖励 */
public class Bee extends FlyingObject implements Award{

	private int xSpeed = 1; //x坐标走步步数
	private int ySpeed = 2; //y坐标走步步数
	private int awardType; //奖励类型
	
	/** 重写getType() */
	public int getType(){
		return 0; 
	}
	
	public Bee(){
		image = ShootGame.bee; //图片
		width = image.getWidth();   //宽
		height = image.getHeight(); //高
		y = -height; //y:负的蜜蜂的高
		Random rand = new Random(); //创建随机数对象
		x = rand.nextInt(ShootGame.WIDTH-this.width); //x:0到(屏幕宽-蜜蜂宽)之内的随机数
		awardType = rand.nextInt(2); //奖励类型为0到1之间的随机数
		//x=100;
		//y=200;
	}

	@Override
	public void step() {
		x+=xSpeed;
		y+=ySpeed;
		if(x>=ShootGame.WIDTH-this.width) {
			xSpeed=-1;
		}
		if(x<=0) {
			xSpeed=1;
		}
		
	}
}

Bullet类

package com.cetc.shoot;

public class Bullet extends FlyingObject{
	
	private int speed = 3; //走步步数
	
	/** 构造方法   x:子弹的x坐标   y:子弹的y坐标*/
	public Bullet(int x,int y){
		image = ShootGame.bullet; //图片
		this.x = x; //x坐标:与英雄机有关
		this.y = y; //y坐标:与英雄机有关
	}

	@Override
	public void step() {
		y-=speed;
		
	}

}

Hero类

package com.cetc.shoot;

import java.awt.image.BufferedImage;

/** 英雄机: 是飞行物 */
public class Hero extends FlyingObject {
	private int life; // 命
	private int doubleFire; // 火力值
	private BufferedImage[] images = {}; // 图片切换数组
	private int index = 0; // 协助图片切换

	/** 构造方法 */
	public Hero() {
		image = ShootGame.hero0; // 图片
		width = image.getWidth(); // 宽
		height = image.getHeight(); // 高
		x = 150; // x坐标:固定的值
		y = 400; // y坐标:固定的值
		life = 3; // 命数为3
		doubleFire = 0; // 火力值为0(单倍火力)
		images = new BufferedImage[] { ShootGame.hero0, ShootGame.hero1 }; // 两张图片切换
	}

	@Override
	public void step() {
		if (images.length > 0) {
			image = images[index++ / 10 % images.length];
		}

	}
	public void moveTo(int x , int y) {
		this.x = x - this.width/2;
		this.y = y - this.height/2;
	}

}

ShootGame类

package com.cetc.shoot;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

//主程序类
public class ShootGame extends JPanel {

	public static final int WIDTH = 400; // 窗口宽
	public static final int HEIGHT = 654; // 窗口高

	public static BufferedImage background; // 背景图
	public static BufferedImage start; // 启动图
	public static BufferedImage pause; // 暂停图
	public static BufferedImage gameover; // 游戏结束图
	public static BufferedImage airplane; // 敌机
	public static BufferedImage bee; // 小蜜蜂
	public static BufferedImage bullet; // 子弹
	public static BufferedImage hero0; // 英雄机0
	public static BufferedImage hero1; // 英雄机1

	private Hero hero = new Hero(); // 英雄机对象
	private FlyingObject[] flyings = {}; // 敌人(敌机+小蜜蜂)数组
	private Bullet[] bullets = {}; // 子弹数组

	private int state;
	private static final int START = 0;
	private static final int RUNNING = 1;

	private static final int PAUSE = 2;
	private static final int GAME_OVER = 3;

	private Timer timer;
	private int intervel = 1000 / 100;

	public ShootGame() {
		flyings = new FlyingObject[2];
		flyings[0] = new Airplane();
		flyings[1] = new Bee();
		bullets = new Bullet[1];
		bullets[0] = new Bullet(150, 180);
	}

	/** 重写paint() g:画笔 */
	public void paint(Graphics g) {
		g.drawImage(background, 0, 0, null); // 画背景图
		paintHero(g); // 画英雄机
		paintFlyingObjects(g); // 画敌人(敌机+小蜜蜂)
		paintBullets(g); // 画子弹
	}

	/** 画英雄机对象 */
	public void paintHero(Graphics g) {
		g.drawImage(hero.image, hero.x, hero.y, null); // 画对象
	}

	/** 画敌人(敌机+小蜜蜂)对象 */
	public void paintFlyingObjects(Graphics g) {
		for (int i = 0; i < flyings.length; i++) { // 遍历敌人(敌机+小蜜蜂)数组
			FlyingObject f = flyings[i]; // 获取每一个敌人
			g.drawImage(f.image, f.x, f.y, null); // 画敌人对象
		}
	}

	/** 画子弹对象 */
	public void paintBullets(Graphics g) {
		for (int i = 0; i < bullets.length; i++) { // 遍历子弹数组
			Bullet b = bullets[i]; // 获取每一个子弹
			g.drawImage(b.image, b.x, b.y, null); // 画子弹对象
		}
	}

	static { // 加载图片
		try {
			background = ImageIO.read(ShootGame.class.getResource("background.png"));
			start = ImageIO.read(ShootGame.class.getResource("start.png"));
			pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
			gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
			airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));
			bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
			bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
			hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
			hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Fly"); // 创建一个Jframe对象
		ShootGame game = new ShootGame(); // 创建一个JPanel对象
		frame.add(game); // 将面板添加到框架中

		frame.setSize(WIDTH, HEIGHT); // 设置窗口大小
		frame.setAlwaysOnTop(true); // 设置总是在最上面
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认关闭操作(窗口关闭时退出程序)
		frame.setLocationRelativeTo(null); // 设置居中显示
		frame.setVisible(true); // 1.设置窗口可见 2.尽快调用
		game.action();
	}

	public FlyingObject nextOne() {
		Random rand = new Random();
		int type = rand.nextInt(20);
		if (type == 0) {
			return new Bee();
		} else {
			return new Airplane();
		}
	}

	int flyEnteredIndex = 0;

	public void enterAction() {
		flyEnteredIndex++;

		if (flyEnteredIndex % 40 == 0) {
			FlyingObject obj = nextOne();
			flyings = Arrays.copyOf(flyings, flyings.length + 1);
			flyings[flyings.length - 1] = obj;
		}
	}

	public void stepAction() {
		hero.step();
		for (int i = 0; i < flyings.length; i++) {
			flyings[i].step();
		}
		for (int i = 0; i < bullets.length; i++) {
			bullets[i].step();
		}
	}

	public void action() {
		MouseAdapter l = new MouseAdapter() {

			public void mouseClicked(MouseEvent e) {
				switch (state) {
				case START:
					state = RUNNING;
					break;
				}
			}
		};
		this.addMouseListener(l);
		this.addMouseMotionListener(l);

		timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				if (state == RUNNING) {
					enterAction();
					stepAction();

				}
				repaint();
			}
		}, intervel, intervel);
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值