Java网课简易飞机大战

因之前用unity做过飞机大战的小游戏,用的脚本是C#。现在上了几节网课,又用java做的简单功能的小游戏,再次记录一下。功能非常简单。鼠标控制飞机一定,子弹发射,敌机出现以及子弹和敌机的碰撞检测。爆炸效果加上去有bug我短时间找不出来,页不太想花太多时间在这上,所以把爆炸效果删除了。素材都是自己在网上找的,凑合着吧

package game;
//背景类

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;

public class Background {
	public int bx;
	public int by;
	public Image imbk;
	public Panel nowp;//当前面板
	public Background(Panel p) {
		bx=0;
		by=0;
		imbk=new ImageIcon("img/bg.jpg").getImage();
		nowp=p;//接受当前面板
	}
	public void drawBackGround(Graphics g) {//画笔
		g.drawImage(imbk, bx, by, nowp);//nowp为监控到一个容器
		g.drawImage(imbk, bx, by-1083, nowp);//绘制两次,实现循环滚动
		
		
	}
	//背景滚动
	public void moveBk() {
		by+=1;//背景往下走
		if(by>=1083) {
			by=0;
		}
	}

}
package game;
//飞机类
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;

import javax.swing.ImageIcon;
public class Plane {
	public int px;
	public int py;
	public int pw;
	public int ph;
	public Image implane;
	public Panel nowp;
	public Plane(Panel p) {
		implane=new ImageIcon("img/BurstAircraftAircraftBig@2x.png").getImage();
		pw=implane.getWidth(nowp);
		ph=implane.getHeight(nowp);
		this.nowp=p;
		this.px=250-pw/2;//居中
		this.py=700;
		
	}
	public void drawPlane(Graphics g) {
		g.drawImage(implane, px, py, nowp);
	}
	//鼠标移动
	public void moveTo(int x,int y) {
		this.px=x-pw/2;
		this.py=y-ph/2;
		
	}
	
}
package game;
//子弹类

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;

import javax.swing.ImageIcon;

public class Bullet {
	public int bx;
	public int by;
	public int bw;
	public int bh;
	
	public Image imbullet;
	public Panel nowp;
	public Bullet(Panel p,int x,int y) {
		this.nowp=p;
		this.bx=x;
		this.by=y;
		imbullet=new ImageIcon("img/bullet1.png").getImage();
		this.bw=imbullet.getWidth(nowp);
		this.bh=imbullet.getHeight(nowp);
		
	}
	public void drawBullet(Graphics g) {
		g.drawImage(imbullet, bx, by,nowp);
	}
	public void  moveTo(int x,int y) {
		this.bx=x;
		this.by=y;
	}
}
package game;
//敌机类

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;

import javax.swing.ImageIcon;

public class Enemy {
	public int ex;
	public int ey;
	public int ew;//敌机宽度
	public int eh;//敌机高度
	public Image imenemy;
	public Panel nowp;
	public Enemy(Panel p,int x,int y) {
		this.nowp=p;
		this.ex=x;
		this.ey=y;
		imenemy=new ImageIcon("img/enemy.png").getImage();
		this.ew=imenemy.getWidth(nowp);
		this.eh=imenemy.getHeight(nowp);
		
	}
	public void drawEnemy(Graphics g) {
		g.drawImage(imenemy, ex,ey,nowp);
	}
	public void  moveTo(int x,int y) {
		this.ex=x;
		this.ey=y;
	}
}
package game;
//面板类
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Vector;

import jdk.jfr.Threshold;

public class GamePanel extends Panel implements Runnable ,MouseMotionListener{

	public Background bgBackground;
	public Plane p;
	public int count=0;//子弹计数器
	public int count1=0;//敌机计数器
	public Vector<Bullet> allBullets;//集合保存所有子弹
	public Vector<Enemy> allnEnemies;//集合保存所有敌机
	public GamePanel() {
		bgBackground = new Background(this);// 创建背景对象
		p = new Plane(this);//创建飞机
		allBullets=new Vector<Bullet>();
		allnEnemies=new Vector<Enemy>();
		// 启动线程
		Thread thread = new Thread(this);
		thread.start();
		//鼠标侦听
		this.addMouseMotionListener(this);
	}

	// paint:控制当前窗口中显示到内容
	public void paint(Graphics g) {
		bgBackground.drawBackGround(g);// 背景层
		p.drawPlane(g);// 创建飞机
		
		for(int i=0;i<allBullets.size();i++) {
			Bullet nowBullet=allBullets.elementAt(i);//获取第i颗子弹
			nowBullet.drawBullet(g);
		}
		for(int i=0;i<allnEnemies.size();i++) {
			Enemy nowEnemy=allnEnemies.elementAt(i);//获取第i个敌机
			nowEnemy.drawEnemy(g);
		}
	

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {//死循环
			//1.游戏逻辑:
			//背景移动;
			bgBackground.moveBk();
			//添加敌机;
			count1++;
			if(count1>=15) {
				//每隔10秒产生新的敌机,添加元素
				allnEnemies.addElement(new Enemy(this, (int)(Math.random()*500),-50));
				count1=0;
			}
			//敌机移动;
			for(int i=0;i<allnEnemies.size();i++) {
				Enemy nowEnemy=allnEnemies.elementAt(i);//获取第i个敌机
				nowEnemy.moveTo(nowEnemy.ex, nowEnemy.ey+10);//子弹向上移动,每隔1秒下移动10像素
				if(nowEnemy.ey>=1083) {
					allnEnemies.remove(nowEnemy);
				}
			}
			//添加子弹;
			count++;
			if(count==9) {
				//每隔3秒产生新的子弹,添加元素
				allBullets.addElement(new Bullet(this, p.px+p.pw/2, p.py));
				count=0;
			}
			//子弹移动;
			for(int i=0;i<allBullets.size();i++) {
				Bullet nowBullet=allBullets.elementAt(i);//获取第i颗子弹
				nowBullet.moveTo(nowBullet.bx, nowBullet.by-10);//子弹向上移动,每隔1秒向上移动10像素
				if(nowBullet.by<=0) {//超出屏幕则移除这个子弹
					allBullets.remove(nowBullet);
				}
			}
			//碰撞检测
			for(int i=0;i<allBullets.size();i++) {
				Bullet nowBullet=allBullets.elementAt(i);
				for(int j=0;j<allnEnemies.size();j++) {
					Enemy nowEnemy=allnEnemies.elementAt(j);
					if (this.checkHit(nowBullet.bx, nowBullet.by, nowBullet.bw, nowBullet.bh, nowEnemy.ex, nowEnemy.ey, nowEnemy.ew, nowEnemy.eh)) {
						allnEnemies.remove(j);
						allBullets.remove(i);
						i--;
						j--;
						break;
					}
				}
			}
			//2.屏幕绘制
			this.repaint();
			//3.执行fps
			try {
				Thread.sleep(100);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			
			
		}
	}

	@Override//鼠标拖动
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override//鼠标移动
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		p.moveTo(e.getX(), e.getY());
	}
	//检测碰撞
	public boolean checkHit(int r1x,int r1y,int r1w,int r1h,int r2x,int r2y,int r2w,int r2h) {
		if(r1x>r2x+r2w||r1x+r1w<r2x||r1y>r2y+r2h||r1y+r1h<r2y) {
			return false;
		}else {
			return true;
		}
	}
}
import game.*;
//入口类
public class GameMain {
		public static void main(String args[]) {
			//创建一个窗口
			GameFrame gFrame=new GameFrame();
			gFrame.setVisible(true);
			
		}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值