Python Forum
How to get screen to clear?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get screen to clear?
#1
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)
Reply
#2
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()
Recommended Tutorials:
Reply
#3
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.
Reply
#4
(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.
Recommended Tutorials:
Reply
#5
It works - sorry, I just made an error with the indents haha, thank you!
Reply
#6
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()
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 954 Apr-30-2023, 10:11 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020