def menu(): pygame.mixer.Sound.play(background_music, loops=-1) start_btn = Button(17, 17, action=main) settings_btn = Button(17, 17, action=settings) quit_btn = Button(17, 17, action=pygame.quit) achievements_btn = Button(6.5, 12, small_inactive_btn, small_active_btn, achievements) show = True while show: screen.blit(background, (0, 0)) head_width, head_height = get_message_size('Dungeon Master', head_font, 150) print_text('Dungeon Master', HALF_WIDTH - head_width // 2, round(WIDTH / 10.8), (176, 0, 0), head_font, 150) # 10.8 - очередной коэффициент start_btn.draw(50, 50, 'Играть', 40) settings_btn.draw(50, 68, 'Настройки', 40) quit_btn.draw(50, 86, 'Выход', 40) achievements_btn.draw(95, 90, 'Достижения', 15) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if pygame.mouse.get_focused(): screen.blit(change_cursor(main_cursor), pygame.mouse.get_pos()) pygame.display.flip() clock.tick(FPS) pygame.quit()
def draw(self, x, y, message, font_size=30): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() self.x, self.y = round(x / 100 * WIDTH) - round(self.width / 2), round( y / 100 * HEIGHT) - round(self.height / 2) # Проверка курсора на кнопке if self.x < mouse[0] < self.x + self.width and self.y < mouse[ 1] < self.y + self.height: screen.blit(self.active, (self.x, self.y)) if click[0] == 1: pygame.mixer.Sound.play(button_sound) pygame.time.delay(300) if self.action is not None: self.action() else: screen.blit(self.inactive, (self.x, self.y)) # Размер сообщения message_width, message_height = get_message_size( message, main_font, font_size) # Перенос сообщения на экран print_text(message, self.x + self.width // 2 - message_width // 2, self.y + self.height // 2 - message_height // 2, font_size=font_size)
def pause(): start_btn = Button(17, 17, action=main) settings_btn = Button(17, 17, action=settings) quit_btn = Button(17, 17, action=sys.exit) show = True while show: screen.blit(background, (0, 0)) head_width, head_height = get_message_size('Pause', head_font, 150) print_text('Pause', HALF_WIDTH - head_width // 2, round(WIDTH / 10.8), (176, 0, 0), head_font, 150) # 10.8 - очередной коэффициент # start_btn.draw(50, 50, 'Играть', 40) settings_btn.draw(50, 68, 'Настройки', 40) quit_btn.draw(50, 86, 'Выход', 40) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return if pygame.mouse.get_focused(): screen.blit(change_cursor(main_cursor), pygame.mouse.get_pos()) pygame.display.flip() clock.tick(FPS)
def show_text(self): from main import screen # Functions that are implemented in C do not have names for their arguments, # and you need to provide positional-only arguments. if self.is_game_over: # Functions that are implemented in C do not have names for their arguments, # and you need to provide positional-only arguments. game_over_text: pygame.Surface = self.game_over_font.render( 'GAME OVER - Score: ' + str(self.score), True, (255, 100, 100)) screen.blit(source=game_over_text, dest=(120, 200)) else: score_text: pygame.Surface = self.font.render( "Score: " + str(self.score), True, (255, 255, 255)) screen.blit(source=score_text, dest=(self.score_x_pos, self.score_y_pos))
def draw(self, x, y, message=None, font_size=30): # x и y задаются в процентах, font_size задаётся для разрешения экрана 1920x1080 x, y = round(x / 100 * WIDTH) - round(self.side / 2), round( y / 100 * HEIGHT) - round(self.side / 2) if self.isactive is True: screen.blit(self.active, (x, y)) else: screen.blit(self.inactive, (x, y)) mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x < mouse[0] < x + self.side and y < mouse[1] < y + self.side: if click[0] == 1: pygame.time.delay(100) if self.isactive is True: if self.action is not None: self.action(False) self.isactive = False screen.blit(self.inactive, (x, y)) else: if self.action is not None: self.action(True) self.isactive = True screen.blit(self.active, (x, y)) pygame.mixer.Sound.play(button_sound) if message is not None: message_height = get_message_size(message, main_font, font_size)[1] print_text(message, x + self.side + round(WIDTH / 38, 4), y + self.side // 2 - message_height // 2, font_size=font_size)
def achievements(): back_btn = Button(13, 13) back_btn.draw(8, 8, 'Назад') while not back_btn.is_clicked(): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((100, 100, 100)) print_text( f'Счёт предыдущей игры: {result["last-score"]}', HALF_WIDTH - get_message_size(f'Счёт предыдущей игры: {result["last-score"]}', main_font, 60)[0] // 2, 200, font_type=main_font, font_size=60) print_text(f'Максимальное кол-во очков: {result["record-score"]}', HALF_WIDTH - get_message_size( f'Максимальное кол-во очков: {result["record-score"]}', main_font, 60)[0] // 2, 300, font_type=main_font, font_size=60) print_text(f'Общее количество убийств: {result["total-kills"]}', HALF_WIDTH - get_message_size( f'Общее количество убийств: {result["total-kills"]}', main_font, 60)[0] // 2, 400, font_type=main_font, font_size=60) back_btn.draw(8, 8, 'Назад') if pygame.mouse.get_focused(): screen.blit(change_cursor(main_cursor), pygame.mouse.get_pos()) pygame.display.flip() clock.tick(FPS) return
def settings(): off_music = Checkbox(10, action=music_off, isactive=False) off_sound = Checkbox(10, action=sounds_off, isactive=False) back_btn = Button(13, 13) back_btn.draw(8, 8, 'Назад') while not back_btn.is_clicked(): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((100, 100, 100)) off_music.draw(8, 25, 'Отключить музыку', 45) off_sound.draw(8, 45, 'Отключить звук', 45) back_btn.draw(8, 8, 'Назад') if pygame.mouse.get_focused(): screen.blit(change_cursor(main_cursor), pygame.mouse.get_pos()) pygame.display.flip() clock.tick(FPS) return
def setup(): from main import screen # Delete old game objects GOL.del_golist() # Add new player, base, hp bars and reset levels GOL.add_go(Player()) GOL.add_go(Base()) GOL.add_go(PlayerInteface()) LM.reset() gamestart = False while not gamestart: # Start screen, fill and text screen.fill((0, 250, 0)) screen.blit(space, (0, 0)) pygame.font.init() font = pygame.font.SysFont("Tahoma MS", size=60) title = font.render("KILL ALL THE ALIENS!", True, (255, 215, 0)) rect = title.get_rect() rect.centerx = screen.get_rect().centerx rect.centery = screen.get_rect().top + 100 screen.blit(title, rect) font = pygame.font.SysFont("Arial MS", size=18) title = font.render("PRESS ANY KEY TO START", True, (250, 250, 0)) rect = title.get_rect() rect.centerx = screen.get_rect().centerx rect.centery = screen.get_rect().top + 400 screen.blit(title, rect) pygame.display.update() clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: gamestart = True
def gameover(): from main import screen pygame.font.init() font = pygame.font.SysFont("Arial MS", 50, bold=True) text_surface = font.render("GAME OVER", False, (250, 250, 0)) screen.blit(text_surface, (500, 300))
def render(self): screen.blit(self.bitmap, (self.x, self.y))