问题标题: AI生成的飞机大战(参数已经改的很舒服了)

1
1
已解决
马泓毅
马泓毅
中级守护
中级守护

#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>

using namespace std;

// 游戏常量
const int WIDTH = 40;
const int HEIGHT = 20;
const int ENEMY_SPEED = 1000;   // 敌人移动间隔(毫秒)
const int BULLET_SPEED = 100;  // 子弹移动间隔(毫秒)
const int ENEMY_SPAWN = 4000;  // 敌人生成间隔(毫秒)

// 坐标结构体
struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {}
};

// 游戏对象
Point player(WIDTH / 2, HEIGHT - 2);
vector<Point> bullets;
vector<Point> enemies;
int score = 0;
bool gameOver = false;

// 控制台操作函数
void setCursorPosition(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void hideCursor() {
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

// 绘制游戏界面
void draw() {
    // 清屏
    system("cls");
    
    // 绘制上边界
    for (int i = 0; i < WIDTH + 2; i++)
        cout << "-";
    cout << endl;
    
    // 绘制游戏区域
    for (int y = 0; y < HEIGHT; y++) {
        cout << "|";  // 左边界
        for (int x = 0; x < WIDTH; x++) {
            bool drawn = false;
            
            // 绘制玩家
            if (x == player.x && y == player.y) {
                cout << "A";
                drawn = true;
            }
            
            // 绘制子弹(改用索引循环)
            if (!drawn) {
                for (size_t i = 0; i < bullets.size(); i++) {  // 修改这里
                    if (x == bullets[i].x && y == bullets[i].y) {
                        cout << "|";
                        drawn = true;
                        break;
                    }
                }
            }
            
            // 绘制敌人(改用索引循环)
            if (!drawn) {
                for (size_t i = 0; i < enemies.size(); i++) {  // 修改这里
                    if (x == enemies[i].x && y == enemies[i].y) {
                        cout << "V";
                        drawn = true;
                        break;
                    }
                }
            }
            
            if (!drawn) cout << " ";
        }
        cout << "|" << endl;  // 右边界
    }
    
    // 绘制下边界
    for (int i = 0; i < WIDTH + 2; i++)
        cout << "-";
    cout << endl;
    
    // 显示分数和状态
    cout << "分数: " << score << "  控制: A(左) D(右) 空格(发射) R(重开) Q(退出)" << endl;
    if (gameOver)
        cout << "游戏结束! 按R重开或Q退出" << endl;
}

// 处理输入
void input() {
    if (_kbhit()) {
        switch (_getch()) {
            case 'a':
                if (player.x > 0) player.x--;
                break;
            case 'd':
                if (player.x < WIDTH - 1) player.x++;
                break;
            case ' ':
                bullets.push_back(Point(player.x, player.y - 1));
                break;
            case 'r':
                if (gameOver) {
                    gameOver = false;
                    score = 0;
                    player = Point(WIDTH / 2, HEIGHT - 2);
                    bullets.clear();
                    enemies.clear();
                }
                break;
            case 'q':
                exit(0);
                break;
        }
    }
}

// 更新游戏状态
void update() {
    if (gameOver) return;
    
    // 更新子弹位置
    for (size_t i = 0; i < bullets.size(); i++) {
        bullets[i].y--;
        if (bullets[i].y < 0) {
            bullets.erase(bullets.begin() + i);
            i--;
        }
    }
    
    // 检测子弹与敌人碰撞
    for (size_t i = 0; i < bullets.size(); i++) {
        for (size_t j = 0; j < enemies.size(); j++) {
            if (bullets[i].x == enemies[j].x && bullets[i].y == enemies[j].y) {
                bullets.erase(bullets.begin() + i);
                enemies.erase(enemies.begin() + j);
                score += 10;
                i--;
                break;
            }
        }
    }
    
    // 检测玩家与敌人碰撞(改用索引循环)
    for (size_t i = 0; i < enemies.size(); i++) {  // 修改这里
        if (player.x == enemies[i].x && player.y == enemies[i].y) {
            gameOver = true;
            return;
        }
    }
}

// 生成敌人
void spawnEnemy() {
    if (gameOver) return;
    int x = rand() % WIDTH;
    enemies.push_back(Point(x, 0));
}

// 移动敌人
void moveEnemies() {
    if (gameOver) return;
    for (size_t i = 0; i < enemies.size(); i++) {
        enemies[i].y++;
        if (enemies[i].y >= HEIGHT) {
            enemies.erase(enemies.begin() + i);
            i--;
        }
    }
}

int main() {
    srand(time(0));
    hideCursor();
    
    // 计时器
    DWORD lastEnemyMove = 0;
    DWORD lastBulletMove = 0;
    DWORD lastEnemySpawn = 0;
    
    while (true) {
        DWORD currentTime = GetTickCount();
        
        // 控制敌人移动频率
        if (currentTime - lastEnemyMove > ENEMY_SPEED) {
            moveEnemies();
            lastEnemyMove = currentTime;
        }
        
        // 控制子弹移动频率
        if (currentTime - lastBulletMove > BULLET_SPEED) {
            update();
            lastBulletMove = currentTime;
        }
        
        // 控制敌人生成频率
        if (currentTime - lastEnemySpawn > ENEMY_SPAWN) {
            spawnEnemy();
            lastEnemySpawn = currentTime;
        }
        
        input();
        draw();
        Sleep(50);  // 控制游戏主循环速度
    }
    
    return 0;
}


0
已采纳
石峻帆
石峻帆
新手光能
新手光能

被闪瞎了

其实也可以不闪的,就这样:

#include<bits/stdc++.h>
#include<windows.h>//头文件
void gotoxy(int x, int y)//覆盖清屏 ,从指定行列覆盖
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
    return ;
}

每次循环开始调用gotoxy(0,0);

隐藏光标:
HANDLE han = GetStdHandle(-11);
void hide(){
    CONSOLE_CURSOR_INFO cursor;
    cursor.bVisible = 0;
    cursor.dwSize = 1;
    SetConsoleCursorInfo(han,&cursor);

}

只需要在程序开始时调用一次就行了

1
1
1
0
陈锦亚
陈锦亚
修练者
修练者

真六百六十六,好玩,抽象

0
0
我要回答