这个问题位于under函数下面,在这里我使用system()
命令。
#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();
}
}
发布于 2022-01-09 20:39:09
我改进了你的代码,现在它在某种程度上起作用了。然而,我不能为你写整个游戏,因为我没有信息,它应该如何写。
看一看:
#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
的结构的实例中。所有函数都可以接收对该对象的引用,然后使用它(读取/修改)。
https://stackoverflow.com/questions/70644482
复制相似问题