Saturday, 23 November 2013

Create your own MAZE GAME

MAZE: THE GAME
It is an addictive and interesting game in which skills matter the most that how you move and control the motion of the square. I has 18 levels from easy to hard.
The game is developed in C++ using OpenGl glut library.
The game has graphical user interface and can be played using keyboard arrow keys.
The goal of the game is to move the green square towards blue square by passing it through various challenging maze without touching the boundary of maze.
The score is relative to the time i.e. less time, more score
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
// Game Code in C++
 
#include<stdio.h>
#include<glut.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<Windows.h>
#include<math.h>
 
 
int stx = 100, sty = 280;
float px = stx, py = sty; //centre pixel of car
float nacc = 2.0;
float accl = 0;
clock_t t1, t2; //to calculate the time taken to reach the goal
float friction = 0.20;
int time1 = 15;
float splimit = 5.0f;
char dir = 'O';
int leveltype = 0; //0 for easy, 1 for medium, 2 for hard
int h = 600, w = 1000;
int goalsize = 10;
int carsize = 4;
bool moveStart = false;
bool nwgame = true;
bool gmover = false;
bool goal = false;
int level = 1;
int highscore = 0;
int score = 0;
int encKey = 123;
int allcomp = 0; //All levels completed
int maxlevel = 6;
unsigned char pic[3] = {0};
bool toggle = true;
FILE *fp;
 
void car(float x, float y, int csize);
void myInit();
void grid1(int);
int collision(int, int);
void reshape(int wx, int hy);
void specialkeys(int key, int x, int y);
void handlekeys(unsigned char key, int x, int y);
void display();
void setPixel(float x, float y);
void automove(int value);
void start();
void mouse(int button, int state, int x, int y);
void changeLevelType();
 
GLubyte rgba[4];
 
int encryptHighscore(int num)
{
return num * encKey - 511377;
}
 
int decryptHighscore(int num)
{
if (num == 0)
return 0;
float s = (float) (num + 511377) / encKey;
float frac = s - (int)s;
if ((frac + 1) * 10000 == 10000.0)
return (int)s;
else return 0;
}
 
void toggleGlutWindowMaximizeBox(char *szWindowTitle)
{
long dwStyle;
HWND hwndGlut;
hwndGlut = FindWindow(NULL, szWindowTitle);
dwStyle = GetWindowLong(hwndGlut, GWL_STYLE);
dwStyle ^= WS_MAXIMIZEBOX;
SetWindowLong(hwndGlut, GWL_STYLE, dwStyle);
}
 
void getPixel(int x, int y, GLubyte *color) {
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
}
 
void *fonts[] =
{
GLUT_BITMAP_9_BY_15,
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_BITMAP_TIMES_ROMAN_24,
GLUT_BITMAP_HELVETICA_18,
GLUT_BITMAP_8_BY_13
};
 
void output(int x, int y, char *string, int f)
{
int len, i;
glPointSize(20.0);
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(fonts[f], string[i]);
}
}
 
void myInit()
{
glClearColor(0.0, 1.0, 1.0, 1.0);
glLineWidth(3.0);
glShadeModel(GL_FLAT);
gluOrtho2D(0, w, h, 0);
changeLevelType();
int ii = encryptHighscore(20000);
int uu = decryptHighscore(ii);
try
{
fp = fopen("highscore.txt", "r+");
if (fp == NULL)
{
fp = fopen("highscore.txt", "w+");
fprintf(fp,"%d", 0);
}
else
{
fscanf(fp, "%d", &highscore);
highscore = decryptHighscore(highscore);
}
}
catch(...)
{
}
}
 
void border()
{
glBegin(GL_LINES);
//border
glVertex2i(1, 1);
glVertex2i(1, 599);
glVertex2i(1, 599);
glVertex2i(999, 599);
glVertex2i(999, 599);
glVertex2i(999, 1);
glVertex2i(999, 1);
glVertex2i(1, 1);
glEnd();
}
 
