简单的C语言打飞机游戏的源代码

以下是一个简单的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;
}
```

这仅仅是一个简单的示例,并不包含完整的游戏功能,可以根据需要进行扩展和优化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yl387113

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值