Hello guys today i am gonna give you the source code to build a simple game in C++.....
for any query you can place a comment..
note this program is build in code blocks compiler
you can also download the source code @ game.cpp
use A W S D for controlling
for any query you can place a comment..
note this program is build in code blocks compiler
you can also download the source code @ game.cpp
use A W S D for controlling
- #include<iostream>
- #include<windows.h>
- #include<conio.h>
- #include<stdlib.h>
- using namespace std;
- COORD coord={0,0};
- void gotoxy(int x,int y)
- {
- coord.X=x;
- coord.Y=y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
- }// gotoxy
- void setup();
- void base();
- void draw();
- void input();
- void logic();
- void level();
- void clr();
- enum edir{stop,l,r,u,d};
- edir dir;
- bool gameover;
- int width,height,x,y,fx,fy,score,slow;
- int main()
- {
- setup();
- base();
- while(gameover==false)
- {
- draw();
- input();
- Sleep(slow);
- clr();
- logic();
- }
- }
- void setup()
- {
- slow=500;
- dir=stop;
- gameover=false;
- width=40;
- height=20;
- x=width/2;
- y=height/2;
- fx=rand()%width;
- if(fx==0)
- fx++;
- fy=rand()%height;
- if(fy==0)
- fy++;
- score=0;
- }
- void base()
- {
- system("color 60");
- for(int i=0;i<width;i++)
- cout<<"_";
- cout<<endl;
- for(int i=0;i<=height-2;i++)
- {
- cout<<"|";
- gotoxy(width-1,i+1);
- cout<<"|"<<endl;
- }
- for(int i=0;i<width;i++)
- cout<<"-";
- gotoxy(width+1,1);
- cout<<"score =";
- gotoxy(width+10,1);
- cout<<"speed will increase after every score";
- }
- void draw()
- {
- gotoxy(x,y);
- cout<<"O";
- gotoxy(fx,fy);
- cout<<"F";
- gotoxy(width+8,1);
- cout<<score;
- }
- void input()
- {
- if(kbhit())
- {
- switch(getch())
- {
- case 'w':dir=u;
- break;
- case 's':dir=d;
- break;
- case 'a':dir=l;
- break;
- case 'd':dir=r;
- break;
- case 'q':gameover=true;
- break;
- case 'W':dir=u;
- break;
- case 'S':dir=d;
- break;
- case 'A':dir=l;
- break;
- case 'D':dir=r;
- break;
- case 'Q':gameover=true;
- break;
- default:
- break;
- }
- }
- }
- void logic()
- {
- switch(dir)
- {
- case u: y--;
- break;
- case d: y++;
- break;
- case r:x++;
- break;
- case l:x--;
- break;
- default:
- break;
- }
- if(x==fx&&y==fy)
- {
- score++;
- cout<<"\a";
- fx=rand()%width;
- if(fx==0)
- fx++;
- fy=rand()%height;
- if(fy==0)
- fy++;
- level();
- }
- if(x<=0||y<=0||x>=width||y>=height)
- {
- gameover=true;
- system("cls");
- cout<<"GAME_OVER\a"<<"\nDEVELOPED BY -MAYANK AGRAWAL";
- }
- }
- void clr()
- {
- gotoxy(x,y);
- cout<<" ";
- }
- void level()
- {
- if(slow>=20)
- slow-=20;
- else
- {
- system("cls");
- cout<<"u won \a"<<"\n DEVELOPED BY-MAYANK AGRAWAL";
- }
- }
0 comments:
Post a Comment