tetris - jerky and unresponsive

This is a discussion on tetris - jerky and unresponsive within the C++ Programming forums, part of the General Programming Boards category; I've been kindof busy, but I got the basic tetris program I had last week working pretty good, added preview, ...

  1. #1
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15

    tetris - jerky and unresponsive

    I've been kindof busy, but I got the basic tetris program I had last week working pretty good, added preview, score ect., could do alot more but its all working pretty good except one more basic problem. It's jerky. I made a better display function, so it doesn't flicker, but it has a tendency to be unresponsive. Most of the program is spent in a loop that checks for a keypress, then checks the time to see if it's ready to move down yet, then loops back again.
    Code:
    while (end - start < timeitvl) //pause between blocks falling
                  {
                        if (kbhit())
                        {
                           int kp=getkeypress();
                           if (kp !=-1 && canmove(board,block,blockx,blocky,kp))
                           {
                                  if (kp==1){blocky++;}
                                  else if(kp==2){blockx--;}
                                  else if(kp==3){blockx++;}
                                  else if (kp==4){rotate_array(block);}
                                  insertblock(board,block);
                                  display(board,0);
                           }
                        }
                        end=double(clock())/1000;
                  }
    I suspect thats what causes the problem, but I don't know how else to do it. The block always moves down on que, it just doesn't always move left/right immediately when pushing a button. So..., yea please take a look and give me any ideas.
    Attached Files Attached Files
    Last edited by Argentum; 12-17-2005 at 03:49 PM.
    "Tis' better to be wise than to be stupid" - me
    -criticism welcome-

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I'd say you have two options.

    1. Decide that you have a fully functional game and put this project up on the shelf. Then go on to the next stage of learning, be it 'pointers' classes' or whatever.

    Or...

    2. Try to correct the flicker. If you seriously want to do this, then I'd consider looking into win32 programming. You will get much more consistent results -

    Remember the console window wasn't designed to handle games whereby the screen needs to be repeatedly cleared.

    Also, look into using the SDL or Allegro libraries if you choose option two.

    Good luck.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    What you wan't is double buffering. But you can't double buffer a console window, there is just no point because as treenef said, the console just wasn't intended for games (not like a Tetris clone anyway).

  4. #4
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15
    Alright, I'll move on. I just don't have a lot of time, so I was just building off what I had. Thanks for replying.
    "Tis' better to be wise than to be stupid" - me
    -criticism welcome-

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    You can double buffer (actually, as many buffers as you want I believe) a console if you are on Windows. I've never actually done it, but:

    CreateConsoleScreenBuffer
    SetConsoleActiveScreenBuffer
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    You are checking for key input only when a block is waiting to fall. This means that while a block is falling, keypresses won't register. This means you are missing 50 to 75 percent of the keypresses.

    Always check for keypresses. In your method let's say the blocks fall once every second. This means every other second, no keypresses will register. Change it to always check for keypresses yet only respond to them when the timer is out. Also in tetris you can rotate the block regardless of the time it takes to make one fall. So that should respond immediately.

Popular pages Recent additions subscribe to a feed

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