首页 / 电竞头条 / 游戏 / 正文

Java飞机大战项目

时间:2019-08-20 08:00:02 作者:

飞机大战游戏是一款十分有趣的射击类小游戏,流畅的画面,高难度的挑战。游戏中,玩家驾驶英雄机,在空中进行战斗。点击并移动自己的英雄机,发射炮弹,打掉敌飞机以及蜜蜂,来获得分数和奖励,打掉一架敌飞机赢得5分,打掉一只蜜蜂赢得1条命或是获得20次双倍火力,如果撞上敌飞机或小蜜蜂,将减少命、双倍火力清零。每撞到一次蜜蜂或是敌飞机命减1,当命数为0时,则游戏结束。游戏界面如下图所示

Java飞机大战项目

下面就是教大家怎么用java来写出这个项目来。

数据建模:使用一个数据模型,描述对象的关系。使用绘图坐标系作为参考模型,英雄机、敌飞机、蜜蜂、子弹都是矩形区域。

Java飞机大战项目

类的设计

Java飞机大战项目

完整代码:

Airplane类的完整代码如下所示:

package com.itkzhan.shoot;

import java.util.Random;

/** 敌机*/
public class Airplane extends FlyingObject implements Enemy{
\tprivate int speed=2;//走步的步数
\tpublic Airplane(){
\t\timage=ShootGame.airplane;
\t\twidth=image.getWidth();
\t\theight=image.getHeight();
\t\tRandom rand=new Random();
\t\tx=rand.nextInt(ShootGame.WIDTH-this.width);
\t\ty=-height;//y坐标
\t}
\tpublic int getScore() {
\t\treturn 5;
\t}
\tpublic void step() {
\t\ty+=speed;
\t}
\tpublic boolean outBounds() {
\t\t
\t\treturn this.y>ShootGame.HEIGHT;
\t}
\t
}
Award类的完整代码如下所示:
package com.itkzhan.shoot;
/** 奖励*/
public interface Award {
\tint DOUBLE_FIRE=0;//双倍火力值
\tint LIFE=1;//命
\tint getType();//得奖励类型(0或1)
}
Bee类的完整代码如下所示:
package com.itkzhan.shoot;

import java.util.Random;

/** 蜜蜂*/
public class Bee extends FlyingObject implements Award{
\tprivate int xSpeed=1;//x坐标走步步数
\tprivate int ySpeed=2;//y坐标走步步数
\tprivate int awardType;//奖励类型
\t/** Bee构造方法*/
\tpublic Bee(){
\t\timage=ShootGame.bee;//图片
\t\twidth=image.getWidth();//图片的宽
\t\theight=image.getHeight();//图片的高
\t\tRandom rand=new Random();
\t\tx=rand.nextInt(ShootGame.WIDTH-this.width);
\t\ty=-this.height;
\t\tawardType=rand.nextInt(2);
\t}
\tpublic int getType() {//奖励类型
\t\treturn awardType;
\t}
\tpublic void step() {
\t\tx+=xSpeed;
\t\ty+=ySpeed;
\t\tif(this.x>=ShootGame.WIDTH-this.width){
\t\t\txSpeed=-1;
\t\t}
\t\tif(x<=0){
\t\t\txSpeed=1;
\t\t}
\t}
\tpublic boolean outBounds() {
\t\treturn this.y>ShootGame.HEIGHT;
\t}

}
Bullet类的完整代码如下所示:
package com.itkzhan.shoot;
/** 子弹*/
public class Bullet extends FlyingObject{
\tprivate int speed=3;//走步的步数
\t/** Bullet构造方法*/
\tpublic Bullet(int x,int y){
\t\timage=ShootGame.bullet;
\t\twidth=image.getWidth();
\t\theight=image.getHeight();
\t\tthis.x=x;//x坐标
\t\tthis.y=y;//y坐标
\t}
\tpublic void step() {
\t\ty-=speed;
\t\t
\t}
\tpublic boolean outBounds() {
\t\treturn this.y<-height;
\t}
}
Enemy类的完整代码如下所示:
package com.itkzhan.shoot;

public interface Enemy {
\tint getScore();
}
FlyingObject类的完整代码如下所示:
package com.itkzhan.shoot;

import java.awt.image.BufferedImage;

