Home > OS >  Drawing square colour stored in a list of tuples?
Drawing square colour stored in a list of tuples?

Time:11-05

I am trying to draw a checkerboard. However when I call the 'drawBoard' function it returns a black screen. I think the problem may be this line here:

colour = con.BOARD_COLOURS[((r c) % 2)]

I was attempting to index the different colour values using either 0 or 1 in order to alternate the colour of the squares as they are drawn. Could any one explain where I am going wrong. Below is the full code:

#Defining constants

# RGB colours
BOARD_COLOURS = [(245, 222, 179),(34, 139, 34)]

# Dimensions
ROWS = 8
COLS = 8
DIMS = 600
SQUARE = 600 // 8

class Board:

    def __init__(self):
        pass

    def drawBoard(self, screen):
        for r in range(con.ROWS):
            for c in range(con.ROWS):
                colour = con.BOARD_COLOURS[((r c) % 2)]
                pg.draw.rect(screen, colour, pg.Rect(c*con.SQUARE, r*con.SQUARE, con.SQUARE, con.SQUARE))

def main():
    t = 60
    clock = pg.time.Clock()
    interface = pg.display.set_mode((con.DIMS, con.DIMS))

    pg.init()
    run = True

    while run:
        clock.tick(t)

        for ev in pg.event.get():
            if ev.type == pg.QUIT:
                run = False

        b = Board()
        b.drawBoard(interface)

    pg.quit()
main()

CodePudding user response:

You need to update the display. Update the display by calling either

  • Related