Displaying Source Code(s)
|
|
Games
--------------------------------------------------------------------------------
Description : L- Change Level
Esc- Exit Game
It is a game developed during my exams as I could not play
easily on my mobile.
#include "stdio.h"
#include "conio.h"
#include "dos.h"
#include "stdlib.h"
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
//Maze variables
#define MAZEHEIGHT 20
#define MAZEWIDTH 60
#define BONUSPOINTS 491
const int leftMargin=2,topMargin=3;
//Globals
int level=90;
unsigned long points=0;
int ratPresent=0,bonusPresent=0;
const unsigned char far* vduPointer=(unsigned char
far*)0xB8000000L;
enum Directions {left,right,up,down};
enum FillerType {
Null=0,snakes,wall,rat,bonus,eatenRat,snakesTail,snakesFace};
//Customizable variables
unsigned char snakeChar='Û'; //'X';
unsigned char snakeFaceChar='O';
unsigned char snakeTailChar=' ';
const unsigned char ratChar='ö'; //'R'
const unsigned char bonusChar='ê'; //'B'
const unsigned char eatenRatChar='°';
class MazePixel;
class MazePixel
{
int x,y;
public:
int actualx,actualy;
Directions direction;
FillerType filledWith;
MazePixel *next,*previous;
void setx(int _x)
{
//Check boundaries
if(_x<0) _x=MAZEWIDTH-1;
if(_x>=MAZEWIDTH) _x=0;
x=_x;
actualx=x+leftMargin+1;
}
void sety(int _y)
{
//Check boundaries
if(_y<0) _y=MAZEHEIGHT-1;
if(_y>=MAZEHEIGHT) _y=0;
y=_y;
actualy=y+topMargin+1;
}
int getx(){ return(x%MAZEWIDTH); } //Check boundaries
int gety(){ return(y%MAZEWIDTH); } //Check boundaries
MazePixel operator = (MazePixel o)
{
direction=o.direction;
filledWith=o.filledWith;
next=o.next;
previous=o.previous;
return(o);
}
void nullify()
{
x=y=direction=0;
filledWith=Null;
next=previous=(MazePixel*)0;
}
/*MazePixel* move(int stpes)
{
struct text_info ti;
gettextinfo(&ti);
int orgCh=(int)*(vduPointer+(actualx-1)*2+(actualy-1)*ti.screenwidth*2);
switch(direction)
{
case left:
setx(getx()-1);
gotoxy(actualx,actualy);
printf("%c",orgCh);
break;
case right:
break;
case up:
break;
case down:
break;
}
return(this);
}*/
};
class Maze
{
public:
MazePixel pixels[(MAZEHEIGHT)*(MAZEWIDTH)];
Maze()
{
int i;
for(i=0;i<(MAZEHEIGHT)*(MAZEWIDTH);i++)
{
pixels[i].nullify();
pixels[i].setx(i%MAZEWIDTH);
pixels[i].sety(i/MAZEWIDTH);
pixels[i].filledWith=Null;
}
draw();
}
MazePixel* getPixel(int _x,int _y)
{
if(_x<0) _x=MAZEWIDTH-1;
if(_x>=MAZEWIDTH) _x=0;
if(_y<0) _y=MAZEHEIGHT-1;
if(_y>=MAZEHEIGHT) _y=0;
return(&pixels[(_x)%MAZEWIDTH+(_y)*MAZEWIDTH]);
}
void draw()
{
int i;
gotoxy(leftMargin,topMargin); printf("Ú");
gotoxy(leftMargin,topMargin+MAZEHEIGHT+1); printf("À");
gotoxy(leftMargin+MAZEWIDTH+1,topMargin); printf("¿");
gotoxy(leftMargin+MAZEWIDTH+1,topMargin+MAZEHEIGHT+1);
printf("Ù");
gotoxy(leftMargin+1,topMargin);
for(i=leftMargin+1;i<leftMargin+MAZEWIDTH+1;i++) printf("Ä");
for(i=topMargin+1;i<topMargin+MAZEHEIGHT+1;i++)
{ gotoxy(leftMargin,i); printf("³"); }
for(i=topMargin+1;i<topMargin+MAZEHEIGHT+1;i++)
{ gotoxy(leftMargin+MAZEWIDTH+1,i); printf("³"); }
gotoxy(leftMargin+1,topMargin+MAZEHEIGHT+1);
for(i=leftMargin+1;i<leftMargin+MAZEWIDTH+1;i++) printf("Ä");
for(i=0;i<MAZEWIDTH*MAZEHEIGHT;i++)
{
switch(pixels[i].filledWith)
{
case snakes:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",snakeChar);
break;
case snakesFace:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",snakeFaceChar);
break;
case snakesTail:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",snakeTailChar);
break;
case Null:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",' ');
break;
case wall:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",'Û');
break;
case rat:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",ratChar);
break;
case bonus:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",bonusChar);
break;
case eatenRat:
gotoxy(pixels[i].actualx,pixels[i].actualy);
printf("%c",eatenRatChar);
break;
}//end switch
}
}//end for
};
Maze maze;
class Snake
{
Directions direction;
MazePixel facePixel,tailPixel,*bodyPixels;
public:
int length,points;
Snake(int _length=5,int startx=MAZEWIDTH-5,int starty=0)
{
points=0;
direction=left;
length=_length;
startx=MAZEWIDTH-_length;
facePixel.setx(startx); facePixel.sety(starty);
facePixel.filledWith=snakesFace; facePixel.direction=direction;
*maze.getPixel(startx,starty)=facePixel;
tailPixel.setx(length+startx-1); tailPixel.sety(starty);
tailPixel.filledWith=snakesTail; tailPixel.direction=direction;
*maze.getPixel(length+startx-1,starty)=tailPixel;
MazePixel *prev;
bodyPixels=maze.getPixel(1+startx,starty);
bodyPixels->previous=&facePixel;
for(int i=1;i<=length-2;i++)
{
prev=bodyPixels;
bodyPixels->filledWith=snakes;
bodyPixels->direction=direction;
bodyPixels=maze.getPixel(i+1+startx,starty);
bodyPixels->previous=prev;
prev->next=bodyPixels;
}
bodyPixels->next=&tailPixel;
tailPixel.previous=bodyPixels;
tailPixel.next=facePixel.previous=NULL;
bodyPixels=maze.getPixel(1+startx,starty);
facePixel.next=bodyPixels;
draw();
}
void draw()
{
MazePixel body;
gotoxy(facePixel.actualx,facePixel.actualy);
printf("%c",snakeFaceChar);
body=*bodyPixels;
body.setx(bodyPixels->getx()); body.sety(bodyPixels->gety());
for(int i=1;i<=length-2;i++)
{
gotoxy(body.actualx,body.actualy);
printf("%c",snakeChar);
//if(body.filledWith!=snakesTail)
body.setx(body.next->getx()); body.sety(body.next->gety());
body=*body.next;
//else break;
}
gotoxy(tailPixel.actualx,tailPixel.actualy);
printf("%c",snakeTailChar);
}
Directions turn(int key)
{
switch(key)
{
case LEFT:
if(direction!=right) direction=left;
break;
case RIGHT:
if(direction!=left) direction=right;
break;
case UP:
if(direction!=down) direction=up;
break;
case DOWN:
if(direction!=up) direction=down;
break;
}
return(direction);
}
int move(int steps=1)
{
int i;
MazePixel *prev,*current;
//Cleaning old Snake
volatile Directions nextPixelDirection,bodyDirection;
if(direction!=facePixel.direction)
facePixel.direction=direction;
switch(facePixel.direction)
{
case left:
current=maze.getPixel(facePixel.getx()-1,facePixel.gety());
break;
case right:
current=maze.getPixel(facePixel.getx()+1,facePixel.gety());;
break;
case up:
current=maze.getPixel(facePixel.getx(),facePixel.gety()-1);
break;
case down:
current=maze.getPixel(facePixel.getx(),facePixel.gety()+1);
break;
}
unsigned char *t,oldt;
if(current->filledWith==rat)
{
oldt=snakeFaceChar;
t=(unsigned char*)&snakeFaceChar;
*t=1;
snakeFaceChar=1;
points++;
::points=(unsigned long)(level+1)*points;
ratPresent=0;
length++;
current->filledWith=Null;
//Convert face to body & generate new face
draw();
*maze.getPixel(facePixel.getx(),facePixel.gety())=*bodyPixels;
maze.getPixel(facePixel.getx(),facePixel.gety())->next=bodyPixels;
bodyPixels=maze.getPixel(facePixel.getx(),facePixel.gety());
bodyPixels->previous=&facePixel;
*current=facePixel;
facePixel.setx(current->getx());
facePixel.sety(current->gety());
facePixel.next=bodyPixels;
facePixel.previous=NULL;
bodyPixels->direction=facePixel.direction;
/*
*current=*bodyPixels;
current->next=bodyPixels;
bodyPixels->previous=current;
bodyPixels=current;
//face generation:
facePixel.previous=NULL;
facePixel.next=current;
switch(facePixel.direction)
{
case left:
facePixel.setx(current->getx()-1);
*maze.getPixel(current->getx()-1,current->gety())=facePixel;
break;
case right:
facePixel.setx(current->getx()+1);
*maze.getPixel(current->getx()+1,current->gety())=facePixel;
break;
case up:
facePixel.sety(current->gety()-1);
*maze.getPixel(current->getx(),current->gety()-1)=facePixel;
break;
case down:
facePixel.sety(current->gety()+1);
*maze.getPixel(current->getx(),current->gety()+1)=facePixel;
break;
}
current->previous=&facePixel;*/
draw();
*t=oldt;
snakeFaceChar=oldt;
}
if(current->filledWith==bonus)
{
points+=BONUSPOINTS;
bonusPresent=0;
current->filledWith=Null;
}
switch(facePixel.direction)
{
case left:
current=maze.getPixel(facePixel.getx()-1,facePixel.gety());
if(current->filledWith==snakes ||
current->filledWith==snakesFace ||
current->filledWith==snakesTail || current->filledWith==eatenRat
||
current->filledWith==wall)
return(0); //snake is dead
*current=facePixel;
current->previous=NULL;
maze.getPixel(facePixel.getx(),facePixel.gety())->filledWith=Null;
gotoxy(facePixel.actualx,facePixel.actualy); printf(" ");
facePixel=*current;
facePixel.filledWith=snakesFace;
facePixel.setx(facePixel.getx()-1);
break;
case right:
current=maze.getPixel(facePixel.getx()+1,facePixel.gety());;
if(current->filledWith==snakes ||
current->filledWith==snakesFace ||
current->filledWith==snakesTail || current->filledWith==eatenRat
||
current->filledWith==wall)
return(0); //snake is dead
*current=facePixel;
current->previous=NULL;
maze.getPixel(facePixel.getx(),facePixel.gety())->filledWith=Null;
gotoxy(facePixel.actualx,facePixel.actualy); printf(" ");
facePixel=*current;
facePixel.filledWith=snakesFace;
facePixel.setx(facePixel.getx()+1);
break;
case up:
current=maze.getPixel(facePixel.getx(),facePixel.gety()-1);
if(current->filledWith==snakes ||
current->filledWith==snakesFace ||
current->filledWith==snakesTail || current->filledWith==eatenRat
||
current->filledWith==wall)
return(0); //snake is dead
*current=facePixel;
current->previous=NULL;
maze.getPixel(facePixel.getx(),facePixel.gety())->filledWith=Null;
gotoxy(facePixel.actualx,facePixel.actualy); printf(" ");
facePixel=*current;
facePixel.filledWith=snakesFace;
facePixel.sety(facePixel.gety()-1);
break;
case down:
current=maze.getPixel(facePixel.getx(),facePixel.gety()+1);
if(current->filledWith==snakes ||
current->filledWith==snakesFace ||
current->filledWith==snakesTail || current->filledWith==eatenRat
||
current->filledWith==wall)
return(0); //snake is dead
*current=facePixel;
current->previous=NULL;
maze.getPixel(facePixel.getx(),facePixel.gety())->filledWith=Null;
gotoxy(facePixel.actualx,facePixel.actualy); printf(" ");
facePixel=*current;
facePixel.filledWith=snakesFace;
facePixel.sety(facePixel.gety()+1);
break;
}
prev=&facePixel;
nextPixelDirection=facePixel.direction;
MazePixel body=*bodyPixels;//,*pBody=bodyPixels;
bodyDirection=bodyPixels->direction;
for(i=1;i<=length-2;i++)
{
switch(body.direction)
{
case left:
current=maze.getPixel(body.getx()-1,body.gety());
break;
case right:
current=maze.getPixel(body.getx()+1,body.gety());
break;
case up:
current=maze.getPixel(body.getx(),body.gety()-1);
break;
case down:
current=maze.getPixel(body.getx(),body.gety()+1);
break;
}//end switch
//pBody->direction=nextPixelDirection;
//pBody=pBody->next;
*current=body; //In next pixel put current values
current->previous=prev;
prev->next=current;
prev=current;
maze.getPixel(body.getx(),body.gety())->filledWith=Null;
//current
pixel filled with null
gotoxy(body.actualx,body.actualy); printf(" "); //Clean current
character
body.setx((body.next)->getx()); body.sety((body.next)->gety());
body=*(body.next);
//nextPixelDirection=body.direction;
}//End for
prev->next=current;
switch(bodyDirection)
{
case left:
bodyPixels=maze.getPixel(bodyPixels->getx()-1,bodyPixels->gety());
break;
case right:
bodyPixels=maze.getPixel(bodyPixels->getx()+1,bodyPixels->gety());
break;
case up:
bodyPixels=maze.getPixel(bodyPixels->getx(),bodyPixels->gety()-1);
break;
case down:
bodyPixels=maze.getPixel(bodyPixels->getx(),bodyPixels->gety()+1);
break;
}
//tailPixel.direction=nextPixelDirection;
switch(tailPixel.direction)
{
case left:
*maze.getPixel(tailPixel.getx()-1,tailPixel.gety())=tailPixel;
maze.getPixel(tailPixel.getx()-1,tailPixel.gety())->previous=prev;
maze.getPixel(tailPixel.getx()-1,tailPixel.gety())->next=NULL;
maze.getPixel(tailPixel.getx(),tailPixel.gety())->filledWith=Null;
gotoxy(tailPixel.actualx,tailPixel.actualy); printf(" ");
tailPixel=*maze.getPixel(tailPixel.getx()-1,tailPixel.gety());
tailPixel.setx(tailPixel.getx()-1);
tailPixel.filledWith=snakesTail;
break;
case right:
*maze.getPixel(tailPixel.getx()+1,tailPixel.gety())=tailPixel;
maze.getPixel(tailPixel.getx()+1,tailPixel.gety())->previous=prev;
maze.getPixel(tailPixel.getx()+1,tailPixel.gety())->next=NULL;
maze.getPixel(tailPixel.getx(),tailPixel.gety())->filledWith=Null;
gotoxy(tailPixel.actualx,tailPixel.actualy); printf(" ");
tailPixel=*maze.getPixel(tailPixel.getx()+1,tailPixel.gety());
tailPixel.setx(tailPixel.getx()+1);
tailPixel.filledWith=snakesTail;
break;
case up:
*maze.getPixel(tailPixel.getx(),tailPixel.gety()-1)=tailPixel;
maze.getPixel(tailPixel.getx(),tailPixel.gety()-1)->previous=prev;
maze.getPixel(tailPixel.getx(),tailPixel.gety()-1)->next=NULL;
maze.getPixel(tailPixel.getx(),tailPixel.gety())->filledWith=Null;
gotoxy(tailPixel.actualx,tailPixel.actualy); printf(" ");
tailPixel=*maze.getPixel(tailPixel.getx()-1,tailPixel.gety());
tailPixel.sety(tailPixel.gety()-1);
tailPixel.filledWith=snakesTail;
break;
case down:
*maze.getPixel(tailPixel.getx(),tailPixel.gety()+1)=tailPixel;
maze.getPixel(tailPixel.getx(),tailPixel.gety()+1)->previous=prev;
maze.getPixel(tailPixel.getx(),tailPixel.gety()+1)->next=NULL;
maze.getPixel(tailPixel.getx(),tailPixel.gety())->filledWith=Null;
gotoxy(tailPixel.actualx,tailPixel.actualy); printf(" ");
tailPixel=*maze.getPixel(tailPixel.getx(),tailPixel.gety()+1);
tailPixel.sety(tailPixel.gety()+1);
tailPixel.filledWith=snakesTail;
break;
}
//Completing Snake Linked List
facePixel.next=bodyPixels;
bodyPixels->previous=&facePixel;
prev=bodyPixels;
for(i=1;i<=length-2-1;i++) prev=prev->next;
prev->next=&tailPixel;
tailPixel.previous=prev;
tailPixel.next=facePixel.previous=NULL;
//Drawing new snake
MazePixel *snk=&facePixel;
for(i=0;i<length-1;i++) snk=snk->next;
for(i=0;i<length-1;i++)
{
snk->direction=snk->previous->direction;
snk=snk->previous;
}
draw();
return(1);//Snake is alive yet
}//End move()
};
int getkey()
{
int ch=getch();
if(ch==0)
{
ch=getch();
return(ch);
}
else return(ch);
}
int generateRat()
{
int ratAt;
reGenerate:
randomize();
ratAt=random(MAZEWIDTH*MAZEHEIGHT);
// for(int i=0;i<MAZEWIDTH*MAZEHEIGHT;i++)
{
if(maze.pixels[ratAt].filledWith!=Null)
goto reGenerate;
}
maze.pixels[ratAt].filledWith=rat;
gotoxy(maze.pixels[ratAt].actualx,maze.pixels[ratAt].actualy);
printf("%c",ratChar);
ratPresent=1;
return(ratAt);
}
class Bonus
{
public:
int timeOut;
int x,y;
int pos;
int bonusAtPoints;
Bonus(int _timeOut=60)
{
timeOut=_timeOut;
bonusAtPoints=0;
}
void refresh(int _timeOut=60)
{
timeOut=_timeOut;
bonusAtPoints=0;
}
};
Bonus bonus1;
int generateBonus()
{
int bonusAt;
reGenerate:
randomize();
bonusAt=random(MAZEWIDTH*MAZEHEIGHT);
// for(int i=0;i<MAZEWIDTH*MAZEHEIGHT;i++)
{
if(maze.pixels[bonusAt].filledWith!=Null)
goto reGenerate;
}
maze.pixels[bonusAt].filledWith=bonus;
bonus1.x=maze.pixels[bonusAt].getx();
bonus1.y=maze.pixels[bonusAt].gety();
gotoxy(maze.pixels[bonusAt].actualx,maze.pixels[bonusAt].actualy);
printf("%c",bonusChar);
bonusPresent=1;
bonus1.pos=bonusAt;
bonus1.refresh();
return(bonusAt);
}
void destroyBonus()
{
maze.pixels[bonus1.pos].filledWith=Null;
gotoxy(maze.pixels[bonus1.pos].actualx,maze.pixels[bonus1.pos].actualy);
printf(" ");
//bonus1.bonusAtPoints=0;
bonusPresent=0;
}
int main()
{
clrscr();
_setcursortype(_NOCURSOR);
Snake snake(7);
maze.draw();
int key='Z';
while(key!=27)
{
// for(int d=100;d<level*100;d+=100)
if(kbhit()) key=getkey();
switch(key)
{
case LEFT: case RIGHT: case UP: case DOWN:
snake.turn(key);
break;
case 'l': case 'L':
gotoxy(16,1);
printf("Level:");
_setcursortype(_SOLIDCURSOR);
scanf("%d",&level);
level%=100;
_setcursortype(_NOCURSOR);
break;
case 27:
goto ending;
}
if(!ratPresent) generateRat();
if(((snake.points+1)%7)==0 && !bonusPresent &&
bonus1.bonusAtPoints!=snake.points &&
bonus1.bonusAtPoints!=snake.points+BONUSPOINTS)
{
generateBonus();
bonus1.bonusAtPoints=snake.points;
}
if(bonusPresent)
{
if(bonus1.timeOut==0) destroyBonus();
bonus1.timeOut--;
}
if(!snake.move())
break;
delay(1010-level*10);
gotoxy(1,1); printf("Points=%.04lu",(unsigned long)points);
gotoxy(16,1); printf("Level=%.03d",level);
if(bonusPresent)
{ gotoxy(27,1); printf("BonusTime=%.02d",bonus1.timeOut); }
fflush(stdin);
key=0;
if(snakeFaceChar=='O') snakeFaceChar='”';
else snakeFaceChar='O';
if(snakeTailChar=='/') snakeTailChar='\';
else snakeTailChar='/';
maze.draw();
}
ending:
_setcursortype(_NORMALCURSOR);
printf("
Your Score: %lu",points);
return(points);
} |
|
|