趣味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;
	}
}

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

欢迎交流学习!

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
package cn.feike.shoot; import java.awt.Graphics; import java.awt.image.BufferedImage; public abstract class FlyingObject { protected double x;//物体的x坐标 protected double y;//物体的y坐标 protected double width;//物体的宽 protected double heigth;//物体的高 protected BufferedImage image;//当前正在显示的图片 protected int index = 0;//图片数组下标序号,子类中使用 protected double step;//飞行物每次(1/24秒)移动的距离 protected int life;//命 protected int state;//飞行物的状态 public static final int ACTIVE=0;//活着状态 public static final int DEAD=1;//死亡状态 public static final int REMOVE=2;//回收状态 //默认构造器 public FlyingObject() { life = 1; state = ACTIVE; } //有参构造器 public FlyingObject(double width,double heigth){ this();//调用无参数的构造器,必须写在第一行. this.x = (int)(Math.random()*(480-width)); this.y = -heigth; this.width = width; this.heigth = heigth; step = Math.random()*3+0.8;//初始化step为[0.8,3.8)之间的数 } //重写toString方法 public String toString() { return x+","+y+","+width+","+heigth+","+image; } //重写paint,方便子类对象的使用 public void paint(Graphics g) { g.drawImage(image, (int)x, (int)y, null);//绘制图片 } //飞行物移动的move方法 /** * 重构了move,方法实现播放销毁动画功能 */ public void move(){ if(state == ACTIVE){ y += step; return ; } if(state == DEAD){ //从子类对象中获取下一张照片 BufferedImage img = nextImage(); if(img == null){ state = REMOVE;//没有照片则回收 }else{ image = img;//否则把子类的图片传给image } //越界则销毁 if(y>=825){ state = REMOVE; } } } /** * 子类中必须有的方法,返回下一个要播放的照片引用, * 如果返回null表示没有可播放的照片了. */ protected abstract BufferedImage nextImage(); /** * 飞行物被打了一下 */ public void hit(){ if(life>0){ life--; } if(life==0){ state = DEAD; } } /** * 碰撞检测的方法 * 检测物体的位置是否在碰撞的范围内. * (子弹是否在飞行物的碰撞范围内) */ public boolean duang(FlyingObject obj){ //this(x,y,w,h) //obj(x,y,w,h) double x1 = this.x - obj.width; double x2 = this.x + this.width; double y1 = this.y - obj.width; double y2 = this.y + this.heigth; return x1<obj.x&&obj;.x<x2&&y1;<obj.y&&obj;.y<y2; } /** 重构FlyingObject,添加了状态检查方法 */ /** 检查飞行物死了吗 */ public boolean isDead(){ return state == DEAD; } /** 检查飞行物是否活动的 */ public boolean isActive(){ return state == ACTIVE; } /** 检查飞行是否可以被删除*/ public boolean canRemove(){ return state == REMOVE; } /** 飞行物添加"去死"方法*/ public void goDead(){ if(isActive()){ state = DEAD; } } }
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值