python实现AI版贪吃蛇

一、主要思路

(1)蛇每走一步,就使用BFS计算游戏界面中每个位置(蛇身除外)到达食物的最短路径长;

(2)将蛇的安全定义为蛇是否可以跟着蛇尾运动,即蛇头和蛇尾间是否存在路径;

(3)蛇每次行动前先利用虚拟的蛇进行探路,若虚拟的蛇吃完食物后是安全的,真蛇才行动;

(4)若蛇和食物之间不存在路径或者吃完食物后并不安全,就跟着蛇尾走;

(5)若蛇和食物之间、蛇和蛇尾之间均不存在路径,就随便挑一步可行的来走;

(6)保证目标是食物时蛇走最短路径,目标是蛇尾时蛇走最长路径。

二、实现代码

部分功能代码如下:

画网格

def drawGrid():
 # 垂直方向
 for x in range(0, SCREENWIDTH, CELLSIZE):
  pygame.draw.line(screen, (40, 40, 40), (x, 0), (x, SCREENHEIGHT))
 # 水平方向
 for y in range(0, SCREENHEIGHT, CELLSIZE):
  pygame.draw.line(screen, (40, 40, 40), (0, y), (SCREENWIDTH, y))

显示蛇

def ShowSnake(coords):
 x = coords[0]['x'] * CELLSIZE
 y = coords[0]['y'] * CELLSIZE
 head_rect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
 pygame.draw.rect(screen, (0, 80, 255), head_rect)
 head_inner_rect = pygame.Rect(x + 4, y + 4, CELLSIZE - 8, CELLSIZE - 8)
 pygame.draw.rect(screen, (0, 80, 255), head_inner_rect)
 for coord in coords[1:]:
  x = coord['x'] * CELLSIZE
  y = coord['y'] * CELLSIZE
  rect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
  pygame.draw.rect(screen, (0, 155, 0), rect)
  inner_rect = pygame.Rect(x + 4, y + 4, CELLSIZE - 8, CELLSIZE - 8)
  pygame.draw.rect(screen, (0, 255, 0), inner_rect)

显示果实

def ShowApple(coord):
 x = coord['x'] * CELLSIZE
 y = coord['y'] * CELLSIZE
 rect = pygame.Rect(x, y, CELLSIZE, CELLSIZE)
 pygame.draw.rect(screen, (255, 0, 0), rect)

显示当前得分

def ShowScore(score):
 score_render = default_font.render('得分: %s' % (score), True, (255, 255, 255))
 rect = score_render.get_rect()
 rect.topleft = (SCREENWIDTH-120, 10)
 screen.blit(score_render, rect)

显示结束界面

def ShowEndInterface():
 title_font = pygame.font.Font('simkai.ttf', 100)
 title_game = title_font.render('Game', True, (233, 150, 122))
 title_over = title_font.render('Over', True, (233, 150, 122))
 game_rect = title_game.get_rect()
 over_rect = title_over.get_rect()
 game_rect.midtop = (SCREENWIDTH/2, 70)
 over_rect.midtop = (SCREENWIDTH/2, game_rect.height+70+25)
 screen.blit(title_game, game_rect)
 screen.blit(title_over, over_rect)
 pygame.display.update()
 pygame.time.wait(500)
 while True:
  for event in pygame.event.get():
   if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
    CloseGame()

广度优先遍历, 计算每个非SNAKENUM元素到达食物的路径长度

