tryagen
tryagen

Reputation: 13

Pygame: Can't draw anythin on screen after "pygame.Surface.fill()"

I'm trying to make a chess game, and I've encountered a problem: I'm trying to update the display for every 'tick' by using pygame.Surface.fill(black_sc). But as a result, it seems I'm not able to draw anything on top of the now black screen:

#GAME LOOP
while True:
    screen.fill(black_sc)
    
    #SQUARE DRAWING LOOP
    s_draw_loop()
    
    #DRAW THE PIECES
    draw_pieces()
    
    m_pos = pygame.mouse.get_pos()
    
    for x in pygame.event.get():
        if x.type == pygame.QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE] == True:
            exit()
        
        if x.type == pygame.MOUSEBUTTONDOWN:
            for space in range(len(rect_database)):
                if rect_database[space].collidepoint(m_pos) == True:
                    print(space)
    
    pygame.display.flip()

Here's the s_draw_loop():

class s_draw_loop():
    s_posx = 0
    s_posy = 0
    s_w = size[0] / 8
    s_h = size[1] / 8
    row = 0
    
    i = 0
    while i < 64:
        if i % 2 == 0:
            if row % 2 == 0:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(white, current_rect)
            else:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(black,current_rect)
            s_posx += s_w
            i += 1
        
        else:
            if row % 2 == 0:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(black,current_rect)
            else:
                current_rect = pygame.Rect(s_posx, s_posy, s_w, s_h)
                screen.fill(white,current_rect)
            s_posx += s_w
            i += 1
        
        if i % 8 == 0:
            s_posx = 0
            s_posy += s_h
            row += 1

I'll spare you from the entire function drawing the pieces, unless you need it, but it's basically just using pygame.Surface.blit() to draw the pictures onto the display.

I've tried including the 'drawing functions' into the game-loop itself, but it seems to make no difference.

Here's the output when including 'screen.fill(black_sc)'

And here it is when commenting 'screen.fill(black_sc)' out

Upvotes: 1

Views: 147

Answers (1)

sloth
sloth

Reputation: 101072

It's because s_draw_loop is a class, not a function.

The drawing code inside s_draw_loop is only executed once, before entering the game loop, when the python runtime reads the code of the class.

Calling s_draw_loop() inside your game loop does actually nothing (except creating an useless instace of that class that does nothing).


Simply change

class s_draw_loop():
    ...

to

def s_draw_loop():
    ...

so the code inside s_draw_loop gets executed every frame.

Upvotes: 1

Related Questions