def render(self, screen: pygame.Surface): width, height = pygame.display.get_surface().get_size() screen.fill(BGCOLOR) text_surf, text_rect = render_text(f"Highscores", resources.get_font("round50"), WHITE) text_rect.centerx, text_rect.y = width // 2, 50 screen.blit(text_surf, text_rect) # Display highscore list highscores = settings.get_highscores() for i, highscore in enumerate(highscores): color = (200 - i * 30, 200 - i * 20, 200 - i * 30) text = (f"{i+1} ___ {highscore['name']:>3} _____ " f"{highscore['score']:3} _____ {highscore['date']} ") text_surf, text_rect = render_text(text, resources.get_font("mono30"), color) text_rect.x, text_rect.y = 75, 150 + i * 50 screen.blit(text_surf, text_rect) text = "Return to Main Menu" text_surf, text_rect = render_text(text, resources.get_font("round40"), APPLE_COLOR) text_rect.centerx, text_rect.y = width // 2, height - 120 screen.blit(text_surf, text_rect)
def render(self, screen: pygame.Surface): width = pygame.display.get_surface().get_width() screen.fill(BGCOLOR) font = resources.get_font("round50") text_surf, text_rect = render_text("Settings", font, WHITE) text_rect.centerx, text_rect.y = width // 2, 50 screen.blit(text_surf, text_rect) pos_y = 180 for i, option in enumerate(self.options): # Leave more space for last option if i == len(self.options) - 1: pos_y += 60 if i == self.index: # Selected color = APPLE_COLOR font = resources.get_font("round40") else: # Inactive color = WHITE font = resources.get_font("round30") text_surf, text_rect = render_text(option, font, color) text_rect.x, text_rect.y = (150, pos_y + 60 * i) screen.blit(text_surf, text_rect) # Sound slider self.sound_slider.percent = self.sound self.sound_slider.rect.x = 450 self.sound_slider.rect.y = 180 if self.index == 0: self.sound_slider.color = APPLE_COLOR else: self.sound_slider.color = BGCOLOR self.sound_slider.draw(screen) # Music slider self.music_slider.percent = self.music self.music_slider.rect.x = 450 self.music_slider.rect.y = 240 if self.index == 1: self.music_slider.color = APPLE_COLOR else: self.music_slider.color = BGCOLOR self.music_slider.draw(screen) # Graphics font = resources.get_font("round30") if self.classic: text_surf, text_rect = render_text("Classic /", font, APPLE_COLOR) text_rect.x, text_rect.y = 465, 300 else: text_surf, text_rect = render_text("/ Modern", font, APPLE_COLOR) text_rect.x, text_rect.y = 564, 300 screen.blit(text_surf, text_rect)
def render(self, canvas): canvas.fill(NOT_SO_WHITE) if self.instructions: font = resources.get_font('prstartcustom.otf') font_renderer = pygame.font.Font(self.font, 10) font_renderer_large = pygame.font.Font(self.font, 18) fight_surface = font_renderer_large.render("PREPARE TO FIGHT.", True, NOT_SO_BLACK) instructions_player1_1_surface = font_renderer.render("USE THE UP & DOWN ARROW KEYS", True, NOT_SO_BLACK) instructions_player1_2_surface = font_renderer.render("USE LEFT ARROW KEY TO DASH", True, NOT_SO_BLACK) instructions_player2_1_surface = font_renderer.render("USE W & S", True, NOT_SO_BLACK) instructions_player2_2_surface = font_renderer.render("USE D TO DASH", True, NOT_SO_BLACK) goodluck_surface = font_renderer_large.render("GOOD LUCK.", True, NOT_SO_BLACK) canvas.blit(fight_surface, (GAME_WIDTH/2 - fight_surface.get_width()/2, GAME_HEIGHT/4)) canvas.blit(instructions_player1_1_surface, (2.57*GAME_WIDTH/4 - instructions_player1_1_surface.get_width()/2, GAME_HEIGHT/2 - instructions_player1_1_surface.get_height()/2 - 10)) canvas.blit(instructions_player1_2_surface, (2.57*GAME_WIDTH/4 - instructions_player1_2_surface.get_width()/2, GAME_HEIGHT/2 + 10)) canvas.blit(instructions_player2_1_surface, (GAME_WIDTH/4 - instructions_player2_1_surface.get_width()/2, GAME_HEIGHT/2 - instructions_player1_2_surface.get_height()/2 - 10)) canvas.blit(instructions_player2_2_surface, (GAME_WIDTH/4 - instructions_player2_2_surface.get_width()/2, GAME_HEIGHT/2 + 10)) canvas.blit(goodluck_surface, (GAME_WIDTH/2 - goodluck_surface.get_width()/2, 3*GAME_HEIGHT/4)) return elif self.winner is not None: if self.winner is self.player1: winner = "PLAYER ONE (RIGHT)" else: winner = "PLAYER TWO (LEFT)" font = resources.get_font('prstartcustom.otf') font_renderer = pygame.font.Font(self.font, 18) end_surface = font_renderer.render(winner + " IS VICTORIOUS", True, NOT_SO_BLACK) canvas.blit(end_surface, (GAME_WIDTH/2 - end_surface.get_width()/2, GAME_HEIGHT/2 - end_surface.get_height()/2)) return self.player1.render(canvas) self.player2.render(canvas) for b in self.balls: b.render(canvas) for p in self.powerups: p.render(canvas) # Render player 1 Text self.player1_lives_surface = self.font_renderer.render(str(self.player1.lives), True, NOT_SO_BLACK) self.player1_charge_surface = self.font_renderer.render(str(100*self.player1.pad.charge/PAD_MAX_CHARGE) + '%', True, NOT_SO_BLACK) canvas.blit(self.player1_lives_label_surface, (GAME_WIDTH - 20 - self.player1_lives_surface.get_width() - self.player1_lives_label_surface.get_width(), 15)) canvas.blit(self.player1_lives_surface, (GAME_WIDTH - 20 - self.player1_lives_surface.get_width(), 15)) canvas.blit(self.player1_charge_label_surface, (GAME_WIDTH - 20 - self.player1_charge_surface.get_width() - self.player1_charge_label_surface.get_width(), 35)) canvas.blit(self.player1_charge_surface, (GAME_WIDTH - 20 - self.player1_charge_surface.get_width(), 35)) # Render player 2 Text self.player2_lives_surface = self.font_renderer.render(str(self.player2.lives), True, NOT_SO_BLACK) self.player2_charge_surface = self.font_renderer.render(str(100*self.player2.pad.charge/PAD_MAX_CHARGE) + '%', True, NOT_SO_BLACK) canvas.blit(self.player2_lives_label_surface, (20, 15)) canvas.blit(self.player2_lives_surface, (20 + self.player2_lives_label_surface.get_width(), 15)) canvas.blit(self.player2_charge_label_surface, (20, 35)) canvas.blit(self.player2_charge_surface, (20 + self.player2_charge_label_surface.get_width(), 35))
def __init__(self, caption, new_state): self.caption = caption self.text_normal = resources.get_font(40).render( caption, True, (128, 255, 128)) self.text_highlight = resources.get_font(50).render( caption, True, (255, 255, 255)) self.text_surface = self.text_normal self.new_state = new_state self.rect = self.text_surface.get_rect()
def render(self, screen): width, height = pygame.display.get_surface().get_size() screen.fill(BGCOLOR) font = resources.get_font("title80") rd_text, rd_rect = render_wrapped_text("Thanks for playing", font, WHITE, True, 10, width - 50) rd_rect.centerx, rd_rect.centery = width // 2, height // 2 - 20 screen.blit(rd_text, rd_rect) font = resources.get_font("normal30") rd_text, rd_rect = render_text("A game by @Alucebur", font, WHITE) rd_rect.x, rd_rect.y = 30, height - 40 screen.blit(rd_text, rd_rect)
def show(self): font = resources.get_font('prstartcustom.otf') # Make title font_renderer = pygame.font.Font(font, 15) self.title_surface = font_renderer.render('Hi-scores', True, NOT_SO_BLACK) # Make all scores # Get the score with highest width max_width_score = max(self.scores, key=self.score_width) # Calculate its width, and add 4 dots max_width = self.score_width(max_width_score) + 4 font_renderer = pygame.font.Font(font, 12) for score in self.scores: self.scores_surfaces.append( font_renderer.render( score.name + '.' * (max_width - self.score_width(score)) + str(score.score), True, NOT_SO_BLACK ) ) # Make the back option self.back_options.init(font, 15, True, NOT_SO_BLACK) # Load all sounds self.select_sound = Sound(resources.get_sound('menu_select.wav'))
def show(self): # Start the music pygame.mixer.music.load(resources.get_music('whatislove.ogg')) pygame.mixer.music.play(-1) # Get font name font = resources.get_font('prstartcustom.otf') # Make Hi-score and rights font_renderer = pygame.font.Font(font, 12) self.hiscore_label_surface = font_renderer.render('Hi-score', True, NOT_SO_BLACK) self.hiscore_surface = font_renderer.render(self.hiscore, True, NOT_SO_BLACK) self.rights_surface = font_renderer.render(self.rights, True, NOT_SO_BLACK) # Make title font_renderer = pygame.font.Font(font, 36) self.title_surface = font_renderer.render(GAME_TITLE, False, NOT_SO_BLACK) # Make all options and change to the main menu self.play_menu_options.init(font, 15, True, NOT_SO_BLACK) self.main_menu_options.init(font, 15, True, NOT_SO_BLACK) self.change_menu_options(self.main_menu_options) # Load all sounds self.select_sound = Sound(resources.get_sound('menu_select.wav'))
def show(self): # Initialize options font = resources.get_font('prstartcustom.otf') self.options.init(font, 15, True, MORE_WHITE) # Initialize sounds self.select_sound = Sound(resources.get_sound('menu_select.wav'))
def init(self): # Initialize super super(MainMenu, self).init() # Initialize the menu font = resources.get_font('prstartcustom.otf') self.menu_options.init(font, 15, True, MORE_WHITE)
def draw(self): # Create the fading background effect screen_rect = self.screen.get_rect() filler = pygame.Surface((screen_rect.width, screen_rect.height), pygame.SRCALPHA, 32) filler.fill((100, 100, 100, 10)) self.screen.blit(filler, (0, 0)) text = resources.get_font(50).render("Game Over!", True, (0, 200, 0)) textpos = text.get_rect(center=self.screen.get_rect().center) self.screen.blit(text, textpos) text = resources.get_font(20).render("Press any key to continue", True, (0, 200, 0)) textpos = text.get_rect(center=self.screen.get_rect().center) textpos.y += 300 self.screen.blit(text, textpos)
def render(self, screen): width = pygame.display.get_surface().get_width() screen.fill(BGCOLOR) # Draw background self.background.draw(screen) # Semitransparent surface behind title overlay = get_surface((width, 140), WHITE, 120) overlay_rect = overlay.get_rect() overlay_rect.centerx, overlay_rect.y = width // 2, 50 screen.blit(overlay, overlay_rect) # Title font = resources.get_font("title175") rd_text, rd_rect = render_text("SNAKE", font, BLACK) rd_rect.centerx, rd_rect.y = width // 2, 60 screen.blit(rd_text, rd_rect) pos_y = 280 # Semitransparent surface behind selected option overlay = get_surface((width, 50), BLACK, 150) overlay_rect = overlay.get_rect() overlay_rect.centerx = width // 2 overlay_rect.centery = pos_y + 60 * self.index + 60 * ( self.index == len(self.options) - 1) screen.blit(overlay, overlay_rect) for i, option in enumerate(self.options): # leave more space for last option 'Quit' if i == len(self.options) - 1: pos_y += 60 if i == self.index: # Selected color = APPLE_COLOR font = resources.get_font("round50") else: # Inactive color = BLACK font = resources.get_font("round40") rd_text, rd_rect = render_text(option[0], font, color) rd_rect.centerx, rd_rect.centery = (width // 2, pos_y + 60 * i) screen.blit(rd_text, rd_rect)
def render(self, screen: pygame.Surface): width, height = pygame.display.get_surface().get_size() # Display gameover message screen.fill(BGCOLOR) text_surf, text_rect = render_text("YOU LOST", resources.get_font("title100"), WHITE) text_rect.centerx, text_rect.y = width // 2, 110 screen.blit(text_surf, text_rect) text_surf, text_rect = render_text(f"Your score was {self.score}", resources.get_font("round30"), WHITE) text_rect.centerx, text_rect.y = width // 2, 270 screen.blit(text_surf, text_rect) if not self.record: accept = pygame.key.name(settings.get_key("accept")) finish = pygame.key.name(settings.get_key("pause")) text = (f"({accept.upper()} to replay," f" {finish.upper()} to exit menu)") text_surf, text_rect = render_text(text, resources.get_font("round30"), WHITE) text_rect.centerx, text_rect.y = width // 2, 325 screen.blit(text_surf, text_rect) else: # Highscore message text_surf, text_rect = render_text("New record!", resources.get_font("round30"), WHITE) text_rect.centerx, text_rect.y = width // 2, 345 screen.blit(text_surf, text_rect) text_surf, text_rect = render_text("Enter your initials: ", resources.get_font("round30"), WHITE) text_rect.centerx, text_rect.y = width // 2 - 58, 420 screen.blit(text_surf, text_rect) # Textbox input_box = pygame.Rect(width // 2 + 82, 415, 100, 35) screen.fill(BLACK, input_box) resources.get_font("round30").render_to( screen, (input_box.x + 15, input_box.y + 5), self.initials, WHITE) # Pun text_surf, text_rect = render_wrapped_text( self.joke, resources.get_font("normal25"), WHITE, True, 10, width - 150) text_rect.centerx, text_rect.y = width // 2, height - 130 screen.blit(text_surf, text_rect)
def render(self, screen): width, height = pygame.display.get_surface().get_size() if not self.is_paused and (not self.has_crashed or (self.has_crashed and not self.event_painted)): # Draw background if not settings.get_setting("classic"): screen.blit(self.background, (0, 0)) else: # Classic look screen.fill(BGCOLOR) # Draw snake, apple, grid self.sneik.draw(screen) self.apple.draw(screen) if self.show_grid: self.draw_grid(screen) if self.has_crashed: self.event_painted = True elif self.has_crashed: # Add snake blood self.sneik.draw_blood(screen) # Paint pause screen once elif not self.event_painted: # Darken the screen surf = get_surface((width, height), BLACK, 160) screen.blit(surf, (0, 0)) # Show pause message font = resources.get_font("title100") text_surf, text_rect = render_text("Paused", font, WHITE) text_rect.center = width // 2, 250 screen.blit(text_surf, text_rect) font = resources.get_font("round30") text_surf, text_rect = render_text( f"Score: {len(self.sneik.body) - 2}", font, WHITE) text_rect.center = width // 2, 330 screen.blit(text_surf, text_rect) self.event_painted = True
def draw(self): # self.screen.fill((0,0,0)) self.background.draw(self.screen) text = resources.get_font(60).render("Begemmed!", True, (128, 255, 128)) textpos = text.get_rect(center=self.screen.get_rect().center) textpos.y = 100 self.screen.blit(text, textpos) text = resources.get_font(35).render("by Oliver Lomax", True, (128, 255, 128)) # textpos = text.get_rect(center = self.screen.get_rect().center) textpos.y += 100 self.screen.blit(text, textpos) text = resources.get_font(25).render("Music by Visager", True, (128, 255, 128)) textpos = text.get_rect(center=self.screen.get_rect().center) textpos.y += 300 self.screen.blit(text, textpos) # Draw the menu items self.menu.draw(self.screen)
def draw(self): # Draw the screen self.screen.fill((0, 0, 0)) self.screen.blit(self.background, (0, 0)) self.sprites.draw(self.screen) # Put the score on the screen below the grid text = resources.get_font(40).render("Score: " + str(self.score), True, (128, 128, 128)) textpos = text.get_rect() textpos.left = self.xOffset textpos.top = self.yOffset * 2 + self.size * 64 self.screen.blit(text, textpos)
def draw(surf, text, font, pos, color=color.WHITE, halign='left', valign='bottom'): if not isinstance(font, pygame.font.Font): font = resources.get_font(font) text_surf = font.render(text, False, color) text_rect = text_surf.get_rect() text_rect.left = pos[0] if halign == halign_center: text_rect.centerx = pos[0] elif halign == halign_right: text_rect.right = pos[0] text_rect.top = pos[1] if valign == valign_center: text_rect.centery = pos[1] elif valign == valign_bottom: text_rect.bottom = pos[1] surf.blit(text_surf, text_rect)
def make_image(self): if self.entity.style: style = self.entity.style else: style = DEFAULT_STYLE font_name = style["font_name"] size = style["font_size"] bold = style.get("bold", False) italic = style.get("italic", False) color = style["font_color"] buffer = style["text_buffer"] cutoff = style.get("text_cutoff", None) nl = style.get("text_newline", True) font = get_font( font_name, size, bold, italic) image = self.make_text_image( self.text, font, color, buffer, cutoff=cutoff, nl=nl) return image
def show(self): # Start the music pygame.mixer.music.load(resources.get_music('mortalkombat.ogg')) pygame.mixer.music.play(-1) self.font = resources.get_font('prstartcustom.otf') self.font_renderer = pygame.font.Font(self.font, 12) # Player 1 Text self.player1_lives_label_surface = self.font_renderer.render('Lives: ', True, NOT_SO_BLACK) self.player1_lives_surface = self.font_renderer.render(str(self.player1.lives), True, NOT_SO_BLACK) self.player1_charge_label_surface = self.font_renderer.render('Charge: ', True, NOT_SO_BLACK) self.player1_charge_surface = self.font_renderer.render(str(self.player1.pad.charge), True, NOT_SO_BLACK) # Player 2 Text self.player2_lives_label_surface = self.font_renderer.render('Lives: ', True, NOT_SO_BLACK) self.player2_lives_surface = self.font_renderer.render(str(self.player2.lives), True, NOT_SO_BLACK) self.player2_charge_label_surface = self.font_renderer.render('Charge: ', True, NOT_SO_BLACK) self.player2_charge_surface = self.font_renderer.render(str(self.player2.pad.charge), True, NOT_SO_BLACK) # Initialize the sounds self.wall_hit_sound = Sound(resources.get_sound('on_wall_hit.wav')) self.pad_hit_sound = Sound(resources.get_sound('on_pad_hit.wav')) self.powerup_sound = Sound(resources.get_sound('powerup.wav'))
def init(self): # Initialize super super(BallSettingsMenu, self).init() font = resources.get_font('prstartcustom.otf') # Initialize all sliders self.speed_limit_slider.init(300, 7, MORE_WHITE, MORE_WHITE) self.speed_multiplier_slider.init(300, 7, MORE_WHITE, MORE_WHITE) self.radius_slider.init(300, 7, MORE_WHITE, MORE_WHITE) # Initialize back option self.options.init(font, 15, True, MORE_WHITE) # Initialize surfaces font_renderer = pygame.font.Font(font, 15) self.speed_limit_label_surface = font_renderer.render('Speed limit:', True, MORE_WHITE) self.speed_multiplier_label_surface = font_renderer.render('Speed multiplier:', True, MORE_WHITE) self.radius_surface = font_renderer.render('Radius:', True, MORE_WHITE) # Update the sliders tuple self.sliders = ( (self.speed_limit_label_surface, self.speed_limit_slider), (self.speed_multiplier_label_surface, self.speed_multiplier_slider), (self.radius_surface, self.radius_slider) )
def init(self): # Initialize super super(PowerUpSettingsMenu, self).init() font = resources.get_font('prstartcustom.otf') # Initialize all sliders self.count_slider.init(300, 7, MORE_WHITE, MORE_WHITE) self.probability_slider.init(300, 7, MORE_WHITE, MORE_WHITE) self.size_slider.init(300, 7, MORE_WHITE, MORE_WHITE) # Initialize back option self.options.init(font, 15, True, MORE_WHITE) # Initialize surfaces font_renderer = pygame.font.Font(font, 15) self.count_label_surface = font_renderer.render('PowerUp Max Count:', True, MORE_WHITE) self.probability_label_surface = font_renderer.render('PowerUp Rarity:', True, MORE_WHITE) self.size_label_surface = font_renderer.render('PowerUp Size:', True, MORE_WHITE) # Update the sliders tuple self.sliders = ( (self.count_label_surface, self.count_slider), (self.probability_label_surface, self.probability_slider), (self.size_label_surface, self.size_slider) )
def init(self): # Initialize super super(PadSettingsMenu, self).init() font = resources.get_font('prstartcustom.otf') # Initialize all sliders self.speed_slider.init(300, 7, MORE_WHITE, MORE_WHITE) self.height_slider.init(300, 7, MORE_WHITE, MORE_WHITE) self.charge_rate_slider.init(300, 7, MORE_WHITE, MORE_WHITE) # Initialize back option self.options.init(font, 15, True, MORE_WHITE) # Initialize surfaces font_renderer = pygame.font.Font(font, 15) self.speed_label_surface = font_renderer.render('Speed:', True, MORE_WHITE) self.height_label_surface = font_renderer.render('Height:', True, MORE_WHITE) self.charge_rate_surface = font_renderer.render('Charging rate:', True, MORE_WHITE) # Update the sliders tuple self.sliders = ( (self.speed_label_surface, self.speed_slider), (self.height_label_surface, self.height_slider), (self.charge_rate_surface, self.charge_rate_slider) )
def render(self, screen: pygame.Surface): width = pygame.display.get_surface().get_width() screen.fill(BGCOLOR) text_surf, text_rect = render_text(f"Change Controls", resources.get_font("round50"), WHITE) text_rect.centerx, text_rect.y = width // 2, 50 screen.blit(text_surf, text_rect) # Display key list pos_y = 150 for i, option in enumerate(self.options): # Leave more space for last option if i == len(self.options) - 1: pos_y += 45 if i == self.index: # Selected color = APPLE_COLOR font = resources.get_font("round40") else: # Inactive color = WHITE font = resources.get_font("round30") text_surf, text_rect = render_text(option, font, color) text_rect.x, text_rect.y = (150, pos_y + 45 * i) screen.blit(text_surf, text_rect) # Display assigned key pos_y = 150 font = resources.get_font("round30") for i, option in enumerate(self.options[:-1]): action = option.lower() if 0 <= i <= 3: # Direction key key_par = { v: k for k, v in self.keys['direction'].items() if v == action } key_name = pygame.key.name(int(key_par[action])) else: # Action key key_name = pygame.key.name(self.keys[action]) # Changing key bind if self.changing and i == self.index: text_surf, text_rect = render_text("Press a key", font, APPLE_COLOR) text_rect.x, text_rect.y = (550, pos_y + 45 * i) screen.blit(text_surf, text_rect) else: # Display key name text_surf, text_rect = render_text( "- - - - - - - - - - - - - - " + key_name, font, WHITE) text_rect.x, text_rect.y = (270, pos_y + 45 * i) screen.blit(text_surf, text_rect)