Привет, мне нужна помощь в C ++ графике. Я застрял

Я хочу сделать прямоугольник, который перемещается со стрелками на клавиатуре, используя C ++. У меня есть программа для клавиш со стрелками и другая программа для неподвижного прямоугольника. Моя проблема в том, что это две программы, я хочу присоединиться к ним и сделать из них одну программу, но я не знаю, как. Кстати, для этого я использую VS-код (код Visual Studio) в режиме отладки программы WinBGI.

#include<graphics>
int main()
{
//rectangle program
int gd = DETECT, gm;
int left = 150, top = 150;
int right = 450, bottom = 450;
initgraph(&gd, &gm, "");
rectangle(left, top, right, bottom);
getch();
closegraph();
system("pause");
return 0;
}

Ниже код для клавиш со стрелками

class Player{
private:
static const int SIZE  = 50;
static const int OUTLINE_COLOR = WHITE;
static const int FILL_COLOR = YELLOW;

int x, y;

void draw(int outline, int fill) const{
setcolor(outline);
setfillstyle(SOLID_FILL, fill);
fillellipse(x,y, SIZE, SIZE);
}

public:
Player(int _x=0, int _y=0){
x = _x; y=_y;
}void show() const{draw(OUTLINE_COLOR, FILL_COLOR); }
void clear() const{draw(BLACK, BLACK); }

void setPos(int _x=0, int _y=0){ x = _x; y=_y;}

void move(int dx=0, int dy=0){
clear();
x += dx; y += dy;
show();
}

};
int main( )
{
int width = getmaxwidth();
int height = getmaxheight();
initwindow(width,height);

string lines[] = { "Press Left Arrow Key or A or a to move to the left",
"Press Right Arrow Key or D or d to move to the right",
"Press Up Arrow Key or W or w to move to the top",
"Press Down Arrow Key or S or s to move to the bottom",
"Press F1 to move to the center of the screen",
"Press Del to exit"};

for (int i=0, y=10; i<6; i++, y+=30)
outtextxy (10,y, (char*)lines[i].c_str());Player player(0, height / 2);
player.show();

char ch = 0;

while ( true){

if (kbhit()){ch = getch();

if (ch>0){
ch = toupper(ch);

if (ch=='A') player.move(-10,0);
else if (ch=='D') player.move(10,0);
else if (ch=='W') player.move(0,-10);
else if (ch=='S') player.move(0,10);
} else{
// if the keys pressed was a control key
ch = getch();
if (ch==75) player.move(-10,0);
else if (ch==77) player.move(10,0);
else if (ch==72) player.move(0,-10);
else if (ch==80) player.move(0,10);
else if (ch==59){
player.clear();
player.setPos(width/2, height/2);
player.show();
}

else if (ch==83) break;
}
}
}
system("pause");
return 0;
}

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector