python 3.x - Progress bar inside a button in Pygame -
the desired behavior is:
when user hold mouse on button, dark gray progress bar appears , starts incremented @ constant pace. want able determine how long take fill (like 2 seconds). if mouse move out button before progress bar has reached 100%, progress bar should go straight 0%. if bar reaches 100%, program should print in terminal.
here code:
import sys import pygame import time pygame.locals import * os import path pygame.init() screen = pygame.display.set_mode((900, int(900 * (16 / 9)))) clock = pygame.time.clock() black = (0, 0, 0) green = (0, 255, 0) white = (255, 255, 255) background_color = (237, 225, 192) light_gray = (60, 60, 60) gray = (30, 30, 30) class button: def __init__(self, screen, x, y, w, h, button_color_active, button_color_inactive, text, font, size = 50, text_color = black): self.screen = screen self.game_folder = path.dirname(__file__) self.font = path.join(self.game_folder, font + '.ttf') self.x, self.y, self.w, self.h = x, y, w, h self.button_color_active = button_color_active self.button_color_inactive = button_color_inactive self.text, self.size = text, size self.text_color = text_color self.button_rect = pygame.rect(self.x, self.y, self.w, self.h) self.button_font = pygame.font.font(self.font, self.size) self.label = self.button_font.render(self.text, 1, self.text_color) def draw(self): if self.button_rect.collidepoint(pygame.mouse.get_pos()): #pygame.draw.rect(self.screen, self.button_color_inactive, self.button_rect) progress in range(42): pygame.draw.rect(screen, light_gray, pygame.rect(50,600,10*progress,80)) pygame.display.update() else: pygame.draw.rect(self.screen, self.button_color_active, self.button_rect) self.screen.blit(self.label, (self.x + 20, self.y + 5)) def is_clicked(self, mouse_pos): return bool(self.button_rect.collidepoint(mouse_pos)) def set_new_color(self, active_color, inactive_color): self.button_color_active = active_color self.button_color_inactive = inactive_color button_start = button(screen, 50, 600, 400, 80, gray, light_gray, 'start', 'roboto-black', 50, white) while true: screen.fill(background_color) event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() #pygame.draw.rect(screen, gray, pygame.rect(50,600,400,80)) #pygame.draw.rect(screen, light_gray, pygame.rect(50,600,10*progress,80)) button_start.draw() pygame.display.flip() clock.tick(60) = str(input('shomething: '))
first need timer. can use dt
(delta time) pygame.clock.tick(fps)
returns increase time
variable. if mouse hovering on button, otherwise reset timer.
to calculate width of rect can (proportionality):
width = time * coefficient
here's minimal example:
import pygame pg pg.init() screen = pg.display.set_mode((640, 480)) clock = pg.time.clock() font = pg.font.font(none, 36) background_color = (237, 225, 192) light_gray = (120, 120, 120) gray = (30, 30, 30) # button variables. button_rect = pg.rect(50, 100, 200, 80) max_width = 200 # maximum width of rect. max_time = 4 # time after button should filled. # coefficient calculate width of rect given time. coefficient = max_width / max_time time = 0 dt = 0 done = false while not done: event in pg.event.get(): if event.type == pg.quit: done = true mouse_pos = pg.mouse.get_pos() if button_rect.collidepoint(mouse_pos): # if mouse on button, increase timer. if time < max_time: # stop increasing if max_time reached. time += dt if time >= max_time: time = max_time else: # if not colliding, reset time. time = 0 width = time * coefficient screen.fill(background_color) pg.draw.rect(screen, light_gray, (51, 100, width, 80)) pg.draw.rect(screen, gray, button_rect, 2) txt = font.render(str(round(time, 2)), true, gray) screen.blit(txt, (20, 20)) pg.display.flip() dt = clock.tick(60) / 1000 pg.quit()
Comments
Post a Comment