Java实现打飞机小游戏(附完整源码)
程序员文章站
2024-02-22 19:35:58
写在前面
技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习。java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对...
写在前面
技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习。java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对象编程的过程,纯属娱乐。代码写的很简单,也很容易理解,并且注释写的很清楚了,还有问题,自己私下去补课学习。
效果如下
完整代码
敌飞机
import java.util.random; 敌飞机: 是飞行物,也是敌人 public class airplane extends flyingobject implements enemy { private int speed = 3; //移动步骤 /** 初始化数据 */ public airplane(){ this.image = shootgame.airplane; width = image.getwidth(); height = image.getheight(); y = -height; random rand = new random(); x = rand.nextint(shootgame.width - width); } /** 获取分数 */ @override public int getscore() { return 5; } /** //越界处理 */ @override public boolean outofbounds() { return y>shootgame.height; } /** 移动 */ @override public void step() { y += speed; } }
分数奖励
/** * 奖励 */ public interface award { int double_fire = 0; //双倍火力 int life = 1; //1条命 /** 获得奖励类型(上面的0或1) */ int gettype(); }
蜜蜂
import java.util.random; /** 蜜蜂 */ public class bee extends flyingobject implements award{ private int xspeed = 1; //x坐标移动速度 private int yspeed = 2; //y坐标移动速度 private int awardtype; //奖励类型 /** 初始化数据 */ public bee(){ this.image = shootgame.bee; width = image.getwidth(); height = image.getheight(); y = -height; random rand = new random(); x = rand.nextint(shootgame.width - width); awardtype = rand.nextint(2); //初始化时给奖励 } /** 获得奖励类型 */ public int gettype(){ return awardtype; } /** 越界处理 */ @override public boolean outofbounds() { return y>shootgame.height; } /** 移动,可斜着飞 */ @override public void step() { x += xspeed; y += yspeed; if(x > shootgame.width-width){ xspeed = -1; } if(x < 0){ xspeed = 1; } } }
子弹类:是飞行物体
/** * 子弹类:是飞行物 */ public class bullet extends flyingobject { private int speed = 3; //移动的速度 /** 初始化数据 */ public bullet(int x,int y){ this.x = x; this.y = y; this.image = shootgame.bullet; } /** 移动 */ @override public void step(){ y-=speed; } /** 越界处理 */ @override public boolean outofbounds() { return y<-height; } }
敌人的分数
/** * 敌人,可以有分数 */ public interface enemy { /** 敌人的分数 */ int getscore(); }
飞行物(敌机,蜜蜂,子弹,英雄机)
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; } /** * 检查是否出界 * @return true 出界与否 */ public abstract boolean outofbounds(); /** * 飞行物移动一步 */ public abstract void step(); /** * 检查当前飞行物体是否被子弹(x,y)击(shoot)中 * @param bullet 子弹对象 * @return true表示被击中了 */ public boolean shootby(bullet bullet){ int x = bullet.x; //子弹横坐标 int y = bullet.y; //子弹纵坐标 return this.x<x && x<this.x+width && this.y<y && y<this.y+height; } }
英雄机
import java.awt.image.bufferedimage; /** * 英雄机:是飞行物 java学习交流qq群:589809992 我们一起学java! */ public class hero extends flyingobject{ private bufferedimage[] images = {}; //英雄机图片 private int index = 0; //英雄机图片切换索引 private int doublefire; //双倍火力 private int life; //命 /** 初始化数据 */ public hero(){ life = 3; //初始3条命 doublefire = 0; //初始火力为0 images = new bufferedimage[]{shootgame.hero0, shootgame.hero1}; //英雄机图片数组 image = shootgame.hero0; //初始为hero0图片 width = image.getwidth(); height = image.getheight(); x = 150; y = 400; } /** 获取双倍火力 */ public int isdoublefire() { return doublefire; } /** 设置双倍火力 */ public void setdoublefire(int doublefire) { this.doublefire = doublefire; } /** 增加火力 */ public void adddoublefire(){ doublefire = 40; } /** 增命 */ public void addlife(){ //增命 life++; } /** 减命 */ public void subtractlife(){ //减命 life--; } /** 获取命 */ public int getlife(){ return life; } /** 当前物体移动了一下,相对距离,x,y鼠标位置 */ public void moveto(int x,int y){ this.x = x - width/2; this.y = y - height/2; } /** 越界处理 */ @override public boolean outofbounds() { return false; } /** 发射子弹 */ public bullet[] shoot(){ int xstep = width/4; //4半 int ystep = 20; //步 if(doublefire>0){ //双倍火力 bullet[] bullets = new bullet[2]; bullets[0] = new bullet(x+xstep,y-ystep); //y-ystep(子弹距飞机的位置) bullets[1] = new bullet(x+3*xstep,y-ystep); return bullets; }else{ //单倍火力 bullet[] bullets = new bullet[1]; bullets[0] = new bullet(x+2*xstep,y-ystep); return bullets; } } /** 移动 */ @override public void step() { if(images.length>0){ image = images[index++/10%images.length]; //切换图片hero0,hero1 } } /** 碰撞算法 */ public boolean hit(flyingobject other){ int x1 = other.x - this.width/2; //x坐标最小距离 int x2 = other.x + this.width/2 + other.width; //x坐标最大距离 int y1 = other.y - this.height/2; //y坐标最小距离 int y2 = other.y + this.height/2 + other.height; //y坐标最大距离 int herox = this.x + this.width/2; //英雄机x坐标中心点距离 int heroy = this.y + this.height/2; //英雄机y坐标中心点距离 return herox>x1 && herox<x2 && heroy>y1 && heroy<y2; //区间范围内为撞上了 } }
游戏启动主类
import java.awt.font; import java.awt.color; import java.awt.graphics; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.util.arrays; import java.util.random; import java.util.timer; import java.util.timertask; import java.awt.image.bufferedimage; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jpanel; /** * java学习交流qq群:589809992 我们一起学java! */ public class shootgame extends jpanel { public static final int width = 400; // 面板宽 public static final int height = 654; // 面板高 /** 游戏的当前状态: start running pause game_over */ 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 int score = 0; // 得分 private timer timer; // 定时器 private int intervel = 1000 / 100; // 时间间隔(毫秒) public static bufferedimage background; public static bufferedimage start; public static bufferedimage airplane; public static bufferedimage bee; public static bufferedimage bullet; public static bufferedimage hero0; public static bufferedimage hero1; public static bufferedimage pause; public static bufferedimage gameover; private flyingobject[] flyings = {}; // 敌机数组 private bullet[] bullets = {}; // 子弹数组 private hero hero = new hero(); // 英雄机 static { // 静态代码块,初始化图片资源 try { background = imageio.read(shootgame.class .getresource("background.png")); start = imageio.read(shootgame.class.getresource("start.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")); pause = imageio.read(shootgame.class.getresource("pause.png")); gameover = imageio .read(shootgame.class.getresource("gameover.png")); } catch (exception e) { e.printstacktrace(); } } /** 画 */ @override public void paint(graphics g) { g.drawimage(background, 0, 0, null); // 画背景图 painthero(g); // 画英雄机 paintbullets(g); // 画子弹 paintflyingobjects(g); // 画飞行物 paintscore(g); // 画分数 paintstate(g); // 画游戏状态 } /** 画英雄机 */ public void painthero(graphics g) { g.drawimage(hero.getimage(), hero.getx(), hero.gety(), null); } /** 画子弹 */ public void paintbullets(graphics g) { for (int i = 0; i < bullets.length; i++) { bullet b = bullets[i]; g.drawimage(b.getimage(), b.getx() - b.getwidth() / 2, b.gety(), null); } } /** 画飞行物 */ public void paintflyingobjects(graphics g) { for (int i = 0; i < flyings.length; i++) { flyingobject f = flyings[i]; g.drawimage(f.getimage(), f.getx(), f.gety(), null); } } /** 画分数 */ public void paintscore(graphics g) { int x = 10; // x坐标 int y = 25; // y坐标 font font = new font(font.sans_serif, font.bold, 22); // 字体 g.setcolor(new color(0xff0000)); g.setfont(font); // 设置字体 g.drawstring("score:" + score, x, y); // 画分数 y=y+20; // y坐标增20 g.drawstring("life:" + hero.getlife(), x, y); // 画命 } /** 画游戏状态 */ public void paintstate(graphics g) { switch (state) { case start: // 启动状态 g.drawimage(start, 0, 0, null); break; case pause: // 暂停状态 g.drawimage(pause, 0, 0, null); break; case game_over: // 游戏终止状态 g.drawimage(gameover, 0, 0, null); break; } } public static void main(string[] args) { jframe frame = new jframe("fly"); shootgame game = new shootgame(); // 面板对象 frame.add(game); // 将面板添加到jframe中 frame.setsize(width, height); // 设置大小 frame.setalwaysontop(true); // 设置其总在最上 frame.setdefaultcloseoperation(jframe.exit_on_close); // 默认关闭操作 frame.seticonimage(new imageicon("images/icon.jpg").getimage()); // 设置窗体的图标 frame.setlocationrelativeto(null); // 设置窗体初始位置 frame.setvisible(true); // 尽快调用paint game.action(); // 启动执行 } /** 启动执行代码 */ public void action() { // 鼠标监听事件 mouseadapter l = new mouseadapter() { @override public void mousemoved(mouseevent e) { // 鼠标移动 if (state == running) { // 运行状态下移动英雄机--随鼠标位置 int x = e.getx(); int y = e.gety(); hero.moveto(x, y); } } @override public void mouseentered(mouseevent e) { // 鼠标进入 if (state == pause) { // 暂停状态下运行 state = running; } } @override public void mouseexited(mouseevent e) { // 鼠标退出 if (state == running) { // 游戏未结束,则设置其为暂停 state = pause; } } @override public void mouseclicked(mouseevent e) { // 鼠标点击 switch (state) { case start: state = running; // 启动状态下运行 break; case game_over: // 游戏结束,清理现场 flyings = new flyingobject[0]; // 清空飞行物 bullets = new bullet[0]; // 清空子弹 hero = new hero(); // 重新创建英雄机 score = 0; // 清空成绩 state = start; // 状态设置为启动 break; } } }; this.addmouselistener(l); // 处理鼠标点击操作 this.addmousemotionlistener(l); // 处理鼠标滑动操作 timer = new timer(); // 主流程控制 timer.schedule(new timertask() { @override public void run() { if (state == running) { // 运行状态 enteraction(); // 飞行物入场 stepaction(); // 走一步 shootaction(); // 英雄机射击 bangaction(); // 子弹打飞行物 outofboundsaction(); // 删除越界飞行物及子弹 checkgameoveraction(); // 检查游戏结束 } repaint(); // 重绘,调用paint()方法 } }, intervel, intervel); } int flyenteredindex = 0; // 飞行物入场计数 /** 飞行物入场 */ public void enteraction() { flyenteredindex++; if (flyenteredindex % 40 == 0) { // 400毫秒生成一个飞行物--10*40 flyingobject obj = nextone(); // 随机生成一个飞行物 flyings = arrays.copyof(flyings, flyings.length + 1); flyings[flyings.length - 1] = obj; } } /** 走一步 */ public void stepaction() { for (int i = 0; i < flyings.length; i++) { // 飞行物走一步 flyingobject f = flyings[i]; f.step(); } for (int i = 0; i < bullets.length; i++) { // 子弹走一步 bullet b = bullets[i]; b.step(); } hero.step(); // 英雄机走一步 } /** 飞行物走一步 */ public void flyingstepaction() { for (int i = 0; i < flyings.length; i++) { flyingobject f = flyings[i]; f.step(); } } int shootindex = 0; // 射击计数 /** 射击 */ public void shootaction() { shootindex++; if (shootindex % 30 == 0) { // 300毫秒发一颗 bullet[] bs = hero.shoot(); // 英雄打出子弹 bullets = arrays.copyof(bullets, bullets.length + bs.length); // 扩容 system.arraycopy(bs, 0, bullets, bullets.length - bs.length, bs.length); // 追加数组 } } /** 子弹与飞行物碰撞检测 */ public void bangaction() { for (int i = 0; i < bullets.length; i++) { // 遍历所有子弹 bullet b = bullets[i]; bang(b); // 子弹和飞行物之间的碰撞检查 } } /** 删除越界飞行物及子弹 */ public void outofboundsaction() { int index = 0; // 索引 flyingobject[] flyinglives = new flyingobject[flyings.length]; // 活着的飞行物 for (int i = 0; i < flyings.length; i++) { flyingobject f = flyings[i]; if (!f.outofbounds()) { flyinglives[index++] = f; // 不越界的留着 } } flyings = arrays.copyof(flyinglives, index); // 将不越界的飞行物都留着 index = 0; // 索引重置为0 bullet[] bulletlives = new bullet[bullets.length]; for (int i = 0; i < bullets.length; i++) { bullet b = bullets[i]; if (!b.outofbounds()) { bulletlives[index++] = b; } } bullets = arrays.copyof(bulletlives, index); // 将不越界的子弹留着 } /** 检查游戏结束 */ public void checkgameoveraction() { if (isgameover()==true) { state = game_over; // 改变状态 } } /** 检查游戏是否结束 */ public boolean isgameover() { for (int i = 0; i < flyings.length; i++) { int index = -1; flyingobject obj = flyings[i]; if (hero.hit(obj)) { // 检查英雄机与飞行物是否碰撞 hero.subtractlife(); // 减命 hero.setdoublefire(0); // 双倍火力解除 index = i; // 记录碰上的飞行物索引 } if (index != -1) { flyingobject t = flyings[index]; flyings[index] = flyings[flyings.length - 1]; flyings[flyings.length - 1] = t; // 碰上的与最后一个飞行物交换 flyings = arrays.copyof(flyings, flyings.length - 1); // 删除碰上的飞行物 } } return hero.getlife() <= 0; } /** 子弹和飞行物之间的碰撞检查 */ public void bang(bullet bullet) { int index = -1; // 击中的飞行物索引 for (int i = 0; i < flyings.length; i++) { flyingobject obj = flyings[i]; if (obj.shootby(bullet)) { // 判断是否击中 index = i; // 记录被击中的飞行物的索引 break; } } if (index != -1) { // 有击中的飞行物 flyingobject one = flyings[index]; // 记录被击中的飞行物 flyingobject temp = flyings[index]; // 被击中的飞行物与最后一个飞行物交换 flyings[index] = flyings[flyings.length - 1]; flyings[flyings.length - 1] = temp; flyings = arrays.copyof(flyings, flyings.length - 1); // 删除最后一个飞行物(即被击中的) // 检查one的类型(敌人加分,奖励获取) if (one instanceof enemy) { // 检查类型,是敌人,则加分 enemy e = (enemy) one; // 强制类型转换 score += e.getscore(); // 加分 } else { // 若为奖励,设置奖励 award a = (award) one; int type = a.gettype(); // 获取奖励类型 switch (type) { case award.double_fire: hero.adddoublefire(); // 设置双倍火力 break; case award.life: hero.addlife(); // 设置加命 break; } } } } /** * 随机生成飞行物 * * @return 飞行物对象 */ public static flyingobject nextone() { random random = new random(); int type = random.nextint(20); // [0,20) if (type < 4) { return new bee(); } else { return new airplane(); } } }
写在最后
以上就是这个游戏我整理的完整代码,最后,我做了一张思维导图贴出来让大家更好的理解oop面向对象编程的过程。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java实现文件点击没反应的方法