英雄机受玩家鼠标控制进行移动并发出子弹对敌人进行攻击,打中敌机会加分,打中小心心会随机获得奖励。奖励类型分为三种:英雄机生命值加一,英雄机双倍火力,英雄机三倍火力(要达到三倍火力首先需要达到双倍火力才行),当英雄机撞到敌机时,火力加成会消失。当分数达到50的时候会出现boss,boss会进行移动并且发射子弹,击败boss会判定游戏胜利。当英雄机撞到敌机,小心心或者被boss发射的子弹击中时,生命值会减一,当生命值为零的时候会判定游戏失败,英雄机如果撞到了boss那就直接白给。游戏期间用鼠标进行控制,点击鼠标游戏开始,将鼠标移动至游戏界面外可以暂停游戏。
1. 素材准备
在开发之前需要先去下载一些素材图片,包括背景图,英雄机图,敌机图,子弹图,boss图,开始,暂停,胜利以及结束图片等。
2.设计构思
3.运用到的知识
测试类代码出现了一点小BUG,已经标出,按理来说这样写当判定游戏结束或者胜利时会先清理现场,然后再修改为启动状态,但是我这个游戏结束后还是战后现场,界面上boss机发射出来的子弹并不能清除,这就很迷惑。
奖励接口
package shoot;
//小心心:奖励
public interface Award {
int TRIPLE_FIRE = 2;//三倍火力
int DOUBLE_FIRE = 1;//双倍火力
int LIFE =0 ;//1条命
public int getType();//获取奖励类型
}
得分接口
package shoot;
//敌机:得分
public interface Enemy {
public int getScope();
}
飞行物类
package shoot;
import java.awt.image.BufferedImage;
//飞行物类 父类
public abstract class FlyingObject {
protected int width;//宽
protected int height;//高
protected int x;//x坐标
protected int y;//y坐标
protected BufferedImage image;//图片
//走步
public abstract void step();
//敌人(小飞机,小心心)被子弹打
public boolean shootBy(Bullet b){
int x = b.x;//子弹的x和y
int y = b.y;
//子弹x在心的x和心的x加宽之间
//并且
//子弹y在心的y和心的y加高之间
return x>this.x && xthis.y && y
敌机类
package shoot;
import java.util.Random;
//小飞机
public class Airplane extends FlyingObject
implements Enemy {
private int speed = 2;//敌机移动的步数
//得分
public int getScope() {
return 5;
}
public Airplane(){
image = ShootGame.airplane;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random r = new Random();
x = r.nextInt(ShootGame.WIDTH - width);
}
//敌机走步
public void step() {
y += speed;
}
//重写出界
public boolean outofBounds() {
return y>ShootGame.HEIGHT;
}
}
奖励类
package shoot;
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 int getType() {
return awardType;
}
//初始化实例变量
public Bee(){
image = ShootGame.bee;
width = image.getWidth();//获取当前图片的宽
height = image.getHeight();//获取当前图片的高
y = -height;
//x = (int)(Math.random()*(ShootGame.WIDTH - width));
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
awardType = rand.nextInt(4);//0,1,2随机生成
}
//小心心走步
public void step() {
x += xSpeed;
y += ySpeed;
if(x<0){
xSpeed = 1;//往右
}
if(x>ShootGame.WIDTH - width){
xSpeed = -1;//往左
}
}
//重写出界
public boolean outofBounds() {
return y>ShootGame.HEIGHT;//y大于面板的高
}
}
英雄机类
package shoot;
import java.awt.image.BufferedImage;
//英雄机
public class Hero extends FlyingObject{
private BufferedImage[] images = {};//存放英雄机图片数组
private int index;//下标
private int doubleFire;//双倍火力
protected int tripleFire;//三倍火力
public int life;//命
//初始化实例变量
public Hero(){
image = ShootGame.hero0;
width = image.getWidth();
height = image.getHeight();
x = 250;
y = 700;
doubleFire = 0;//单倍火力
tripleFire = 0;//单倍火力
life = 3;//命3条
images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
}
//英雄机走步
public void step() {
int num = index++/10%images.length;
image = images[num];
}
//发射子弹
public Bullet[] shoot(){
int xstep = this.width/4;//4半
int ystep = 20;
if(doubleFire<=0&&tripleFire>0){
tripleFire=0;
life++;
Bullet[] bullets = new Bullet[1];
bullets[0] = new Bullet(this.x+2*xstep,this.y-ystep);
return bullets;
}else if(tripleFire>0&&doubleFire>0){
Bullet[] bullets = new Bullet[3];
bullets[0] = new Bullet(this.x+1*xstep,this.y-ystep);
bullets[1] = new Bullet(this.x+3*xstep,this.y-ystep);
bullets[2] = new Bullet(this.x+2*xstep,this.y-ystep);
return bullets;
}else if(doubleFire > 0&&tripleFire<=0){//双倍火力
Bullet[] bullets = new Bullet[2];
bullets[0] = new Bullet(this.x+1*xstep,this.y-ystep);
bullets[1] = new Bullet(this.x+3*xstep,this.y-ystep);
return bullets;
}else{//单倍火力
Bullet[] bullets = new Bullet[1];
bullets[0] = new Bullet(this.x+2*xstep,this.y-ystep);
return bullets;
}
}
//移动 传入的是鼠标的x和y
public void moveTo(int x,int y){
this.x = x-this.width/2;
this.y = y-this.height/2;
}
//添加三倍火力
public void addTripleFire(){
tripleFire+=40;
}
//添加双倍火力
public void addDoubleFire(){
doubleFire += 40;
}
//添加命
public void addLife(){
life++;
}
//获取命
public int getLife(){
return life;
}
//重写出界
public boolean outofBounds() {
return false;//永不出界
}
//判断英雄机与敌人是否发生碰撞
//other:敌人
public boolean hit(FlyingObject other){
int x1 = other.x-this.width/2;
int x2 = other.x+other.width+this.width/2;
int y1 = other.y-this.height/2;
int y2 = other.y+other.height+this.height/2;
int heroX = this.x+this.width/2;
int heroY = this.y+this.height/2;
return heroX>x1 && heroXy1 && heroYX1&&HeroXY1&&HeroYthis.x && xthis.y && y
英雄机子弹类
package shoot;
//子弹
public class Bullet extends FlyingObject {
private int speed = 5;//移动走步
public Bullet(int x,int y){
image = ShootGame.bullet;
width = image.getWidth();
height = image.getHeight();
this.x = x;//跟随英雄机坐标
this.y = y;
}
//子弹走步
public void step() {
y -= speed;
}
//重写出界
public boolean outofBounds() {
return y<-height;
}
}
boss类
package shoot;
import java.awt.image.BufferedImage;
import java.util.Random;
//import java.util.Random;
public class Boss extends FlyingObject{
protected BufferedImage image=ShootGame.boss;//图片
protected int Xspeed=2;
protected int Yspeed=1;
public int bossLife=1000;
protected int width=image.getWidth();//宽
protected int height=image.getHeight();//高
Random bx=new Random();
protected int x=bx.nextInt(ShootGame.WIDTH-width);//x坐标
protected int y=-height;//y坐标
//private BufferedImage image=ShootGame.boss;//图片
// public Boss(){
// image=ShootGame.boss;
// width=image.getWidth();
// height=image.getHeight();
// x=100;
// y=100;
// bossLife=100;
// }
//boss被子弹打
public boolean bossShootBy(Bullet b){
int x = b.x;//子弹的x和y
int y = b.y;
//子弹x在boss的x和boss的x加宽之间
//并且
//子弹y在boss的y和boss的y加高之间
return x>this.x && xthis.y && y=height){
Yspeed=-1;
}
if(x<=0){
Xspeed=2;
}
if(x>=ShootGame.WIDTH-width){
Xspeed=-2;
}
}
@Override
public boolean outofBounds() {//检测出界
// TODO Auto-generated method stub
return false;
}
}
boss子弹类
package shoot;
import java.awt.image.BufferedImage;
public class BossBullet extends FlyingObject {
private int B_Bulspeed=2;//设置Boss子弹速度
public BossBullet(int x,int y){
image=ShootGame.bossbullet;
width=image.getWidth();
height=image.getHeight();
this.x=x;//跟随boss坐标
this.y=y;
}
@Override
public void step() {
// TODO Auto-generated method stub
y+=B_Bulspeed;
}
@Override
public boolean outofBounds() {
// TODO Auto-generated method stub
return y>ShootGame.HEIGHT+height;
}
}
测试类
package shoot;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Arrays;import java.util.Random;import java.util.Timer;import java.util.TimerTask;import javax.swing.JFrame;import javax.swing.JPanel;import javax.imageio.ImageIO; //游戏主界面,主类public class ShootGame extends JPanel{ public static final int WIDTH = 600;//游戏界面的宽 public static final int HEIGHT = 900;//游戏界面的高 public static BufferedImage background; public static BufferedImage airplane; public static BufferedImage bee; public static BufferedImage bullet; public static BufferedImage gameover; public static BufferedImage hero0; public static BufferedImage hero1; public static BufferedImage start; public static BufferedImage pause; public static BufferedImage boss; public static BufferedImage bossbullet; public static BufferedImage win; public Hero hero = new Hero();//英雄级对象 public Bullet[] bullets = {};//子弹数组 public BossBullet[] Bullets={};//boss子弹数组 public FlyingObject[] flyings = {};//敌人 public Boss bosses=new Boss();//boss //定时器 private Timer timer; //时间间隔(毫秒) private int intervar = 10; public int score = 0;//纪录分数 private int state;//状态 public static final int START = 0; public static final int RUNNING = 1; public static final int PAUSE = 2; public static final int GAME_OVER = 3; public static final int WIN=4; //加载静态资源 static{ try { background = ImageIO.read(ShootGame.class.getResource("background.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")); gameover = ImageIO.read(ShootGame.class.getResource("gameover.png")); hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png")); hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png")); start = ImageIO.read(ShootGame.class.getResource("start.png")); pause = ImageIO.read(ShootGame.class.getResource("pause.png")); boss=ImageIO.read(ShootGame.class.getResource("boss.png")); bossbullet=ImageIO.read(ShootGame.class.getResource("bossbullet.png")); win=ImageIO.read(ShootGame.class.getResource("win.png")); } catch (IOException e) { e.printStackTrace(); } } //重写绘制方法 //g就是画笔 public void paint(Graphics g){ g.drawImage(background,0,0,null); paintHero(g);//英雄 paintBullets(g);//子弹 paintFlyingObjects(g);//敌人 paintScore(g);//分数 paintState(g);//状态 paintBoss(g);//boss paintBosslife(g);//boss生命值 paintB_Bullet(g);//画boss子弹 } //画游戏状态 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; case WIN://胜利 // g.drawImage(win,0,0,null); } } //画分数 public void paintScore(Graphics g){ int x = 10; int y = 25; //设置字体 g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20)); //设置字体颜色 g.setColor(new Color(0xFF0000)); g.drawString("SCORE:"+score, x, y);//画分数 g.drawString("LIFE:"+hero.getLife(), x, y+20);//画英雄生命值 } //画boss生命值 public void paintBosslife(Graphics g){ int x=420; int y=25; //设置字体 g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20)); //设置字体颜色 g.setColor(new Color(0xFF00));//画字体颜色 if(bosses.y>=0){ g.drawString("BOSS LIFE:"+bosses.bossLife,x,y);//画boss生命值 } } //画boss public void paintBoss(Graphics g){ g.drawImage(bosses.image, bosses.x,bosses.y, null); } //画boss子弹 public void paintB_Bullet(Graphics g){ for(int j=0;j=0){ B_shootAction();//boss子弹入场 } heroBangAction();//子弹打中英雄机 bangAction();//子弹打敌人 outofBoundsAction();//删除出界的飞行物 checkGameOverAction();//检测游戏是否结束 checkWinAction();//检测游戏是否胜利 } //重构画板 repaint(); } },intervar,intervar); } //检测游戏是否结束 public void checkGameOverAction(){ if(isGameOver()){//判断游戏是否结束 state = GAME_OVER; } } //判断是否结束 public boolean isGameOver(){ for(int i=0;i=0){ herobang(a); } } } //判断boss子弹与英雄机撞击 public void herobang(BossBullet a){ if(hero.heroShootBy(a)){//是否被击中 hero.subLife();//英雄机生命值减一 BossBullet heroone=Bullets[B_flag];//删除击中后的子弹 Bullets[B_flag]=Bullets[Bullets.length-1]; Bullets[Bullets.length-1]=heroone; Bullets=Arrays.copyOf(Bullets, Bullets.length-1); } } //判断子弹与boss撞击 public void bossbang(Bullet b){ if(bosses.bossLife>0&&score>=50){ if(bosses.bossShootBy(b)){//boss是否被击中 bosses.bossLife-=10; Bullet bossone=bullets[flag];//删除击中后的子弹 bullets[flag]=bullets[bullets.length-1]; bullets[bullets.length-1]=bossone; bullets=Arrays.copyOf(bullets, bullets.length-1); } }else{ bosses.y=-bosses.height; } } //判断子弹和敌人撞击 public void bang(Bullet b){ int index = -1; //遍历所有敌人 for(int j=0;j5){ return new Bee(); }else{//其他则生成小飞机 return new Airplane(); } } int flyingIndex = 0;//飞行物入场计数 //飞行物入场 public void enterAction(){//每10毫秒调一次 flyingIndex++;//调一次自增1 if(flyingIndex % 80 == 0 ){//走30次,10*30 300毫秒 FlyingObject obj = nextOne();//随机生成的敌人 flyings = Arrays.copyOf(flyings,flyings.length+1);//扩容 flyings[flyings.length-1] = obj;//将生成的敌人放入扩容后的数组最后一位 } } //飞行物走步 public void stepAction(){ //敌人(小心心,小飞机) for(int i=0;i=30){ bosses.step(); } hero.step(); } public static void main(String[] args) { //画框 JFrame frame = new JFrame("FLY shoot"); ShootGame game = new ShootGame();//画板 frame.add(game); //将画板嵌入画框上 frame.setSize(WIDTH,HEIGHT);//大小 frame.setAlwaysOnTop(true);//总在最上 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭 frame.setLocationRelativeTo(null);//初始窗体位置 frame.setVisible(true);//显示,尽快调用paint()方法 game.action();//界面动起来 }}
页面更新:2024-04-22
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号