c语言雷霆战机小游戏

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#define MAX 100
long long int speed = 0;//控制敌机的速度 
int position_x, position_y;//飞机的所在位置 
int high, width;//地图的大小 
int bullet_x, bullet_y;//子弹的位置 
int enemy_x, enemy_y;//敌人的位置 
int map[MAX][MAX];
/*0表示空白,1表示战机*的区域,2表示敌人战机的位置。
3表示上下围墙,4表示左右围墙,5表示子弹的位置*/
int score;
void starup()//初始化所有的信息 
{
    high = 20;
    width = 30;
    position_x = high / 2;
    position_y = width / 2;
    bullet_x = 0;
    bullet_y = position_y;
    enemy_x = 2;
    enemy_y = position_y - 1;
    score = 0;

}
void startMap()
{
    int i, j;
    for (i = 1; i <= high - 1; i++)
    {
        map[i][1] = 4;
        for (j = 2; j <= width - 1; j++)
            map[i][j] = 0;
        map[i][width] = 4;
    }
    //下方围墙的初始化 
    i = high;
    for (j = 1; j <= width; j++)
        map[i][j] = 3;

    map[bullet_x][bullet_y] = 5;
    /*这里是战机大小的初始化开始*/
    map[position_x - 1][position_y] = 1;
    i = position_x;
    for (j = position_y - 2; j <= position_y + 2; j++)
        map[i][j] = 1;
    map[position_x + 1][position_y - 1] = 1;
    map[position_x + 1][position_y + 1] = 1;
    /***      初始化结束         **/

    /* 敌人战机的初始化 */
    map[enemy_x][enemy_y] = 2;
    map[enemy_x - 1][enemy_y - 1] = 2;
    map[enemy_x - 1][enemy_y + 1] = 2;
    /* 敌人战机初始化结束*/
}
void HideCursor()//隐藏光标 
{
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)//清理一部分屏幕 
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
void updateWithoutInput()//于输入无关的跟新 
{
    if (bullet_x > 0)
        bullet_x--;
    if ((bullet_x == enemy_x) && (bullet_y == enemy_y)||(bullet_x== enemy_x-1&&bullet_y == enemy_y-1)||(bullet_x== enemy_x-1&&bullet_y == enemy_y+1))//当敌人的飞机被击中时 
    {
        score++;
        enemy_x = 0;
        enemy_y = rand() % width;
        bullet_x = 0;
    }
    if (enemy_x > high)//当飞机超出区域 
    {
        enemy_x = 0;
        enemy_y = rand() % width;
    }
    if (speed == 1)
        for (int i = 1; i <= 100000; i++)//用来控制敌机的速度 
        {
            for (int j = 1; j <= 1000; j++)
            {
                speed = 1;
            }
        }
    speed = 0;
    if (speed == 0)
    {
        enemy_x++;
        if(map[enemy_x][enemy_y]==1) score--;
        speed = 1;
    }
}
void updateWithInput()//与输入有关的更新 
{
    char input;
    if (kbhit())//在VC6.0++下,为_kbhit()
    {
        input = getch();//在VC6.0++下为_getch();
        if (input == 'a'&&map[position_x][position_y - 3]!=4)
            position_y--;
        if (input == 's'&&map[position_x + 2][position_y - 1]!=3)
            position_x++;
        if (input == 'd'&&map[position_x][position_y+3]!=4)
            position_y++;
        if (input == 'w'&&position_x-2>=0)
            position_x--;
        if (input == ' ')
        {
            bullet_x = position_x - 1;
            bullet_y = position_y;
        }
    }
}
void show()//展示的内容 
{
    gotoxy(0, 0);
    int i, j;
    for (i = 1; i <= high; i++)
    {
        for (j = 1; j <= width; j++)
        {
            if (map[i][j] == 0)
                printf(" ");
            if (map[i][j] == 1)
                printf("*");
            if (map[i][j] == 2)
                printf("#");
            if (map[i][j] == 3)
                printf("~");
            if (map[i][j] == 4)
                printf("|");
            if (map[i][j] == 5)
                printf("|");
        }
        printf("\n");
    }
    printf("\n你的得分:%d\n\n", score);
    printf("操作说明: ASDW分别操作 左下右上四个的移动\n");
    printf("**空格是发出子弹**\n");
    printf("被敌机头部碰到会扣掉1~2分,具体取决与敌机头部与战机的接触点有多少个\n");
}
int main()
{
    starup();
    while (1)
    {
        HideCursor();
        startMap();
        show();
        updateWithoutInput();
        updateWithInput();
    }
    return 0;
}

