如何使用按键在屏幕上移动我的图片?
How can I move my picture on the screen using a keypress?
我目前正在编写一个程序,它会根据房子的大小在屏幕上显示房子,它还会检测 'A'
'S'
'W'
'D'
键按下并在 esc 键按下时中断。现在我已经让所有这些功能正常工作,以及借用清屏功能。我想上下移动屏幕上的这张图片。我知道我非常接近,但是我不确定应该如何移动图片。如果用户按下 "d",我希望程序向右移动,我假设这就像在 x 坐标上加 1,然后通过按下 "a" 从 x 减一来向左移动,同样对于向上和向下,将按 "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 );
}
这里缺少的是绘制房子的位置。让我们相应地更改绘图功能:
void draw(int h, int v, int j)
{
cout << string(v, '\n'); //vertical offset
string hs(h,' '); // prepare horizontal offset
cout <<hs<< " /"; // add horizontal offset at each start of line
for (int c = 0; c<j; c++) {
cout << " ";
}
...
}
现在移动只需要改变水平和垂直偏移。所以让我们修改函数。我们将通过引用传递这两个,以便在调用函数中更改它们的值:
void Move(int key, int& h, int& v, int housesize)
{
if (key == 'a') { // would worth considering tolower(key)
if (h>1)
h--;
}
else if (key == 's') {
h++; // is there a maximum (e.g. screensize - housewidth) ?
}
else if (key == 'd') {
v++;
}
else if (key == 'w') {
if (v>1)
v--;
}
}
最后,在main()
中你需要保持水平和垂直偏移,并在重新设计的函数中使用它们:
int h = 0, v = 0; // horizontal and vertical offset
...
while (1) {
letter = getch();
...
else if (number != 27) {
Move(number, h, v, i); // <=== just change position. Will redraw later
}
ClearScreen();
draw(h, v, i); // draw at new position what ever happened
}
Moves around, fluently! thankyou!
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
using namespace std;
void ClearScreen();
void draw(int v, int h,int j)
{
cout << string(v, '\n');
string hs(h,' ');
cout <<hs<<" /";
for(int c=0; c<j; c++)
{
cout <<" ";
}
cout << "\ \n";
cout <<hs<<" /";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<" \ \n";
cout <<hs<<"/";
for(int c=0; c<j; c++)
{
cout <<" ";
}
cout <<" \ \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << "----";
}
cout <<"| \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<"| \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<"| \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << "----";
}
cout <<"|";
}
/////////////////MOVE///////////////
void Move(int key, int& h, int& v, int housesize)
{
if (key ==97)
{///////////////a//////////
if(h>1)
v--;
}
else if (key==115)
{////////////s////////////
h++;
}
else if (key==100)
{//////////d//////////////
v++;
}
else if (key==119)
{//////////w////////////
if(v>1)
h--;
}
}
/////////MAIN/////////////////
int main(int argc, char *argv[])
{
int h=0, v=0;
int i,number;
char letter;
cout << "How large would you like me to draw a house from 1 to 5? \n";
cin >> i;
draw(h,v,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,h,v,i);
}
ClearScreen();
draw(h,v,i);
}
system ("PAUSE");
return 0;
}
//////////////////////CLEARSCREAN///////////////
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 );
}
我目前正在编写一个程序,它会根据房子的大小在屏幕上显示房子,它还会检测 'A'
'S'
'W'
'D'
键按下并在 esc 键按下时中断。现在我已经让所有这些功能正常工作,以及借用清屏功能。我想上下移动屏幕上的这张图片。我知道我非常接近,但是我不确定应该如何移动图片。如果用户按下 "d",我希望程序向右移动,我假设这就像在 x 坐标上加 1,然后通过按下 "a" 从 x 减一来向左移动,同样对于向上和向下,将按 "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 );
}
这里缺少的是绘制房子的位置。让我们相应地更改绘图功能:
void draw(int h, int v, int j)
{
cout << string(v, '\n'); //vertical offset
string hs(h,' '); // prepare horizontal offset
cout <<hs<< " /"; // add horizontal offset at each start of line
for (int c = 0; c<j; c++) {
cout << " ";
}
...
}
现在移动只需要改变水平和垂直偏移。所以让我们修改函数。我们将通过引用传递这两个,以便在调用函数中更改它们的值:
void Move(int key, int& h, int& v, int housesize)
{
if (key == 'a') { // would worth considering tolower(key)
if (h>1)
h--;
}
else if (key == 's') {
h++; // is there a maximum (e.g. screensize - housewidth) ?
}
else if (key == 'd') {
v++;
}
else if (key == 'w') {
if (v>1)
v--;
}
}
最后,在main()
中你需要保持水平和垂直偏移,并在重新设计的函数中使用它们:
int h = 0, v = 0; // horizontal and vertical offset
...
while (1) {
letter = getch();
...
else if (number != 27) {
Move(number, h, v, i); // <=== just change position. Will redraw later
}
ClearScreen();
draw(h, v, i); // draw at new position what ever happened
}
Moves around, fluently! thankyou!
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
using namespace std;
void ClearScreen();
void draw(int v, int h,int j)
{
cout << string(v, '\n');
string hs(h,' ');
cout <<hs<<" /";
for(int c=0; c<j; c++)
{
cout <<" ";
}
cout << "\ \n";
cout <<hs<<" /";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<" \ \n";
cout <<hs<<"/";
for(int c=0; c<j; c++)
{
cout <<" ";
}
cout <<" \ \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << "----";
}
cout <<"| \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<"| \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << " ";
}
cout <<"| \n";
cout <<hs<<"|";
for(int c=0; c<j; c++)
{
cout << "----";
}
cout <<"|";
}
/////////////////MOVE///////////////
void Move(int key, int& h, int& v, int housesize)
{
if (key ==97)
{///////////////a//////////
if(h>1)
v--;
}
else if (key==115)
{////////////s////////////
h++;
}
else if (key==100)
{//////////d//////////////
v++;
}
else if (key==119)
{//////////w////////////
if(v>1)
h--;
}
}
/////////MAIN/////////////////
int main(int argc, char *argv[])
{
int h=0, v=0;
int i,number;
char letter;
cout << "How large would you like me to draw a house from 1 to 5? \n";
cin >> i;
draw(h,v,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,h,v,i);
}
ClearScreen();
draw(h,v,i);
}
system ("PAUSE");
return 0;
}
//////////////////////CLEARSCREAN///////////////
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 );
}