以下是一个简单的C语言射击小游戏的实现示例。这个游戏中,玩家控制一个飞船,敌方飞船会随机出现并向玩家移动。如果玩家的飞船与敌方飞船相撞,玩家就失去一条生命,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 10
#define HEIGHT 5
#define ENEMY_SHIP 'E'
#define PLAYER_SHIP 'S'
#define BULLET '|'
char game_field[HEIGHT][WIDTH + 1];
int player_ship_x = WIDTH / 2;
int enemy_ship_x = -1;
int enemy_ship_y = -1;
int bullet_x = -1;
int bullet_y = -1;
int lives = 3;
void draw_game_field() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (j == player_ship_x && i == bullet_y) {
printf("%c", BULLET);
} else if (j == play