import pygame class Progress_Bar: """ Progress Bar: The constructor needs only a surface (which is mandatory) but can take 7 args more. Prototype: Progress_Bar(screen, [[[[[[font_type,] font_px,] incr,] space_x,] space_y,] col]) font_type : do you need any explanations? :), defaults to None font_px : font dimensions, it defaults to 35 font_col : RGB font color, defaults to [255,0,0], Red incr : initial increment of the progress bar, defaults to 0 space_x, space_y : X and Y padding between the surface and the progress bar, they defaults to 5 col : progress bar color, it defaults to [255,255,255], white Progress_Bar.inc(incr) increments the progress bar by a relative amount in percentage Progress_Bar.stop(text) Finalizes the speedbar with a text which appears on it Progress_Bar.clean() Cleans the progress while full and is handy at times when incrementing it by a stated amount doesn't fill it completely like when you increment by 30% for 3 times and the last one you have only 10%, with Progress_Bar.clean() you can have it full at once. If you go over 100% of the value of the Progress Bar it goes to the max and stays there. """ def __init__(self, screen, font_type = None, font_px = 35, font_col=[255,0,0], incr=0, space_x=5, space_y=5, col=[255,255,255]): self.screen = screen self.screen_x, self.screen_y = self.screen.get_size() self.top_y = space_x self.top_x = space_y self.font_type = font_type self.font_px = font_px self.font_col = font_col self.col = col self.progr_height = self.screen_y - (2*space_y) self.progr_percentage = 0 self.progr_max_width = self.screen_x - (2*space_x) print self.progr_max_width # Debug info self.progr_rect = pygame.Rect(self.top_x, self.top_y, self.progr_percentage, self.progr_height) self.progr = self.screen.subsurface(self.progr_rect) self.screen.blit(self.progr, (self.top_x, self.top_y)) pygame.display.flip() def inc(self, incr): try: self.progr_percentage += incr self.progr_width = round(self.progr_max_width/100.0) * self.progr_percentage print self.progr_width, self.progr_percentage , self.progr_max_width/100 # Debug info self.progr_rect = pygame.Rect(self.top_x, self.top_y, self.progr_width, self.progr_height) self.progr = self.screen.subsurface(self.progr_rect) self.progr.fill(self.col) self.screen.blit(self.progr, (self.top_x, self.top_y)) pygame.display.flip() except ValueError: print "End interval" # Debug info self.clean() return 0 return 1 def stop(self, text='Mi fermoooooo...'): self.text = text self.rendered_text = pygame.font.Font(self.font_type, self.font_px).render(self.text, 1, self.font_col) self.font_topx, self.font_topy = self.__center(self.rendered_text) self.screen.blit(self.rendered_text, (self.font_topx, self.font_topy)) pygame.display.flip() def __center(self, surf_rect): surf_rectX, surf_rectY = surf_rect.get_size() surf_topX = (self.screen_x - surf_rectX)/2 surf_topY = (self.screen_y - surf_rectY)/2 return surf_topX, surf_topY def clean(self): self.progr_width = self.screen_x - 2*self.top_x self.progr_rect = pygame.Rect(self.top_x, self.top_y, self.progr_width, self.progr_height) self.progr = self.screen.subsurface(self.progr_rect) self.progr.fill(self.col) self.screen.blit(self.progr, (self.top_x, self.top_y)) pygame.display.flip() if '__main__' == __name__: pygame.init() screen = pygame.display.set_mode((800,40)) progress_bar = Progress_Bar(screen) condition = 1 while condition: condition = progress_bar.inc(30) pygame.time.wait(100) progress_bar.stop() pygame.time.wait(500) progress_bar.clean() pygame.time.wait(500) progress_bar.stop() pygame.time.wait(1000)
Attachment:
orologio.tar.bz2
Description: Unix tar archive