好的,下面是C语言雷霆战机小游戏代码及其解释: ```c #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> #include<time.h> #define WIDTH 80 // 游戏区域宽度 #define HEIGHT 30 // 游戏区域高度 #define MAX_ENEMY_NUM 5 // 最大敌机数量 #define BULLET_SPEED 2 // 子弹速度 #define ENEMY_SPEED 1 // 敌机速度 #define FPS 30 // 游戏帧率 #define SCORE_PER_ENEMY 10 // 消灭一个敌机的得分 int score = 0; // 玩家得分 int level = 1; // 玩家关卡 int player_x = WIDTH/2; // 玩家飞机的x坐标 int player_y = HEIGHT-2; // 玩家飞机的y坐标 int bullet_x[HEIGHT]; // 子弹的x坐标 int bullet_y[HEIGHT]; // 子弹的y坐标 int bullet_num = 0; // 当前子弹数量 int enemy_x[MAX_ENEMY_NUM]; // 敌机的x坐标 int enemy_y[MAX_ENEMY_NUM]; // 敌机的y坐标 int enemy_num = 0; // 当前敌机数量 void gotoxy(int x, int y) // 光标移动函数 { COORD pos; HANDLE hOutput; pos.X = x; pos.Y = y; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput, pos); } void hidecursor() // 隐藏光标函数 { CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void draw_player() // 绘制玩家飞机函数 { gotoxy(player_x, player_y); printf("^"); } void draw_bullet(int i) // 绘制子弹函数 { gotoxy(bullet_x[i], bullet_y[i]); printf("|"); } void draw_enemy(int i) // 绘制敌机函数 { gotoxy(enemy_x[i], enemy_y[i]); printf("v"); } void update_bullet() // 更新子弹位置函数 { for(int i=0; i<bullet_num; i++) { bullet_y[i] -= BULLET_SPEED; if(bullet_y[i] <= 0) { for(int j=i; j<bullet_num-1; j++) { bullet_x[j] = bullet_x[j+1]; bullet_y[j] = bullet_y[j+1]; } bullet_num--; i--; } } } void update_enemy() // 更新敌机位置函数 { for(int i=0; i<enemy_num; i++) { enemy_y[i] += ENEMY_SPEED; if(enemy_y[i] >= HEIGHT-1) { for(int j=i; j<enemy_num-1; j++) { enemy_x[j] = enemy_x[j+1]; enemy_y[j] = enemy_y[j+1]; } enemy_num--; i--; } } } void draw_score() // 绘制得分函数 { gotoxy(0, HEIGHT); printf("Score: %d", score); } void draw_level() // 绘制关卡函数 { gotoxy(WIDTH-10, HEIGHT); printf("Level: %d", level); } void draw_gameover() // 绘制游戏结束函数 { gotoxy(WIDTH/2-5, HEIGHT/2); printf("Game Over!"); } void generate_enemy() // 生成敌机函数 { if(enemy_num < MAX_ENEMY_NUM) { enemy_x[enemy_num] = rand() % WIDTH; enemy_y[enemy_num] = 0; enemy_num++; } } void check_collision() // 检查碰撞函数 { for(int i=0; i<bullet_num; i++) { for(int j=0; j<enemy_num; j++) { if(bullet_x[i] == enemy_x[j] && bullet_y[i] == enemy_y[j]) { score += SCORE_PER_ENEMY; for(int k=i; k<bullet_num-1; k++) { bullet_x[k] = bullet_x[k+1]; bullet_y[k] = bullet_y[k+1]; } bullet_num--; for(int k=j; k<enemy_num-1; k++) { enemy_x[k] = enemy_x[k+1]; enemy_y[k] = enemy_y[k+1]; } enemy_num--; } } } for(int i=0; i<enemy_num; i++) { if(enemy_x[i] == player_x && enemy_y[i] == player_y) { draw_gameover(); exit(0); } } } void game_loop() // 游戏循环函数 { while(1) { system("cls"); draw_player(); for(int i=0; i<bullet_num; i++) { draw_bullet(i); } for(int i=0; i<enemy_num; i++) { draw_enemy(i); } draw_score(); draw_level(); update_bullet(); update_enemy(); check_collision(); if(rand() % 100 < level) { generate_enemy(); } if(kbhit()) { char c = getch(); if(c == 'a' && player_x > 0) { player_x--; } else if(c == 'd' && player_x < WIDTH-1) { player_x++; } else if(c == ' ') { bullet_x[bullet_num] = player_x; bullet_y[bullet_num] = player_y-1; bullet_num++; } } Sleep(1000/FPS); } } int main() { srand(time(NULL)); hidecursor(); game_loop(); return 0; } ``` 以上是C语言雷霆战机小游戏代码及其解释,希望能够帮助您了解该游戏的实现和原理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值