1

I am trying to learn how to use graphics.h and conio.h libraries.I am developing a graphic program which i need to move a rectangle after keyboard input.ex: if player press right , rectangle should move right side.Problem is i don't know how to get user input.I need to get user input inside a loop continuous.Here is my code.Any help is appreciated(keyword,function name etc)

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void drawrect(int left,int top,int right,int bot);

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    drawrect(5,400,40,450); // default start position
    firsttime=1;//counter for if its first time in for loop
    int currentl=5;
    int currentt=400;
    int currentr=40;
    int currentb=450;
        if(firsttime==1)
        {
              //get user input and drawrectangle with new inputs
              //if player press right add 5 to currentl and current r and
              //redraw the rectangle
        }


    getch();
    closegraph();
}

void drawrect(int left,int top,int right,int bot)
{
 rectangle(left,top,right,bot);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
user3524633
  • 71
  • 2
  • 11
  • Before if clause make an infinite while loop (something like while true) give some calls on if loop.. Else loop take input.. Call function.. (Remember to clear the screen or it may redraw) give some exit loop on some keywords or keystokes – Ashish Feb 05 '15 at 18:10
  • i'll put an infinite loop inside if(firsttime==1) but which funchtions should i use to get input from keyboard and how can i detect them – user3524633 Feb 05 '15 at 20:07
  • my problem is getting input and detecting it – user3524633 Feb 05 '15 at 20:08
  • solved code is on answer – user3524633 Feb 05 '15 at 23:01

2 Answers2

0

You can use getch() or _getch() to read codes of keys and react on it. But some things you should think over.

1) loop is needed to perform continuois action in your program.

2) keys such as "arrow left", "arrow up", etc. is given by getch() as two codes - the first -32 and the second depends on key.

Use the following programm to see the loop example and to find codes for keys:

#include <stdio.h>
#include <ctype.h>
#include <conio.h>

int main(void)
{
    char ch;
    printf("Press 'q' to exit prom program\n");
    do{
        ch = _getch();
        printf("%c (%d)\n", ch, ch);
    } while( ch != 'q');
}
VolAnd
  • 6,367
  • 3
  • 25
  • 43
0

Its solved this code works thanks for help

#include #include

void drawrect(int left,int top,int right,int bot);

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TC\\BGI");

    int firsttime=1;//counter for if its first time in for loop
    int currentl=5;
    int currentt=400;
    int currentr=40;
    int currentb=450;

    char ch;
    settextstyle(0, HORIZ_DIR, 1);
    outtextxy(20, 20, "To start press 'S'");
    ch = getch();
    cleardevice(); 
    drawrect(5,400,40,450); // default start position

        while(ch!='q')
       { 
        ch = getch();
        switch (ch)
        {
         case KEY_RIGHT:currentr=currentr+5;
                        currentl=currentl+5;
                        break;
         case KEY_LEFT:currentr=currentr-5;
                       currentl=currentl-5;
                       break;
        }
        cleardevice();
        drawrect(currentl,currentt,currentr,currentb);
       }




    getch();
    closegraph();
}

void drawrect(int left,int top,int right,int bot)
{
 rectangle(left,top,right,bot);
}
user3524633
  • 71
  • 2
  • 11