Let us use C programming concepts to develop games.
Find the Ball Game using C language.
Use Up, Down, Right and Left arrow keys to move your bat on screen to search the Ball. Press X to exit from the game.
Find the Ball Game using C language.
Game: Find the Ball
Use Up, Down, Right and Left arrow keys to move your bat on screen to search the Ball. Press X to exit from the game.
![]() | |
|
//Find the Ball Game using C language.
#include <stdio.h>
#include <windows.h>
void display_border();
COORD c = {0, 0};
void setxy (int x, int y)
{
c.X = x; c.Y = y; // Set X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
int batx=15,baty=13, ballx=25, bally=7;
int x=25, y=10, ch1, ch2, b;
setxy(14,1);
printf("...Find The Ball...") ;
display_border();
setxy(batx, baty);
printf("===");
setxy(ballx, bally);
printf("%c",2);
ch1 = getch();
ch2 = 0;
/*When accepting arrow key, function must be called twice;
first call returns 0/0xE0;
second call returns actual key code.*/
if (ch1 == 0xE0)
{
while(ch2=getch())
{
if(ch2 == 'X') exit(0);
if(ch2 == 75)//Left
{
if(batx>11) //Restrict bat on left side wall
{
setxy(--batx,baty);
printf("===");
setxy(batx+3,baty);//Space to clear bat route
printf(" ");
}
}
if(ch2 == 77) //Right
{
if(batx<37)//Restrict bat on right side wall
{
setxy(++batx,baty);
printf("===");
setxy(batx-1,baty);//Space to clear bat route
printf(" ");
}
}
if(ch2 == 72) //Up
{
if(baty>6)
{
setxy(batx,--baty);
printf("===");
setxy(batx,baty+1);
printf(" ");
}
}
if(ch2 == 80) //Down
{
if(baty<14)
{
setxy(batx,++baty);
printf("===");
setxy(batx,baty-1);
printf(" ");
}
}
if(batx==ballx-1 && baty==bally)
{
setxy(ballx-9,bally+4);
printf("...Congratulations...");
setxy(ballx-9,bally+5);
printf(" ...Ball Found..");
setxy(ballx-9,bally+6);
printf(" ...Game Over..");
getch();
exit(0);
}
}
}
getch();
return 0;
}
void display_border()
{
int i, j;
//Top border line...
setxy(10,5);
for(j=0; j<30; j++)
printf("%c", 223);
//Bottom border line...
setxy(10,15);
for(j=0; j<=30; j++)
printf("%c", 223);
//Left and Right border line...
for(j=0; j<10; j++)
{
setxy(10,5+j);
printf("%c",219);
setxy(40,5+j);
printf("%c",219);
}
printf("\n");
}
//Code is design and developed by CProgramPracticals. Blogspot. In
//Give reference to cprogrampracticals.blogspot.in while using this code.
1.Game - Moving Objects on Screen using Arrow Keys
This game demonstrate how to move objects on screen using arrow keys. Use Up, Down, Right
and Left arrow keys to move object on screen. Press X to exit from the game. Using this game
you can draw interesting design on your screen.
Game-1 Use of Arrow Keys to move object on screen.
This game demonstrate how to move objects on screen using arrow keys.
Game logic is designed under guidance of Dr Kuntal Patel.
#include <stdio.h>
#include <windows.h>
COORD c = {0, 0};
void setxy (int x, int y)
{
c.X = x; c.Y = y; // Set X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
int x=20, y=10, ch1, ch2;
printf("Press UP, DOWN, RIGHT, LEFT Arrow Key..\n");
printf("Press X to exit game... \n");
ch1 = getch();
ch2 = 0;
if (ch1 == 0xE0)
//When accepting arrow key, function must be called twice; first call returns 0/0xE0; second call returns actual key code
{
setxy(x,y);
while(ch2 != 'X')
{
ch2 = getch();
switch(ch2)
{
case 72: setxy(x,y--);
printf("%c",2);
break;
case 80:
setxy(x,y++);
printf("%c",2);
break;
case 75:
setxy(x--,y);
printf("%c",2);
break;
case 77:
setxy(x++,y);
printf("%c",2);
break;
default: break;
};
}
}
else
printf("key pressed: %d %c\n", ch1, ch2);
system("pause");
return 0;
}
Game - Score Management using C.
Following C program demonstrate how to maintain score in a Game. Score of respective arrow key press event will be increased by one on every arrow key press event.
#include <stdio.h>
#include <windows.h>
COORD c = {0, 0};
void setxy (int x, int y)
{
c.X = x; c.Y = y; // Set X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
int i, j, x=25, y=10, ch1, ch2;
int score_x1=5, score_y1=4, up=1, down=1, right=1, left=1;
printf("Press UP, DOWN, RIGHT, LEFT Arrow Key..\n");
printf("Press X to exit game... \n");
setxy(5,3); //Top border line...
for(i=0; i<44; i++)
printf("%c", 196);
setxy(5,5);//Bottom border line...
for(i=0; i<44; i++)
printf("%c", 196);
//Verticle lines
setxy(5,4);
printf("| Left:0 | Right:0 | Up:0 | Down:0 |");
ch1 = getch();
ch2 = 0;
//When accepting arrow key, function must be called twice; first call returns 0/0xE0; second call returns actual key code
if (ch1 == 0xE0)
{
setxy(x,y);
while(ch2 != 'X')
{
ch2 = getch();
switch(ch2)
{
case 72: //Up
setxy(x,--y);
printf("%c",2);
setxy(score_x1+28,score_y1);
printf("%d",up++);
break;
case 80: //Down
setxy(x,++y);
printf("%c",2);
setxy(score_x1+39,score_y1);
printf("%d",down++);
break;
case 75: //Left
setxy(--x,y);
printf("%c",2);
setxy(score_x1+7,score_y1);
printf("%d",left++);
break;
case 77: //Right
setxy(++x,y);
printf("%c",2);
setxy(score_x1+19,score_y1);
printf("%d",right++); break;
default: break;
}
}
}
return 0;
}
//Design and developed by cprogrampracticals.blogspot.in
No comments:
Post a Comment