游戏描述:
在这个简单的文本版弹弹堂游戏中,你将控制一个角色通过调整角度和力度来射击敌人。每个敌人都有一定的生命值和位置。你的目标是用最少的射击次数消灭所有的敌人,获得最高的分数。
下面是一个简单的C语言代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define PI 3.14159265358979323846
// 定义敌人的结构体
struct Enemy {
int x; // x坐标
int y; // y坐标
int hp; // 生命值
};
// 初始化敌人
void initEnemies(struct Enemy *enemies, int numEnemies) {
srand(time(NULL)); // 初始化随机数种子
for (int i = 0; i < numEnemies; i++) {
enemies[i].x = rand() % 21 - 10; // 随机生成x坐标
enemies[i].y = rand() % 21 + 10; // 随机生成y坐标
enemies[i].hp = rand() % 4 + 1; // 随机生成生命值
}
}
// 计算两点之间的距离
double distance(int x1, int y1, int x2, int y2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
int main() {
int numEnemies = 5; // 敌人数量
struct Enemy enemies[numEnemies]; // 定义敌人数组
initEnemies(enemies, numEnemies); // 初始化敌人
int angle; // 射击角度
int power; // 射击力度
int totalShots = 0; // 总射击次数
int totalScore = 0; // 总得分
// 开始游戏循环
while (1) {
// 输出当前场景
printf("Current Scene:\n");
for (int i = 0; i < numEnemies; i++) {
printf("Enemy %d: (%d, %d), HP: %d\n", i+1, enemies[i].x, enemies[i].y, enemies[i].hp);
}
printf("\n");
// 输入射击角度和力度
printf("Enter angle (0-90): ");
scanf("%d", &angle);
printf("Enter power (1-10): ");
scanf("%d", &power);
// 计算射击结果
double radian = angle * PI / 180.0; // 角度转弧度
int shotX = power * cos(radian); // 计算射击落点x坐标
int shotY = power * sin(radian); // 计算射击落点y坐标
// 查找最近的敌人
int nearestEnemy = 0;
double minDistance = distance(shotX, shotY, enemies[0].x, enemies[0].y);
for (int i = 1; i < numEnemies; i++) {
double d = distance(shotX, shotY, enemies[i].x, enemies[i].y);
if (d < minDistance) {
nearestEnemy = i;
minDistance = d;
}
}
// 减少敌人生命值
enemies[nearestEnemy].hp--;
totalShots++;
// 判断游戏是否结束
int allEnemiesDead = 1;
for (int i = 0; i < numEnemies; i++) {
if (enemies[i].hp > 0) {
allEnemiesDead = 0;
break;
}
}
if (allEnemiesDead){
printf("Game Over! Total Shots: %d, Total Score: %d\n", totalShots, totalScore);
break;
}
// 计算得分
int score = 10 - enemies[nearestEnemy].hp;
totalScore += score;
printf("Hit Enemy %d! Score: %d, Total Score: %d\n", nearestEnemy+1, score, totalScore);
printf("\n");
}
return 0;
}
该游戏实现了一个简单的文本版弹弹堂,其中包括随机生成敌人、输入射击角度和力度、计算射击结果、查找最近的敌人、减少敌人生命值、判断游戏是否结束、计算得分等功能。运行结果将在控制台中输出。
以下是一个简单的运行结果:
Current Scene:
Enemy 1: (5, 13), HP: 2
Enemy 2: (-10, 15), HP: 4
Enemy 3: (-9, 22), HP: 4
Enemy 4: (-5, 17), HP: 2
Enemy 5: (7, 11), HP: 2
Enter angle (0-90): 45
Enter power (1-10): 8
Hit Enemy 2! Score: 6, Total Score: 6
Current Scene:
Enemy 1: (5, 13), HP: 2
Enemy 2: (-10, 15), HP: 3
Enemy 3: (-9, 22), HP: 4
Enemy 4: (-5, 17), HP: 2
Enemy 5: (7, 11), HP: 2
Enter angle (0-90): 30
Enter power (1-10): 5
Hit Enemy 5! Score: 8, Total Score: 14
Current Scene:
Enemy 1: (5, 13), HP: 2
Enemy 2: (-10, 15), HP: 3
Enemy 3: (-9, 22), HP: 4
Enemy 4: (-5, 17), HP: 2
Enemy 5: (3, 3), HP: 2
Enter angle (0-90): 60
Enter power (1-10): 10
Hit Enemy 3! Score: 6, Total Score: 20
Current Scene:
Enemy 1: (5, 13), HP: 2
Enemy 2: (-10, 15), HP: 3
Enemy 3: (-9, 22), HP: 3
Enemy 4: (-5, 17), HP: 2
Enemy 5: (3, 3), HP: 2
Enter angle (0-90): 75
Enter power (1-10): 7
Hit Enemy 3! Score: 7, Total Score: 27
Current Scene:
Enemy 1: (5, 13), HP: 2
Enemy 2: (-10, 15), HP: 3
Enemy 3: (-9, 22), HP: 2
Enemy 4: (-5, 17), HP: 2
Enemy 5: (3, 3), HP: 2
Enter angle (0-90): 80
Enter power (1-10): 9
Hit Enemy 4! Score: 8, Total Score: 35
Current Scene:
Enemy 1: (5, 13), HP: 2
Enemy 2: (-10, 15), HP: 3
Enemy 3: (-9, 22), HP: 2
Enemy 4: (-5, 17), HP: 1
Enemy 5: (3, 3), HP: 2
Enter angle (0-90): 50
Enter power (1-10): 6
Hit Enemy 1! Score: 9, Total Score: 44
Current Scene:
Enemy 1: (-2, -6), HP: 0
Enemy 2: (-10, 15), HP: 3
Enemy 3: (-9, 22), HP: 2
Enemy 4: (-5, 17), HP: 1
Enemy 5: (3, 3), HP: 2
Game Over! Total Shots: 7, Total Score: 44