编写一个射击游戏的c程序
时间: 2024-05-01 13:17:12 浏览: 9
抱歉,我是一名语言模型,无法编写和执行代码。以下是一个简单的射击游戏的C程序的框架,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_WIDTH 20
#define BOARD_HEIGHT 10
#define MAX_ENEMIES 10
#define PLAYER_SYMBOL 'P'
#define ENEMY_SYMBOL 'E'
#define BULLET_SYMBOL '*'
#define EMPTY_SYMBOL ' '
// Define the player structure
struct player {
int x;
int y;
int health;
};
// Define the enemy structure
struct enemy {
int x;
int y;
int health;
int active;
};
// Define the bullet structure
struct bullet {
int x;
int y;
int active;
};
// Initialize the game board
char board[BOARD_HEIGHT][BOARD_WIDTH];
// Initialize the player, enemies, and bullets
struct player player1;
struct enemy enemies[MAX_ENEMIES];
struct bullet bullets[MAX_ENEMIES];
// Function to initialize the game board
void init_board() {
int i, j;
for (i = 0; i < BOARD_HEIGHT; i++) {
for (j = 0; j < BOARD_WIDTH; j++) {
board[i][j] = EMPTY_SYMBOL;
}
}
}
// Function to print the game board
void print_board() {
int i, j;
for (i = 0; i < BOARD_HEIGHT; i++) {
for (j = 0; j < BOARD_WIDTH; j++) {
printf("%c", board[i][j]);
}
printf("\n");
}
}
// Function to initialize the player
void init_player() {
player1.x = BOARD_WIDTH / 2;
player1.y = BOARD_HEIGHT - 1;
player1.health = 100;
board[player1.y][player1.x] = PLAYER_SYMBOL;
}
// Function to initialize the enemies
void init_enemies() {
int i;
for (i = 0; i < MAX_ENEMIES; i++) {
enemies[i].x = rand() % BOARD_WIDTH;
enemies[i].y = rand() % BOARD_HEIGHT;
enemies[i].health = 100;
enemies[i].active = 1;
board[enemies[i].y][enemies[i].x] = ENEMY_SYMBOL;
}
}
// Function to initialize the bullets
void init_bullets() {
int i;
for (i = 0; i < MAX_ENEMIES; i++) {
bullets[i].x = player1.x;
bullets[i].y = player1.y - 1;
bullets[i].active = 0;
}
}
// Function to move the player
void move_player(int direction) {
int new_x = player1.x + direction;
if (new_x >= 0 && new_x < BOARD_WIDTH) {
board[player1.y][player1.x] = EMPTY_SYMBOL;
player1.x = new_x;
board[player1.y][player1.x] = PLAYER_SYMBOL;
}
}
// Function to move the enemies
void move_enemies() {
int i;
for (i = 0; i < MAX_ENEMIES; i++) {
if (enemies[i].active) {
board[enemies[i].y][enemies[i].x] = EMPTY_SYMBOL;
enemies[i].y++;
board[enemies[i].y][enemies[i].x] = ENEMY_SYMBOL;
}
}
}
// Function to fire a bullet
void fire_bullet() {
int i;
for (i = 0; i < MAX_ENEMIES; i++) {
if (!bullets[i].active) {
bullets[i].x = player1.x;
bullets[i].y = player1.y - 1;
bullets[i].active = 1;
break;
}
}
}
// Function to move the bullets
void move_bullets() {
int i;
for (i = 0; i < MAX_ENEMIES; i++) {
if (bullets[i].active) {
board[bullets[i].y][bullets[i].x] = EMPTY_SYMBOL;
bullets[i].y--;
board[bullets[i].y][bullets[i].x] = BULLET_SYMBOL;
}
}
}
// Function to check for collisions between enemies and bullets
void check_collisions() {
int i, j;
for (i = 0; i < MAX_ENEMIES; i++) {
if (enemies[i].active) {
for (j = 0; j < MAX_ENEMIES; j++) {
if (bullets[j].active && bullets[j].y == enemies[i].y && bullets[j].x == enemies[i].x) {
bullets[j].active = 0;
enemies[i].health -= 50;
if (enemies[i].health <= 0) {
enemies[i].active = 0;
board[enemies[i].y][enemies[i].x] = EMPTY_SYMBOL;
}
}
}
}
}
}
// Function to check for collisions between player and enemies
void check_player_collisions() {
int i;
for (i = 0; i < MAX_ENEMIES; i++) {
if (enemies[i].active && enemies[i].y == player1.y && enemies[i].x == player1.x) {
player1.health -= 25;
if (player1.health <= 0) {
printf("Game over!\n");
exit(0);
}
}
}
}
int main() {
srand(time(NULL));
init_board();
init_player();
init_enemies();
init_bullets();
print_board();
while (1) {
// Move player and enemies
int input = getchar();
if (input == 'a') {
move_player(-1);
} else if (input == 'd') {
move_player(1);
}
move_enemies();
// Fire and move bullets
if (input == ' ') {
fire_bullet();
}
move_bullets();
// Check for collisions
check_collisions();
check_player_collisions();
// Print the updated game board
system("clear");
print_board();
}
return 0;
}
```