能换成c语言吗?我不会python
时间: 2023-08-10 07:40:43 浏览: 49
当然可以,下面是使用C语言实现的一个简单的射击类游戏程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL)); // initialize random number generator
int player_health = 100;
int enemy_health = 100;
int player_ammo = 10;
int enemy_ammo = 10;
while (1) {
// print player and enemy health
printf("Your health: %d\n", player_health);
printf("Enemy health: %d\n", enemy_health);
// player's turn
printf("Your turn. You have %d ammo.\n", player_ammo);
char action;
printf("Enter 's' to shoot or 'r' to reload: ");
scanf(" %c", &action);
if (action == 's') {
if (player_ammo > 0) {
int damage = rand() % 11 + 10; // random damage between 10 and 20
enemy_health -= damage;
player_ammo--;
printf("You shot the enemy for %d damage!\n", damage);
} else {
printf("You don't have any ammo left!\n");
}
} else if (action == 'r') {
player_ammo = 10;
printf("You reloaded your ammo.\n");
}
// check if enemy is dead
if (enemy_health <= 0) {
printf("You won!\n");
break;
}
// enemy's turn
printf("Enemy's turn. Enemy has %d ammo.\n", enemy_ammo);
if (enemy_ammo > 0) {
char action = rand() % 2 == 0 ? 's' : 'r'; // randomly shoot or reload
if (action == 's') {
int damage = rand() % 11 + 5; // random damage between 5 and 15
player_health -= damage;
enemy_ammo--;
printf("Enemy shot you for %d damage!\n", damage);
} else if (action == 'r') {
enemy_ammo = 10;
printf("Enemy reloaded their ammo.\n");
}
} else {
printf("Enemy doesn't have any ammo left!\n");
}
// check if player is dead
if (player_health <= 0) {
printf("You lost!\n");
break;
}
}
return 0;
}
```
这个示例代码和Python版本的实现逻辑相同,只是采用了C语言的语法和标准库函数。