Text mode arcade game

This is a discussion on Text mode arcade game within the C++ Programming forums, part of the General Programming Boards category; Hello masters, I'm teaching C++ in my technical school, and I wanted to introduce my class in games world with ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Text mode arcade game

    Hello masters,

    I'm teaching C++ in my technical school, and I wanted to introduce my class in games world with some simple experiences.

    We are making a very simple arcade game, where a ship (^) fires some shots (* . ! A) and so on.

    We made it with simple functions, and it worked, but cannot fire more than a projectile simultaneously. So, I decided to buid a class.

    But, the main problem is that I have to create one shot when I press the keyboard (and this worked), but it have to keep moving even after I release it. And, when I press again, I want to have another shot created (and moving). How to do this?

    If helps to understand, follow the class:
    Code:
    class cannon
    {
    protected:
        typedef struct fire
        {
            char bullet;
            int x;
            int y;
        };
    
        void setArms(void)
        {
            arms[0].bullet='*';
            arms[1].bullet='!';
        }
    
    public:
        fire arms[2];
    
        void shoot(int i)
        {
            clock_t runTime;
            runTime = clock();
            gotoxy(arms[i].x,(int)(arms[i].y-runTime/30));
            printf("%c", arms[i].bullet);
            gotoxy(arms[i].x,(int)(arms[i].y-runTime/30-1));
            printf(" ");
        };
    
        cannon(int x,int y, int i)
        {
            setArms();
            arms[i].x = x;
            arms[i].y = y;
            shoot(i);
        };
    };

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Which OS/Compiler are you using?

    You basically need to poll the keyboard in some way, so that even if there is no key press, you get a chance every half second or so to update the position of the projectiles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    If it is not too far for your students to reach (or if you can black-box it so they don't have to deal with it) I would do three simple things:
    1. Create a "shot" class that maintains the shots current position, maybe a char to indicate what kind of shot it is (*, ., etc) and finally an "active" boolean initialized to false.
    2. At program start, spin off a thread that periodically locks a mutex that would guard this data and walks the array. For each :live" shot, the coordinates would be updated to the next position, the old one erased and potentially do collision detection. If the shot has reached the top of your play-field w/o hitting something, just set the active flag to false.
    3. In your main play loop, when the user fires, you lock the mutex guarding shot array, find the first one that is not active, set to coordinates to where ever the shot is supposed to start and set the active flag to true. Your animation thread will do the rest.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [freefps]Tactical Assault coders needed
    By Sickmind in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 05-07-2010, 05:06 PM
  2. Event driven game engine
    By abachler in forum Game Programming
    Replies: 9
    Last Post: 12-01-2009, 05:03 AM
  3. A begginer writing a text based dungeon game.
    By deadherorising in forum C Programming
    Replies: 55
    Last Post: 11-10-2007, 05:16 PM
  4. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 06:39 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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