私は現在、家のサイズに基づいて画面に家を表示するプログラムを書いています。また、'A'
'S'
'W'
'D'
キーの押下と esc のキー押下でのブレークも検出します。これらの機能がすべて機能するようになり、クリアスクリーン機能を借りることができました。画面に表示されているこの画像を上下に移動したいと思います。私は非常に近いことを知っていますが、写真を移動するにはどうすればよいかわかりません. ユーザーが「d」を押すとプログラムが右に移動するようにしたいと思います。これは、x 座標に 1 を追加し、「a」を押して左に移動すると x から 1 を減算し、同様に上に移動するようなものだと思います。下に移動するには、「w」または「s」を押して、y 値を加算または減算します。
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
void ClearScreen();
void draw(int j)
{
cout <<" /";
for(int c=0; c<j; c++)
{
cout <<" ";
}
{
cout << "\\ \n";
}
cout << " /";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<" \\ \n";
cout << "/";
for(int c=0; c<j; c++)
{
cout <<" ";
}
cout <<" \\ \n";
cout <<"|";
for(int c=0; c<j; c++)
{
cout << "----";
}
cout <<"| \n";
cout <<"|";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<"| \n";
cout <<"|";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<"| \n";
cout <<"|";
for(int c=0; c<j; c++)
{
cout << "----";
}
cout <<"| \n";
}
/////////////////////////////////
////////////////////////////////
void Move(int key,int housesize)
{
if (key ==97)
{
cout << "you pressed 'a' \n";
draw(housesize);
}
else if (key==115)
{
cout<< "you pressed 's' \n \t";
draw(housesize);
}
else if (key==100)
{
cout<< "you pressed 'd' \n";
draw(housesize);
}
else if (key==119)
{
cout<< "you pressed 'w' \n";
draw(housesize);
}
}
////// MAIN //////////////
int main(int argc, char *argv[])
{
int i,number;
char letter;
cout << "How large would you like me to draw a house from 1 to 5? \n";
cin >> i;
draw(i);
cout << "Press a key to move the picture and esc to close \n";
while(1)
{
letter=getch();
number=letter;
if(number==27)
{
break;
}
else if (number !=27)
{
Move(number, i);
}
ClearScreen();
Move(number, i);
}
system ("PAUSE");
return 0;
}
////// CLEARSCREEN ////////////////////////////////////
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,(TCHAR) ' ',cellCount,homeCoords,&count)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,csbi.wAttributes,cellCount,homeCoords,&count)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}