Python Forum
[PyGame] Pygame : clearing a screen issue - 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: [PyGame] Pygame : clearing a screen issue (/thread-27522.html)



Pygame : clearing a screen issue - Reldaing - Jun-09-2020

Hi, I'm currently stuggling with clearing my screen on pygame when a button is clicked. For example, when pygame.mouse.get_pressed()[0]==1 in my loop for the start button,it launches the start function and clears all the screen. Thanks!
import pygame
import os
os.chdir("C://Users//Ridha//Desktop//MAze-game-master")
pygame.init()
from pygame.locals import *
ecran = pygame.display.set_mode((1080,675))


class Options:
    hovered = False
    def __init__(self, text, pos, action):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()
        self.action=action

    def get_color(self):
        if self.hovered:
            return (255, 255, 255)
        else:
            return (100, 0,0)
    def set_rend(self):
        self.rend = menu_font.render(self.text, True, self.get_color())

    def draw(self):
        self.set_rend()
        ecran.blit(self.rend, self.rect)
    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos
    def launch(self):
        if self.action=='Start':
            Start()
        elif self.action=='Settings':
            Settings()
        else:
            Credits()



def Start() :
    print('hey')
def Settings():
    print("hoy")
def Credits():
    print("how")


menu_font = pygame.font.Font('Police//Gilmoore Rough.otf', 45)
options = [Options("PLAY", (520, 300),'Start'), Options("SETTINGS", (465, 370),'Settings'),
           Options("CREDITS", (470, 440),'Credits')]
image = pygame.image.load("Foondpc.jpg").convert_alpha()

continuer=True
while continuer:
    ecran.blit(image,(0, 0))
    for option in options:
        if option.rect.collidepoint(pygame.mouse.get_pos()):
            option.hovered = True
            if pygame.mouse.get_pressed()[0]==1:
                option.launch()

        else:
            option.hovered = False
        option.draw()
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            continuer = False
        pygame.display.flip()


    pygame.display.update()
pygame.quit()



RE: Pygame : clearing a screen issue - michael1789 - Jun-09-2020

To clear a screen, fill it with a solid color:
ecran.fill((0, 0, 0))



RE: Pygame : clearing a screen issue - Windspar - Jun-09-2020

Only have one pygame.display.flip() or pygame.display.update(). Many graphic glitches will happen if you use multiply flips or/and updates. You be forcing an unnatural refresh rate. Which some graphics cards will not handle it.


RE: Pygame : clearing a screen issue - metulburr - Jun-09-2020

normally people put it in the main game loop too by the way
while continuer:
    ecran.fill((0, 0, 0))
    ecran.blit(image,(0, 0))
    ...
You are also on the verge of needing a state machine to handle multiple states.


RE: Pygame : clearing a screen issue - JudyLarose - Jun-23-2020

(Jun-09-2020, 03:14 PM)Reldaing Wrote: Hi, I'm currently stuggling with clearing my screen on pygame when a button is clicked. For example, when pygame.mouse.get_pressed()[0]==1 in my loop for the start button,it launches the start function and clears all the screen. Thanks!
import pygame [url=https://cpstest.org/typing-speed-test]typing speed test[/url]
import os
os.chdir("C://Users//Ridha//Desktop//MAze-game-master")
pygame.init()
from pygame.locals import *
ecran = pygame.display.set_mode((1080,675))


class Options:
    hovered = False
    def __init__(self, text, pos, action):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()
        self.action=action

    def get_color(self):
        if self.hovered:
            return (255, 255, 255)
        else:
            return (100, 0,0)
    def set_rend(self):
        self.rend = menu_font.render(self.text, True, self.get_color())

    def draw(self):
        self.set_rend()
        ecran.blit(self.rend, self.rect)
    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos
    def launch(self):
        if self.action=='Start':
            Start()
        elif self.action=='Settings':
            Settings()
        else:
            Credits()



def Start() :
    print('hey')
def Settings():
    print("hoy")
def Credits():
    print("how")


menu_font = pygame.font.Font('Police//Gilmoore Rough.otf', 45)
options = [Options("PLAY", (520, 300),'Start'), Options("SETTINGS", (465, 370),'Settings'),
           Options("CREDITS", (470, 440),'Credits')]
image = pygame.image.load("Foondpc.jpg").convert_alpha()

continuer=True
while continuer:
    ecran.blit(image,(0, 0))
    for option in options:
        if option.rect.collidepoint(pygame.mouse.get_pos()):
            option.hovered = True
            if pygame.mouse.get_pressed()[0]==1:
                option.launch()

        else:
            option.hovered = False
        option.draw()
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            continuer = False
        pygame.display.flip()


    pygame.display.update()
pygame.quit()

this is working for me, thank you!