def show_message(self, text, box=False): """ Uses pygame to puts text on the screen. Optionally, the size and color of the message can be provided. The text will be centered. If box is false, text will be a single line, otherwise it will be presented in a textbox """ if not box: self.screen.blit(self.background, self.background_rect) font = pygame.font.Font(None, self.textsize) textimage = font.render(text, True, self.textcolor) textrect = textimage.get_rect(center=(self.screenWidth / 2, self.screenHeight / 2)) self.screen.blit(textimage, textrect) pygame.display.flip() else: edgecolor = 100, 100, 255 boxsize = (500, 300) textbox = Textbox(pos=(self.screenWidth / 2, self.screenHeight / 2), text=text, textsize=self.textsize, size=boxsize, edgecolor=edgecolor) textbox.refresh() self.screen.blit(self.background, self.background_rect) self.screen.blit(textbox.image, textbox.rect) pygame.display.flip()
def show_message(self, text, box=False): """ Uses pygame to puts text on the screen. Optionally, the size and color of the message can be provided. The text will be centered. If box is false, text will be a single line, otherwise it will be presented in a textbox """ if not box: self.screen.blit(self.background, self.background_rect) font = pygame.font.Font(None, self.textsize) textimage = font.render(text, True, self.textcolor); textrect = textimage.get_rect(center=(self.screenWidth / 2, self.screenHeight / 2)) self.screen.blit(textimage, textrect) pygame.display.flip() else: edgecolor = 100, 100, 255 boxsize = (500, 300) textbox = Textbox(pos=(self.screenWidth / 2, self.screenHeight / 2), text=text, textsize=self.textsize, size=boxsize, edgecolor=edgecolor) textbox.refresh() self.screen.blit(self.background, self.background_rect) self.screen.blit(textbox.image, textbox.rect) pygame.display.flip()
def fadeout_message(self, text, box=False, opts=None): """ A fancy fading-out message. You can provide options in form of a dictionary: * "show_time" : how long the text is displayed before fadeout * "fadeout_time" : how long the fadeout takes * "wait_for_key" : waits for key-press before fadeout (in this case, the value in show_time is not used) * "textsize", "textcolor" - size and color of text """ back, TRANSPARENT = None, (1, 2, 3) show_time, fadeout_time, wait_for_key = 1500, 500, False textsize = self.textsize textcolor = self.textcolor if opts is not None: #if opts.has_key("show_time"): show_time = opts["show_time"] if "show_time" in opts: show_time = opts["show_time"] #if opts.has_key("fadeout_time"): fadeout_time = opts["fadeout_time"] if "fadeout_time" in opts: fadeout_time = opts["fadeout_time"] #if opts.has_key("wait_for_key"): wait_for_key = opts["wait_for_key"] if "wait_for_key" in opts: wait_for_key = opts["wait_for_key"] #if opts.has_key("textsize"): textsize = opts["textsize"] if "textsize" in opts: textsize = opts["textsize"] #if opts.has_key("textcolor"): textcolor = opts["textcolor"] if "textcolor" in opts: textcolor = opts["textcolor"] textimage, textrect = None, None if not box: font = pygame.font.Font(None, textsize) textimage = font.render(text, True, textcolor) textrect = textimage.get_rect(center=(self.screenWidth / 2, self.screenHeight / 2)) else: edgecolor = 100, 100, 255 boxsize = (500, 300) textbox = Textbox(pos=(self.screenWidth / 2, self.screenHeight / 2), text=text, textsize=textsize, size=boxsize, edgecolor=edgecolor) textbox.refresh() textimage = textbox.image textrect = textbox.rect # Make a transparent back upon which the text is set (otherwise transparency fails) back = pygame.Surface((textrect.width, textrect.height)) back.fill(TRANSPARENT) back.set_colorkey(TRANSPARENT) back.blit(textimage, (0, 0, textrect.width, textrect.height)) self.screen.blit(self.background, [0, 0]) self.screen.blit(back, textrect) pygame.display.flip() if wait_for_key: self.wait_for_key() else: pygame.time.delay(show_time) current_time = pygame.time.get_ticks() end_time = current_time + fadeout_time # Start fading out in 50 ms steps while current_time < end_time: fade = 255 * (end_time - current_time) / fadeout_time # Fading value 255 - 0 back.set_alpha(round(fade)) #bef = pygame.time.get_ticks() self.screen.blit(self.background, [0, 0]) #aft = pygame.time.get_ticks() #print "Time :",str(aft-bef) self.screen.blit(back, textrect) pygame.display.flip() pygame.time.delay(50) current_time = pygame.time.get_ticks() self.screen.blit(self.background, [0, 0]) pygame.display.flip()
def fadeout_message(self, text, box=False, opts=None): """ A fancy fading-out message. You can provide options in form of a dictionary: * "show_time" : how long the text is displayed before fadeout * "fadeout_time" : how long the fadeout takes * "wait_for_key" : waits for key-press before fadeout (in this case, the value in show_time is not used) * "textsize", "textcolor" - size and color of text """ back, TRANSPARENT = None, (1, 2, 3) show_time, fadeout_time, wait_for_key = 1500, 500, False textsize = self.textsize textcolor = self.textcolor if opts is not None: if opts.has_key("show_time"): show_time = opts["show_time"] if opts.has_key("fadeout_time"): fadeout_time = opts["fadeout_time"] if opts.has_key("wait_for_key"): wait_for_key = opts["wait_for_key"] if opts.has_key("textsize"): textsize = opts["textsize"] if opts.has_key("textcolor"): textcolor = opts["textcolor"] textimage, textrect = None, None if not box: font = pygame.font.Font(None, textsize) textimage = font.render(text, True, textcolor); textrect = textimage.get_rect(center=(self.screenWidth / 2, self.screenHeight / 2)) else: edgecolor = 100, 100, 255 boxsize = (500, 300) textbox = Textbox(pos=(self.screenWidth / 2, self.screenHeight / 2), text=text, textsize=textsize, size=boxsize, edgecolor=edgecolor) textbox.refresh() textimage = textbox.image textrect = textbox.rect # Make a transparent back upon which the text is set (otherwise transparency fails) back = pygame.Surface((textrect.width, textrect.height)) back.fill(TRANSPARENT) back.set_colorkey(TRANSPARENT) back.blit(textimage, (0, 0, textrect.width, textrect.height)) self.screen.blit(self.background, [0, 0]) self.screen.blit(back, textrect) pygame.display.flip() if wait_for_key: self.wait_for_key() else: pygame.time.delay(show_time) current_time = pygame.time.get_ticks() end_time = current_time + fadeout_time # Start fading out in 50 ms steps while current_time < end_time: fade = 255 * (end_time - current_time) / fadeout_time # Fading value 255 - 0 back.set_alpha(round(fade)) #bef = pygame.time.get_ticks() self.screen.blit(self.background, [0, 0]) #aft = pygame.time.get_ticks() #print "Time :",str(aft-bef) self.screen.blit(back, textrect) pygame.display.flip() pygame.time.delay(50) current_time = pygame.time.get_ticks() self.screen.blit(self.background, [0, 0]) pygame.display.flip()