Here is the Source code for my 1st graphics game in C language. Share the code for non-profit purpose.
© All Rights Reserved.
Explanation :
Here I have created 11 row and 9 col char matrix which actually acts as platform for the puzzle.
To differentiate all the elements different characters are used.
H for wall
O as moving object or "Hero"
U as the object that has to pushed by hero
X as the hurdles , U must be pushed into X
a as empty spaces.
Placing Walls, Moving objects U, hurdles X and empty spaces along with the initial place of "Hero" is covered in designing part.
You can customize your own puzzle by changing the Array elements accordingly.
Rules:
- Movement of Hero is done by using keys w as up, a as left, s as down, d as right.
- Hero is restricted through walls ans Xs.
- Hero can push only one object at a time.
- Remember only pushing is possible pulling is restricted
- When you push box in hurdles both the object and hurdles will disappear and the position acquired by X before pushing will become free space.
- At any stage to Quit press o.
- To restart press r.
- Play keeping caps lock off.
/*
Author: Roshan Kedar
Compatibility: Win XP and before, Win7 32 bit (somewhat unstable)
*/
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void display(char a[11][9]);
int pos(char a[11][9]);
void swap(char *a,char *b);
int final(char a[11][9]);
void main()
{
char a[11][9],ch;
int i,j,no,upos,final1=-1;
int gdriver = DETECT, gmode, errorcode; /* request auto detection */
int x0,x1,y0,y1;
initgraph(&gdriver, &gmode, ""); /* initialize graphics mode */
cleardevice();
ab:;
/* Designing the Stage */
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
a[i][j]='a';
}
}
for(i=0;i<11;i++)
{
a[i][0]='H';
a[i][8]='H';
}
for(i=0;i<9;i++)
{
a[0][i]='H';
a[10][i]='H';
}
for(i=1;i<11;i++)
{
for(j=2;j<9;j++)
{
if((i%2==0)&&(j%2==0))
a[i][j]='H';
}
}
a[1][1]='X';
a[1][7]='X';
a[9][1]='X';
a[9][7]='X';
a[4][3]='U';
a[4][5]='U';
a[6][3]='U';
a[6][5]='U';
a[5][1]='O';
a[1][2]='H';
a[1][4]='H';
a[1][6]='H';
a[6][7]='H';
a[7][2]='H';
a[7][4]='H';
a[7][6]='H';
/* You can change the designing part to customize your own puzzle */
abc:;
final1=final(a);
if(final1==0)
{
clrscr();
display(a);
printf("\n\n********Congrats Puzzule Solved********");
}
else
{
clrscr();
display(a);
upos=pos(a);
ch=getche();
no=ch;
if(no==119)
{
if(a[(upos/9)-1][upos%9]!='H')
{
if(a[2][1]=='U'&&a[3][1]=='O')
{
a[1][1]='a';
a[2][1]='O';
a[3][1]='a';
goto abc;
}
else if(a[2][7]=='U'&&a[3][7]=='O')
{
a[1][7]='a';
a[2][7]='O';
a[3][7]='a';
goto abc;
}
else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]=='H'))
goto abc;
else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]=='U'))
goto abc;
else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]!='H'))
{
swap(&a[(upos/9)-2][upos%9],&a[(upos/9)-1][upos%9]);
swap(&a[upos/9][upos%9],&a[(upos/9)-1][upos%9]);
}
else if(a[(upos/9)-1][upos%9]!='X')
swap(&a[upos/9][upos%9],&a[(upos/9)-1][upos%9]);
}
goto abc;
}
else if(no==115)
{
if(a[upos/9+1][upos%9]!='H')
{
if(a[8][1]=='U'&&a[7][1]=='O')
{
a[9][1]='a';
a[8][1]='O';
a[7][1]='a';
}
else if(a[8][7]=='U'&&a[7][7]=='O')
{
a[9][7]='a';
a[8][7]='O';
a[7][7]='a';
}
else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='H'))
goto abc;
else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='U'))
goto abc;
else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]!='H'))
{
swap(&a[upos/9+2][upos%9],&a[upos/9+1][upos%9]);
swap(&a[upos/9][upos%9],&a[upos/9+1][upos%9]);
}
else if(a[(upos/9)+1][upos%9]!='X')
swap(&a[upos/9][upos%9],&a[(upos/9)+1][upos%9]);
}
goto abc;
}
else if(no==100)
{
if(a[upos/9][upos%9+1]!='H')
{
if(a[1][6]=='U'&&a[1][5]=='O')
{
a[1][7]='a';
a[1][6]='O';
a[1][5]='a';
}
else if(a[9][6]=='U'&&a[9][5]=='O')
{
a[9][7]='a';
a[9][6]='O';
a[9][5]='a';
}
else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='H'))
goto abc;
else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='U'))
goto abc;
else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]!='H'))
{
swap(&a[upos/9][upos%9+2],&a[upos/9][upos%9+1]);
swap(&a[upos/9][upos%9],&a[upos/9][upos%9+1]);
}
else if(a[(upos/9)][upos%9+1]!='X')
swap(&a[upos/9][upos%9],&a[(upos/9)][upos%9+1]);
}
goto abc;
}
else if(no==97)
{
if(a[upos/9][upos%9-1]!='H')
{
if(a[1][2]=='U'&&a[1][3]=='O')
{
a[1][1]='a';
a[1][2]='O';
a[1][3]='a';
}
else if(a[9][2]=='U'&&a[9][3]=='O')
{
a[9][1]='a';
a[9][2]='O';
a[9][3]='a';
}
else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='H'))
goto abc;
else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='U'))
goto abc;
else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]!='H'))
{
swap(&a[upos/9][upos%9-2],&a[upos/9][upos%9-1]);
swap(&a[upos/9][upos%9],&a[upos/9][upos%9-1]);
}
else if(a[(upos/9)][upos%9-1]!='X')
swap(&a[upos/9][upos%9],&a[(upos/9)][upos%9-1]);
}
goto abc;
}
else if(no==111)
{
return 0;
}
else if(no==114)
{
goto ab;
}
else
goto abc;
}
getch(); /* clean up */
closegraph();
}
void display(char a[11][9])
{
int i,j,x1=100,y1=100,x2=120,y2=120;
int poly[8];
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j]=='H')
{
poly[0]=poly[6]=x1;
poly[1]=poly[3]=y1;
poly[4]=poly[2]=x2;
poly[7]=poly[5]=y2;
rectangle(x1,y1,x2,y2);
setfillstyle(SOLID_FILL,RED);
fillpoly(4,poly);
}
if(a[i][j]=='U')
{
poly[0]=poly[6]=x1+3;
poly[1]=poly[3]=y1+3;
poly[4]=poly[2]=x2-3;
poly[7]=poly[5]=y2-3;
rectangle(x1+3,y1+3,x2-3,y2-3);
setfillstyle(SOLID_FILL,YELLOW);
fillpoly(4,poly);
}
if(a[i][j]=='X')
{
poly[0]=poly[6]=x1+3;
poly[1]=poly[3]=y1+3;
poly[4]=poly[2]=x2-3;
poly[7]=poly[5]=y2-3;
rectangle(x1+3,y1+3,x2-3,y2-3);
setfillstyle(SOLID_FILL,BLACK);
fillpoly(4,poly);
}
if(a[i][j]=='O')
{
poly[0]=poly[6]=x1+3;
poly[1]=poly[3]=y1+3;
poly[4]=poly[2]=x2-3;
poly[7]=poly[5]=y2-3;
rectangle(x1+3,y1+3,x2-3,y2-3);
setfillstyle(SOLID_FILL,CYAN);
fillpoly(4,poly);
}
if(a[i][j]=='a')
{
poly[0]=poly[6]=x1;
poly[1]=poly[3]=y1;
poly[4]=poly[2]=x2;
poly[7]=poly[5]=y2;
rectangle(x1,y1,x2,y2);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(4,poly);
}
x1=x1+20;
x2=x2+20;
}
x1=100;
x2=120;
y1=y1+20;
y2=y2+20;
}
}
int pos(char a[11][9])
{
int i,j,pos;
for(i=1;i<11;i++)
{
for(j=1;j<9;j++)
{
if(a[i][j]=='O')
{
pos=9*i+j;
return(pos);
}
}
}
}
void swap(char *a,char *b)
{
char temp;
temp=*a;
*a=*b;
*b=temp;
}
int final(char a[11][9])
{
int i,j,final=0;
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j]=='U')
{
final=1;
return(final);
}
}
}
return(final);
}
Thanks to Prerak Dhoot, Pritesh Dhokchaule, Saurabh Kabra, Ankesh Porwal for supporting me.
Thanks to Anirudh Tomer for sharing his knowledge.
Thanks to Microsoft who provided graphics.h with much simpler help topics.

This comment has been removed by a blog administrator.
ReplyDeletepostingan yang bagus tentang "Push Box - Source Code"
ReplyDelete