0
\$\begingroup\$

I am creating a Tetris game using Pygame. It generates a random shape from a list and draws it onto the screen. However, when the arrow key is pressed by the user, it draws the shape again without removing the previous one. The problem is that I can't use screen.fill(White) as it will remove the other shapes at the bottom of the grid. How can I refresh the screen without removing the shapes at the bottom of the grid, and so that it doesn't draw the shape multiple times? The first image shows the original shape, and the second image shows the result of pressing the arrow key. Original shape Shape after left arrow key is pressed.

\$\endgroup\$
1
\$\begingroup\$

You can fill only some area, not all screen by calling screen.fill(White, areaToFill). Also, your problem might be that you draw the shape again in different place, though you need to move rect position of the shape - this should automatically redraw shape on other position not leaving any previous shape.

For Surface.fill look here: Surface.fill. This seems to be a good start to understanding how everything works in pygame: Pygame Tutorial . Aslo, Introduction to pygame.

\$\endgroup\$
0
\$\begingroup\$

This is not how games are usually done. The basic gameloop is usually implemented as follows:

while True:
    handle_events()
    update_objects()

    clear_screen()

    for object in objects:
        object.draw()

    update_screen()

So instead of only updating a certain part of the screen, you redraw the whole screen every frame. That is extremely common.

So what you want to do is to keep track of all your objects, and implement a way to cleanly redraw them. Predrawing each piece to a surface might be something you'd want to consider. Doing this will save you a lot of trouble in the future.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.