飞机大战C++代码

飞机大战的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))//当敌人的飞机被击中时 
    {
        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 <= 10000; i++)//用来控制敌机的速度 
        {
            for (int j = 1; j <= 1000; j++)
            {
                speed = 1;
            }
        }
    speed = 0;
    if (speed == 0)
    {
        enemy_x++;
        speed = 1;
    }
}
void updateWithInput()//与输入有关的更新 
{
    char input;
    if (kbhit())//在VC6.0++下,为_kbhit()
    {
        input = getch();//在VC6.0++下为_getch();
        if (input == 'a')
            position_y--;
        if (input == 's')
            position_x++;
        if (input == 'd')
            position_y++;
        if (input == 'w')
            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");
}
int main()
{
    starup();
    while (1)
    {
        HideCursor();
        startMap();
        show();
        updateWithoutInput();
        updateWithInput();
    }
    return 0;
} 

效果图

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的飞机大战C++代码示例,仅供参考: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x > width || x < 0 || y > height || y < 0) gameOver = true; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) gameOver = true; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); } return 0; } ``` 这个代码仅实现了最基本的功能,可以根据需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值