python开发打地鼠7 #
- 添加开始界面
- 添加结束界面
完成效果 #
显示本节课完成的效果图
收获 #
掌握python类的使用方法,通过PyGame实践学会独立建立和更新项目,完成打地鼠游戏开发。
添加开始界面 #
本小节介绍开始界面的创建。
def startInterface(screen, begin_images): begin_image = begin_images[0] while True: for event in pygame.event.get(): if event.type == pygame.QUIT: QuitGame() elif event.type == pygame.MOUSEMOTION: mouse_pos = pygame.mouse.get_pos() if mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)): begin_image = begin_images[1] else: begin_image = begin_images[0] elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if event.button == 1 and mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)): return True screen.blit(begin_image, (0, 0)) pygame.display.update()
添加结束界面 #
本小节介绍结束界面的创建。
def endInterface(screen, end_image, again_images, score_info, font_path, font_colors, screensize): again_image = again_images[0] font = pygame.font.Font(font_path, 50) your_score_text = font.render('Your Score: %s' % score_info['your_score'], True, font_colors[0]) your_score_rect = your_score_text.get_rect() your_score_rect.left, your_score_rect.top = (screensize[0] - your_score_rect.width) / 2, 215 best_score_text = font.render('Best Score: %s' % score_info['best_score'], True, font_colors[1]) best_score_rect = best_score_text.get_rect() best_score_rect.left, best_score_rect.top = (screensize[0] - best_score_rect.width) / 2, 275 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: QuitGame() elif event.type == pygame.MOUSEMOTION: mouse_pos = pygame.mouse.get_pos() if mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)): again_image = again_images[1] else: again_image = again_images[0] elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if event.button == 1 and mouse_pos[0] in list(range(419, 574)) and mouse_pos[1] in list(range(374, 416)): return True screen.blit(end_image, (0, 0)) screen.blit(again_image, (416, 370)) screen.blit(your_score_text, your_score_rect) screen.blit(best_score_text, best_score_rect) pygame.display.update()
小结 #
本节介绍了开始界面和结束界面的创建方法