我是靠谱客的博主 现代信封,最近开发中收集的这篇文章主要介绍趣味JAVA——飞机大战源码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

趣味JAVA——飞机大战源码

代码结构:

在这里插入图片描述

运行截图:

登录界面:
在这里插入图片描述
查看排行榜:
在这里插入图片描述
进入界面:
在这里插入图片描述
游戏界面:
在这里插入图片描述

主要代码展示

1.Bee.java

package com.jyb.airplane.bee;

import java.util.Random;

import com.jyb.airplane.flyingObject.FlyingObject;
import com.jyb.airplane.ownplane.OwnPlane;
import com.jyb.airplane.view.ShootGame;

public class Bee extends FlyingObject {
	Random r = new Random();
    private int xSpeed = r.nextInt(2) + 1;    //x坐标走动步数
    private int ySpeed = r.nextInt(2) + 1;    //y坐标走动步数
    private int awardType;     //奖励类型
    
    public Bee() {
    	image = ShootGame.bee;
    	width = image.getWidth();
    	height = image.getHeight();
    	Random r = new Random();
    	x = r.nextInt(ShootGame.WIDTH - this.width);
    	y = -this.height;
    	awardType = r.nextInt(10); //随机生成奖励类型
    }
    
	public int getType() {
		return awardType;
	}

	@Override
	public void step(OwnPlane ownPlane) {
        if (x >= ShootGame.WIDTH - this.width) 
			xSpeed = -1;
		if (x <= 0) 
			xSpeed = 1;
		x += xSpeed;
		if (ownPlane.getScore() < 200000) {
			ySpeed = r.nextInt(2) + 1; 
		} else if (ownPlane.getScore() >= 200000 
				&& ownPlane.getScore() < 400000) {
			ySpeed = r.nextInt(2) + 2;
		} else if (ownPlane.getScore() >= 400000 
				&& ownPlane.getScore() < 600000) {
			ySpeed = r.nextInt(2) + 3;
		} else if (ownPlane.getScore() >= 600000 
				&& ownPlane.getScore() < 800000) {
			ySpeed = r.nextInt(2) + 4;
		} else if (ownPlane.getScore() >= 800000 
				&& ownPlane.getScore() < 1000000) {
			ySpeed = r.nextInt(2) + 5;
		} else {
			ySpeed = r.nextInt(2) + 6;
		}
		y += ySpeed;
	}

	@Override
	public boolean outOfBounds() {
		return this.y > ShootGame.HEIGHT;
	}

}

3.Bullet.java

package com.jyb.airplane.bullet;

import com.jyb.airplane.flyingObject.FlyingObject;
import com.jyb.airplane.ownplane.OwnPlane;
import com.jyb.airplane.view.ShootGame;
//子弹只是飞行物,因此继承飞行物类即可
public class Bullet extends FlyingObject {
	private int speed = 3;  //子弹走步步数,只有y坐标在变
	
	public Bullet(int x, int y) { //子弹的步数随着英雄机的变化而变化
		image = ShootGame.bullet;
		width = image.getWidth();
		height = image.getHeight();
		this.x = x;
		this.y = y;
	}

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

	@Override
	public boolean outOfBounds() {
		// TODO 自动生成的方法存根
		return this.y < -this.height;
	}

}

4.UserDao.java

package com.jyb.airplane.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.jyb.airplane.entity.User;

