0

I am currently working in C++. I need to make a number catcher game thus have to do two things simultaneously

  1. Moving Character
  2. Numbers continuously falling from the top.

Moreover I need to throw more than one number at a time. How can I do all this stuff simultaneously?

Fraser
  • 65,864
  • 15
  • 215
  • 204
Enno
  • 1
  • 1
  • 1

6 Answers6

5

You're based on a very faulty premise. This does not call for doing any such thing simultaneously. All you have to do is do each thing then update. You need to get a book on very basic game architecture.

Puppy
  • 138,897
  • 33
  • 232
  • 446
1

I believe you need to start with your project and learn from others simultaneously. Basically, you do not need to do all that at the same time, nobody's watching if you're doing that simultaneously under the hood, you only need to do it all before you present results to the user.

Michael Krelin - hacker
  • 122,635
  • 21
  • 184
  • 169
0

There are cases where we need to call multiple functionality.And that is the place where threading comes in action. For example, On windows you can use CreateThreadEx functions to call multiple functions simultaneously.However you'll have to deal with the complexities of threading.

Read articles on threading and you'll gain its basic concepts.

perilbrain
  • 7,496
  • 1
  • 25
  • 35
0

I will elaborate slightly from other answers advocating the study of game programming and provide some basic insight.

The main task of your program is to loop (until game over, or the player is tired), and at each iteration:

  • compute the current state of the game,
  • render the view,
  • read player input.

You don't have to do this in this order, the game should run fast enough to make it seems everything is happening simultaneously.

The state of the game is actually the state of all the elements that are part of the game:

  • background,
  • still objects,
  • moving objects,
  • objects generating other objects,
  • objects destroying other objects,
  • player,
  • etc.

There might be rules governing how all these objects, different rules for different objects, interactions between objects, and more. It's up to you to code up these rules in the engine, and make sure all the objects get updated. The simplest way is to iterate through all your object list and update them one by one.

Rendering is basically going through all your objects and draw them on screen (if they are visible), according to their state (color, position, etc.).

Polling input is just that: verifying if the player did some input by using the keyboard, the mouse, the touchscreen, or anyother input device you wish to handle.

In your project's case, your object list will be just the list of falling characters, the main rule is gravity (or a constant speed going downard the bottom of the screen), and input is the keyboard.

If you wish to have the game running in text mode, the main hurdles will be moving the cursor around, get non blocking input, clear the screen. On UNIX, look for the curses library, which provide all the necessary functions.

Given the simplicity of the game, your program will certainly run very quickly, so you will want to include a small sleep period in the loop. However it is better to have a consistent framerate (at least 30hz), so you could measure the time taken to do the work, and have the process sleep for the remaining time to reach the correct time for one loop.

One last thing: don't look into threads or concurrent programming for this project, it would not help. The rendering has to be done by one single thread in most graphic setups, and you would end up doing a lot of synchronization, unless you use messaging between threads (that would be an interesting exercise, but keep it for version 2).

didierc
  • 14,109
  • 3
  • 30
  • 51
0

The Matrix  falling numbers

Code for if you are on Windows:

/* The Matrix  falling numbers */

#include <iostream>
#include <windows.h> 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;


#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66


/* Variables*/

char screen_buffer[2000]={' '};
int y_coord[2000]={0};
int x=0, y=0,dy=0;
int XMAX=77;
int YMAX=23;
int KB_code=0;
bool QuitGame=false;
int platformX=35, platformY=23;

/* function prototypes*/

void gotoxy(int x, int y);
void clrscr(void);
void setcolor(WORD color); 
void simple_keyboard_input();  
void draw_falling_numbers();
void draw_platform();

/*  main  */

int main(void)
{
  /* generate random seed */
  srand ( time(NULL) );

  /* generate random number*/
  for(int i=0;i<XMAX;i++) y_coord[i]=   rand() % YMAX;

  while(!QuitGame)
  {
      /* simple keyboard input */
      simple_keyboard_input();

      /* draw falling numbers */
      draw_falling_numbers();

  }

  /* restore text color */
  setcolor(7);
  clrscr( );
  cout<<" \n";

  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();

   return 0;
}

/* functions  */

void draw_falling_numbers()
{

    for(x=0;x<=XMAX;x++)
    {
        /* generate random number */
        int MatixNumber=rand() % 2 ;

        /* update falling number */
        y_coord[x]=y_coord[x]+1;

        if (y_coord[x]>YMAX) y_coord[x]=0;

        /* draw dark color */
        setcolor(2);
        gotoxy(x ,y_coord[x]-1); cout<<"  "<<MatixNumber<<"   ";

        /* draw light color */
        setcolor(10);
        gotoxy(x ,y_coord[x]); cout<<"  "<<MatixNumber<<"   ";
    }
    /* wait some milliseconds */
    Sleep(50);
    //clrscr( );
}


void draw_platform()
{
  setcolor(7);
 gotoxy(platformX ,platformY);cout<<"       ";

 gotoxy(platformX ,platformY);cout<<"ÜÜÜÜÜÜ";
 setcolor(7);
 Sleep(5);
}




void simple_keyboard_input()
{
    if (kbhit())
      {
            KB_code = getch();
            //cout<<"KB_code = "<<KB_code<<"\n";

            switch (KB_code)
            {

                case KB_ESCAPE:

                    QuitGame=true;

                break;

                case KB_LEFT:
                           //Do something
                    platformX=platformX-4;if(platformX<3) platformX=3;
                break;

                case KB_RIGHT:
                           //Do something     
                    platformX=platformX+4;if(platformX>74) platformX=74;
                break;

                case KB_UP:
                           //Do something                     
                break;

                case KB_DOWN:
                           //Do something                     
                break;

            }        

      }

}


void setcolor(WORD color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}


void gotoxy(int x, int y)
{
  static HANDLE hStdout = NULL;
  COORD coord;

  coord.X = x;
  coord.Y = y;

  if(!hStdout)
  {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  }

  SetConsoleCursorPosition(hStdout,coord);
}


void clrscr(void)
{
  static HANDLE hStdout = NULL;      
  static CONSOLE_SCREEN_BUFFER_INFO csbi;
  const COORD startCoords = {0,0};   
  DWORD dummy;

  if(!hStdout)               
  {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hStdout,&csbi);
  }

  FillConsoleOutputCharacter(hStdout,
                             ' ',
                             csbi.dwSize.X * csbi.dwSize.Y,
                             startCoords,
                             &dummy);    
  gotoxy(0,0);
}
Software_Designer
  • 7,800
  • 3
  • 21
  • 24
-1

Look into concurrent programming methods that will include the use of openmp or pthreads. The only reason I suggest openmp is that it is pretty easy to learn if you have a limited amount of time and to some extent pthreads is pretty easy to wrap your head around also. Also it would be helpful to know what platform you are using so that everyone answer the question in a more specific manner.

Big_t
  • 41
  • 5