-
C++ Game
Well i have writeen a silly game in C++. I have attached it with the source and even the compiled ones. So please give me suggestions to improve the game. Well to move the misile left and right you need to press the num pad key 4 and 6. And to fire lasers you have to press the space bar. And to quit the game you have to press escape.. I made this using Turbo C++ v3.0
# include <graphics.h>
# include <conio.h>
# include <stdio.h>
# include <dos.h>
# include <iostream.h>
# include <stdlib.h>
# include <time.h>
# include <bios.h>
int a,ra,point=0,counter,ka,q1,q2;
float t1,t2;
timer()
{
long bios_time;
bios_time = biostime(0, 0L);
t1=(bios_time / CLK_TCK);
}
timer1()
{
long bios_time;
bios_time = biostime(0, 0L);
t2=(bios_time / CLK_TCK);
}
ship()
{
randomize();
ra=(rand() % 600);
ka=(rand() % 700);
ra=(ra+ka)/2;
circle(ra,ka,10);
q1=(rand() % 590);
q2=(rand() % 658);
q1=(q1+q2)/2;
circle(q1,q2,10);
}
inig()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
}
lin()
{
clearviewport();
gotoxy(2,2);
cout<<" Game designed by Vasanth.M I HND ";
ship();
line(a,450,a,435);
line(a+1,450,a+1,440);
line(a-1,450,a-1,440);
delay(10);
sound(1900);
delay(10);
nosound();
gotoxy(60,30);
cout<<" Your points >> ";
cout<<point;
gotoxy(5,30);
int ti,tq;
timer1();
ti=t2-t1;
tq=60-ti;
cout<<" Units Left >> ";
cout<<tq;
gotoxy(30,30);
cout<<" No of lasers fired >> ";
cout<<counter;
if(ti>59)
{
textmode(C40);
cout<<" Game Over\n";
cout<<" Your Score = ";
cout<<point<<"\n";
cout<<" Number of Lasers Fired = ";
cout<<counter<<"\n";
cout<<" Number of correct hits = ";
int h=(point/10);
cout<<h<<"\n";
cout<<" Number of Misses = ";
cout<<counter-h;
getche();
textmode(C80);
exit(0);
}
}
start()
{
int val;
a=300;
prog:
lin();
val=getche();
if(val==32)
{
counter=counter+1;
for(int w=0;w<600;w=w+20)
{
line(a,450-w,a,450);
delay(10);
}
}
if(val==27)
exit(0);
if(val==52)
{
a=a-10;
lin();
if(a<1)
a=0;
}
if(val==54)
{
a=a+10;
if(a>639)
a=639;
lin();
}
if(val==32)
{
if( ra==a+1 || ra==a+2 || ra==a+3 || ra==a+4 || ra==a-1 || ra==a-2 || ra==a-3 || ra==a-4 || ra==a+5 || ra==a+6 || ra==a+7 || ra==a+8 || ra==a-5 || ra==a-6 || ra==a-7 || ra==a-8 )
{
for(int ex=0;ex<50;ex++)
{
circle(ra,ka,ex);
sound(9000);
delay(50);
nosound();
}
clearviewport();
point=point+10;
}
if( q1==a+1 || q1==a+2 || q1==a+3 || q1==a+4 || q1==a-1 || q1==a-2 || q1==a-3 || q1==a-4 || q1==a+5 || q1==a+6 || q1==a+7 || q1==a+8 || q1==a-5 || q1==a-6 || q1==a-7 || q1==a-8 )
{
for(int ex=0;ex<50;ex++)
{
circle(q1,q2,ex);
sound(9000);
delay(50);
nosound();
}
clearviewport();
point=point+10;
}
}
goto prog;
}
main()
{
clrscr();
timer();
inig();
start();
ship();
}
-
um, i ran the exe and it doesnt work...i hope this isnt some virus thingy :(
-
-
cool game, need to run it from comand prompt tho
-
Lol, thats a weird game, why'd you make it? what do you have to do
-
very nice! I enjoyed it. Though you could do the same thing just without Borland's help...gets rid of BGI crap
-
Thank you.. But i have a little problem with my game.. That is.. When the program waits for the users input to move.. The circles moving also stop.. How do i make them work(move) when the prog is waiting for a input.. And i was also thinking on how to use the arrow keys.. How do i recognize an input from them...
Hope some one can help me..
-
yeah, thats my prob too, i freeze the program when its waiting for the key to be pressed...please help us!
-
MY Problem too!!!
anyway , I can help you with your arrow key problem.
The Left arrow key's hexcode is 0x4B.
The Right arrow key's hexcode is 0x4D.
So instead of your values 52 and 54(4 and 6), just say
Code:
if(val == 0x4B)//if the key value is the left arrow key
-
actually the input problem is easy...unless you are using delay.
at the end of your little gun moving left or right, just say val=0. then say when your about to move your circle:
Code:
while(val == 0)
{
//randomly move circles
}
that way the circles will keep moving unless there is input. then your gun will move and then the circles will start moving again. The problem with this though is that the circles will be moving at an incredible speed. To fix this, you use delay(). But the problem with THAT is that delay() stops everything else when it waits. Thus you cannot do input. someone, we need an alternative for delay()!
-
instead of delay you should get the record the time to a variable and then have something like this
if(variable holding old time > timeright now-5)
{
continue with game
}
the problem seems to be that first your using io input try this
including these defines
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
and then you can do this if (KEY_DOWN(VK_RIGHT) or say this
if (KEY_UP(VK_LEFT) though im not sure if this will work in a dos based game.
Also you should use a loop instead of goto
-
-
Remember '#include <graphics.h>'
Borland Turbo....you get the idea.
-
non-stopping [until you press q]keyboard loop:
It's non-standard tho
Code:
#include <conio.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int x;
while(x != 'q')
{
while(!kbhit())
{
printf("weee");
}
if(kbhit())
{
x = getch();
}
}
return 0;
}
hope it helps. :D
-
actually we just need an alternative for delay().