2016-11-19 11 views
0

私は現在、家の大きさに基づいて画面に家を表示するプログラムを書いています。また、escのキーを押すと、'A''S''W''D'キーが押されてブレークします。今、私はこれらの機能のすべてを働かせ、クリアスクリーン機能を借用しました。私は画面上にあるこの画像を上下に移動したいと思います。私は非常に近いと知っていますが、私はどのように絵を動かすべきかわかりません。ユーザーが "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); 
} 

答えて

0

ここで欠けているのは、あなたが家を描く位置です。それに応じて描画関数を変更してみましょう:水平移動と垂直移動を変更するだけで移動が可能です。それでは、関数を修正しましょう。呼び出し元の関数にその値を変更するように、我々は、参照することによって、これらの2つを渡します:

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 
} 
+0

大丈夫、その見栄えが良いです。50x50の最小値を持つように、パラメータの部分で手伝ってもらえますか?ボックスの上を上に行くとランタイムエラーが発生します。 – CaliBreeze

+0

住宅の大きさに応じて家の高さと幅を計算する関数が必要です。あなたが限界を超えていないことを確認するために、Move()でこれらを使用してください(例: 'if(v + width(housesize)<50)v ++;') – Christophe

+0

int&vとint&h部?このような宣言は一度も見たことがありません – CaliBreeze

0
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); 
} 
関連する問題