C++ Game

This is a discussion on C++ Game within the C++ Programming forums, part of the General Programming Boards category; Well i have writeen a silly game in C++. I have attached it with the source and even the compiled ...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Cool 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();

    }
    Attached Files Attached Files

  2. #2
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Exclamation

    um, i ran the exe and it doesnt work...i hope this isnt some virus thingy
    Paro

  3. #3
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    does it work for anyone?
    Paro

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    cool game, need to run it from comand prompt tho
    Last edited by Dummies102; 02-22-2002 at 09:53 PM.

  5. #5
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Lol, thats a weird game, why'd you make it? what do you have to do
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  6. #6
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    very nice! I enjoyed it. Though you could do the same thing just without Borland's help...gets rid of BGI crap

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    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..
    Last edited by vasanth; 02-22-2002 at 11:32 PM.

  8. #8
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    yeah, thats my prob too, i freeze the program when its waiting for the key to be pressed...please help us!
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  9. #9
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    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

  10. #10
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    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()!

  11. #11
    bobish
    Guest
    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

  12. #12
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    what the hell? :X
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  13. #13
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    Remember '#include <graphics.h>'
    Borland Turbo....you get the idea.

  14. #14
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    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.

  15. #15
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    actually we just need an alternative for delay().

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-07-2010, 11:08 PM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 02:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21