更新時(shí)間:2021-05-06 13:25:15 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽994次
敵飛機(jī)
import java.util.Random;
敵飛機(jī): 是飛行物,也是敵人
public class Airplane extends FlyingObject implements Enemy {
private int speed = 3; //移動(dòng)步驟
/** 初始化數(shù)據(jù) */
public Airplane(){
this.image = ShootGame.airplane;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
}
/** 獲取分?jǐn)?shù) */
@Override
public int getScore() {
return 5;
}
/** //越界處理 */
@Override
public boolean outOfBounds() {
return y>ShootGame.HEIGHT;
}
/** 移動(dòng) */
@Override
public void step() {
y += speed;
}
}
分?jǐn)?shù)獎(jiǎng)勵(lì)
/**
* 獎(jiǎng)勵(lì)
*/
public interface Award {
int DOUBLE_FIRE = 0; //雙倍火力
int LIFE = 1; //1條命
/** 獲得獎(jiǎng)勵(lì)類型(上面的0或1) */
int getType();
}
蜜蜂
import java.util.Random;
/** 蜜蜂 */
public class Bee extends FlyingObject implements Award{
private int xSpeed = 1; //x坐標(biāo)移動(dòng)速度
private int ySpeed = 2; //y坐標(biāo)移動(dòng)速度
private int awardType; //獎(jiǎng)勵(lì)類型
/** 初始化數(shù)據(jù) */
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); //初始化時(shí)給獎(jiǎng)勵(lì)
}
/** 獲得獎(jiǎng)勵(lì)類型 */
public int getType(){
return awardType;
}
/** 越界處理 */
@Override
public boolean outOfBounds() {
return y>ShootGame.HEIGHT;
}
/** 移動(dòng),可斜著飛 */
@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; //移動(dòng)的速度
/** 初始化數(shù)據(jù) */
public Bullet(int x,int y){
this.x = x;
this.y = y;
this.image = ShootGame.bullet;
}
/** 移動(dòng) */
@Override
public void step(){
y-=speed;
}
/** 越界處理 */
@Override
public boolean outOfBounds() {
return y<-height;
}
}
以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"Java游戲代碼之打飛機(jī)小游戲"的內(nèi)容,希望對(duì)大家有幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為您服務(wù)。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)