void grid1(int value)
{
//black grid rgba values are 0 0 0 255, a (alpha) is always same.
glBegin(GL_LINES);
//1st level
glVertex2i(260, 360);
glVertex2i(260, 200);
glVertex2i(260, 200);
glVertex2i(740, 200);
glVertex2i(740, 200);
glVertex2i(740, 400);
glVertex2i(740, 400);
glVertex2i(260, 400);
//2nd level
if (value >= 5)
{
glVertex2i(300, 240);
glVertex2i(300, 400);
}
glVertex2i(340, 200);
glVertex2i(340, 280);
glVertex2i(340, 320);
glVertex2i(340, 400);
if (value >= 3)
{
glVertex2i(380, 200);
glVertex2i(380, 360);
}
glVertex2i(420, 240);
glVertex2i(420, 400);
//3rd level
if (value > 1)
{
glVertex2i(460, 200);
glVertex2i(460, 280);
glVertex2i(460, 280);
glVertex2i(580, 280);
}
if (value > 1)
{
glVertex2i(460, 320);
glVertex2i(460, 360);
glVertex2i(460, 320);
glVertex2i(540, 320);
glVertex2i(460, 360);
glVertex2i(540, 360);
glVertex2i(540, 320);
glVertex2i(540, 360);
}
if (value > 2)
{
glVertex2i(540, 200);
glVertex2i(540, 240);
}
if (value >= 1)
{
glVertex2i(660, 280);
glVertex2i(740, 280);
}
if (value > 3)
{
glVertex2i(620, 240);
glVertex2i(700, 240);
}
if (value >= 1)
{
glVertex2i(620, 320);
glVertex2i(700, 320);
glVertex2i(700, 320);
glVertex2i(700, 360);
}
if (value >= 4)
{
glVertex2i(660, 360);
glVertex2i(700, 400);
}
glVertex2i(620, 240);
glVertex2i(620, 400);
glEnd();
glColor3f(0.0f, 0.0, 1.0);
if (value == 1)
car(580, 320, goalsize);
else if (value == 2)
car(570, 360, goalsize);
else if (value == 3)
car(520, 220, goalsize);
else if (value == 4)
car(720, 260, goalsize);
else if (value == 5)
car(720, 370, goalsize);
else if (value == 6)
car(660, 385, goalsize);
}
 
int collision(int size, int value)
{
// 0 if not collision, 1 if collision, 2 if goal
//checking goal color i.e. blue
getPixel(px,h - 1 - py, rgba);
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 255)
return 2;
getPixel(px,h - 1 - py+size, rgba);
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 255)
return 2;
getPixel(px+size,h - 1 - py, rgba);
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 255)
return 2;
getPixel(px-size,h - 1 - py, rgba);
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 255)
return 2;
// checking for 4 directions of car
getPixel(px,600 - 1 - py-size, rgba); // getting pixel from cooardinates, initially viewport coordinates are bottom left, then inverting y to set viewport
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0)
return 1;
getPixel(px,600 - 1 - py+size, rgba);
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0)
return 1;
getPixel(px+size,600 - 1 - py, rgba);
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0)
return 1;
getPixel(px-size,600 - 1 - py, rgba);
if (rgba[0] == 0 && rgba[1] == 0 && rgba[2] == 0)
return 1;
return 0;
}
 
void setPixel(float x, float y)
{
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
}
 
void car(float x, float y, int csize)
{
glBegin(GL_POLYGON);
glVertex2i(x-csize, y-csize);
glVertex2i(x-csize, y+csize);
glVertex2i(x+csize, y+csize);
glVertex2i(x+csize, y-csize);
glEnd();
}
 
float ax = 1.0, ay = 1.0;
 
void movement(int value)
{
px += ax;
py += ay;
if (px <= 0)
ax = -ax;
if (px > 1000)
ax = -ax;
if (py < 0)
ay = -ay;
if (py >= 600)
ay = -ay;
glutPostRedisplay();
if(!toggle)
glutTimerFunc(time1, movement, 1);
}
 
void changeLevelType()
{
if (leveltype == 1)
{
carsize = 7;
goalsize = 10;
friction = 0.15;
nacc = 2.0;
splimit = 6.0;
}
else if(leveltype == 2)
{
carsize = 8;
goalsize = 10;
friction = 0.10;
nacc = 2.0;
splimit = 8.0;
}
else if(leveltype == 0)
{
level = 1;
accl = 0;
t1 = t2;
leveltype = 0;
px = stx;
py = sty;
nwgame = true;
goal = false;
carsize = 4;
goalsize = 10;
score = 0;
friction = 0.20;
nacc = 1.0;
splimit = 5.0;
}
level = 1;
}
 
void automove(int value)
{
if (dir == 'w')
py -= accl;
else if (dir == 's')
py += accl;
else if (dir == 'a')
px -= accl;
else if (dir == 'd')
px += accl;
else dir = 'O';
//time -= accl;
accl -= friction;
if (px < 0)
px = 1000;
if (px > 1000)
px = 0;
if (py < 0)
py = 600;
if (py > 600)
py = 0;
if (collision(carsize, level) == 1) //wall collision
{
accl = 0;
px = stx;
nwgame = false;
py = sty;
goal = false;
if (score >= 0)
score -= 100;
if (score < 0)
score = 0;
}
if(collision(carsize, level) == 2) //goal collision
{
if (score > highscore)
{
highscore = score;
try{
fp = fopen("highscore.txt", "w+");
fprintf(fp, "%d", encryptHighscore(highscore));
}
catch(...)
{
}
}
accl = 0;
level++;
if (level > maxlevel)
{
leveltype++;
if (leveltype == 3)
allcomp = 1;
changeLevelType();
}
px = stx;
nwgame = true;
py = sty;
goal = false;
}
glutPostRedisplay();
if (accl > 0)
glutTimerFunc(time1, automove, 0);
}
 
