import pygame import sys #函数名含义:check events for mode (selecting),下同 def check_mode_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 = 2 def check_play_events(snake): """检测游戏过程中的按键""" for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() elif event.key == pygame.K_LEFT and not snake.direction == 'right': #防止反向移动 snake.direction = 'left' break elif event.key == pygame.K_RIGHT and not snake.direction == 'left': snake.direction = 'right' break elif event.key == pygame.K_UP and not snake.direction == 'down': snake.direction = 'up' break elif event.key == pygame.K_DOWN and not snake.direction == 'up': snake.direction = 'down' break def autocheck_events(ai,snake,apple,settings): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() if eat_apple(snake,apple): ai.start(snake,apple,settings) def show_start(settings, screen): """模式选择界面""" title_Font = pygame.font.Font(settings.font_name, 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(settings.font_name, 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 screen.fill(settings.bg_color) # 绘制屏幕 screen.blit(title_image, title_rect) # 绘制标题 screen.blit(presskey_image, presskey_rect) # 绘制说明文字 while True: check_mode_events(settings) # 检测键盘 if settings.game_mode != 0: # 说明按了1或2,退出循环 break pygame.display.flip() def show_end(settings, screen): """失败结算界面""" title_font = pygame.font.Font(settings.font_name, 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 def draw_grid(settings, screen): # 绘制横向线 for x in range(0, settings.width, settings.cell_size): pygame.draw.line(screen, (40, 40, 40), (x, 0), (x, settings.height)) # 绘制竖向线 for y in range(0, settings.height, settings.cell_size): pygame.draw.line(screen, (40, 40, 40), (0, y), (settings.width, y)) def draw_snake(settings, screen, snake): # 头部用白色 head_index = snake.coords[0] x = head_index['x'] * settings.cell_size y = head_index['y'] * settings.cell_size snake_head_rect = pygame.Rect( x, y, settings.cell_size, settings.cell_size) pygame.draw.rect(screen, (255, 255, 255), snake_head_rect) # 蛇身内部用浅绿,外框用深绿 for bodies_index in snake.coords[1: -1]: x = bodies_index['x'] * settings.cell_size y = bodies_index['y'] * settings.cell_size snake_part_rect = pygame.Rect( x, y, settings.cell_size, settings.cell_size) pygame.draw.rect(screen, (0, 155, 0), snake_part_rect) snake_part_inner_rect = pygame.Rect( x + 4, y + 4, settings.cell_size - 8, settings.cell_size - 8) pygame.draw.rect(screen, (0, 255, 0), snake_part_inner_rect) # 蛇尾用浅绿 tail_index = snake.coords[-1] x = tail_index['x'] * settings.cell_size y = tail_index['y'] * settings.cell_size snake_tail_rect = pygame.Rect( x, y, settings.cell_size, settings.cell_size) pygame.draw.rect(screen, (0, 255, 0), snake_tail_rect) def draw_apple(apple,settings,screen): x = apple.x * settings.cell_size y = apple.y * settings.cell_size apple_rect = pygame.Rect( x, y, settings.cell_size, settings.cell_size) pygame.draw.rect(screen, (255, 0, 0), apple_rect) # 绘制游戏界面 def update_screen(settings, screen, snake,apple): screen.fill(settings.bg_color) draw_grid(settings, screen) draw_apple(apple,settings,screen) # 移动蛇 snake.move_head() # 加蛇头 if game_over(snake,settings): settings.game_mode = -1 elif eat_apple(snake,apple): apple.spawn(settings) draw_apple(apple,settings,screen) else: del snake.coords[-1] # 去蛇尾 draw_snake(settings, screen, snake) pygame.display.flip() # 暂停一下 settings.my_clock.tick(settings.clock_frq) def autoupdate_screen(settings, screen, snake,apple,ai): screen.fill(settings.bg_color) draw_grid(settings, screen) draw_apple(apple,settings,screen) # 移动蛇 snake.automove_head(ai) # 加蛇头 if game_over(snake,settings): settings.game_mode = -1 elif eat_apple(snake,apple): apple.spawn(settings) draw_apple(apple,settings,screen) else: del snake.coords[-1] # 去蛇尾 draw_snake(settings, screen, snake) pygame.display.flip() # 暂停一下 settings.my_clock.tick(settings.clock_frq) def game_over(snake,settings): # 碰到左右墙壁 if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w): return True # 碰到上下墙壁 if (snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h): return True # 碰到自己 if (snake.coords[snake.head_index] in snake.coords[1:]): return True return False def eat_apple(snake,apple): if (snake.coords[snake.head_index] == apple.coords): return True return False