/**
* 飞行物
* @author HP
*
*/
public abstract class FlyingObject {
\tprotected int x;
\tprotected int y;
\tprotected int width;
\tprotected int height;
\tprotected BufferedImage image;
\tpublic int getX() {
\t\treturn x;
\t}
\tpublic void setX(int x) {
\t\tthis.x = x;
\t}
\tpublic int getY() {
\t\treturn y;
\t}
\tpublic void setY(int y) {
\t\tthis.y = y;
\t}
\tpublic int getWidth() {
\t\treturn width;
\t}
\tpublic void setWidth(int width) {
\t\tthis.width = width;
\t}
\tpublic int getHeight() {
\t\treturn height;
\t}
\tpublic void setHeight(int height) {
\t\tthis.height = height;
\t}
\tpublic BufferedImage getImage() {
\t\treturn image;
\t}
\tpublic void setImage(BufferedImage image) {
\t\tthis.image = image;
\t}
\tpublic abstract void step();
\t//敌人被子弹打
\tpublic boolean shootBy(Bullet bullet){
\t\tint x=bullet.x;//子弹的x
\t\tint y=bullet.y;//子弹的y
\t\treturn x>this.x&&x<this.x+this.width
\t\t&&
\t\ty>this.y&&y<this.y+this.height;
\t};
\tpublic abstract boolean outBounds();
}
Hero类的完整代码如下所示:
package com.itkzhan.shoot;

import java.awt.image.BufferedImage;

/** 英雄机:是飞行物*/
public class Hero extends FlyingObject{
\tprivate int life;//命
\tprivate int doubleFire;//火力值
\tprivate BufferedImage[] images;//图片
\tprivate int index;//图片切换的频率
\t/** Hero构造方法*/
\tpublic Hero(){
\t\timage=ShootGame.hero0;//图片
\t\twidth=image.getWidth();//宽
\t\theight=image.getHeight();//高
\t\tx=150;//固定150
\t\ty=400;//固定的400
\t\tlife=3;//命值为3
\t\tdoubleFire=0;//火力值为0,单倍火力
\t\timages=new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
\t\tindex=0;
\t}
\tpublic void step() {
\t\tindex++;
\t\tint a=index/10;//每100m b=0,1
\t\tint b=a%2;
\t\timage=images[b];
\t\timage=images[b];
\t}
\t/** 发射子弹*/
\tpublic Bullet[] shoot(){
\t\tint xStep=this.width/4;//1/4英雄机的宽度
\t\tint yStep=20;//子弹离飞机的距离
\t\tif(doubleFire>0){//双倍火力
\t\t\tBullet[] bullets=new Bullet[2];
\t\t\tbullets[0]=new Bullet(this.x+xStep,this.y-yStep);
\t\t\tbullets[1]=new Bullet(this.x+3*xStep,this.y-yStep);
\t\t\tdoubleFire-=-2;
\t\t\treturn bullets;
\t\t}else{//单倍火力
\t\t\tBullet[] bullets=new Bullet[1];
\t\t\tbullets[0]=new Bullet(this.x+2*xStep,this.y-yStep);
\t\t\treturn bullets;
\t\t}
\t}
\t/** 英雄机随着鼠标移动x:鼠标的x坐标 y鼠标的y坐标*/
\tpublic void moveTo(int x,int y){
\t\tthis.x=x-this.width/2;
\t\tthis.y=y-this.height/2;
\t}
\t
\t/**获取命*/
\tpublic int getLife(){
\t\treturn life;
\t}
\t/** 减命*/
\tpublic void subtractLife(){
\t\tlife--;
\t}
\t
\tpublic void addLife(){
\t\tlife++;
\t}
\t/** 增火力值*/
\tpublic void addDoubleFire(){
\t\tdoubleFire+=40;
\t}
\t/** 设置火力值*/
\tpublic void setDoubleFire(int doubleFire){
\t\tthis.doubleFire=doubleFire;
\t}
\t@Override
\tpublic boolean outBounds() {
\t\treturn false;//永不越界
\t}
\tpublic boolean hit(FlyingObject other){
\t\tint x1=other.x-this.width/2;
\t\tint x2=other.x+other.width+this.width/2;
\t\tint y1=other.y-this.height/2;
\t\tint y2=other.y+other.height/2;
\t\tint hx=this.x+this.width/2;//
\t\tint hy=this.y+this.height/2;
\t\treturn hx>x1 && hx<x2
\t\t &&
\t\t hy>y1 && hy<y2;
\t}

}
ShootGame类的完整代码如下所示:
package com.itkzhan.shoot;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
impo
相关资讯