1

I want to start my main game loop with surface.fill(WHITE) to repaint my display white, but every surface from the last frame still appears.

To demonstrate, the following example blits a red surface on a green surface. The blit is created by an event trigger every 1 second. However, the red surface does not disappear when it is not being triggered by the event.

import pygame as pg
pg.init()

GREEN, WHITE, RED = (60, 120, 60), (255,255,255), (120, 60, 60)
screen = pg.display.set_mode((200, 200))

box = pg.Surface((150, 150))
box.fill(GREEN)

running = True
EVENT_SECOND_TIMER = pg.event.custom_type()
pg.time.set_timer(EVENT_SECOND_TIMER, 1000)

while running:
    screen.fill(WHITE)    
    for event in pg.event.get():
        # Draw circle every second
        if event.type == EVENT_SECOND_TIMER:
            surf = pg.Surface((50,50))
            surf.fill(RED)

            box.blit(surf, (50, 50))

   
    screen.blit(box, (25, 25))
    pg.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Owen Meyer
  • 21
  • 5
  • Welcome back to Stack Overflow. "and I'd appreciate any help." Most people would, but that is [not how the site works](https://meta.stackoverflow.com/questions/284236). Please first identify a concrete problem, and then ask a clear, specific **question**. For example, if there is an error message, then [try to](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) find a cause, [look for](https://meta.stackoverflow.com/questions/261592) an existing answer, and finally create a [mre] to ask about. If the code runs but does something wrong, then also explain **what is wrong**. – Karl Knechtel Jan 01 '23 at 04:36
  • @KarlKnechtel, Point taken. I thought I'd at least rephrase. – Owen Meyer Jan 01 '23 at 05:37

1 Answers1

1

If you want to draw something permanently, you have to draw it in the application loop. If you want to see surf instead of box for an image, you have to set a state variable and draw the scene depending on the state. Reset the state variable after drawing the scene:

import pygame as pg
pg.init()

GREEN, WHITE, RED = (60, 120, 60), (255,255,255), (120, 60, 60)
screen = pg.display.set_mode((200, 200))
clock = pg.time.Clock()

box = pg.Surface((150, 150))
box.fill(GREEN)
surf = pg.Surface((50,50))
surf.fill(RED) 

EVENT_SECOND_TIMER = pg.event.custom_type()
pg.time.set_timer(EVENT_SECOND_TIMER, 1000)

running = True
while running: 
    clock.tick(60)

    draw_surf = False
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        if event.type == EVENT_SECOND_TIMER:
            draw_surf = True 

    screen.fill(WHITE) 
    screen.blit(box, (25, 25))
    if draw_surf:
        screen.blit(surf, (50, 50))        
    pg.display.update() 

pg.quit() 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • amazing, thank you. I was under the impression I could blit a surface on top of another surface. Do all surfaces need to be blit directly to display for them to update? – Owen Meyer Jan 01 '23 at 22:48
  • The reason I ask is because I am making a surface for the game background which moves so that the display(screen) only views part of the background (similar to a side scrolling game) – Owen Meyer Jan 01 '23 at 22:54
  • I understand now that you must clear the host surface explicitly if you are going to blit to it, as the blit surfaces remained blit until the host surface is cleared. In my case, I will clear the display and the background. Marking as complete. I appreciate your communication – Owen Meyer Jan 01 '23 at 23:29