java特种兵射击游戏
时间: 2023-12-31 13:24:50 浏览: 140
Java实现蜘蛛纸牌小游戏源码.zip
5星 · 资源好评率100%
根据提供的引用内容,以下是一个简单的Java特种兵射击游戏的示例:
```java
package specialForce;
public class Main {
public static void main(String[] args) {
// 创建游戏对象
Game game = new Game();
// 启动游戏
game.start();
}
}
class Game {
private int score;
private int bulletCount;
private Player player;
private Animal enemy;
public Game() {
score = 0;
bulletCount = 10;
player = new Player();
enemy = new Animal();
}
public void start() {
System.out.println("游戏开始!");
while (bulletCount > 0) {
// 玩家射击
player.shoot(enemy);
// 判断是否击中敌人
if (enemy.isHit()) {
score += 10;
System.out.println("击中敌人!得分 +10");
} else {
score -= 5;
System.out.println("未击中敌人!得分 -5");
}
// 子弹数量减一
bulletCount--;
}
System.out.println("游戏结束!得分:" + score);
}
}
class Player {
public void shoot(Animal enemy) {
Bullet bullet = new Bullet();
bullet.hitEnemy(enemy);
}
}
class Animal {
private boolean isHit;
public Animal() {
isHit = false;
}
public void damage(int hurt) {
isHit = true;
}
public boolean isHit() {
return isHit;
}
}
class Bullet {
public void hitEnemy(Animal enemy) {
int hurt = (int)(Math.random() * 15 + 1);
enemy.damage(hurt);
}
}
```
阅读全文