Python Forum
[PyGame] Pygame : clearing a screen issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Pygame : clearing a screen issue
#1
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()
Reply
#2
To clear a screen, fill it with a solid color:
ecran.fill((0, 0, 0))
Reply
#3
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.
99 percent of computer problems exists between chair and keyboard.
Reply
#4
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.
Recommended Tutorials:
Reply
#5
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame full screen help corsasri 2 2,835 Mar-20-2024, 07:35 AM
Last Post: reginarodriguez
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 984 Apr-30-2023, 10:11 PM
Last Post: deanhystad
  [PyGame] How to Display pygame on SSD1351 screen kalihotname 0 1,868 Nov-11-2019, 07:32 AM
Last Post: kalihotname
  pygame.mixer.init() devicename issue wanhr 2 4,786 Sep-15-2019, 09:08 AM
Last Post: wanhr
  [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 3,646 Jun-11-2019, 08:32 PM
Last Post: metulburr
  [PyGame] Copying the Screen (PyGame) Zman350x 4 5,137 Feb-11-2017, 08:26 PM
Last Post: Zman350x

Forum Jump:

User Panel Messages

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