svn redis常用语句 arduino Semantic UI Plupload vue框架 vue代码规范 vue绑定事件 sql server 视频教程 java二维数组赋值 python自学教程 python抛出异常 python怎么配置环境 python当前日期 python设置环境变量 python正则匹配数字 javaobject java的string java基础数据类型 java使用正则表达式 java实例方法 java索引 java获取url参数 java网页 linux入门 python输入数字 js图片上传 cfqq网吧任务 地下城怎么双开 tt语音官网 管理文件 原创检测工具 怎么看淘龄 cdlinux教程 光标变粗 氤氲之息哪里爆率高 python去重 python进制转换 id书籍排版教程 金融大师
当前位置: 首页 > 学习教程  > 编程语言

飞机大战特殊简洁版

2020/12/28 19:50:13 文章标签:

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 i…

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);
	}
}

 


本文链接: http://www.dtmao.cc/news_show_550396.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?