[PyGame] inset countdown in panel - 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] inset countdown in panel (/thread-37123.html) |
inset countdown in panel - Frankduc - May-03-2022 i am trying to create a countdown clock below the numbers. I am king of stuck to how insert the timer in my actual panel without creating a new panel. # -*- coding: utf-8 -*- import pygame from pygame.locals import * import sys import time clock = pygame.time.Clock() pygame.init() display = pygame.display.set_mode((300, 300)) pygame.display.set_caption('Panneau de contr�le') FPS_CLOCK = pygame.time.Clock() counter, text = 10, '10'.rjust(3) pygame.time.set_timer(pygame.USEREVENT, 1000) font = pygame.font.SysFont('Consolas', 30) #pygame.init() BLEU = (0, 0, 255) BLANC = (255, 255, 255) ROUGE = (255, 0, 0) GREEN = (0, 255, 0) BLACK = (0, 0, 0) GRIS = (200, 200, 200) BLANC = (255, 255, 255) NOIR = (0, 0, 0) screen = pygame.display.set_mode([700, 500]) # Charger un police de caract�re sysfont = pygame.font.get_default_font() font = pygame.font.SysFont(None, 48) def bouton_A(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, GREEN, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_D(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, ROUGE, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_nb(screen, pos, texte): rayon = 25 pygame.draw.circle(screen, GRIS, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) sysfont = pygame.font.get_default_font() font = pygame.font.SysFont(None, 36) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False #mouse_presses = pygame.mouse.get_pressed() #if mouse_presses[0]: if event.type == pygame.MOUSEBUTTONDOWN: x , y = pygame.mouse.get_pos() print(x,y) if x > 104 and x < 204 and y > 196 and y < 301: img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, NOIR) screen.blit(img, (20, 100)) img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC) screen.blit(img, (20, 100)) if x < 549 and x > 444 and y > 192 and y < 309: img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, NOIR) screen.blit(img, (20, 100)) img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC) screen.blit(img, (20, 100)) if event.type == pygame.USEREVENT: counter -= 1 text = str(counter).rjust(3) if counter > 0 else 'boom!' if event.type == pygame.QUIT: run = False screen.fill((255, 255, 255)) screen.blit(font.render(text, True, (0, 0, 0)), (32, 48)) #pygame.display.flip() clock.tick(60) #screen.fill(BLANC) bouton_A(screen, (150, 250), "Activ�") bouton_D(screen, (500, 250), "D�sactiv�") bouton_nb(screen, (265, 200), "1") bouton_nb(screen, (320, 200), "2") bouton_nb(screen, (375, 200), "3") bouton_nb(screen, (265, 255), "4") bouton_nb(screen, (320, 255), "5") bouton_nb(screen, (375, 255), "6") bouton_nb(screen, (265, 310), "7") bouton_nb(screen, (320, 310), "8") bouton_nb(screen, (375, 310), "9") #pygame.draw.circle(screen, GRIS, (260, 250), 25) #pygame.display.flip() img = font.render("Bienvenue", True, ROUGE) screen.blit(img, (20, 20)) pygame.display.flip() pygame.display.update() FPS_CLOCK.tick(30) pygame.quit()thank you RE: inset countdown in panel - deanhystad - May-03-2022 I do not understand the question. You know how to erase and draw text, so that can't be it. Why do you think this would be easier if you had another panel? RE: inset countdown in panel - Frankduc - May-03-2022 There is a count down in my code : , if event.type =, = pygame.USEREVENT: counter -= 1 text = str(counter).rjust(3) if counter > 0 else 'boom!' if event.type == pygame.QUIT: run = False screen.fill((255, 255, 255)) screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))If you run the script at it is, you will notice a 10 on left corner that urn the screen white. That should be under the panel of numbers. It should countdown , 10 9 8 7 ...to boom! The script i use was made to create a count down in a seperate panel: The original script is here: https://stackoverflow.com/questions/30720665/countdown-timer-in-pygame Now i want to use that script to create my countdown in my panel under the numbers. Thank you RE: inset countdown in panel - deanhystad - May-03-2022 There are multiple solutions presented. You are using half of one of them. You process a user event, but you are not setting up the timer to generate the event (the pygame.time.set_timer() part). Copy the remainder of the example. RE: inset countdown in panel - Frankduc - May-03-2022 now the countdown is at the right place and right color. andit works its was indentation. Unfortunatly my counter count over the numbers . Same problem as earlier. # -*- coding: utf-8 -*- import pygame from pygame.locals import * import sys import time clock = pygame.time.Clock() pygame.init() display = pygame.display.set_mode((300, 300)) pygame.display.set_caption('Panneau de contr�le') FPS_CLOCK = pygame.time.Clock() counter, text = 10, '10'.rjust(3) pygame.time.set_timer(pygame.USEREVENT, 1000) font = pygame.font.SysFont('Consolas', 30) #pygame.init() BLEU = (0, 0, 255) BLANC = (255, 255, 255) ROUGE = (255, 0, 0) GREEN = (0, 255, 0) BLACK = (0, 0, 0) GRIS = (200, 200, 200) BLANC = (255, 255, 255) NOIR = (0, 0, 0) screen = pygame.display.set_mode([700, 500]) # Charger un police de caract�re sysfont = pygame.font.get_default_font() font = pygame.font.SysFont(None, 48) def bouton_A(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, GREEN, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_D(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, ROUGE, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_nb(screen, pos, texte): rayon = 25 pygame.draw.circle(screen, GRIS, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) sysfont = pygame.font.get_default_font() font = pygame.font.SysFont(None, 36) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False #mouse_presses = pygame.mouse.get_pressed() #if mouse_presses[0]: if event.type == pygame.MOUSEBUTTONDOWN: x , y = pygame.mouse.get_pos() print(x,y) if x > 104 and x < 204 and y > 196 and y < 301: img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, NOIR) screen.blit(img, (20, 100)) img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC) screen.blit(img, (20, 100)) if x < 549 and x > 444 and y > 192 and y < 309: img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, NOIR) screen.blit(img, (20, 100)) img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC) screen.blit(img, (20, 100)) if event.type == pygame.USEREVENT: counter -= 1 text = str(counter).rjust(3) if counter > 0 else 'boom!' if event.type == pygame.QUIT: run = False #screen.fill((255, 255, 255)) screen.blit(font.render(text, True, (255, 255, 255)), (300, 408)) #pygame.display.flip() clock.tick(60) #screen.fill(BLANC) bouton_A(screen, (150, 250), "Activ�") bouton_D(screen, (500, 250), "D�sactiv�") bouton_nb(screen, (265, 200), "1") bouton_nb(screen, (320, 200), "2") bouton_nb(screen, (375, 200), "3") bouton_nb(screen, (265, 255), "4") bouton_nb(screen, (320, 255), "5") bouton_nb(screen, (375, 255), "6") bouton_nb(screen, (265, 310), "7") bouton_nb(screen, (320, 310), "8") bouton_nb(screen, (375, 310), "9") #pygame.draw.circle(screen, GRIS, (260, 250), 25) #pygame.display.flip() img = font.render("Bienvenue", True, ROUGE) screen.blit(img, (20, 20)) pygame.display.flip() pygame.display.update() FPS_CLOCK.tick(30) pygame.quit() RE: inset countdown in panel - deanhystad - May-03-2022 You know how to fix that problem. There has to be a better way to do buttons in pytalk. RE: inset countdown in panel - deanhystad - May-05-2022 Here is a couple of rough pygame classes that act somewhat like Label and Button. import pygame pygame.init() FOREGROUND_COLOR = "White" BACKGROUND_COLOR = "Black" DEFAULT_FONT = pygame.font.SysFont(None, 32) display = pygame.display.set_mode((300, 300)) screen = pygame.display.set_mode([450, 350]) class Widget(pygame.surface.Surface): """Base class for pygame widgets""" def __init__(self, parent, size, foreground=None): super().__init__(size) self.parent = parent self.foreground = FOREGROUND_COLOR if foreground is None else foreground self.background = BACKGROUND_COLOR self.rect = self.get_rect() def draw(self): """Draw (blit) self on parent surface.""" self.parent.blit(self, (self.rect.x, self.rect.y)) return self def at(self, x, y): """Set upper left corner at x, y""" self.rect.x = x self.rect.y = y return self def center_at(self, x, y): """Set center at x, y""" self.rect.centerx = x self.rect.centery = y return self class Label(Widget): """A tkinter Label like thing for pygame""" def __init__(self, parent, text, width=None, font=None, foreground=None): self.width = len(text) if width is None else width self.font = DEFAULT_FONT if font is None else font size = self.font.render("W"*self.width, True, BACKGROUND_COLOR).get_size() super().__init__(parent, size, foreground) self.set_text(text) def set_text(self, text): self.text = text text_img = self.font.render(self.text, True, self.foreground) y = (self.rect.height - text_img.get_height()) // 2 self.fill(self.background) self.blit(text_img, (0, y)) self.draw() return self class CircleButton(Widget): """A tkinter Button like think for pygame""" buttons = [] @classmethod def clicked(cls, x, y): """Call this method to find what button was pressed""" for button in cls.buttons: if button.click(x, y): return True return False def __init__(self, parent, text, radius, foreground=None, text_color=None, font=None): super().__init__(parent, (radius*2, radius*2), foreground) self.font = DEFAULT_FONT if font is None else font self.text_color = self.background if text_color is None else text_color self.callback = None self.buttons.append(self) self.set_text(text) def set_text(self, text): """Set my label text. Label is centered in button""" self.text = text text_img = self.font.render(self.text, True, self.text_color) x = (self.rect.width - text_img.get_width()) // 2 y = (self.rect.height - text_img.get_height()) // 2 self.fill(self.background) pygame.draw.ellipse(self, self.foreground, self.get_rect()) self.blit(text_img, (x, y)) return self def connect(self, func): """Set function to call when clicked""" self.callback = func return self def click(self, x, y): """Check if I was clicked. Execute callback method if clicked""" clicked = self.rect.collidepoint(x, y) if clicked and self.callback: self.callback(self) return clicked # Give the buttons something to do def button_pressed(button): label.set_text(label.text + button.text) # Create a label and some buttons label = Label(screen, "", width=20, font=pygame.font.SysFont(None, 36)).at(50, 50) for number in range(9): x = 50 + (number % 3) * 80 y = 260 - (number // 3) * 80 CircleButton(screen, str(number+1), 30, "Blue", "White") \ .connect(button_pressed).at(x, y).draw() CircleButton(screen, "Clear", 55, "Red") \ .connect(lambda x: label.set_text("")).at(300, 100).draw() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False break if event.type == pygame.MOUSEBUTTONUP: CircleButton.clicked(*pygame.mouse.get_pos()) pygame.display.flip() pygame.display.update() pygame.quit() |