def r_w_leaderboard(screen, font, font_text, rw=None, name=None, score=None): running = True write_flag = 0 while running: # read from leaderboard.txt and blit on the screen if rw == "r": screen.fill((255, 255, 255)) draw_text.draw_text('Leaderboard', font, (0, 0, 0), screen, 20, 20) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False f = open("leaderboard.txt") lines = f.readlines() y_position = 50 for line in lines: draw_text.draw_text(line[:-1], font_text, (0, 0, 0), screen, 20, y_position) y_position = y_position + 25 f.close() # write into leaderboard.txt elif rw == "w" and write_flag == 0: f = open("leaderboard.txt", 'a') f.write(str(name) + " score:" + str(score) + '\n') f.close() return 1 pygame.display.update() mainClock.tick(25)
def main_menu(): while True: screen.fill(WHITE) draw_text.draw_text('Tetris game with AI', font_title, BLACK, screen, 70, 20) button_1 = button.Button(100, 100, 200, 50, 40, text='Start game') button_2 = button.Button(100, 200, 200, 50, 60, text='AI game') button_3 = button.Button(100, 300, 200, 50, 35, text='Leaderboard') button_4 = button.Button(100, 400, 200, 50, 50, text='End game') button_boxes = [button_1, button_2, button_3, button_4] for each in button_boxes: each.draw(screen) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit() for each in button_boxes: each.handle_event(event) if button_1.click: game_by_player(screen) if button_2.click: game_by_AI() if button_3.click: show_leaderboard(screen, font_title, font_text) if button_4.click: pygame.quit() sys.exit() pygame.display.update() mainClock.tick(25)
def predict(test_img, face_recognizer): # 将标签1,2转换成文字 subjects = ['', 'Happy', 'Sad'] # 得到图像副本 img = test_img.copy() # 从图像中检测脸部 face, rect = detect_face(img) # 使用我们的脸部识别器预测图像 label = face_recognizer.predict(face) # 获取由人脸识别器返回的相应标签的名称 label_text = subjects[label[0]] # 注意,下面两个函数我们还未编写!!! # 在检测到的脸部周围画一个矩形 draw_rectangle(img, rect) # 在矩形周围标出人脸情绪 draw_text(img, label_text, rect[0], rect[1] - 5) return img
def custom_text(surface): global last_game_turn if last_game_turn > cur_turn and not Game.redraw_sidebar: return if not Game.sidebar_enabled: return s="Now that's what i call text." * 100 s += "Turn {cur_turn}".format(cur_turn=cur_turn) wrapped_text = textwrap.wrap(s, 80) color = (255,255,255) tile_size = 16 sidebar_start_loc = (Game.game_width) * tile_size + 3 y_px_loc = 0 for line in wrapped_text: draw_text.draw_text(line, surface, sidebar_start_loc, y_px_loc, color=color) y_px_loc += 16 #libtcod.console_set_dirty(Game.game_width, 0, Game.game_width - Game.screen_width, Game.game_height) #libtcod.console_set_dirty(sidebar_start_loc, y_px_loc + tile_size, 30 * 16, len(line) * 16) if Game.redraw_sidebar: Game.redraw_sidebar = False else: last_game_turn += 1
def render(surface): draw_text.draw_text("it works!", surface, 25, 25)