以下是一个简单的C语言打飞机游戏的源代码示例:
```c
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 20
#define HEIGHT 15
int x, y; // 飞机的坐标
int bulletX, bulletY; // 子弹的坐标
int enemyX, enemyY; // 敌机的坐标
int score; // 得分
void gotoxy(int x, int y) {
COORD pos = {x, y};
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, pos);
}
void hideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() {
x = WIDTH / 2;
y = HEIGHT - 1;
bulletX = x;
bulletY = y - 1;
enemyX = rand() % WIDTH;
enemyY = 0;
score = 0;
}
void show() {
gotoxy(0, 0);
int i, j;
for (i = 0; i < WIDTH + 2; i++) {
printf("-");
}
printf("\n");
for (j = 0; j < HEIGHT; j++) {
for (i = 0; i < WIDTH; i++) {
if (i == 0) printf("|");
if (i == x && j == y) printf("*");
else if (i == bulletX && j == bulletY) printf("|");
else if (i == enemyX && j == enemyY) printf("@");
else printf(" ");
if (i == WIDTH - 1) printf("|");
}
printf("\n");
}
for (i = 0; i < WIDTH + 2; i++) {
printf("-");
}
printf("\n");
printf("Score: %d\n", score);
}
void updateWithoutInput() {
if (bulletY >= 0) {
bulletY--;
if (bulletY < 0) {
bulletX = x;
}
}
if (enemyY < HEIGHT) {
enemyY++;
} else {
enemyY = 0;
enemyX = rand() % WIDTH;
score++;
}
}
void updateWithInput() {
char input;
if (_kbhit()) {
input = _getch();
if (input == 'a' && x > 0) {
x--;
}
if (input == 'd' && x < WIDTH - 1) {
x++;
}
if (input == ' ') {
bulletX = x;
bulletY = y - 1;
}
}
}
int main() {
hideCursor();
startup();
while (1) {
show();
updateWithoutInput();
updateWithInput();
Sleep(50); // 控制刷新速度
}
return 0;
}
```
这仅仅是一个简单的示例,并不包含完整的游戏功能,可以根据需要进行扩展和优化。