Software & Finance





Turbo C Graphics - Move Object Using Cursor Keys





Here is the source code for moving graphic object using UP, Down, Left and Right Arrow Keys. The core functions for moving objects used are getimage and putimage. To read the keystorokes from keyboard, bioskey function is used.

 


Source Code


#include <stdio.h>

#include <string.h>

#include <alloc.h>

#include <graphics.h>

#include <stdlib.h>

#include <conio.h>

#include <bios.h>

#include <math.h>

#include <dos.h>

 

#define LTARROW 0x4B

#define RTARROW 0x4D

#define UPARROW 0x48

#define DNARROW 0x50

#define CR 0x0d

#define ESC 0x1b

#define ALT_X 0x2d

 

long size = 0;

void far *buffer2 = 0;

void far *buffer1 = 0;

 

void draw_pixelbackground()

{

      int i, xpos, ypos, color;

      int xmax = getmaxx();

      int ymax = getmaxy();

 

      setbkcolor(BLUE);

      setcolor(WHITE);

      rectangle(0,0,xmax, ymax);

 

      for(i = 0; i < 5000; i++)

      {

            xpos = rand() % xmax;

            ypos = rand() % ymax;

            color = rand() % 12;

            putpixel(xpos, ypos, color);

      }

}

 

void draw_object(int left, int top, int right, int bottom)

{

      size = imagesize(left, top, right, bottom);

      buffer1 = farmalloc(size);

      buffer2 = farmalloc(size);

      getimage(left, top, right, bottom, buffer1);

 

      rectangle(left, top, right, bottom);

      line(left, top, right, bottom);

      line(left, bottom, right, top);

}

 

void move_object(int left, int top, int right, int bottom,int destx, int desty)

{

      int width = right - left;

      int height = bottom - top;

      getimage(left, top, right, bottom, buffer2);

      putimage(left, top, buffer1, COPY_PUT);

 

      getimage(destx, desty, destx + width, desty + height, buffer1);

      putimage(destx, desty, buffer2, COPY_PUT);

}

 

 

void main()

{

      int grd, grm;

      int key;

      int x1 = 10, y1 = 30, x2 = 100, y2 = 70;

      int newx, newy;

      int xoffset = 10, yoffset = 5;

      detectgraph(&grd,&grm);

      initgraph(&grd, &grm, "");

 

      draw_pixelbackground();

 

      draw_object(x1, y1, x2, y2);

 

      while(1)

      {

            key = bioskey(0);

            if( (key & 0x00FF) > 0)

                  key = key & 0x00FF;

            else

                  key = (key & 0xFF00) >> 8;

 

            switch(key)

            {

            case LTARROW:

                  newx = x1 - xoffset;

                  newy = y1;

                  move_object(x1, y1, x2, y2, newx, newy);

                  x1 -= xoffset;

                  x2 -= xoffset;

                  break;

 

            case RTARROW:

                  newx = x1 + xoffset;

                  newy = y1;

                  move_object(x1, y1, x2, y2, newx, newy);

                  x1 += xoffset;

                  x2 += xoffset;

                  break;

 

            case DNARROW:

                  newx = x1;

                  newy = y1 + yoffset;

                  move_object(x1, y1, x2, y2, newx, newy);

                  y1 += yoffset;

                  y2 += yoffset;

                  break;

 

            case UPARROW:

                  newx = x1;

                  newy = y1 - yoffset;

                  move_object(x1, y1, x2, y2, newx, newy);

                  y1 -= yoffset;

                  y2 -= yoffset;

                  break;

 

            case ALT_X:

            case ESC:

            case CR:

                  closegraph();

                  exit(0);

            }

      }

 

}

 

Click here to download the Turbo C source code and EXE

 

Output



Use the UP, DOWN, LEFT and RIGHT arrow keys to move the object. Press Enter, ESC or ALT + X keys to exit from the program.