Hi, I need help in C++ graphics. I am stuck
I want to make a a rectangle that moves with the keyboard arrows using C++. I have a program for the arrow keys and another program for a non-moving rectangle. My problem is it's 2 programs, I want to join them and make it a single program, but I have no idea how. By the way, to run this, I use VS code (Visual Studio code) WinBGI program debugging mode.
#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;
}
Below code is for arrow keys
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;
}
c++ graphics rectangles drawrectangle winbgi
|
show 1 more comment
I want to make a a rectangle that moves with the keyboard arrows using C++. I have a program for the arrow keys and another program for a non-moving rectangle. My problem is it's 2 programs, I want to join them and make it a single program, but I have no idea how. By the way, to run this, I use VS code (Visual Studio code) WinBGI program debugging mode.
#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;
}
Below code is for arrow keys
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;
}
c++ graphics rectangles drawrectangle winbgi
7
The usual way to achieve this is to take the code from one program and copy it into the other.
– paddy
Nov 15 '18 at 7:08
Did not work :(
– programmergirl2345
Nov 15 '18 at 8:58
4
Edit your question to show what you tried, and we might be able to help. We cannot read your mind.
– paddy
Nov 15 '18 at 9:18
if you can help by joining the 2 programs, please do. if you don't kindly leave
– programmergirl2345
Nov 16 '18 at 7:55
1
@programmergirl2345 we are not here to do your work for you... We can hint even code some part but not do the whole thing that would be counter productive for both sides.Did not work :(is not a problem description what do you mean by that? You got syntax errors, linker errors ? which ones ... or compiles and runs but not working as expected (do what instead of what?)? ... Without showing us how you merge the codes into single file we can only guess that you have 2x main function or forgot or duplicate includes or mess up the order of initializations etc ...
– Spektre
Nov 16 '18 at 8:38
|
show 1 more comment
I want to make a a rectangle that moves with the keyboard arrows using C++. I have a program for the arrow keys and another program for a non-moving rectangle. My problem is it's 2 programs, I want to join them and make it a single program, but I have no idea how. By the way, to run this, I use VS code (Visual Studio code) WinBGI program debugging mode.
#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;
}
Below code is for arrow keys
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;
}
c++ graphics rectangles drawrectangle winbgi
I want to make a a rectangle that moves with the keyboard arrows using C++. I have a program for the arrow keys and another program for a non-moving rectangle. My problem is it's 2 programs, I want to join them and make it a single program, but I have no idea how. By the way, to run this, I use VS code (Visual Studio code) WinBGI program debugging mode.
#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;
}
Below code is for arrow keys
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;
}
c++ graphics rectangles drawrectangle winbgi
c++ graphics rectangles drawrectangle winbgi
edited Nov 15 '18 at 10:22
Armali
7,050936100
7,050936100
asked Nov 15 '18 at 7:05
programmergirl2345programmergirl2345
13
13
7
The usual way to achieve this is to take the code from one program and copy it into the other.
– paddy
Nov 15 '18 at 7:08
Did not work :(
– programmergirl2345
Nov 15 '18 at 8:58
4
Edit your question to show what you tried, and we might be able to help. We cannot read your mind.
– paddy
Nov 15 '18 at 9:18
if you can help by joining the 2 programs, please do. if you don't kindly leave
– programmergirl2345
Nov 16 '18 at 7:55
1
@programmergirl2345 we are not here to do your work for you... We can hint even code some part but not do the whole thing that would be counter productive for both sides.Did not work :(is not a problem description what do you mean by that? You got syntax errors, linker errors ? which ones ... or compiles and runs but not working as expected (do what instead of what?)? ... Without showing us how you merge the codes into single file we can only guess that you have 2x main function or forgot or duplicate includes or mess up the order of initializations etc ...
– Spektre
Nov 16 '18 at 8:38
|
show 1 more comment
7
The usual way to achieve this is to take the code from one program and copy it into the other.
– paddy
Nov 15 '18 at 7:08
Did not work :(
– programmergirl2345
Nov 15 '18 at 8:58
4
Edit your question to show what you tried, and we might be able to help. We cannot read your mind.
– paddy
Nov 15 '18 at 9:18
if you can help by joining the 2 programs, please do. if you don't kindly leave
– programmergirl2345
Nov 16 '18 at 7:55
1
@programmergirl2345 we are not here to do your work for you... We can hint even code some part but not do the whole thing that would be counter productive for both sides.Did not work :(is not a problem description what do you mean by that? You got syntax errors, linker errors ? which ones ... or compiles and runs but not working as expected (do what instead of what?)? ... Without showing us how you merge the codes into single file we can only guess that you have 2x main function or forgot or duplicate includes or mess up the order of initializations etc ...
– Spektre
Nov 16 '18 at 8:38
7
7
The usual way to achieve this is to take the code from one program and copy it into the other.
– paddy
Nov 15 '18 at 7:08
The usual way to achieve this is to take the code from one program and copy it into the other.
– paddy
Nov 15 '18 at 7:08
Did not work :(
– programmergirl2345
Nov 15 '18 at 8:58
Did not work :(
– programmergirl2345
Nov 15 '18 at 8:58
4
4
Edit your question to show what you tried, and we might be able to help. We cannot read your mind.
– paddy
Nov 15 '18 at 9:18
Edit your question to show what you tried, and we might be able to help. We cannot read your mind.
– paddy
Nov 15 '18 at 9:18
if you can help by joining the 2 programs, please do. if you don't kindly leave
– programmergirl2345
Nov 16 '18 at 7:55
if you can help by joining the 2 programs, please do. if you don't kindly leave
– programmergirl2345
Nov 16 '18 at 7:55
1
1
@programmergirl2345 we are not here to do your work for you... We can hint even code some part but not do the whole thing that would be counter productive for both sides.
Did not work :( is not a problem description what do you mean by that? You got syntax errors, linker errors ? which ones ... or compiles and runs but not working as expected (do what instead of what?)? ... Without showing us how you merge the codes into single file we can only guess that you have 2x main function or forgot or duplicate includes or mess up the order of initializations etc ...– Spektre
Nov 16 '18 at 8:38
@programmergirl2345 we are not here to do your work for you... We can hint even code some part but not do the whole thing that would be counter productive for both sides.
Did not work :( is not a problem description what do you mean by that? You got syntax errors, linker errors ? which ones ... or compiles and runs but not working as expected (do what instead of what?)? ... Without showing us how you merge the codes into single file we can only guess that you have 2x main function or forgot or duplicate includes or mess up the order of initializations etc ...– Spektre
Nov 16 '18 at 8:38
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314076%2fhi-i-need-help-in-c-graphics-i-am-stuck%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314076%2fhi-i-need-help-in-c-graphics-i-am-stuck%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
The usual way to achieve this is to take the code from one program and copy it into the other.
– paddy
Nov 15 '18 at 7:08
Did not work :(
– programmergirl2345
Nov 15 '18 at 8:58
4
Edit your question to show what you tried, and we might be able to help. We cannot read your mind.
– paddy
Nov 15 '18 at 9:18
if you can help by joining the 2 programs, please do. if you don't kindly leave
– programmergirl2345
Nov 16 '18 at 7:55
1
@programmergirl2345 we are not here to do your work for you... We can hint even code some part but not do the whole thing that would be counter productive for both sides.
Did not work :(is not a problem description what do you mean by that? You got syntax errors, linker errors ? which ones ... or compiles and runs but not working as expected (do what instead of what?)? ... Without showing us how you merge the codes into single file we can only guess that you have 2x main function or forgot or duplicate includes or mess up the order of initializations etc ...– Spektre
Nov 16 '18 at 8:38