用c语言写一个魔塔游戏
时间: 2023-11-17 10:04:51 浏览: 104
C语言写的一个魔方游戏
好的,我可以为您提供一个简单的魔塔游戏的C语言代码示例,但是需要注意的是,这只是一个基础版本,可能不具备完整的游戏体验。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAP_SIZE 15
#define LEVEL_NUM 10
int map[MAP_SIZE][MAP_SIZE];
int level = 1;
int hp = 100;
int attack = 10;
void init_map() {
int i, j;
for (i = 0; i < MAP_SIZE; i++) {
for (j = 0; j < MAP_SIZE; j++) {
if (i == 0 || i == MAP_SIZE - 1 || j == 0 || j == MAP_SIZE - 1) {
map[i][j] = 1;
} else {
map[i][j] = 0;
}
}
}
}
void print_map() {
int i, j;
for (i = 0; i < MAP_SIZE; i++) {
for (j = 0; j < MAP_SIZE; j++) {
if (map[i][j] == 0) {
printf(" ");
} else if (map[i][j] == 1) {
printf("#");
} else if (map[i][j] == 2) {
printf("S");
} else if (map[i][j] == 3) {
printf("M");
} else if (map[i][j] == 4) {
printf("T");
} else if (map[i][j] == 5) {
printf("E");
} else if (map[i][j] == 6) {
printf("B");
}
}
printf("\n");
}
printf("Level: %d, HP: %d, Attack: %d\n", level, hp, attack);
}
int get_random(int min, int max) {
srand(time(NULL));
return rand() % (max - min + 1) + min;
}
void generate_monster() {
int i, j;
do {
i = get_random(1, MAP_SIZE - 2);
j = get_random(1, MAP_SIZE - 2);
} while (map[i][j] != 0);
map[i][j] = 3;
}
void generate_treasure() {
int i, j;
do {
i = get_random(1, MAP_SIZE - 2);
j = get_random(1, MAP_SIZE - 2);
} while (map[i][j] != 0);
map[i][j] = 4;
}
void generate_exit() {
int i, j;
do {
i = get_random(1, MAP_SIZE - 2);
j = get_random(1, MAP_SIZE - 2);
} while (map[i][j] != 0);
map[i][j] = 5;
}
void generate_boss() {
int i, j;
do {
i = get_random(1, MAP_SIZE - 2);
j = get_random(1, MAP_SIZE - 2);
} while (map[i][j] != 0);
map[i][j] = 6;
}
void move(int x, int y) {
if (map[x][y] == 1) {
printf("You hit a wall!\n");
} else if (map[x][y] == 2) {
printf("You find a sword, attack + 5!\n");
attack += 5;
map[x][y] = 0;
} else if (map[x][y] == 3) {
int monster_hp = get_random(50, 100);
printf("You encounter a monster, HP: %d!\n", monster_hp);
while (hp > 0 && monster_hp > 0) {
printf("Your HP: %d, Monster HP: %d\n", hp, monster_hp);
monster_hp -= attack;
if (monster_hp <= 0) {
printf("You kill the monster!\n");
map[x][y] = 0;
break;
}
hp -= get_random(10, 20);
if (hp <= 0) {
printf("You are dead!\n");
exit(0);
}
}
} else if (map[x][y] == 4) {
printf("You find a treasure, HP + 20!\n");
hp += 20;
map[x][y] = 0;
} else if (map[x][y] == 5) {
printf("You find the exit, level up!\n");
level++;
hp = 100;
attack += 5;
if (level == LEVEL_NUM) {
printf("Congratulations, you win!\n");
exit(0);
}
init_map();
generate_monster();
generate_treasure();
generate_exit();
generate_boss();
} else if (map[x][y] == 6) {
int boss_hp = 200;
printf("You encounter the boss, HP: %d!\n", boss_hp);
while (hp > 0 && boss_hp > 0) {
printf("Your HP: %d, Boss HP: %d\n", hp, boss_hp);
boss_hp -= attack;
if (boss_hp <= 0) {
printf("You kill the boss, level up!\n");
level++;
hp = 100;
attack += 5;
if (level == LEVEL_NUM) {
printf("Congratulations, you win!\n");
exit(0);
}
init_map();
generate_monster();
generate_treasure();
generate_exit();
generate_boss();
break;
}
hp -= get_random(20, 30);
if (hp <= 0) {
printf("You are dead!\n");
exit(0);
}
}
} else {
map[x][y] = 2;
}
}
int main() {
init_map();
generate_monster();
generate_treasure();
generate_exit();
generate_boss();
while (1) {
print_map();
printf("Please input the direction you want to go (w/a/s/d): ");
char direction;
scanf("%c", &direction);
getchar();
int x = 0, y = 0;
switch (direction) {
case 'w':
x = -1, y = 0;
break;
case 'a':
x = 0, y = -1;
break;
case 's':
x = 1, y = 0;
break;
case 'd':
x = 0, y = 1;
break;
default:
printf("Invalid input!\n");
continue;
}
move(x + 1, y + 1);
}
return 0;
}
```
以上代码实现了一个简单的魔塔游戏,包括地图初始化、怪物、宝藏、出口和Boss的生成,以及人物的移动和战斗等基本功能。您可以根据自己的需求进行修改和完善。
阅读全文