用C语言实现一个飞机大战游戏

本文详细介绍了使用C语言编写的控制台飞机射击游戏的源码,涉及角色移动、子弹发射、敌人生成、碰撞检测和得分系统。玩家通过键盘控制飞机,与随机出现的敌人进行互动。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

#define WIDTH 40
#define HEIGHT 20
#define MAX_ENEMIES 5

int 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值