贪吃蛇自动版
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 Noneelif 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 = Truewhile 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 = Falsereturn apple_location
# 顯示果實
def Show_Apple(coord):x = coord['x'] * Cell_Sizey = coord['y'] * Cell_Sizeapple_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_Sizey = coords[0]['y'] * Cell_SizeSnake_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_Sizey = coord['y'] * Cell_SizeSnake_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 = 0while 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()returnpygame.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_Wlocation_y = idx // Cell_Widx = {'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_Wfor i in range(FIELD_SIZE):if i == pfood_idx:temp_board[i] = FOODelif Is_Cell_Free(i, psnake):temp_board[i] = FREE_PLACEelse:temp_board[i] = SNAKE_PLACEreturn temp_board# 檢查位置idx是否可以向當前move方向運動
def is_move_possible(idx, move_direction):flag = Falseif move_direction == 'left':if idx%Cell_W > 0:flag = Trueelse:flag = Falseelif move_direction == 'right':if idx%Cell_W < Cell_W-1:flag = Trueelse:flag = Falseelif move_direction == 'up':if idx > Cell_W-1:flag = Trueelse:flag = Falseelif move_direction == 'down':if idx < FIELD_SIZE - Cell_W:flag = Trueelse:flag = Falsereturn flag# 廣度優先搜索遍歷整個board
# 計算出board中每個非SNAKE_PLACE元素到達食物的路徑長度
def board_refresh(psnake, pfood, pboard):temp_board = pboard[:]pfood_idx = pfood['x'] + pfood['y'] * Cell_Wqueue = []queue.append(pfood_idx)inqueue = [0] * FIELD_SIZEfound = Falsewhile len(queue) != 0:idx = queue.pop(0)if inqueue[idx] == 1:continueinqueue[idx] = 1for 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] + 1if 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 = ERRmin_distance = SNAKE_PLACEfor move_direction in ['left', 'right', 'up', 'down']:idx = psnake[Head_index]['x'] + psnake[Head_index]['y']*Cell_Wif 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_directionreturn 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_tboardfood_eated = Falsewhile not food_eated:refresh_tboard = board_refresh(temp_snake, pfood, temp_board)[1]temp_board = refresh_tboardmove_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_tboardpfood_idx = pfood['x'] + pfood['y'] * Cell_Wtemp_board[pfood_idx] = SNAKE_PLACEfood_eated = Trueelse:newHead_idx = temp_snake[0]['x'] + temp_snake[0]['y'] * Cell_Wtemp_board[newHead_idx] = SNAKE_PLACEend_idx = temp_snake[-1]['x'] + temp_snake[-1]['y'] * Cell_Wtemp_board[end_idx] = FREE_PLACEdel 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_Wtemp_board[end_idx] = FOODv_food = temp_snake[-1]# 食物看作蛇身(重復賦值了)pfood_idx = pfood['x'] + pfood['y'] * Cell_Wtemp_board[pfood_idx] = SNAKE_PLACE# 求得每個位置到蛇尾的路徑長度result, refresh_tboard = board_refresh(temp_snake, v_food, temp_board)temp_board = refresh_tboardfor move_direction in ['left', 'right', 'up', 'down']:idx = temp_snake[Head_index]['x'] + temp_snake[Head_index]['y']*Cell_Wend_idx = temp_snake[-1]['x'] + temp_snake[-1]['y']*Cell_Wif is_move_possible(idx, move_direction) and (idx+move_directions[move_direction] == end_idx) and (len(temp_snake)>3):result = Falsereturn result
# 根據board中元素值
# 從蛇頭周圍4個領域點中選擇最遠路徑
def choose_longest_safe_move(psnake, pboard):best_move = ERRmax_distance = -1for move_direction in ['left', 'right', 'up', 'down']:idx = psnake[Head_index]['x'] + psnake[Head_index]['y']*Cell_Wif is_move_possible(idx, move_direction) and (pboard[idx+move_directions[move_direction]]>max_distance) and (pboard[idx+move_directions[move_direction]]<FREE_PLACE):max_distance = pboard[idx+move_directions[move_direction]]best_move = move_directionreturn best_move
# 讓蛇頭朝著蛇尾運行一步
def follow_tail(psnake, pboard, pfood):temp_snake = psnake[:]temp_board = board_reset(temp_snake, pboard, pfood)# 將蛇尾看作食物end_idx = temp_snake[-1]['x'] + temp_snake[-1]['y'] * Cell_Wtemp_board[end_idx] = FOODv_food = temp_snake[-1]# 食物看作蛇身pfood_idx = pfood['x'] + pfood['y'] * Cell_Wtemp_board[pfood_idx] = SNAKE_PLACE# 求得每個位置到蛇尾的路徑長度result, refresh_tboard = board_refresh(temp_snake, v_food, temp_board)temp_board = refresh_tboard# 還原temp_board[end_idx] = SNAKE_PLACE# temp_board[pfood_idx] = FOODreturn choose_longest_safe_move(temp_snake, temp_board)
# 如果蛇和食物間有路徑
# 則需要找一條安全的路徑
def find_safe_way(psnake, pboard, pfood):safe_move = ERRreal_snake = psnake[:]real_board = pboard[:]v_psnake, v_pboard = virtual_move(psnake, pboard, pfood)# 如果虛擬運行后,蛇頭蛇尾間有通路,則選最短路運行if is_tail_inside(v_psnake, v_pboard, pfood):safe_move = choose_shortest_safe_move(real_snake, real_board)else:safe_move = follow_tail(real_snake, real_board, pfood)return safe_move
# 各種方案均無效時,隨便走一步
def any_possible_move(psnake, pboard, pfood):best_move = ERRreset_board = board_reset(psnake, pboard, pfood)pboard = reset_boardresult, refresh_board = board_refresh(psnake, pfood, pboard)pboard = refresh_boardmin_distance = SNAKE_PLACEfor move_direction in ['left', 'right', 'up', 'down']:idx = psnake[Head_index]['x'] + psnake[Head_index]['y']*Cell_Wif 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_directionreturn best_move
# 運行游戲
def Run_Game():# 一維數組來表示蛇運動的矩形場地board = [0] * FIELD_SIZE# 蛇出生地start_x = random.randint(5, Cell_W-6)start_y = random.randint(5, Cell_H-6)snake_Coords = [{'x': start_x, 'y': start_y},{'x': start_x-1, 'y': start_y},{'x': start_x-2, 'y': start_y}]apple_location = Get_Apple_Location(snake_Coords)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()Main_Display.fill(Background_Color)draw_Grid()Show_Snake(snake_Coords)Show_Apple(apple_location)Show_Score(len(snake_Coords)-3)# 重置boardreset_board = board_reset(snake_Coords, board, apple_location)board = reset_boardresult, refresh_board = board_refresh(snake_Coords, apple_location, board)board = refresh_board# 如果蛇可以吃到食物if result:best_move = find_safe_way(snake_Coords, board, apple_location)else:best_move = follow_tail(snake_Coords, board, apple_location)if best_move == ERR:best_move = any_possible_move(snake_Coords, board, apple_location)if best_move != ERR:newHead = find_snake_head(snake_Coords, best_move)snake_Coords.insert(0, newHead)head_idx = snake_Coords[Head_index]['x'] + snake_Coords[Head_index]['y']*Cell_Wend_idx = snake_Coords[-1]['x'] + snake_Coords[-1]['y']*Cell_Wif (snake_Coords[Head_index]['x'] == apple_location['x']) and (snake_Coords[Head_index]['y'] == apple_location['y']):board[head_idx] = SNAKE_PLACEif len(snake_Coords) < FIELD_SIZE:apple_location = Get_Apple_Location(snake_Coords)else:board[head_idx] = SNAKE_PLACEboard[end_idx] = FREE_PLACEdel snake_Coords[-1]else:returnpygame.display.update()Snake_Clock.tick(Display_Clock)
# 主函數
def main():global Main_Display, Main_Font, Snake_Clockpygame.init()Snake_Clock = pygame.time.Clock()Main_Display = pygame.display.set_mode((Window_Width, Window_Height))Main_Font = pygame.font.Font('simkai.ttf', 18)pygame.display.set_caption('AI_snake')Show_Start_Interface()while True:Run_Game()Show_End_Interface()
if __name__ == '__main__':main()
https://github.com/MaoliRUNsen/AI_snake.git
總結
- 上一篇: 小熊软糖已黑化什么意思
- 下一篇: 汽车玻璃生产日期相差一个月正常吗(汽车玻