import pygame import sys def check_events(settings): for event in pygame.event.get(): # 当按关闭或者按ESC键退出游戏 if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() # 按1键选择手动模式 elif event.key == pygame.K_1: settings.game_mode = -1 # 按2键选择自动模式 elif event.key == pygame.K_2: settings.game_mode = -1 def show_start(settings, screen): """模式选择界面""" title_Font = pygame.font.Font('simkai.ttf', 80) # 设置标题字体 title_image = title_Font.render("贪吃蛇", True, (255, 255, 255), (0, 0, 0)) title_rect = title_image.get_rect() title_rect.center = screen.get_rect().center presskey_font = pygame.font.Font('simkai.ttf', 15) # 设置说明文字的字体 presskey_image = presskey_font.render( '按1为手动模式,按2为自动模式,按ESC可退出游戏', True, (255, 255, 255), (0, 0, 0)) presskey_rect = presskey_image.get_rect() presskey_rect.centerx = title_rect.centerx presskey_rect.top = title_rect.bottom while True: screen.fill(settings.bg_color) # 绘制屏幕 screen.blit(title_image, title_rect) # 绘制标题 screen.blit(presskey_image, presskey_rect) # 绘制说明文字 check_events(settings) # 检测键盘 if settings.game_mode != 0: # 说明按了1或2,退出循环 break pygame.display.flip() def show_end(settings, screen): """结算界面""" title_font = pygame.font.Font('simkai.ttf', 80) game_image = title_font.render('Game', True, (233, 150, 122)) over_image = title_font.render('Over', True, (233, 150, 122)) game_rect = game_image.get_rect() over_rect = over_image.get_rect() screen_rect = screen.get_rect() game_rect.midtop = (settings.width / 2, screen_rect.top + 70) over_rect.midtop = (settings.width / 2, game_rect.bottom + 50) screen.blit(game_image, game_rect) screen.blit(over_image, over_rect) pygame.display.flip() pygame.time.wait(1500) settings.game_mode = 0