Search code examples
pygame

Pygame Background is drawing on the other image


I am new to Python and Pygame and I am wondering why The game Window prints the chessboard on top of the pieces so the pieces arent shown. I think if you put the drawing of the background before everything the other parts should be visible but they arent. Can anyone help?

This is the Program I wrote:

import pygame
pygame.init()
xBp = 0
yBp = 50
countBp = 0
pygame.display.set_caption("epic Chess")
screen = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
brett = pygame.image.load("Chessbot/schachbrett.jpg")
imagesB = [pygame.image.load("Chessbot/imagesbR.png"),pygame.image.load("Chessbot/images/bN.png"),pygame.image.load("Chessbot/images/bB.png"),pygame.image.load("Chessbot/images/bK.png"),pygame.image.load("Chessbot/images/bQ.png"),pygame.image.load("Chessbot/images/bB.png"),pygame.image.load("Chessbot/images/bN.png"),pygame.image.load("Chessbot/images/bR.png")]
running = True
def board_setup():
    global xBp,yBp,countBp
    for i in imagesB:
        countBp += 1
        if countBp <=8:
            screen.blit(i,(xBp,yBp))
            xBp +=50

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    board_setup()
    screen.blit(brett, (0, 0))

    clock.tick(60)
    pygame.display.update()

Solution

  • You keep calling board_setupt in your while, you want to do that just once.

    Also, draw the background before placing the pieces.

    I'd split the script in a 'initial' and 'game-logic' parts

    import pygame
    
    def board_setup():
        global xBp,yBp,countBp
        for i in imagesB:
            countBp += 1
            if countBp <=8:
                screen.blit(i,(xBp,yBp))
                xBp +=50
    
    # Initial setup
    pygame.init()
    xBp = 0
    yBp = 50
    countBp = 0
    pygame.display.set_caption("epic Chess")
    screen = pygame.display.set_mode((800,800))
    clock = pygame.time.Clock()
    brett = pygame.image.load("Chessbot/schachbrett.jpg")
    imagesB = [pygame.image.load("Chessbot/imagesbR.png"),pygame.image.load("Chessbot/images/bN.png"),pygame.image.load("Chessbot/images/bB.png"),pygame.image.load("Chessbot/images/bK.png"),pygame.image.load("Chessbot/images/bQ.png"),pygame.image.load("Chessbot/images/bB.png"),pygame.image.load("Chessbot/images/bN.png"),pygame.image.load("Chessbot/images/bR.png")]screen.blit(brett, (0, 0))
    board_setup()
    running = True
    pygame.display.update()
    
    # Game Logic
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        clock.tick(60)
        pygame.display.update()
    
    

    Running this gives a chessboard with multiple pieces on top.