下面是一个简单的C语言飞机大战的示例代码:

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

#define WIDTH 40
#define HEIGHT 20

void drawPlane(int x, int y) {
    printf("\033[%d;%dH", y, x);
    printf("  *  \n");
    printf(" *** \n");
    printf("*****\n");
    printf(" *** \n");
}

void clearScreen() {
    system("cls"); // 清空屏幕
}

int main() {
    int planeX = WIDTH / 2;
    int planeY = HEIGHT - 5;
    int key;
    
    while (1) {
        clearScreen();
        drawPlane(planeX, planeY);
        
        if (_kbhit()) {
            key = _getch();
            
            if (key == 'a' && planeX > 0) {
                planeX--;
            }
            else if (key == 'd' && planeX < WIDTH - 4) {
                planeX++;
            }
            else if (key == 'w' && planeY > 0) {
                planeY--;
            }
            else if (key == 's' && planeY < HEIGHT - 5) {
                planeY++;
            }
        }
        
        Sleep(100); // 控制刷新速度
    }
    
    return 0;
}

这是一个简单的飞机大战的示例,你可以根据自己的需求进行扩展和修改。代码中使用了Windows下的头文件conio.hwindows.h来实现在控制台中绘制飞机和控制键盘输入


原文地址: https://www.cveoy.top/t/topic/h7Vr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录