私は単純なゲームを作ろうとしています。基本的なコンセプトは、プレーヤー(P)が地図上を移動していることです。最終的には、プレイヤーは敵を避けてレベルを完了する必要があります。現在、私はプレイヤーが弾薬をつかんで前に出ることができます。私は画面全体をクリアし、マップを頻繁に書き直して、ユーザーがリアルタイムでゲームを入力してプレイできるようにしています。ここに私の現在のコードです: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, ammoX, ammoY, score;
bool shot;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN, ALL};
eDirecton dir,shoot;
void Setup()
{
gameOver = false;
dir = STOP;
x = width/2;
y = height/2;
ammoX = rand() % width;
ammoY = rand() % height;
score = 0;
}
void Draw()
{
system("cls"); //system("clear");
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(shoot == UP){
if(i < y && j == x && score > 0){
cout << ":";
shot = true;
}
}
/*if(shoot == LEFT || shoot == ALL){
if(i == y && j < x && score > 0){
cout << "~";
shot = true;
}
}
if(shoot == RIGHT || shoot == ALL){
if(i == y && j > x && score > 0){
cout << "~";
shot = true;
}
}
if(shoot == DOWN){
if(i > y && j == x && score > 0){
cout << ":";
shot = true;
}
}*/
else if (i == y && j == x)
cout << "P";
else if (i == ammoY && j == ammoX && score < 50)
cout << "A";
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;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
/*
if(_kbhit()){
cout << _getch() << endl;
}//*/
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 75:
dir = LEFT;
break;
case 77:
dir = RIGHT;
break;
case 72:
dir = UP;
break;
case 80:
dir = DOWN;
break;
case 'w':
shoot = UP;
break;
case 'a':
shoot = LEFT;
break;
case 's':
shoot = DOWN;
break;
case 'd':
shoot = RIGHT;
break;
case 32:
shoot = ALL;
break;
case 27:
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--;
}
else if(x < 0){
x++;
}
if(y>=height){
y--;
}
else if(y < 0){
y++;
}
//if (x > width || x < 0 || y > height || y < 0)
// gameOver = true;
//if (x >= width) x = 0; else if (x < 0) x = width - 1;
//if (y >= height) y = 0; else if (y < 0) y = height - 1;
/* for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameOver = true;*/
if (x == ammoX && y == ammoY)
{
score += 10;
ammoX = rand() % width;
ammoY = rand() % height;
//nTail++;
}
if(shot == true){
score -= 10;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
shoot = STOP;
Input();
Logic();
shot = false;
dir = STOP;
Sleep(30); //sleep(10);
}
return 0;
}
が特定の行を書き換える方法がある(例えばラインとしてプレーヤーがオンになっており、プレイヤーがに動いている1)私が唯一の一部を書き換えるために持っているので、地図全体の代わりに地図。それは現在頻繁に点滅しているので、現在点滅しており、見えにくいです。ユーザーがキーを入力したときに特定の行を書き換えることができたかどうか。私は\ rとsetCursorPos()関数を試しましたが、私はそれらを完全に理解しているとは思いません。私はWindows 10とWindows 7を使用していて、Microsoft Visual 2010を使用しています。 おかげ
あなたは 'SetCursorPos'について何を理解しませんでしたか?それと、おそらく 'FillConsoleOutputCharacter'はあなたのソリューションのように聞こえます。 –
コードを保護する+すべてを読む時間がありません –