Friday 23 July 2021

Pygame tetris block not moving down at certain condition

I'm making tetris in Pygame but in pieces of only 1 block (not 4). Here is the block class :

# Creating an environment representing the grid (virtual one)
environment = numpy.zeros((21, 16), dtype=numpy.int32)

class Block(object) :
    def __init__(self):
        self.x, self.y, self.color, self.row, self.column = 631, 3, 0, 0, 7
        self.img = next_block
        self.mv_step = 32
        pygame.time.set_timer(pygame.USEREVENT+1, 800)
        self.is_current_block = True
    
    def blit_and_manage(self):
        MyScreen.blit(self.img, (self.x, self.y))
        # this makes the first line doesn't count, because very block spawns outside the grid at first and then enters it
        if self.y == 35:
            self.row = 0
        # handling events
        for e in events:
            # moving a block to the left
            if (((e.type == pygame.KEYDOWN and e.key == pygame.K_q) or (e.type == pygame.KEYDOWN and e.key == pygame.K_LEFT)) and self.x != 407) and self.is_current_block and environment[self.row, self.column-1] == 0:
                self.x -= 32
                self.column -= 1
                environment[self.row, self.column+1] = 0
            # moving a block to the right
            elif (((e.type == pygame.KEYDOWN and e.key == pygame.K_d) or (e.type == pygame.KEYDOWN and e.key == pygame.K_RIGHT)) and self.x != 887) and self.is_current_block and environment[self.row, self.column+1] == 0:
                self.x += 32
                self.column += 1
                environment[self.row, self.column-1] = 0
            # moving a block down
            if ((e.type == pygame.USEREVENT+1) or ((e.type == pygame.KEYDOWN and (e.key == pygame.K_s or e.key == pygame.K_DOWN))) and self.is_current_block) and environment[self.row+1, self.column] == 0:
                self.y += self.mv_step
                if self.y > 35:
                    self.row += 1
                    environment[self.row-1, self.column] = 0
        # preventing blocks to get outside of the grid when they reach the bottom boundary
        if self.y >= 675:
            self.y = 675
            self.is_current_block = False
        # modifying the block's corresponding coordinate in the numpy array (environment) to its corresponding color number
        if self.y > 3:
            environment[self.row, self.column] = self.color
        # preventing blocks from moving down if there is another block underneath
        if self.row != 20 and environment[self.row+1, self.column] != 0:
            self.is_current_block = False

Now, everything works well but there is just one bug : if I try to fill a vertical line with blocks (by spamming the s button or the arrow down, btw I use ZQSD and not WASD), blocks will stand on each other until there are only two spaces left, the next block that will spawn won't move down at a certain position even that the space underneath it is empty and then the game will freeze and become unresponsive until I do a keyboard interrupt with ctrl + c. You may haven't understood the problem, so here is a quick representation :

representation

NOTES :

  1. Keep in mind that I haven't made a game over condition so the issue couldn't be related to an uncomplete game over function or something like that.
  2. Don't forget that every block's color value is put in the corresponding position in the environment array just when the block's y reaches 35, because as I said before, there is an extra line at the begining that's neither drawn in the grid in the game nor included in the environment array.
  3. The issue is only occuring in the columns in which blocks first spawn.

Here what the environment variable looks like when that problem occur:

[[0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]]


from Pygame tetris block not moving down at certain condition

No comments:

Post a Comment