首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >system("cls")命令关闭输入终端

system("cls")命令关闭输入终端
EN

Stack Overflow用户
提问于 2022-01-09 18:54:36
回答 1查看 83关注 0票数 0

这个问题位于under函数下面,在这里我使用system()命令。

代码语言:javascript
运行
复制
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

bool gameover;
const int width = 60;
const int height = 30;
int x, y, FruitX, FruitY;
enum edirection { Stop = 0, Left, Right, Up, Down, };
edirection dir;

void Setup()
{
    gameover = true;
    dir = Stop;
    x = width / 2;
    y = height / 2;
    FruitX = rand() % width;
    FruitY = rand() % height;
    
}

void Draw()
{
    system("cls"); //Clears Screen, but is not working!!!

    //Top Line
    for (int i = 0; i <= width; i++)
        cout << "#";
        cout << endl;
    
    //Side Lines
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j == 0 || j == width - 1)
                cout << "#";
            
            if (i == y && j == x) 
                cout << "<";

            else if (i == FruitY && j == FruitX)
                cout << "@";
            
            else if (j > 0 || j != width - 1)
                cout << " ";
        } cout << endl;
    } 

    //Bottom Line
    for (int i = 0; i <= width; i++)
        cout << "#";
    cout << 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()
{
    switch (dir)
    {
    case Left:
        x--;
        break;
    case Right:
        x++;
        break;
    case Up:
        y--;
        break;
    case Down:
        y++;
        break;
    default:
        break;
    }
}

int main()
{
    Setup();
    while (!gameover);
    {
        Draw();
        Input();
        Logic();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-09 20:39:09

我改进了你的代码,现在它在某种程度上起作用了。然而,我不能为你写整个游戏,因为我没有信息,它应该如何写。

看一看:

代码语言:javascript
运行
复制
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>


struct GameStatus
{
    static constexpr std::size_t width { 60 };
    static constexpr std::size_t height { 30 };

    std::size_t x { };
    std::size_t y { };
    std::size_t FruitX { };
    std::size_t FruitY { };
    enum edirection { Stop = 0, Left, Right, Up, Down, };
    edirection dir;

    bool gameover { };
};

void Setup( GameStatus& game_status )
{
    game_status.gameover = false;
    game_status.dir = game_status.Stop;
    game_status.x = game_status.width / 2;
    game_status.y = game_status.height / 2;

    std::srand( std::time( 0 ) );
    game_status.FruitX = std::rand( ) % game_status.width;
    game_status.FruitY = std::rand( ) % game_status.height;
}

void Draw( GameStatus& game_status )
{
    system( "cls" );

    //Top Line
    for ( std::size_t i { }; i <= game_status.width; ++i )
    {
        std::cout << '#';
    }

    std::cout << '\n';

    //Side Lines
    for ( std::size_t i { }; i < game_status.height; ++i )
    {
        for ( std::size_t j { }; j < game_status.width; ++j )
        {
            if ( j == 0 || j == game_status.width - 1 )
                std::cout << '#';
            
            if ( i == game_status.y && j == game_status.x )
                std::cout << '<';

            else if ( i == game_status.FruitY && j == game_status.FruitX )
                std::cout << '@';
            
            else if ( j > 0 || j != game_status.width - 1 )
                std::cout << ' ';
        }

        std::cout << '\n';
    } 

    //Bottom Line
    for ( std::size_t i { }; i <= game_status.width; ++i )
        std::cout << '#';

    std::cout << '\n';
}

void Input( GameStatus& game_status )
{
    if ( _kbhit( ) )
    {
        switch ( _getch( ) )
        {
            case 'a':
                game_status.dir = game_status.Left;
                break;
            case 'd':
                game_status.dir = game_status.Right;
                break;
            case 'w':
                game_status.dir = game_status.Up;
                break;
            case 's':
                game_status.dir = game_status.Down;
                break;
            case 'x':
                game_status.gameover = true;
                break;
        }
    }
}

void Logic( GameStatus& game_status )
{
    switch ( game_status.dir )
    {
        case game_status.Left:
            --game_status.x;
            break;
        case game_status.Right:
            ++game_status.x;
            break;
        case game_status.Up:
            --game_status.y;
            break;
        case game_status.Down:
            ++game_status.y;
            break;
        default:
            break;
    }
}


int main( )
{
    GameStatus game_status;

    Setup( game_status );

    while ( !game_status.gameover )
    {
        Draw( game_status );
        Input( game_status );
        Logic( game_status );
    }
}

您可以看到结构GameStatus。所有这些危险的全局现在都封装在这个名为game_status的结构的实例中。所有函数都可以接收对该对象的引用,然后使用它(读取/修改)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70644482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档