Pregunta

I made something like a game, you move your character(black box) up and down and there is a wall with some blank in middle of it, that wall is at right side, its coming from right to left side and if you touch the wall game ends.

I get keypresses in a while loop like;

while(keyboardInput=getKeyPress()){
        switch(keyboardInput){/*checks the key and goes up or down*/}
        //draws the main character, then also draws the wall.
}

and I made a wall() function, it prints blocks to make a wall.

Problem is, the wall moves only after getting a keypress, my keypress function works with _getch()

The thing I want to see is, that wall will always move like every one second to left side even if user doesn't press any key, and key press/movement will work the same way.

I put wall() function into the wall() function. It prints it just like I want but I cant get any keypress because that wall() loops forever.

Game looks like this:

                                    *
                                    *
                                    *
                                    *
                                    *

■

                                    *
                                    *
                                    *
                                    *
                                    *

I am waiting for your answers, thank you!

¿Fue útil?

Solución

For anything that is close to a game or a simulation, you need to learn what is a "game loop" also called "main loop". There is a nice explaination of the principle on wikipedia.

Basically you should NOT be waiting the input, but you should loop to update the state of the game even if nothing happen: observe the input, see if something happened and if not, just continue looping, if something happen, do it, then continue looping. You must NEVER wait for the input, you just check if there is something to do or not.

This also mean that you should not use a blocking call like _getch(). You have to use whatever input function you have available that only check if there is something to do. It depends a lot on what input API you are using exactly, so I can't get into details without knowing which API you use.

A simple (or more correctly: naive) game loop looks like this:

while( !exit_game )
{
     read_input(); // acquire inputs then generate actions interpreted from inputs, if any
     update_game(); // update the game to generate it's new state after applying all the actions from all the game entities
     render(); // render the graphics and audio
}

This is very naive and don't work well in a real case but it's basically the idea.

I recommand reading this and this to better understand what a game loop is, but it don't focus on getting inputs.

Otros consejos

If you're programming in Windows, take a look at _kbhit(). You can find the MSDN page for _kbhit() here.

Although _kbhit() was historically an MS-DOS (and later, Windows) function (it's defined in <conio.h> on Windows systems), you can roll your own version of it in Linux, as shown here and here.

However, an easier route might be to use the ncurses library to turn getch() into a non-blocking call by calling nodelay(stdscreen, TRUE), as shown here and here.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top