Jun-09-2020, 03:14 PM
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()