C++ Game sample – Egg Catcher by thatsalok


This article is a small game using graphics in c++ (DOS Based). I programmed this game and all of the graphics design are the creations of my friend Amit Mehta. “This is a simple game for new programmers. It will help them to get a clear view of 2D Graphics The output is a game where the player catches eggs dropped by a egg thrower and goes on to various levels “    The following is the brief on the class I created with some of my logic. Here is main Class that move the egg catcher using keyboard.


class EggCatcher
{
	int strx,stry,endx,endy; //coordinate of the moving box
	void *buff;////for handling the box image
	short bool;
	board *Board; 

    void BackGround();
	void DrawCatcher(int,int); 

    public: 

    char Player[10]; 

    EggCatcher(int,int,board&);
	void Start(int,int);
	void End();
	void CatcherMove(int);
	void PreScreen();
	void SayByeBye();
	~EggCatcher()
	{
		free(buff);
		delete Board;
	}
};

This is little function that reads the keyboard input, I found this function in Mr. Yashwant Kanetkar book “Let us C”.


#define UP 72
#define DOWN 80
#define RIGHT 77
#define LEFT 75
#define ENTER 28
#define END 28
#define ESC 1 

int getscan()
{
REGS i,o;
//while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
};

The above function will return current key state by the ASCII value of the key which is pressed. Using switch statement we can track the movement of direction keys and some special keys, like this.

switch(ch)
{ 

      case RIGHT:
		Box.CatcherMove(ch);
		break; 

      case LEFT:
		Box.CatcherMove(ch);
		break; 

      case ESC:
		Box.SayByeBye();
		Thrower1.EndGameProperties();
		exit(0); 
}

Now I have created special bitmap class of my own to display images. Here it is (it is defined and declared in second.cpp)


struct bitmap
{
	short bit[20][20];
}; 

class BITMAP
{
	void *character; 
    public:
	// BITMAP();
	void* DrawImage(bitmap &bit,int x,int y); 
}; 

void* BITMAP::DrawImage(bitmap &bit,int x,int y)
{ 

 int i,j;
for(j=y;j<y+20;j++)
{ 

   	for(i=x;i<x+20;i++)
	{
		putpixel(i,j,bit.bit[j-y][i-x]);
	} 
} 

 int size=imagesize(x,y,x+20,y+20);
 character=malloc(size);
 getimage(x,y,x+20,y+20,character);
 putimage(x,y,character,XOR_PUT);
 return character; 
}

Here how to use it.

1. First create a bitmap bits using this.

const short W=15;//white 

const short Y=14;//yellow 

const short S=10; 

const short E=11; 

bitmap Egg1={0,0,0,0,0,0,0,0,0,W,W,W,0,0,0,0,0,0,0,0, 

                            0,0,0,0,0,0,0,0,W,W,W,W,W,0,0,0,0,0,0,0, 

                            0,0,0,0,0,0,0,W,Y,Y,Y,Y,Y,W,0,0,0,0,0,0, 

                            0,0,0,0,0,0,W,W,0,Y,Y,Y,Y,W,W,0,0,0,0,0, 

                            0,0,0,0,0,W,W,0,Y,Y,Y,Y,Y,Y,W,W,0,0,0,0, 

                            0,0,0,0,W,W,0,Y,Y,Y,Y,Y,Y,Y,Y,W,0,0,0,0, 

                            0,0,0,0,W,Y,Y,Y,Y,Y,Y,Y,Y,Y,Y,W,0,0,0,0, 

                            0,0,0,0,W,Y,Y,Y,Y,Y,Y,Y,Y,Y,Y,W,0,0,0,0, 

                            0,0,0,0,W,Y,Y,Y,Y,Y,8,8,Y,Y,Y,W,0,0,0,0, 

                            0,0,0,0,W,Y,Y,Y,Y,8,8,8,8,Y,Y,W,0,0,0,0, 

                            0,0,0,0,W,Y,Y,Y,Y,8,8,8,8,Y,Y,W,0,0,0,0, 

                            0,0,0,0,0,W,Y,Y,Y,8,8,8,8,Y,Y,W,0,0,0,0, 

                            0,0,0,0,0,W,Y,Y,Y,Y,8,8,Y,Y,Y,W,0,0,0,0, 

                            0,0,0,0,0,W,Y,Y,Y,Y,Y,Y,Y,Y,W,0,0,0,0,0, 

                            0,0,0,0,0,0,W,Y,Y,Y,Y,Y,Y,W,0,0,0,0,0,0, 

                            0,0,0,0,0,0,W,Y,Y,Y,Y,Y,W,0,0,0,0,0,0,0, 

                            0,0,0,0,0,0,0,W,Y,Y,Y,W,0,0,0,0,0,0,0,0, 

                            0,0,0,0,0,0,0,W,W,W,W,0,0,0,0,0,0,0,0,0, 

                            0,0,0,0,0,0,0,0,W,W,0,0,0,0,0,0,0,0,0,0, 

                            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

2. Now in any part of function, call the show function.

void show
{

BITMAP *bit;

void * eggbuff=bit.DrawImage(egg,40,40);

//and to show

putimage(x,y,eggbuff,XOR_PUT); 

}

and this will show up the image of bitmap

The Logic Behind:-

I am just using random function to generate the egg and at any given time. All the eggs move downward on the board matrix that is defined in the beginning of the program and if it is at the end, the catcher the score is incremented by one or you lose the chance.

Let me explain it more, I have a board according to which graphics displayed on the screen if the board matrix element is 0 is doesn?t display anything, same case for egg and egg catcher.

Logic behind moving of eggs->every time thrower want to drop egg he put egg value in top of matrix, since every 100 msec the matrix is reviewed and screen is redrawn the egg can be shown on the screen, now for moving action each time top row is copied in it lower row and soon that and last row is discarded this is the logic of moving multiple egg in screen.

You can download the source files and executable here.

Hope you like the game and problem you can mail me at thatsalok#gmail.com (replace # with @)