贪吃蛇自动版

在这里插入图片描述

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]]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[move_direction]]>max_distance) and (pboard[idx+move_directions[move_direction]]

贪吃蛇自动版_第1张图片

https://github.com/MaoliRUNsen/AI_snake.git

你可能感兴趣的:(贪吃蛇自动版)