devris lua
devris lua

Reputation: 55

How do I make the whole scene invisible, pygame

I am trying to make a game where if someone press left-click, pygame draws a circle, then a line connecting the circle to the upper left corner. If he/she press right-click, it removes everything created from the screen. I thought about just covering everything with a big black square, but that is not really clearing the screen, just covering it. My code is as follow:

    from pygame import * 
    init()
    size = width, height = 650, 650
    screen = display.set_mode(size)
    button = 0

    BLACK = (0, 0, 0)
    GREEN = (0, 255, 0)

    def drawScene(screen, button):
        # Draw circle if the left mouse button is down.
        if button==1:
            draw.circle(screen,GREEN,(mx,my), 10)
            draw.line(screen,GREEN, (0,0),(mx,my))
        if button == 3:
                mouse.set_visible(False)

        display.flip()


    running = True
    myClock = time.Clock()

    # Game Loop
    while running:
        for e in event.get():             # checks all events that happen
            if e.type == QUIT:
                running = False
            if e.type == MOUSEBUTTONDOWN:
                mx, my = e.pos          
                button = e.button        


        drawScene(screen, button)
        myClock.tick(60)                     # waits long enough to have 60 fps

    quit()

Kindly guide me in this situation. Thanks in advance.

Upvotes: 2

Views: 123

Answers (2)

user19273
user19273

Reputation: 108

In computer graphics, there is no such thing as "clearing" a screen. you can only overwrite the previous drawing with new pixels. your idea of drawing a big black rectangle would work perfectly but I may be more efficient to use screen.fill(0)

Upvotes: 0

Rabbid76
Rabbid76

Reputation: 210909

I thought about just covering everything with a big black square, but that is not really clearing the screen, just covering it.

There is not any difference between cover and clear. The screen consist of pixels. Drawing an object just changes the color of the pixels. Drawing a black square changes the color of the pixel again.

Note, operations like pygame.draw.circle() and pygame.draw.line() do not create any object. They just change the colors of some pixels of the surface which is passed to the operation.

The easiest way to clear the entire window is pygame.Surface.fill():

screen.fill((0, 0, 0))

respectively

screen.fill(BLACK)

Upvotes: 1

Related Questions