#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>#define WIDTH 40
#define HEIGHT 20
#define MAX_ENEMIES 5int planeX, planeY;
int bulletX, bulletY;
int enemyX[MAX_ENEMIES], enemyY[MAX_ENEMIES];
int score;void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}void drawPlane() {
gotoxy(planeX, planeY);
printf(" ^ ");
gotoxy(planeX, planeY + 1);
printf("^^^^^");
gotoxy(planeX, planeY + 2);
printf(" ^ ^ ");
}void erasePlane() {
gotoxy(planeX, planeY);
printf(" ");
gotoxy(planeX, planeY + 1);
printf(" ");
gotoxy(planeX, planeY + 2);
printf(" ");
}void drawBullet() {
gotoxy(bulletX, bulletY);
printf("|");
}void eraseBullet() {
gotoxy(bulletX, bulletY);
printf(" ");
}void drawEnemy(int index) {
gotoxy(enemyX[index], enemyY[index]);
printf("@");
}void eraseEnemy(int index) {
gotoxy(enemyX[index], enemyY[index]);
printf(" ");
}void updateBullet() {
eraseBullet();
if (bulletY > 0) {
bulletY--;
drawBullet();
} else {
bulletY = planeY - 1;
bulletX = planeX + 2;
}
}void updateEnemy(int index) {
eraseEnemy(index);
if (enemyY[index] < HEIGHT - 1) {
enemyY[index]++;
drawEnemy(index);
} else {
enemyY[index] = 0;
enemyX[index] = rand() % (WIDTH - 2) + 1;
}
}void checkCollision() {
for (int i = 0; i < MAX_ENEMIES; i++) {
if (bulletX == enemyX[i] && bulletY == enemyY[i]) {
score++;
bulletY = planeY - 1;
bulletX = planeX + 2;
enemyY[i] = 0;
enemyX[i] = rand() % (WIDTH - 2) + 1;
}
if ((bulletX == enemyX[i] || bulletX == enemyX[i] + 1 || bulletX == enemyX[i] + 2) && bulletY == enemyY[i] + 1) {
score++;
bulletY = planeY - 1;
bulletX = planeX + 2;
enemyY[i] = 0;
enemyX[i] = rand() % (WIDTH - 2) + 1;
}
if (enemyY[i] >= planeY && (enemyX[i] == planeX || enemyX[i] == planeX + 1 || enemyX[i] == planeX + 2)) {
printf("\nGame Over\n");
exit(0);
}
}
}void setup() {
planeX = WIDTH / 2 - 1;
planeY = HEIGHT - 3;
bulletX = planeX + 2;
bulletY = planeY - 1;
for (int i = 0; i < MAX_ENEMIES; i++) {
enemyX[i] = rand() % (WIDTH - 2) + 1;
enemyY[i] = 0;
}
score = 0;
}void drawBoard() {
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (i == planeY && (j == planeX || j == planeX + 1 || j == planeX + 2)) {
printf("^");
} else if (i == bulletY && j == bulletX) {
printf("|");
} else {
int isEnemy = 0;
for (int k = 0; k < MAX_ENEMIES; k++) {
if (i == enemyY[k] && (j == enemyX[k] || j == enemyX[k] + 1 || j == enemyX[k] + 2)) {
printf("@");
isEnemy = 1;
break;
}
}
if (!isEnemy) {
printf(" ");
}
}
}
printf("\n");
}
printf("Score: %d\n", score);
}int main() {
setup();while (1) {
if (_kbhit()) {
char ch = _getch();
if (ch == 'a' && planeX > 0) {
erasePlane();
planeX--;
} else if (ch == 'd' && planeX < WIDTH - 3) {
erasePlane();
planeX++;
} else if (ch == 'w' && bulletY == planeY - 1) {
eraseBullet();
bulletY--;
}
}updateBullet();
for (int i = 0; i < MAX_ENEMIES; i++) {
updateEnemy(i);
}
checkCollision();
drawBoard();Sleep(100);
}return 0;
}
本文详细介绍了使用C语言编写的控制台飞机射击游戏的源码,涉及角色移动、子弹发射、敌人生成、碰撞检测和得分系统。玩家通过键盘控制飞机,与随机出现的敌人进行互动。
3415

被折叠的 条评论
为什么被折叠?



