Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 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)
Quote
#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()
Quote
#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.
Quote
#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.
Quote
#5
It works - sorry, I just made an error with the indents haha, thank you!
Quote
#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()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Make a screen appear without using import ctrembla 10 279 Sep-29-2018, 07:52 AM
Last Post: DeaD_EyE
  [PyGame] How to stop the sprite's movement when it touches the edges of the screen? mrmn 5 517 May-13-2018, 06:33 PM
Last Post: mrmn
  [PyGame] Copying the Screen (PyGame) Zman350x 4 1,173 Feb-11-2017, 08:26 PM
Last Post: Zman350x
  [PyGame] text-to-screen help Zman350x 9 1,899 Jan-13-2017, 12:27 AM
Last Post: Zman350x

Forum Jump:


Users browsing this thread: 1 Guest(s)