Hero Yu
Hero Yu

Reputation: 11

How do I toggle the colors of a tile in pygame?

So I am trying to make a mini game in which you have to answer some questions and based on those you draw a qrcode. For this I have to be able to toggle between white and black as the user should have more than one shot at guessing the answer of a question.

Here is the code:

back = pygame.image.load("qrcode3outerM.jpg").convert()



def draw_scene(screen):
    screen.fill(WHITE)
    #draw the outer from a picture because I am too lazy to generate it
    screen.blit(back,(0,0))
    for row in range(size):
        for col in range(size):
            rect = pygame.Rect((MARGIN + row)*BOX_SIZE,(MARGIN+col)*BOX_SIZE, BOX_SIZE, BOX_SIZE)
            pygame.draw.rect(screen, BLACK, rect, 1)
            # if answ[row][col]:
            #     pygame.draw.rect(screen, BLACK, ((MARGIN + col) * BOX_SIZE, (MARGIN + row)*BOX_SIZE, BOX_SIZE, BOX_SIZE))


if __name__ == '__main__':
    done = False
    draw_scene(screen)
    while not done:
        state = 0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            #get the location of the cell in terms of tiles where the mouse was clicked
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                x = pos[0] // BOX_SIZE - 12 #magic numbers here to make it work for the time being
                y = pos[1] // BOX_SIZE - 12
                state = screen.get_at((x,y))
                print(f'Current state of the pixel {state}')
                print(f'coordinate of the mouse press: {x} {y} ')
                print(answ[x][y])
                print('black' if answ[x][y] else 'white')
                user_answ[x][y] = state
                pygame.draw.rect(screen, BLACK if state else WHITE, ((MARGIN + x) * BOX_SIZE, (MARGIN + y) * BOX_SIZE, BOX_SIZE, BOX_SIZE))
        pygame.display.update()

    pygame.quit()

Here I am drawing all of the tiles white first and then black at the location at which the mouse was pressed. My idea was to get the current state of each cell using the get_at() method but for some reason even after I click a cell and it turns black on my screen the get_at() method still returns (255,255,255,255).

The question is: why does it still return white when it should return black Also, if you have a better method of toggling please let me know

Upvotes: 0

Views: 100

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

You calculate x and y as follows:

x = pos[0] // BOX_SIZE - 12 #magic numbers here to make it work for the time being
y = pos[1] // BOX_SIZE - 12

x and y are not the position of the tile on the screen, but the column and row of the tile. The top left position of the tile is ((MARGIN + x) * BOX_SIZE, (MARGIN + y) * BOX_SIZE). You don't need to determine the color of the pixel at all, because the state of the tile is stored in user_answ[x][y]. All you need to do is get the state from user_answ, then invert the state and write the new state back:

state = user_answ[x][y]
state = not state
user_answ[x][y] = state

Upvotes: 1

Related Questions