getting input without waiting for it(tetris)

This is a discussion on getting input without waiting for it(tetris) within the C++ Programming forums, part of the General Programming Boards category; Im making a tetris program. The only other program I've made was a simple "rock, paper, scissors" program (besides "hello ...

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

    getting input without waiting for it(tetris)

    Im making a tetris program. The only other program I've made was a simple "rock, paper, scissors" program (besides "hello world", farenheit to celsius converters, etc, and other 10-20 liners). Im teaching myself and I just finished a chapter (only 4 or 5 into the book) on arrays, and I wanted to do something to use things I learned, or there's no way Id remember them. After I got going (with tetris) I realized I probably wasn't ready for tetris yet. But I did it anyway, cause I wanted to. Had to look a bunch of stuff up though. The only thing I don't fully understand is getting the arrow key presses, but I understood enough to use it (with only a little copying and pasting ). Anyway I got the basic program working (it even seems to have somehow gotten the natural tendency to give you the worst block at the worst time that all tetris programs have )

    Anyway heres my problem now. I want to make the prog. update, every, say, .2 seconds. I can do that, but when I use getch, it waits for input, making the timer pointless soooo... thats my problem. I don't know if you can make it do two things at once, or what, but im hoping you can tell me.

    If you look at the code, remember, I'm almost an absolute begginner, so I expect its probably all done terribly.(but it does work)
    If you have any suggestions, suggest away. (try to be somewhat tactful )
    In fact, any advise would be helpful as I have alot to learn.
    Attached Files Attached Files

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Maybe this perhaps?

    Code:
    if ( kbhit( ) )
    getch();
    http://cboard.cprogramming.com/archi...p/t-46375.html

    To avoid all these problems your best bet would be to write it using win32 or something.

    Your overall design looks good though:- Logical.


    Code:
    //Example will loop until a key is pressed
    //Example will not work with all compilers
    #include <conio.h>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        while(1)
        {
            if(kbhit())
            {
                char ch;
                ch=getch();
                if(ch=='a')
                {
                
                cout<<"move left\n";
                
                } 
                if(ch=='d')
                {
                
                cout<<"move right\n";
                
                } 
                //etc    
                
            }
            cout<<"Piece dropping"<<endl;
            Sleep(1000);
           //or use your time function
        }
    }
    Last edited by treenef; 12-10-2005 at 03:53 AM.

  3. #3
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15
    Thanks thats what I was looking for.
    So I got that working now but I still want to do some other things. First , the program seems kindof choppy( not just that the display flickers) it seems to pause, and jerk a little. The thread you gave the link to looks like it might solve some of my problems, so I'll read through that, and see if I can make sense of it all.
    I want to make a main menu now, with diff. options like highscores (to try file I/O) , username (to keep track of high scores), and anything else I can think of. I think I can handle all that, but one of the things I was thinking about would be a user defined board.
    So im just wondering if I can do something like this
    Code:
    cout <<"Enter width"
    cin>>width
    cout <<"enter height"
    cin >>height
    int board[width][height]
    and Id also have to change my functions:
    Code:
    void insertblock(int array[][height],int array2[][4]);
    void changeblock(int array[][4]);
    void display(int array[10][height]);
    void seta(int array[][height]);
    etc., basically, im wondering if I can just go through the program and replace all the 20's with 'height' and 10's with 'width'.
    And I suppose Id have to have the prototype after getting input for height and width.
    One more thing, how much would I have to change to port this to linux? (Theres only a few windows specific things in it, isnt there?)
    Is programming for linux any harder than for windows?
    Im using windows now, and pobably will be for a while, but I've used linux before, and want to go back someday (had to return to windows for school software).
    So, when I decide to switch to linux programming (assuming I've gotten half decent by then). Will it be a difficult switch, or are things pretty similar?
    Attached Files Attached Files

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    In my past programs I have found that you can not use non const variables to make a matrix. Instead you need to use pointers. Example code follows:

    Code:
          int **board;    
          .
          .
          .
          board = new int*[x];
          for(int i = 0; i < x; i++)
          board[i] = new int[y];
    That code will size your board to whatever the user inputs. This assumes they entered their sizes in x and y.
    Last edited by ~Kyo~; 12-10-2005 at 01:58 PM.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    You shouldn't have to change much at all to port this to Linux (but I haven't looked at your program yet so I might edit this and change my mind ) and programming for Linux is no harder / easier really, until it comes down to assembly, in which case I'd say Linux is easier because of the way interrupts are handled (you'll see what I mean if you start learning assembly for whatever reason - be sure to check it out on both Windows and Linux ).

    EDIT: Yeah, all you'll have to tweak are the system() calls (eg. change cls to clear for Linux) and you might have to change your timer function (someone verify this).
    Last edited by cboard_member; 12-10-2005 at 02:08 PM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15
    Alright, thanks. I dont really understand pointers and some other things yet, so at this point I think I'll start reading again for a few days. Try to understand everything I need to. If I have any questions when start finishing the prog. I'll post then.

    To anyone who has the time to look at the code, I would welcome criticism at this point. I don't have a teacher, so I need someone to keep me from developing bad habits.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Your layout isn't terrible, but there is a lack of spacing. I know it can get laborious but pressing that long key at the bottom of your keyboard will make a lot of difference when it comes to readability.

    That's about the only critiscm I have, so rejoice ---> \o/
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15
    Yea, about the linux thing, thats nice to know. When I'm good enough to make any real contributions to opensource software, I'd be much happier helping llinux than windows.
    I started programming a few months ago with a programming class on MSVB(still going) but I felt like I was a cheater, so I've just started teeaching myself c++ (no classes available). I feel much better about myself, but I still feel a little like a cheater, so someday I want to go to the very bottom and learn a little about assembly language (that is the bottom isn't it, where everything is like 1001110101010).

  9. #9
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15
    I thought the spacing was a little messy too, I was going to fix it all up, but I didn't, so I guess I should.

    Oh, and I am rejoicing .

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Well think of assembly as "one step up" from machine code.
    I kind of see what you mean - I started with VB then moved onto C & C++ (I sort of learnt the basics of C, then C++, then swapped in and out until I was satisfied I'd learnt enough). I find C++ much more rewarding, probably because you generally put more effort in to your programs.

    I was involved in a new Linux distribution about 2 months ago, but our forum went down along with our server so the only person I can contact is the project lead who wants to wait till the server is back up, and... it's all messy and off-topic, but you get the idea .

    I love open source development; observe my fruity wallpaper:
    http://www.ahluka.co.uk/opensource.jpg
    Last edited by cboard_member; 12-10-2005 at 02:48 PM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    and you might have to change your timer function (someone verify this).
    Code:
    //Timer Function
    int timer(double t)
    {
        int kp;
        double start=double(clock())/1000,end=double(clock())/1000;
        for (double dif=0;dif<=t;dif=end-start){end=double(clock())/1000;}
        while (kbhit())
        {
        kp=getkeypress();
        return kp;
        break;}
    }
    clock()'s standard, but getkeypress() isn't. BTW, you don't need a while loop, and your casts were wrong:
    Code:
    //Timer Function
    int timer(double t)
    {
        int kp;
        double start=(double)(clock())/1000,end=(double)(clock())/1000;
        for (double dif=0;dif<=t;dif=end-start){end=(double)(clock())/1000;}
        if (kbhit())
        {
        kp=getkeypress();
        return kp;
        }
        return 0;
    }
    Last edited by dwks; 12-10-2005 at 04:23 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15
    The version I have right now (the second that I posted) doesn't use the timer function I was trying to work out how to include the kbhit() function, but I ended up rewriting so the timer is in main. I forgot to delete timer(). And getkeypress() is my own function. Why are the casts wrong? the tutorial gives this as an example:
    Code:
    int main()       
    {
      cout<< char ( 65 ) <<"\n"; 
      cin.get();
    }
    Anyway I've tested it (tetris) and I've got two problems:
    1 The display flickers. The link treenef gave looks like it will help solve this (been kindof busy, so I havn't gotten around to it yet)

    2 The program is choppy. This can be fatal in tetris. Mostly, I push a key and the block moves accordingly, but sometimes it lags, or take several times. I'm not sure why, maybe the way I have it looping isn't the best way to do things

    Oh, yes. On the topic of crossplatforming, how difficult is it to prog for the palm os (4 or less, not 5). I'm kindof curious, cause while its unlikely that I will be programming something I'd actually use much for a while, even this tetris program would be fun if I could make a palm version.
    Attached Files Attached Files
    Last edited by Argentum; 12-10-2005 at 06:30 PM.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Aside from the flickering, I think you did an excellent job with this program. It plays alot like the real game. The only thing missing is the display of what piece is coming next. That's very key to high level player's strategies.

    Very nice, though. I barely mind the flickering when I play it.
    Sent from my iPadŽ

  14. #14
    Selfteaching c++ student
    Join Date
    Dec 2005
    Location
    good old US of A
    Posts
    15
    I think my computer may be much slower than yours, cause the flickerings really bad. I am going to add a preview to it though (along with main menu and other stuff). Theres got to be a way to make this less cpu intensive though.

    Oh yeah, and someone please tell me about the palm thing (if anyones ever programmed on the palm.)
    Last edited by Argentum; 12-10-2005 at 11:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  4. Getting input without waiting for keypress
    By Anon48 in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 03:18 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

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