我目前正在编写一个程序,该程序将根据房屋的大小在屏幕上显示房屋,它还将检测'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 );
}