python版AI貪吃蛇
阿新 • • 發佈:2018-11-29
本文轉自:https://mp.weixin.qq.com/s?__biz=MzU2NDI1MjkwNA==&mid=2247484020&idx=1&sn=73aa1452ddba05ae233e3e4a3aaa2ad6&chksm=fc4c9bf3cb3b12e5877f0bfc64c819bf7ffc302d1ad473ed3bea618cc923e978c58f6b27cd4c&mpshare=1&scene=1&srcid=1128srZL77HPhK48AIdyWfKh#rd
主要思路
(1)蛇每走一步,就使用BFS計算遊戲介面中每個位置(蛇身除外)到達食物的最短路徑長;
(2)將蛇的安全定義為蛇是否可以跟著蛇尾運動,即蛇頭和蛇尾間是否存在路徑;
(3)蛇每次行動前先利用虛擬的蛇進行探路,若虛擬的蛇吃完食物後是安全的,真蛇才行動;
(4)若蛇和食物之間不存在路徑或者吃完食物後並不安全,就跟著蛇尾走;
(5)若蛇和食物之間、蛇和蛇尾之間均不存在路徑,就隨便挑一步可行的來走;
(6)保證目標是食物時蛇走最短路徑,目標是蛇尾時蛇走最長路徑。
不足之處
由於食物是隨機出現的,若虛擬的蛇跑一遍發現去吃食物是不安全的,真蛇就不會去吃食物,而是選擇追著蛇尾跑,若一直如此,就陷入了死迴圈,蛇一直追著蛇尾跑跑跑。。。
直到你終止遊戲為止。。。
開發工具
Python版本:3.5.4
相關模組:pygame模組以及一些Python自帶的模組。
執行方式
在cmd視窗執行AI_snake.py檔案即可。
程式碼
import random
import pygame
import sys
from pygame.locals import *
# 錯誤碼
ERR = -404
# 螢幕大小
Window_Width = 800
Window_Height = 500
# 重新整理頻率
Display_Clock = 17
# 一塊蛇身大小
Cell_Size = 20
assert Window_Width % Cell_Size == 0
assert Window_Height % Cell_Size == 0
# 等價的運動區域大小
Cell_W = int(Window_Width/Cell_Size)
Cell_H = int(Window_Height/Cell_Size)
FIELD_SIZE = Cell_W * Cell_H
# 背景顏色
Background_Color = (0, 0, 0)
# 蛇頭索引
Head_index = 0
# 運動方向
best_move = ERR
# 不同東西在矩陣裡用不同的數字表示
FOOD = 0
FREE_PLACE = (Cell_W+1) * (Cell_H+1)
SNAKE_PLACE = 2 * FREE_PLACE
# 運動方向字典
move_directions = {
'left': -1,
'right': 1,
'up': -Cell_W,
'down': Cell_W
}
# 關閉遊戲介面
def close_game():
pygame.quit()
sys.exit()
# 檢測玩家的按鍵
def Check_PressKey():
if len(pygame.event.get(QUIT)) > 0:
close_game()
KeyUp_Events = pygame.event.get(KEYUP)
if len(KeyUp_Events) == 0:
return None
elif KeyUp_Events[0].key == K_ESCAPE:
close_game()
return KeyUp_Events[0].key
# 顯示當前得分
def Show_Score(score):
score_Content = Main_Font.render('得分:%s' % (score), True, (255, 255, 255))
score_Rect = score_Content.get_rect()
score_Rect.topleft = (Window_Width-120, 10)
Main_Display.blit(score_Content, score_Rect)
# 獲得果實位置
def Get_Apple_Location(snake_Coords):
flag = True
while flag:
apple_location = {'x': random.randint(0, Cell_W-1), 'y': random.randint(0, Cell_H-1)}
if apple_location not in snake_Coords:
flag = False
return apple_location
# 顯示果實
def Show_Apple(coord):
x = coord['x'] * Cell_Size
y = coord['y'] * Cell_Size
apple_Rect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Main_Display, (255, 0, 0), apple_Rect)
# 顯示蛇
def Show_Snake(coords):
x = coords[0]['x'] * Cell_Size
y = coords[0]['y'] * Cell_Size
Snake_head_Rect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Main_Display, (0, 80, 255), Snake_head_Rect)
Snake_head_Inner_Rect = pygame.Rect(x+4, y+4, Cell_Size-8, Cell_Size-8)
pygame.draw.rect(Main_Display, (0, 80, 255), Snake_head_Inner_Rect)
for coord in coords[1:]:
x = coord['x'] * Cell_Size
y = coord['y'] * Cell_Size
Snake_part_Rect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Main_Display, (0, 155, 0), Snake_part_Rect)
Snake_part_Inner_Rect = pygame.Rect(x+4, y+4, Cell_Size-8, Cell_Size-8)
pygame.draw.rect(Main_Display, (0, 255, 0), Snake_part_Inner_Rect)
# 畫網格
def draw_Grid():
# 垂直方向
for x in range(0, Window_Width, Cell_Size):
pygame.draw.line(Main_Display, (40, 40, 40), (x, 0), (x, Window_Height))
# 水平方向
for y in range(0, Window_Height, Cell_Size):
pygame.draw.line(Main_Display, (40, 40, 40), (0, y), (Window_Width, y))
# 顯示開始介面
def Show_Start_Interface():
title_Font = pygame.font.Font('simkai.ttf', 100)
title_content = title_Font.render('貪吃蛇', True, (255, 255, 255), (0, 0, 160))
angle = 0
while True:
Main_Display.fill(Background_Color)
rotated_title = pygame.transform.rotate(title_content, angle)
rotated_title_Rect = rotated_title.get_rect()
rotated_title_Rect.center = (Window_Width/2, Window_Height/2)
Main_Display.blit(rotated_title, rotated_title_Rect)
pressKey_content = Main_Font.render('按任意鍵開始遊戲!', True, (255, 255, 255))
pressKey_Rect = pressKey_content.get_rect()
pressKey_Rect.topleft = (Window_Width-200, Window_Height-30)
Main_Display.blit(pressKey_content, pressKey_Rect)
if Check_PressKey():
# 清除事件佇列
pygame.event.get()
return
pygame.display.update()
Snake_Clock.tick(Display_Clock)
angle -= 5
# 顯示結束介面
def Show_End_Interface():
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 = (Window_Width/2, 70)
over_Rect.midtop = (Window_Width/2, game_Rect.height+70+25)
Main_Display.blit(title_game, game_Rect)
Main_Display.blit(title_over, over_Rect)
pygame.display.update()
pygame.time.wait(500)
while True:
for event in pygame.event.get():
if event.type == QUIT:
close_game()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
close_game()
# 判斷該位置是否為空
def Is_Cell_Free(idx, psnake):
location_x = idx % Cell_W
location_y = idx // Cell_W
idx = {'x': location_x, 'y': location_y}
return (idx not in psnake)
# 重置board
def board_reset(psnake, pboard, pfood):
temp_board = pboard[:]
pfood_idx = pfood['x'] + pfood['y'] * Cell_W
for i in range(FIELD_SIZE):
if i == pfood_idx:
temp_board[i] = FOOD
elif Is_Cell_Free(i, psnake):
temp_board[i] = FREE_PLACE
else:
temp_board[i] = SNAKE_PLACE
return temp_board
# 檢查位置idx是否可以向當前move方向運動
def is_move_possible(idx, move_direction):
flag = False
if move_direction == 'left':
if idx%Cell_W > 0:
flag = True
else:
flag = False
elif move_direction == 'right':
if idx%Cell_W < Cell_W-1:
flag = True
else:
flag = False
elif move_direction == 'up':
if idx > Cell_W-1:
flag = True
else:
flag = False
elif move_direction == 'down':
if idx < FIELD_SIZE - Cell_W:
flag = True
else:
flag = False
return flag
# 廣度優先搜尋遍歷整個board
# 計算出board中每個非SNAKE_PLACE元素到達食物的路徑長度
def board_refresh(psnake, pfood, pboard):
temp_board = pboard[:]
pfood_idx = pfood['x'] + pfood['y'] * Cell_W
queue = []
queue.append(pfood_idx)
inqueue = [0] * FIELD_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 is_move_possible(idx, move_direction):
if (idx+move_directions[move_direction]) == (psnake[Head_index]['x'] + psnake[Head_index]['y']*Cell_W):
found = True
# 該點不是蛇身(食物是0才可以這樣子寫)
if temp_board[idx+move_directions[move_direction]] < SNAKE_PLACE:
if temp_board[idx+move_directions[move_direction]] > temp_board[idx]+1:
temp_board[idx+move_directions[move_direction]] = temp_board[idx] + 1
if inqueue[idx+move_directions[move_direction]] == 0:
queue.append(idx+move_directions[move_direction])
return (found, temp_board)
# 根據board中元素值
# 從蛇頭周圍4個領域點中選擇最短路徑
def choose_shortest_safe_move(psnake, pboard):
best_move = ERR
min_distance = SNAKE_PLACE
for move_direction in ['left', 'right', 'up', 'down']:
idx = psnake[Head_index]['x'] + psnake[Head_index]['y']*Cell_W
if is_move_possible(idx, move_direction) and (pboard[idx+move_directions[move_direction]]<min_distance):
min_distance = pboard[idx+move_directions[move_direction]]
best_move = move_direction
return best_move
# 找到移動後蛇頭的位置
def find_snake_head(snake_Coords, direction):
if direction == 'up':
newHead = {'x': snake_Coords[Head_index]['x'],
'y': snake_Coords[Head_index]['y']-1}
elif direction == 'down':
newHead = {'x': snake_Coords[Head_index]['x'],
'y': snake_Coords[Head_index]['y']+1}
elif direction == 'left':
newHead = {'x': snake_Coords[Head_index]['x']-1,
'y': snake_Coords[Head_index]['y']}
elif direction == 'right':
newHead = {'x': snake_Coords[Head_index]['x']+1,
'y': snake_Coords[Head_index]['y']}
return newHead
# 虛擬地執行一次
def virtual_move(psnake, pboard, pfood):
temp_snake = psnake[:]
temp_board = pboard[:]
reset_tboard = board_reset(temp_snake, temp_board, pfood)
temp_board = reset_tboard
food_eated = False
while not food_eated:
refresh_tboard = board_refresh(temp_snake, pfood, temp_board)[1]
temp_board = refresh_tboard
move_direction = choose_shortest_safe_move(temp_snake, temp_board)
snake_Coords = temp_snake[:]
temp_snake.insert(0, find_snake_head(snake_Coords, move_direction))
# 如果新的蛇頭正好是食物的位置
if temp_snake[Head_index] == pfood:
reset_tboard = board_reset(temp_snake, temp_board, pfood)
temp_board = reset_tboard
pfood_idx = pfood['x'] + pfood['y'] * Cell_W
temp_board[pfood_idx] = SNAKE_PLACE
food_eated = True
else:
newHead_idx = temp_snake[0]['x'] + temp_snake[0]['y'] * Cell_W
temp_board[newHead_idx] = SNAKE_PLACE
end_idx = temp_snake[-1]['x'] + temp_snake[-1]['y'] * Cell_W
temp_board[end_idx] = FREE_PLACE
del temp_snake[-1]
return temp_snake, temp_board
# 檢查蛇頭和蛇尾間是有路徑的
# 避免蛇陷入死路
def is_tail_inside(psnake, pboard, pfood):
temp_board = pboard[:]
temp_snake = psnake[:]
# 將蛇尾看作食物
end_idx = temp_snake[-1]['x'] + temp_snake[-1]['y'] * Cell_W
temp_board[end_idx] = FOOD
v_food = temp_snake[-1]
# 食物看作蛇身(重複賦值了)
pfood_idx = pfood['x'] + pfood['y'] * Cell_W
temp_board[pfood_idx] = SNAKE_PLACE
# 求得每個位置到蛇尾的路徑長度
result, refresh_tboard = board_refresh(temp_snake, v_food, temp_board)
temp_board = refresh_tboard
for move_direction in ['left', 'right', 'up', 'down']:
idx = temp_snake[Head_index]['x'] + temp_snake[Head_index]['y']*Cell_W
end_idx = temp_snake[-1]['x'] + temp_snake[-1]['y']*Cell_W
if is_move_possible(idx, move_direction) and (idx+move_directions[move_direction] == end_idx) and (len(temp_snake)>3):
result = False
return result
# 根據board中元素值
# 從蛇頭周圍4個領域點中選擇最遠路徑
def choose_longest_safe_move(psnake, pboard):
best_move = ERR
max_distance = -1
for move_direction in ['left', 'right', 'up', 'down']:
idx = psnake[Head_index]['x'] + psnake[Head_index]['y']*Cell_W
if is_move_possible(idx, move_direction) and (pboard[idx+move_directions<