开发者

Tetris Timing Problem

I am writing a Tetris program with PyGame, and came across a funny problem.

Before I ask the question, here is the pseudo-code:

while True:
    # In this part, the human controls the block to go left, right, or speed down
    if a key is pressed and the block isnt touching the floor:
        if the key is K-left:
            move piece left one step
        if the key is K-right:
            move piece right one step
        if the key is K-down:
            move piece down one step


    # This part of the code makes the piece fall by itself
    if the block isnt touching the floor:
        move block down one step

    # This part makes the while loop wait 0.4 seconds so t开发者_JAVA百科hat the block does not move
    # down so quickly
    wait 0.4 seconds

The problem is that, because of the "wait 0.4 seconds" part of the code, the part that the human controls can only move every 0.4 seconds. I would like it so that the block moves as fast as the human can press the key, while at the same time, the block dropping every 0.4 seconds. How could I arrange the code so that it will do that? Thanks!


The main problem I see here is that you are limiting your framerate using a wait of 0.4 seconds.

You should not limit framerate, but instead, you should limit how fast your block falls.

If I remember well, there was a formula you could use to do just that. It was based on the amout of time elapsed since the last frame. It looked like:

fraction of a second elapsed since last frame * distance you want your block to move in a second

This way, you can keep your mainloop intact, and the move processing will happen at every frame.


You could also do...

    ...
    # This part of the code makes the piece fall by itself
    if the block isn't touching the floor and 
       the block hasn't automatically moved in the last 0.4 seconds:
        move block down one step
    ...

Just realize you'll be doing a lot of polling if the user hasn't struck any keys.


You may try asking gamedev.stackexchange.com instead. Check the site for Game Loops, and check out other example pygame projects to see how they're doing it. Having a good game loop is essential and will take care of things for you such as user inputs and a consistent frame rate.

Edit: https://gamedev.stackexchange.com/questions/651/tips-for-writing-the-main-game-loop


When doing games you should always try to do something like this:

while not finished:
    events = get_events() # get the user input
    # update the world based on the time that elapsed and the events
    world.update(events, dt) 
    word.draw() # render the world
    sleep(1/30s) # go to next frame

The sleep time should be variable so it takes into consideration the amount of time spend drawing and calculating the world updates.

The world update method would look something like this:

def update(self, events, dt):
    self.move(events) # interpret user action
    self.elapsed += dt
    if self.elapsed > ADVANCE_TIME:
        self.piece.advance()
        self.elapsed = 0

The other way of implementing this (so you dont redraw too much) is to have events fired when the user orders a piece to be moved or when ADVANCE_TIME time passes. In each event handler you would then update the world and redraw.

This is assuming you want the pieces to move one step at a time and not continuous. In any case, the change for continuous movement is pretty trivial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