1
def game():
running = True
while running:
    screen.fill((0, 0, 0))
    mx, my = pygame.mouse.get_pos()
    button_3 = pygame.Rect(300, 100, 1300, 700)
    if button_3.collidepoint((mx, my)):
        if click:
            begin()

    draw_text('game', font, (255, 255, 255), screen, 20, 20)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False


    # set the pygame window name

    img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
    img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
    img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
    img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")

    images = [img1, img2, img3, img4]

    current_image = -1
    img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]

    LeftButton = 0
    while 1:
        for e in pygame.event.get():
            if e.type == QUIT:
                pygame.quit()
                exit(0)

            if e.type == pygame.MOUSEBUTTONDOWN:
                mouse_rect = pygame.Rect(e.pos, (1, 1))
                current_image = mouse_rect.collidelist(img_rects)

            if e.type == MOUSEMOTION:
                if e.buttons[LeftButton]:
                    rel = e.rel
                    if 0 <= current_image < len(images):
                        img_rects[current_image].x += rel[0]
                        img_rects[current_image].y += rel[1]

        screen.fill(0)
        for i in range(len(images)):
            screen.blit(images[i], img_rects[i])
        pygame.display.flip()
        pygame.time.delay(30)

    screen.blit(BACKGROUND_FASE, (0, 0))
    pygame.display.update()
    mainClock.tick(60)

I would like to add a background image on this screen, but every time I try to make an error or it doesn't appear, how can I add a background to that part without error , the other part of the code is a menu that when I click on start this screen appears and whole code is here https://pastebin.com/Rb7jh2GH

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

You have to blit the background before drawing the scene, and you have to draw it in your game loop. When you draw a background, it covers everything that was previously drawn:

  1. Draw background
  2. Draw scene (all the objects)
  3. Update display

Note that there is no need to clear the background when you draw a background image. If the background image covers the entire window, there is no need to clear the background.

screen.fill(0)

while running:
    # [...]

    while 1:
        for e in pygame.event.get():
            if e.type == QUIT:
                pygame.quit()
                exit(0)

        # [...]

        # draw background
        # screen.fill(0)  <--- superfluous
        screen.blit(BACKGROUND_FASE, (0, 0))

        # draw scene (objects)
        for i in range(len(images)):
            screen.blit(images[i], img_rects[i])

        # update display
        pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174