def RefreshBoard(psnake, pfood, pboard):
 temp_board = pboard[:]
 pfood_idx = pfood['x'] + pfood['y'] * MATRIX_W
 queue = []
 queue.append(pfood_idx)
 inqueue = [0] * MATRIX_SIZE
 found = False
 while len(queue) != 0:
  idx = queue.pop(0)
  if inqueue[idx] == 1:
   continue
  inqueue[idx] = 1
  for move_direction in ['left', 'right', 'up', 'down']:
   if isMovePossible(idx, move_direction):
    if (idx + MOVEDIRECTIONS[move_direction]) == (psnake[HEADINDEX]['x'] + psnake[HEADINDEX]['y'] * MATRIX_W):
     found = True
    # 该点不是蛇身(食物是0才可以这样子写)
    if temp_board[idx + MOVEDIRECTIONS[move_direction]] < SNAKENUM:
     if temp_board[idx + MOVEDIRECTIONS[move_direction]] > temp_board[idx]+1:
      temp_board[idx + MOVEDIRECTIONS[move_direction]] = temp_board[idx] + 1
     if inqueue[idx + MOVEDIRECTIONS[move_direction]] == 0:
      queue.append(idx + MOVEDIRECTIONS[move_direction])

三、效果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、不足之处

  由于食物是随机出现的,若虚拟的蛇跑一遍发现去吃食物是不安全的,真蛇就不会去吃食物,而是选择追着蛇尾跑,若一直如此,就陷入了死循环,蛇一直追着蛇尾跑,直到你终止游戏为止。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为一个 AI贪吃蛇需要具备以下能力: 1. 寻找食物:贪吃蛇需要知道当前场景中食物的位置,并能够计算出到达食物的最短路径。 2. 避免碰壁:贪吃蛇需要知道当前场景的边界,并且在移动时需要避免碰到边界。 3. 避免自杀:贪吃蛇需要知道自己的身体位置,并在移动时需要避免撞到自己的身体。 下面是一个简单的贪吃蛇 AI 实现: ```python import random class SnakeAI: def __init__(self, board, snake): self.board = board self.snake = snake def find_food(self): # 从当前位置出发,寻找最近的食物 food_pos = None min_distance = float('inf') for i in range(self.board.width): for j in range(self.board.height): if self.board.grid[i][j] == 'food': distance = abs(i - self.snake.head.x) + abs(j - self.snake.head.y) if distance < min_distance: min_distance = distance food_pos = (i, j) return food_pos def avoid_wall(self): # 判断当前位置是否靠近边界 if self.snake.head.x < 2 or self.snake.head.x > self.board.width - 3 \ or self.snake.head.y < 2 or self.snake.head.y > self.board.height - 3: return True else: return False def avoid_self(self): # 判断当前位置是否与自己的身体相撞 for part in self.snake.body[1:]: if self.snake.head.x == part.x and self.snake.head.y == part.y: return True return False def get_move(self): food_pos = self.find_food() if food_pos is None: # 如果没有食物,则随机移动 return random.choice(['up', 'down', 'left', 'right']) if self.snake.head.x < food_pos[0]: if not self.avoid_wall() and not self.avoid_self(): return 'right' elif self.snake.head.x > food_pos[0]: if not self.avoid_wall() and not self.avoid_self(): return 'left' elif self.snake.head.y < food_pos[1]: if not self.avoid_wall() and not self.avoid_self(): return 'down' elif self.snake.head.y > food_pos[1]: if not self.avoid_wall() and not self.avoid_self(): return 'up' # 如果无法直接到达食物,则随机移动 return random.choice(['up', 'down', 'left', 'right']) ``` 在上面的代码中,SnakeAI 类接受两个参数:board 和 snake,分别表示当前场景和贪吃蛇的状态。它实现了三个方法:find_food、avoid_wall 和 avoid_self。 find_food 方法用于寻找最近的食物,它遍历整个场景,找到距离当前位置最近的食物,返回其坐标。 avoid_wall 方法用于判断当前位置是否靠近边界,如果靠近边界则返回 True,否则返回 False。 avoid_self 方法用于判断当前位置是否与自己的身体相撞,如果相撞则返回 True,否则返回 False。 最后,get_move 方法根据当前位置和食物位置,决定贪吃蛇下一步的移动方向。如果能直接到达食物,则选择向食物移动;否则随机移动。在移动时,还需要检查是否靠近边界或与自己的身体相撞,如果是,则选择随机移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值