Java飞机小游戏
纯属自己娱乐,不足之处,请大佬指点,参考于尚学堂高淇300集。
里面还附带了图片
由于还是萌新,所以变量名的定义都很。。。。简单易懂,注释也写的算是很全面了
对于萌新就很友好
这个是游戏父类
public class GameObject {
Image img;
double x,y;
int sudu;
int kuan,gao;
public void drawSelf(Graphics g) {
g.drawImage(img, (int)x,(int) y, null);
}
public GameObject(Image img, double x, double y, int sudu, int kuan, int gao) {
super();
this.img = img;
this.x = x;
this.y = y;
this.sudu = sudu;
this.kuan = kuan;
this.gao = gao;
}
public GameObject(Image img, double x, double y) {
super();
this.img = img;
this.x = x;
this.y = y;
}
public GameObject() {
}
public Rectangle getPeng() {
return new Rectangle((int)x,(int) y, kuan, gao);
}
}
这个是飞机类
public class plane extends GameObject{
boolean shang,xia,zuo,you;
boolean die = true ;
public void drawSelf(Graphics g) {
if(die) {
g.drawImage(img, (int)x,(int) y, null);
if(zuo) {
x-= sudu;
}
if(you) {
x+=sudu;
}
if(shang) {
y-= sudu;
}
if(xia) {
y+= sudu;
}
}else {
}
}
public plane(Image img,double x,double y) {
this.img = img;
this.x = x;
this .y = y;
this.sudu = 10;
this.kuan = img .getWidth(null);
this.gao = img .getHeight(null);
}
//按下某个键,增加相应的方向
public void addDirection(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
zuo = true;
break;
case KeyEvent.VK_UP:
shang = true;
break;
case KeyEvent.VK_RIGHT:
you = true;
break;
case KeyEvent.VK_DOWN:
xia = true;
break;
}
}
//按下某个键,取消相应的方向
public void minusDirection(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
zuo = false;
break;
case KeyEvent.VK_UP:
shang = false;
break;
case KeyEvent.VK_RIGHT:
you = false;
break;
case KeyEvent.VK_DOWN:
xia = false;
break;
}
}
}
这个是炮弹类
public class paodan extends GameObject {
double degree; //弧度
public paodan() {
x=500;
y= 400;
kuan = 10;
gao = 10;
sudu = 3;
degree = Math.random()*Math.PI*2; //0到2PI的随机数
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval((int)x, (int)y, kuan, gao);
//炮弹沿着任意角度飞
x += sudu*Math.cos(degree);
y += sudu*Math.sin(degree);
if(x<0||x>Constant.GAME_KUAN-kuan) {
degree = Math.PI-degree;
}
if(y<30||y>Constant.GAME_GAO-gao) {
degree = -degree;
}
g.setColor(c);
}
}
炮弹类和飞机类都是继承于游戏父类
然后窗口的高和宽专门弄了的常量
public class Constant {
public static final int GAME_GAO = 1200;
public static final int GAME_KUAN = 900;
}
然后是这个工具类,用来加载图片的
public class GameUtil {
// 工具类最好将构造器私有化。
private GameUtil() {
}
public static Image getImage(String path) {
BufferedImage bi = null;
try {
URL u = GameUtil.class.getClassLoader().getResource(path);
bi = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return bi;
}
}
这是游戏主窗口类,由于我的游戏父类定义了碰撞检测,所以后续的飞机和子弹相撞,他们本身其实是两个矩形,只要两个矩形相交,就会被检测到,然后就会被判断出飞机牺牲,继而加载爆炸动画,然后我还提供了计时,从飞机对象创建开始,到飞机与炮弹碰撞结束,便于让用户清楚自己的飞机存活了多久,
刚开始会发现窗口在不停的闪烁,体验巨差,在实际开发中,绘制图形是非常复杂的,绘图可能需要几秒甚至更长时间,也经常发生闪烁现象, 所以添加了双缓冲(假如窗口类继承的是Java.swing.JFrame则本身已解决了闪烁问题,但是并不完美,还是会有些许闪烁,后期还是需要改回到双缓冲)
public class MyGame extends Frame{
Image fj = GameUtil.getImage("tu/fj.png");//将图片定义为成员变量
Image bj = GameUtil.getImage("tu/bj.jpg");
plane pl =new plane(fj,250,20);
ArrayList<paodan> paodans= new ArrayList<paodan>();//容器对象存储多发炮弹
baozha bao;//创建爆炸对象
Date playTime = new Date();//创建开始时间
Date dieTime;
int time;//游戏持续的时间
@Override
public void paint(Graphics g) {//paint的作用是画出整个窗口及内部内容。被系统自动调用。g相当于画笔
Color c = g.getColor();
g.drawImage(bj, 0, 0, null);
pl.drawSelf(g);//画飞机
//画出容器中所有的炮弹
for(int i=0;i<paodans.size();i++) {
paodan p = paodans.get(i);
p.draw(g);
//飞机与炮弹的碰撞检测
boolean peng = p.getPeng().intersects(pl.getPeng());
if(peng) {
pl.die = false ;
if(bao ==null) {
bao = new baozha(pl.x, pl.y);
dieTime = new Date();
time = (int)((dieTime.getTime()-playTime.getTime())/1000);
}
bao.draw(g);
}
if(!pl.die) {
g.setColor(Color.blue);
Font f = new Font("黑体", Font.BOLD, 60);
g.setFont(f);
g.drawString("你个菜鸡存活了:"+time+"秒", 300, 300);
}
}
g.setColor(c);
}
class xiancheng extends Thread{//这个线程可以帮我们反复的重画窗口
@Override
public void run() {
while(true) {
repaint();//重画,不可以调上边的paint,因为他是自动被系统调的,我不能调
try {
Thread.sleep(40);//1s = 1000ms 40ms画一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//键盘监听的内部类
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
pl.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
pl.minusDirection(e);
}
}
public void chuang() {
this.setTitle("作者:小陈");//窗口标题
this.setVisible(true);//窗口原本默认是不可见的,为了让窗口可见
this.setSize(Constant.GAME_GAO,Constant.GAME_KUAN);// 窗口的大小
this.setLocation(100, 10);//窗口的出现位置
this.setVisible(true);//窗口原本默认是不可见的,为了让窗口可见
//增加关闭窗口监听,这样用户点击右上角关闭图标,可以关闭游戏程序
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//表示应用正常结束
}
});
new xiancheng().start();//启动线程
addKeyListener(new KeyMonitor());//给窗口增加键盘的监听
//给炮弹初始化
for(int i = 0; i<50;i++) {
paodan p= new paodan();
paodans.add(p);
}
}
public static void main(String[] args) {
MyGame zz = new MyGame();//创建对象
zz.chuang();//调用上面的初始化窗口
}
private Image offScreenImage = null;
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_GAO,Constant.GAME_KUAN);//这是游戏窗口的宽度和高度
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}
这是爆炸类,是一组16张图片组成的动画,形成的爆炸效果
public class baozha {
double x,y;
static Image[] imgs = new Image[16];
static {
for(int i=0;i<16;i++){
imgs[i] = GameUtil.getImage("tu/explode/e"+(i+1)+".gif");
imgs[i].getWidth(null);
}
}
int count;
public void draw(Graphics g){
if(count<=15){
g.drawImage(imgs[count], (int)x, (int)y, null);
count++;
}
}
public baozha(double x,double y){
this.x = x;
this.y = y;
}
}
图片奉上
这是一组爆炸动画图片