This is tutorial for creating a Two player game in C.
The name of game is keeper. It is very simple game and very easy to implement. Even if you are novice to Graphics programming in c, you will be able to grasp everything.
Download Source code from Here
Required Structures :
There are several functions like game,draw, get_dir, mv_ball etc.. all of them are self explainatory. comments are provided in the source code.
draw function will draw whole screen including boundaries, ball, pads.
mv_ball will provide new coordinates of ball according to it's direction
game function is main function for game logic. It will check that whether ball hits boundary, pad , any key is pressed or not etc...
The name of game is keeper. It is very simple game and very easy to implement. Even if you are novice to Graphics programming in c, you will be able to grasp everything.
Download Source code from Here
Required Structures :
- pad : This structure will keep track of location of pad and size of pad.
- ball : This structure will keep track of ball size, position and direction of ball
- limit : This structure consist the coordinates of boundary of game area.
- game_data : This structure will keep track of score.
struct pad { int x,y,l; }pd1={100,200,30},pd2={440,200,30}; struct game_data { int score; }; struct game_data p1={0},p2={0}; struct limit { int lx1,ly1,lx2,ly2; }; struct limit l={90,90,450,400}; struct ball { int x,y; int dir; int size; }ball={250,250,7,5};
draw function will draw whole screen including boundaries, ball, pads.
draw(void) { rectangle(l.lx1,l.ly1,l.lx2,l.ly2); circle(ball.x,ball.y,ball.size); rectangle(pd1.x-5,pd1.y-30,pd1.x+5,pd1.y+30); rectangle(pd2.x-5,pd2.y-30,pd2.x+5,pd2.y+30); delay(15); while(!kbhit()){goto e;} k=getche(); if(k=='w' || k=='s'||k=='e'){key1=k;} else if(k==72||k==80||k==77||k=='o'){key2=k;} e: cleardevice(); }
mv_ball will provide new coordinates of ball according to it's direction
void mv_ball(void) { switch(ball.dir) { case 0 : ball.y-=2; break; case 8 : ball.y+=2; break; case 2 : ball.x+=2; break; case 6 : ball.x-=2; break; case 1 : ball.x+=2; ball.y-=2; break; case 3 : ball.x-=2; ball.y+=2; break; case 7 : ball.x+=2; ball.y+=2; break; case 5 : ball.x-=2; ball.y-=2; break; } }
game function is main function for game logic. It will check that whether ball hits boundary, pad , any key is pressed or not etc...
void game(void) { draw(); while(1) { if(ball.y-l.ly1<=ball.size||l.ly2-ball.y<ball.size) { d=0; get_dir(); } if((ball.x-105<=ball.size && abs(ball.y-pd1.y)<=32 )||(435-ball.x<=ball.size&& abs(ball.y-pd2.y)<=32)) { d=1; get_dir(); } if(ball.x<l.lx1||l.lx2<ball.x) { if(ball.x<l.lx1) {p2.score=p2.score+100;} else {p1.score=p1.score+100;} ball.x=250;//+random(245); ball.y=150;//+random(245); pd1.y=250; pd2.y=250; // ball.dir=random(7); while(!kbhit()){ draw();} } sswitch(key1) { case 'w': if(pd1.y-pd1.l-l.ly1>10) pd1.y-=2; break; case 's': if(l.ly2-(pd1.y+pd1.l)>10) pd1.y+=2; break; default : break; } switch(key2) { case 72: if(pd2.y-pd2.l-l.ly1>10) pd2.y-=2; break; case 80: if(l.ly2-(pd2.y+pd2.l)>10) pd2.y+=2; break; case 'o' : gameover(); default : break; } draw(); mv_ball(); } }
When 'o' is pressed ,The game will be over and score of each player will be displayed.
If you have any query or suggestion then leave comment !
Slotty Casino | Mapyro
ReplyDeleteSlotty Casino. 37 대구광역 출장샵 likes. Welcome to 광주 출장안마 the Slotty Casino 경상북도 출장마사지 website! See all the information and see where 제주 출장안마 everything 경기도 출장샵 is located. Visit Mapyro and find