Results 1 to 7 of 7

Thread: Help making a Tetris game - Timers, user input and file handling

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    3

    Help making a Tetris game - Timers, user input and file handling

    Ok, so here's the situation. I've been trying to recreate the classic game Tetris and have been stumped on a problem for the past couple of weeks regarding the use of a timer and user input. I've already tried multiple methods detailed on the internet, but I haven't managed to get any of them to work for me.
    In the program the user presses start, which initializes a bunch of variables and then the program is as follows:

    Generate a block
    Do Until gameOver = True
    If landedBool = True then
    Create a new block
    Generate the block after the current one
    End If
    landedBool = False
    Call currentBlockMovement
    Call GameOverCheck
    If gameOver = true then
    Do a bunch of stuff
    End If
    Call CurrentBlockAndTetrisArrayMerge
    Call Linecreation
    Call DifficultyChange
    Loop

    So basically, the tetris itself works fine. The only thing is that right now, the way that input is entered is that when currentBlockMovement is called, all that happens is that an inputbox comes up saying "How do you want to move? l/r" and then another asking if the user wants to rotate. What I've been stuck on for a while is somehow making the program wait for an amount of time dependent on the difficulty during CurrentBlockMovement, and allowing the user to press keys/ click onscreen buttons to input directions. Do you guys know how I should go about implementing this. I'd also like to know how to, when the game ends, edit a text document if the user got in the top 10 scores. This document should ten be used to display a highscore table with the top 10 scores and the users name beside the corresponding score. This list should be sorted and should also be searchable to see if a certain person is in the top10. Thanks for any help in advance.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: Help making a Tetris game - Timers, user input and file handling

    You should not be using a timer, nor an InputBox, nor a Do loop in this situation. Instead, you should use a game loop and conditions to check when the game should end.

    Here is my contribution on a game loop: http://www.vbforums.com/showthread.p...aged-Game-Loop

    The way the flow should work is:
    1. Start the game loop
    2. After each Tick event is raised check for the following conditions:
      1. If the game is over
      2. If there is user input
      3. If the next piece should be dropped
    3. If the game is over then stop the game loop and display the final score
    4. If there is user input then change the dropping piece accordingly
    5. If the next piece should be dropped then drop the next piece and display what the new next piece will be


    User input can be done by checking if there was a valid key pressed during one of the various key events.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    3

    Re: Help making a Tetris game - Timers, user input and file handling

    Doesn't using this mean that all block movement will happen at the same time? As in, if the user pressed right and then left, wouldn't the block just stay in the same place once the tick event is raised rather than move right and then left just as the user pressed the button, as it would in normal Tetris?
    Last edited by fatbatman; Mar 3rd, 2015 at 03:49 PM.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: Help making a Tetris game - Timers, user input and file handling

    Sorry I realized that after I posted it but forgot to edit it, checking the key events should not be checked in game loop. It should be checked in one of the various key events.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,238

    Re: Help making a Tetris game - Timers, user input and file handling

    It might, it might not. Keep in mind that the blocks can move really fast. The limitting step is whether or not the screen re-draws between keypresses, which is likely, because people are VASTLY slower than computers. So, if the game loop is running fast enough, the screen will draw several times between keypresses, no matter how fast you can press keys. After all, smooth animation requires better than 20Hz, which means that the screen will draw better than 20 times per second. You can't press keys that fast.

    So, it is likely that there will be numerous cycles where there will be no user input. The block will move down the screen by some amount each time. The amount it descends will be determined by how fast it is falling. If you are running the loop at 20Hz, and the block will take 5 seconds to travel from the top of the screen to the bottom, then each iteration of the loop would take the block down 1/100th of the screen (20 iterations per second X 5 seconds). If there is no user input during this time, the block will descend smoothly from the top of the screen to the bottom (though it usually won't make it, as it will hit something and stop). If there is user input, it will be one of two keys. If a key is pressed, then the block moves down by it's 1/100th of the screen, but also moves either left or right by one block width. It would be kinder to the user, though only slightly, to do the horizonal motion before the vertical motion, and check for contact with the bottom as a final step.

    So, could somebody press left and right so fast that the net result would be no horizontal motion? Yeah, if the block is moving slowly enough, that might be possible. It's terribly unlikely, though. More likely is that the block will jump left, get drawn in that position, then jump back right and be drawn in the new position. As long as the block gets drawn in the new position, your eye will likely see it. However, if it happens fast enough, the block would just appear to stutter left or right.

    I remember Tetris as being very responsive to left and right moves, so you might leave it at that. Alternatively, you could say that after moving in one direction, the user has to wait at least 1/10th of a second before they can move again. That's easy enough to do, since 1/10th of a second would mean just skipping one cycle. Freezing out user input for longer than that is sure to lead to frustration, though. There was no such rule in the actual Tetris game.

    Finally, I would note that game loops generally run well above 20Hz, and could attain 70Hz, or 100Hz. I haven't looked at it extensively, but I think the loop that DDay wrote is designed to throttle the speed to some level rather than going as fast as possible. A game loop designed to go as fast as possible would be figuring out the elapsed time between cycles. At times, it might take 1 millisecond (1000Hz), while at other times it might take 50 milliseconds (20Hz). The block would have a rate of falling, and you'd calculate the distance it moved between ticks by multiplying the rate of descent by the ellapsed time. This can get silly for something like Tetris, though, because the game would put so little demand on the system that ticks would be happening faster than the screen will physically draw (around 60Hz, these days). That would mean that there would be moves that wouldn't even show up because the next move occured before the last one ever had a chance to display.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    3

    Re: Help making a Tetris game - Timers, user input and file handling

    Ok thanks. This stuff looks kinda complicated but I'll see if I can implement it with my rather limited vb knowledge.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,238

    Re: Help making a Tetris game - Timers, user input and file handling

    Quote Originally Posted by fatbatman View Post
    Ok thanks. This stuff looks kinda complicated but I'll see if I can implement it with my rather limited vb knowledge.
    This is actually a pretty good thing to start out on. You don't want to be working on problems that you already know how to solve. You want to always be tackling things that are just beyond your current grasp. This one is within your reach, clearly, but just beyond where you think you are, and it could be really fun to get it working.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width