void start()
{
accl = 0;
glutTimerFunc(time1, automove, 0);
}
 
void showtime()
{
char strr[20];
int tot;
tot = (double)(t2 - t1)/1000; // calculating time
itoa(tot, strr, 10);
output(650, 50, strr, 3); // displaying time
itoa(level, strr, 10);
output(650, 80, strr, 3); // displaying level
itoa(score, strr, 10);
output(650, 140, strr, 3); // displaying score
output(600,110, "HIGHSCORE: ", 4);
itoa(highscore, strr, 10);
output(680, 110, strr, 3);
}
 
void playtime(int value)
{
glutPostRedisplay();
t2 = clock();
if (score > 0)
score -= 1; // score calculation
if (goal == true)
glutTimerFunc(100, playtime, 1);
}
 
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.01, 0.0);
border();
if (allcomp == 1)
{
output(260,170, "CONGRATULATIONS! YOU HAVE COMPLETED ALL LEVELS.", 4);
output(260,190, "FOR MORE VISIT WWW.YOUTUBE.COM/SHIVNARR", 4);
leveltype = 0;
changeLevelType();
}
glColor3f(0.0f, 0.0, 0.0);
grid1(level);
glColor3f(0.0f, 0.1, 0.0);
output(800, 580, "SHIVINDER SINGH NARR", 4);
output(700, 592, "http://shivindernarr.blogspot.com", 4);
output(50, 50, "GOAL - MOVE GREEN SQUARE TO BLUE SQUARE", 4);
output(50, 80, "USE ARROW KEYS TO MOVE SQUARE", 4);
output(50, 580, "Exit(ESC)", 4);
output(150, 580, "Restart(R)", 4);
output(600, 80, "LEVEL: ", 4);
output(600,140, "SCORE: ", 4);
output(665, 80, "/ 6", 3);
if (leveltype == 0)
{
output(690, 80, "(EASY)", 4);
}
else if (leveltype == 1)
{
output(690, 80, "(MEDIUM)", 4);
}
else if (leveltype == 2)
{
output(690, 80, "(HARD)", 4);
}
output(600, 50, "TIME: ", 4);
output(680, 50, "SECONDS", 4);
showtime();
glColor3f(0.0f, 0.3, 0.0);
car(px, py, carsize);
glutSwapBuffers();
glFlush();
}
 
void handlekeys(unsigned char key, int x, int y)
{
if (key == 27) // Esc key
exit(0);
if (key == 'S') // Cheat key
{
toggle = !toggle;
movement(1);
}
if (allcomp == 1) // Enter key
{
level = 1;
allcomp = 0;
accl = 0;
t1 = t2;
leveltype = 0;
px = stx;
py = sty;
nwgame = true;
goal = false;
carsize = 4;
goalsize = 10;
score = 0;
friction = 0.20;
nacc = 1.0;
splimit = 5.0;
glutPostRedisplay();
}
if (key == 'r' || key == 'R')
{
level = 1;
accl = 0;
t1 = t2;
leveltype = 0;
px = stx;
py = sty;
nwgame = true;
goal = false;
carsize = 4;
goalsize = 10;
score = 0;
friction = 0.20;
nacc = 1.0;
splimit = 5.0;
glutPostRedisplay();
}
}
 
void specialkeys(int key, int x, int y) {
if (key == GLUT_KEY_UP){
dir = 'w';
}
else if (key == GLUT_KEY_DOWN) {
dir = 's';
}
else if (key == GLUT_KEY_LEFT) {
dir = 'a';
}
else if (key == GLUT_KEY_RIGHT) {
dir = 'd';
}
if (accl <= 0)
{
start();
}
if (accl == 0 && px == stx && py == sty)
{
t1 = clock();
goal = true;
if (nwgame)
score = score + 500 * level; //score addition
playtime(1);
}
if (moveStart == false && accl <= 0)
{
moveStart = true;
}
accl += nacc;
if (accl > splimit)
accl = splimit;
}
 
void specialkeysup(int key, int x, int y)
{
if (key == GLUT_KEY_UP){
moveStart = false;
}
else if (key == GLUT_KEY_DOWN) {
moveStart = false;
}
else if (key == GLUT_KEY_LEFT) {
moveStart = false;
}
else if (key == GLUT_KEY_RIGHT) {
moveStart = false;
}
}
 
void reshape(int wx, int hy)
{
glViewport(0,0, w, h); // Set the viewport to the size of our window
}
 
int main(int argc, char ** argv)
{
char * winMess = "Maze Ver 1.0";
HWND h1 = GetConsoleWindow();
ShowWindow(h1, SW_HIDE);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize(w, h);
glutInitWindowPosition(150, 100);
glutCreateWindow(winMess);
myInit();
glutDisplayFunc(display);
glutKeyboardFunc(handlekeys);
glutSpecialFunc(specialkeys);
glutSpecialUpFunc(specialkeysup);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

No comments:

Post a Comment