Tuesday, December 3, 2019

Arrow Keys Movement in Turbo C

A   Arrow Keys Movement in Turbo C in the C Programming Language program was written by my close friend and fellow software engineer Sir Ernel Fadriquilan. Thank you for sharing your code Sir Ernel.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.


My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.




Thank you very much for your help and support.


Program Listing

#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);
}
}

}

No comments:

Post a Comment