public class UserDao implements ImplUserDao {
    private String path = "./user.properties";
	@Override
	public List<User> readAll() {
		List<User> list = new ArrayList<User>();
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			is = new FileInputStream(path);
			isr = new InputStreamReader(is, "GB2312");
			br = new BufferedReader(isr);
			String context = "";
			try {
				while ((context = br.readLine()) != null) {
					if (context.length() == 0) {
						continue;
					}
					String[] messages = context.split(";");
					User e = new User(
							messages[0], 
							messages[1], 
							messages[2], 
							new SimpleDateFormat("yyyy-MM-dd").parse(messages[3]), 
							new SimpleDateFormat("yyyy-MM-dd").parse(messages[4]),  
							Integer.parseInt(messages[5]));
					list.add(e);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br!=null) {
					br.close();
				}
				if (isr!=null) {
					isr.close();
				}
				if (is!=null) {
					is.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public boolean writeAll(List<User> list) {
		boolean flag = false;
		OutputStream os = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		try {
			os = new FileOutputStream(path);
			osw = new OutputStreamWriter(os, "GB2312");
			bw = new BufferedWriter(osw);
			for (User u : list) {
				StringBuilder sb = new StringBuilder();
				sb.append(u.getUserId() + ";");
				sb.append(u.getNickName() + ";");
				sb.append(u.getPassword() + ";");
				sb.append(new SimpleDateFormat("yyyy-MM-dd")
						.format(u.getRegistTime()) + ";");
				sb.append(new SimpleDateFormat("yyyy-MM-dd")
						.format(u.getCreateShoreRecordTime()) + ";");
				sb.append(u.getShore());
				bw.write(sb.toString());
				bw.newLine();
			}
			bw.flush();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(bw != null) {
					bw.close();
				}
				if(osw != null) {
					osw.close();
				}
				if(os != null) {
					os.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	@Override
	public String getNewID() {
		Random r = new Random();
		StringBuilder sb = new StringBuilder();
		char[] numbers = new char[]
				{'0', '1', '2', '3', '4', '5',
				'6', '7', '8', '9', 'a', 'b', 
				'c', 'd', 'e', 'f', 'g', 'h', 
				'i', 'j', 'k', 'l', 'm', 'n',
				'o', 'p', 'q', 'r', 's', 't',
				'u', 'v', 'w', 'x', 'y', 'z'};
		for (int i = 0; i < numbers.length; i++) {
			sb.append(numbers[r.nextInt(numbers.length)]);
		}
		return sb.toString();
	}

}

5.User.java

package com.jyb.airplane.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable, Comparable<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userId; // 用户编号
	private String nickName; // 用户昵称
	private String password; // 用户密码
	private Date registTime; // 用户注册时间
	private Date createShoreRecordTime; // 用户最高分纪录创建时间
	private Integer shore; // 用户得分

	@Override
	public String toString() {
		return "n用户信息: [用户编号=" + userId + ", " + "用户昵称=" + nickName + ", " + "密码=" + password + "n, " + "注册时间="
				+ registTime + ", " + "创建时间=" + createShoreRecordTime + ", " + "得分=" + shore + "]";
	}

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(String userId, String nickName, String password, Date registTime, Date createShoreRecordTime,
			Integer shore) {
		super();
		this.userId = userId;
		this.nickName = nickName;
		this.password = password;
		this.registTime = registTime;
		this.createShoreRecordTime = createShoreRecordTime;
		this.shore = shore;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getRegistTime() {
		return registTime;
	}

	public void setRegistTime(Date registTime) {
		this.registTime = registTime;
	}

	public Date getCreateShoreRecordTime() {
		return createShoreRecordTime;
	}

	public void setCreateShoreRecordTime(Date createShoreRecordTime) {
		this.createShoreRecordTime = createShoreRecordTime;
	}

	public Integer getShore() {
		return shore;
	}

	public void setShore(Integer shore) {
		this.shore = shore;
	}

	@Override
	public int compareTo(User u) {
		if (u.shore > this.shore) {
			return 1;
		} else if (u.shore == this.shore) {
			return 0;
		} else {
			return -1;
		}
	}

}

6.OwnPlane.java

package com.jyb.airplane.ownplane;

import java.awt.image.BufferedImage;

import com.jyb.airplane.bullet.Bullet;
import com.jyb.airplane.flyingObject.FlyingObject;
import com.jyb.airplane.view.ShootGame;
//英雄机是飞行物
public class OwnPlane extends FlyingObject {
    private int life;    //生命值
    private int doubleFire;   //火力值
    private BufferedImage[] images;    //英雄机图片数组
    private int index;        //协助图片切换
    private int score;    //打死敌人后得分
    public OwnPlane() {
    	image = ShootGame.ownPlane0;
    	width = image.getWidth();
		height = image.getHeight();
		x = 150;
		y = 400;  
		life = 3;     //设置生命数为3
		doubleFire = 0;        //设置火力值为单倍
		images = new BufferedImage[]{ShootGame.ownPlane0, ShootGame.ownPlane1};
		index = 0;
		score = 0;
    }
	@Override
	public void step(OwnPlane ownPlane) {
		// 每100毫秒切换一次图片
		image = images[index++ / 10 % images.length];
	}

	public Bullet[] shoot() {
		int xStep = this.width / 9;
	    if (doubleFire > 0) {           //双发
			Bullet[] bullets = new Bullet[2];
			bullets[0] = new Bullet(this.x + 2 * xStep, this.y);
			bullets[1] = new Bullet(this.x + 6 * xStep, this.y);
			doubleFire -= 2;      //发射双倍火力,每次减2,限制双倍火力的持续时间
			return bullets;
		} else {                        //单发
			Bullet[] bullets = new Bullet[1];
			bullets[0] = new Bullet(this.x + 4 * xStep, this.y);
			return bullets;
		}
	}
    
	public void moveTo(int x, int y) {
		this.x = x - this.width / 2;
		this.y = y - this.height / 2;
	}
	@Override
	public boolean outOfBounds() {
		//英雄机永不越界
		return false;
	}
	
    public void setLife(int life) {
    	this.life = life;
    }
    //得到生命值
    public int getLife() {
    	return life;
    }
    //增加火力值
    public void addDoubleFire(){
    	doubleFire += 60;
    }
    //火力值清零
    public void setDoubleFire(int doubleFire){
    	this.doubleFire = doubleFire;
    }
    //得到双倍火力的剩余时间
    public int getDoubleFireTime() {
    	return doubleFire / 2;
    }
    
    //英雄机撞敌人
    public boolean hit(FlyingObject fo){
    	int x1 = fo.x - this.width / 2;
    	int x2 = fo.x + this.width / 2;
    	int y1 = fo.y - this.height / 2;
    	int y2 = fo.y + this.height / 2;
    	int hx = this.x + this.width / 2;
    	int hy = this.y + this.height / 2;
    	return hx > x1 && hx < x2 && hy > y1 && hy < y2;
    }
    
    //得到分数
	public int getScore() {
		return score;
	}
	//设置分数
	public void setScore(int score) {
		this.score = score;
	}
}

有任何疑问和和源码需求敬请关注公众号【蜗牛资源社】

欢迎交流学习!

最后

以上就是现代信封为你收集整理的趣味JAVA——飞机大战源码的全部内容,希望文章能够帮你解决趣味JAVA——飞机大战源码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部