求一個模擬大口徑狙擊步槍射擊的游戲
樓主說的一定是這個,模擬狙擊步槍射擊的Flash游戲,名字叫
使用容器來模擬槍射擊
// 無法排版,請自己整理一下
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
Gun gun = new Gun();
// 裝彈
System.out.println(開始裝彈...);
int index = 0;
while (true) {
try {
Bullet bullet = new Bullet(++index, M54);
gun.load(bullet);
} catch (Exception e) {
System.out.println(e);
break;
}
}
// 打印彈夾子彈列表
System.out.println(彈夾子彈信息列表:);
gun.print();
// 射擊
System.out.println(開始射擊...);
while (true) {
try {
Bullet bullet = gun.shooting();
bullet.print();
} catch (Exception e) {
System.out.println(e);
break;
}
}
}
}
// 子彈類
class Bullet {
private int id;
private String type;
public Bullet() {
this.id = 1;
this.type = M54;
}
public Bullet(int id, String type) {
this.id = id;
this.type = type;
}
public int getId() {
return id;
}
public String getType() {
return type;
}
public void print() {
System.out.println(this.id + - + this.type);
}
}
// 槍類
class Gun {
private int id; // 編號
private String type;// 型號
private List<Bullet> bullets; // 彈夾
private int count; // 裝彈數量
public Gun() {
this.id = 1;
this.type = M54;
this.count = 6;
this.bullets = new ArrayList<>(count);
}
public Gun(int id, String type, int count) {
this.id = id;
this.type = type;
this.count = count;
this.bullets = new ArrayList<>(count);
}
// 裝彈
public void load(Bullet bullet) throws Exception {
if (bullets.size() == this.count) {
throw new Exception(彈夾已滿);
}
bullets.add(bullet);
}
// 射擊
public Bullet shooting() throws Exception {
if (bullets.isEmpty()) {
throw new Exception(彈夾為空);
}
return bullets.remove(0);
}
// 取得當前彈夾子彈數
public int getCount() {
return bullets.size();
}
// 顯示彈夾內子彈列表
public void print() {
for (Bullet bullet : bullets) {
bullet.print();
}
}
}
