ホームページ  >  記事  >  Java  >  Javaでオナニーゲームを実装するためのサンプルコードの共有

Javaでオナニーゲームを実装するためのサンプルコードの共有

黄舟
黄舟オリジナル
2017-10-20 10:08:382972ブラウズ

この記事では主に Java でのオナニー ゲームの実装を紹介します (完全なソース コード付き)。詳細なコードはここにまとめられています。興味のある方は参考にしてください。

前に書いています

。は共有から生まれるので、今日は時間をかけて、みんなの参考と勉強のために Java で作った小さなゲームをコンパイルして投稿しました。 Java は確かにデスクトップ アプリケーションの作成には適していません。このゲームは、単にオブジェクト指向プログラミングのプロセスを理解するためのものです。コードは非常にシンプルで理解しやすく、コメントも明確に書かれています。まだ質問がある場合は、プライベートで補講してください。

効果は以下の通りです

全コード

敵機



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

Bee



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; //区间范围内为撞上了 
 } 

}

最後に書きました

上記は、このゲームのためにコンパイルした完全なコードです。最後に、誰もがプロセスをよりよく理解できるように、マインドマップを作成して投稿しました。 OOP オブジェクト指向プログラミング。

以上がJavaでオナニーゲームを実装するためのサンプルコードの共有の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。