更新時間:2021-05-06 13:25:15 來源:動力節點 瀏覽969次
敵飛機
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游戲代碼之打飛機小游戲"的內容,希望對大家有幫助,如有疑問,請在線咨詢,有專業老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習