Python Forum
How to get screen to clear? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: How to get screen to clear? (/thread-13303.html)



How to get screen to clear? - mzmingle - Oct-09-2018

Hi, I'm confused again! I'm trying to code it so that when they click a button, it moves to a new screen. However, at the moment, it only goes to the cleared screen as long as i keep hold of the mouse button - how do I get it to stay after it's been clicked?

I know this is probably really obvious, but it won't work for me haha!

    if event.type == pygame.MOUSEBUTTONDOWN:
        mouse = pygame.mouse.get_pos()
        if student.collidepoint(mouse):
            screen.fill(WHITE)



RE: How to get screen to clear? - metulburr - Oct-09-2018

You have to set a flag and fill the screen every frame. Currently you are only filling the screen on the frame that a mouse button is down and the mouse is in collision with your student rect.

Here is an example that when you press a mouse button it switches to and from white.
import pygame as pg

pg.init()
screen = pg.display.set_mode((800,600))
running = True
bg_white = False


while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        elif event.type == pg.MOUSEBUTTONDOWN:
            bg_white = not bg_white
            
    if bg_white:
        screen.fill((255,255,255))
    else:
        screen.fill((0,0,0))
    pg.display.update()



RE: How to get screen to clear? - mzmingle - Oct-10-2018

The issue is I created a button, which should open a new screen when it is pressed. How can I get this to work without using .collidepoint (since this only seems to imply it will work when I am keeping hold of the mouse)?

UPDATE: I got it to work - I put the new if true part in the mouse button down bit rather than just in while running.


RE: How to get screen to clear? - metulburr - Oct-10-2018

(Oct-10-2018, 08:22 AM)mzmingle Wrote: The issue is I created a button, which should open a new screen when it is pressed
when you say open a screen it makes me think that you are really doing something more complicated called states. Which is a whole other level of complexity.

(Oct-10-2018, 08:22 AM)mzmingle Wrote: How can I get this to work without using .collidepoint (since this only seems to imply it will work when I am keeping hold of the mouse)?
collidepoint will work. You just have to set a flag after it to draw the screen, not draw the screen itself after the collision.


RE: How to get screen to clear? - mzmingle - Oct-10-2018

It works - sorry, I just made an error with the indents haha, thank you!


RE: How to get screen to clear? - metulburr - Oct-10-2018

here is a more full fledged example
import pygame as pg

pg.init()

def flip_color():
    global bg_white
    bg_white = not bg_white

class Button:
    def __init__(self, rect, command):
        self.color = (255,0,0)
        self.rect = pg.Rect(rect)
        self.image = pg.Surface(self.rect.size)
        self.image.fill(self.color)
        self.command = command
    def render(self, screen):
        screen.blit(self.image, self.rect)
    def get_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
            if self.rect.collidepoint(pg.mouse.get_pos()):
                self.command()
                
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
running = True
bg_white = False

btn = Button((10,10,105,25), flip_color)

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        btn.get_event(event)
    if bg_white:
        screen.fill((255,255,255))
    else:
        screen.fill((0,0,0))
    btn.render(screen)
    pg.display.update()