专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Java游戏代码之打飞机小游戏

Java游戏代码之打飞机小游戏

更新时间:2021-05-06 13:25:15 来源:动力节点 浏览745次

效果如下

完整代码

敌飞机

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; 
 } 
 
}

以上就是动力节点小编介绍的"Java游戏代码之打飞机小游戏"的内容,希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>