【Python小游戏】没点儿技术真不敢这么玩儿:人工智能挑战《贪吃蛇》,来自AI的艺术——超级游戏高手世界最高纪录秒被盘?

news2024/10/2 6:37:08

前言

 每天分享各种Python项目、好玩的Pygame游戏、Python的爬虫、数据分析案例、有趣的人

工智能知识等。期待你的关注哦!

所有文章完整的素材+源码都在👇👇

粉丝白嫖源码福利,请移步至CSDN社区或文末公众hao即可免费。

哈喽,我是栗子同学。

这款Pygame以前已经发过不同的版本了,不过好多小可爱反映哪款游戏没有挑战性,今天重

新编写了一款重新发一次,希望让更多的人看到。

今天小编给大家编程的小游戏就是大众所熟知的《贪吃蛇》小游戏啦,让我们看看普通人版本

的《贪吃蛇》跟《AI版本的贪吃蛇》有什么不同呢?

正文

一、游戏简介

经典Pygame代码版本——游戏《贪吃蛇》华丽升级为《AI贪吃蛇大作战》,全新玩法等你来

挑战!这是一款超好玩的蛇蛇大作战休闲游戏,不仅比拼手速,更考验你的眼神!在贪吃蛇

大作战的世界中,每个人在初始都化身为一条小蛇,通过不断努力变得越来越长,终于成长为

贪吃榜排名榜首的贪吃蛇哟。触碰边界即凉凉哦。

二、普通版本《贪吃蛇》

1)代码展示

import random
import pygame
import sys
from pygame.locals import *


# 屏幕大小
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)
# 背景颜色
Background_Color = (0, 0, 0)
# 蛇头索引
Head_index = 0


# 关闭游戏界面
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)
	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)
	pygame.display.update()
	pygame.time.wait(500)
	# 清除事件队列
	Check_PressKey()
	while True:
		if Check_PressKey():
			pygame.event.get()
			return


# 运行游戏
def Run_Game():
	# 蛇出生地
	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}]
	direction = 'right'
	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_LEFT) and (direction != 'right'):
					direction = 'left'
				elif (event.key == K_RIGHT) and (direction != 'left'):
					direction = 'right'
				elif (event.key == K_UP) and (direction != 'down'):
					direction = 'up'
				elif (event.key == K_DOWN) and (direction != 'up'):
					direction = 'down'
				elif event.key == K_ESCAPE:
					close_game()
		# 碰到墙壁或者自己则游戏结束
		if (snake_Coords[Head_index]['x'] == -1) or (snake_Coords[Head_index]['x'] == Cell_W) or \
		   (snake_Coords[Head_index]['y'] == -1) or (snake_Coords[Head_index]['y'] == Cell_H):
			return
		if snake_Coords[Head_index] in snake_Coords[1:]:
			return
		if (snake_Coords[Head_index]['x'] == apple_location['x']) and (snake_Coords[Head_index]['y'] == apple_location['y']):
			apple_location = Get_Apple_Location(snake_Coords)
		else:
			del snake_Coords[-1]
		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']}
		snake_Coords.insert(0, newHead)
		Main_Display.fill(Background_Color)
		draw_Grid()
		Show_Snake(snake_Coords)
		Show_Apple(apple_location)
		Show_Score(len(snake_Coords)-3)
		pygame.display.update()
		Snake_Clock.tick(Display_Clock)


# 主函数
def main():
	global Main_Display, Main_Font, Snake_Clock
	pygame.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('Normal_snake')
	Show_Start_Interface()
	while True:
		Run_Game()
		Show_End_Interface()



if __name__ == '__main__':
	main()

2)效果展示

界面截图——

效果截图——

​游戏结束截图——

三、AI版本《贪吃蛇》

1)代码展示

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[move_direction]]>max_distance) and (pboard[idx+move_directions[move_direction]]<FREE_PLACE):
			max_distance = pboard[idx+move_directions[move_direction]]
			best_move = move_direction
	return 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_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
	# 还原
	temp_board[end_idx] = SNAKE_PLACE
	# temp_board[pfood_idx] = FOOD
	return choose_longest_safe_move(temp_snake, temp_board)


# 如果蛇和食物间有路径
# 则需要找一条安全的路径
def find_safe_way(psnake, pboard, pfood):
	safe_move = ERR
	real_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 = ERR
	reset_board = board_reset(psnake, pboard, pfood)
	pboard = reset_board
	result, refresh_board = board_refresh(psnake, pfood, pboard)
	pboard = refresh_board
	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 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)
		# 重置board
		reset_board = board_reset(snake_Coords, board, apple_location)
		board = reset_board
		result, 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_W
			end_idx = snake_Coords[-1]['x'] + snake_Coords[-1]['y']*Cell_W
			if (snake_Coords[Head_index]['x'] == apple_location['x']) and (snake_Coords[Head_index]['y'] == apple_location['y']):
				board[head_idx] = SNAKE_PLACE
				if len(snake_Coords) < FIELD_SIZE:
					apple_location = Get_Apple_Location(snake_Coords)
			else:
				board[head_idx] = SNAKE_PLACE
				board[end_idx] = FREE_PLACE
				del snake_Coords[-1]
		else:
			return
		pygame.display.update()
		Snake_Clock.tick(Display_Clock)


# 主函数
def main():
	global Main_Display, Main_Font, Snake_Clock
	pygame.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()

2)效果展示

超超超AI的,这速度还是杠杠的,我是懒得拍视频,上面的视频是最后一幕可以不看的,哈哈哈,

AI的一直在玩儿不撞墙的哦!

总结

贪吃蛇进化论:经典贪吃蛇小游戏!从普通版本到AI版本简直玩儿坏了,哈哈哈。要不要拿来

研究下普通人玩儿的俄罗斯方块到AI版本的方块,最后到简单版本的五子棋到AI版本的......

总之,现在一切皆可AI,自动化的功能现在还是挺厉害的啦,发展的越来越快啦。

以后肯定还有AI自动驾车系统等等,我是不是就不用考驾照了!!哈哈哈,狠狠的期待住了。

✨完整的素材源码等:可以滴滴我吖!或者点击文末hao自取免费拿的哈~

 🔨推荐往期文章——

项目4.4 【Pygame实战】这两款脑洞大开的文字剧情版游戏,99% 的人打了五星好评-《巨龙之洞》-《太空矿工》

项目4.5  【Pygamre实战】2023人气超高的模拟经营类游戏:“梦想小镇“代码版火爆全场,免费体验分享下载哦~

项目1.5  Pygame小游戏:植物大战僵尸游戏真的有“毒”?戒不掉啊~

项目1.6 【Pygame小游戏】斗地主我见多了,BUT 这款开源欢乐斗地主,最让人服气~

项目0.5   重温经典:Python版飞机大战源码,装逼神器。玩游戏就玩自己开发的~

项目0.6 【Python实战项目】做一个 刮刮乐 案例,一不小心....着实惊艳到我了。

🎁文章汇总——

Python文章合集 | (入门到实战、游戏、Turtle、案例等)

(文章汇总还有更多你案例等你来学习啦~源码找我即可免费!)    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/348090.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

探索IP地址的应用

无论是互联网行业还是传统行业都会用到网络&#xff0c;作为企业如何维护网络安全&#xff0c;保障网站不被攻击&#xff0c;数据不被泄露等。这个时候我们就会通查询IP归属地&#xff0c;辅助企业解决安全问题。下面介绍一下ip归属地在各行业的具体应用。1.网安行业应用一&…

SpringBoot实现登录拦截器超详细(springboot拦截器excludePathPatterns方法不生效的坑)

文章目录SpringBoot实现登录拦截器1、SpringBoot 实现登录拦截的原理1.1、实现HandlerInterceptor接口1.2、实现WebMvcConfigurer接口&#xff0c;注册拦截器1.3、保持登录状态springboot拦截器excludePathPatterns方法不生效的坑与解决方法一、前言二、问题三、解决方法四、总…

C语言编译过程

C语言编译过程1、C语言编译过程2、单c文件编译实践3、多c文件编译实践4、define4.1、不带参宏4.2、带参宏4.3、带参宏和带参函数的区别5、选择性编译ifdef、ifndef、if5.1、#ifdef5.2、#ifndef5.3、#if6、静态库和动态链接库6.1、静态库实践6.1.1、将mylib.c制作成静态库6.1.2、…

Baklib知识库管理平台,协助组织提升知识管理水平

随着信息时代和知识经济时代的到来&#xff0c;企业内部信息资料繁多冗杂&#xff0c;知识管理逐渐成为各大企业的重要工作之一&#xff0c;企业管理者无不感受到巨大的压力&#xff0c;怎么样将知识进行有效的管理&#xff0c;成为一个难点&#xff0c;并且随着信息不断的更迭…

Java企业级信息系统开发学习笔记(1)初探Spring与骑士傻龙实例

该文章主要为完成实训任务&#xff0c;详细实现过程及结果见【http://t.csdn.cn/iK3aM】 文章目录1. 创建Maven项目2. 添加Spring依赖3. 创建杀龙任务4. 创建勇士类5. 采用传统的方式6. 采用Spring容器让勇敢骑士完成杀龙任务6.1 创建日志属性文件6.2 创建Spring配置文件6.3 在…

CobaltStrike上线微信通知

CobaltStrike上线微信通知 利用pushplus公众号&#xff08;每天免费发送200条消息&#xff09; http://www.pushplus.plus/push1.html 扫码登录后需要复制token 可以测试一下发送一下消息&#xff0c;手机会受到如下消息。可以在微信提示里将消息免打扰关闭&#xff08;默认…

分布式光伏储能系统的优化配置方法(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

期末复习提纲

复习提纲 题型&#xff1a;编程题3题&#xff0c;综合题4题。 一、编程题&#xff1a; 1、链表的类型定义&#xff1b;邻接矩阵表示图的类型定义&#xff1b;链接表表示图的类型定义&#xff1b;vector数组表示图的定义和使用方法。 2、链表中结点的插入和删除操作&#xff…

linux高级命令之编辑器 vim

编辑器 vim学习目标能够说出vim的三种工作模式能够说出vim对应复制和粘贴命令1. vim 的介绍vim 是一款功能强大的文本编辑器&#xff0c;也是早年 Vi 编辑器的加强版&#xff0c;它的最大特色就是使用命令进行编辑&#xff0c;完全脱离了鼠标的操作。2. vim 的工作模式命令模式…

2023想转行软件测试的看过来,你想要了解的薪资、前景、岗位方向、学习路线都讲明白了

在过去的一年中&#xff0c;软件测试行业发展迅速&#xff0c;随着数字化技术应用的广泛普及&#xff0c;业界对于软件测试的要求也在持续迭代与增加。 同样的&#xff0c;有市场就有需求&#xff0c;软件测试逐渐成为企业中不可或缺的岗位&#xff0c;作为一个高薪又需求广的…

怎么解密MD5,常见的MD5解密方法,一看就会

MD5是一种被广泛使用的密码散列函数&#xff0c;曾在计算机安全领域使用很广泛&#xff0c;但是也因为它容易发生碰撞&#xff0c;而被人们认为不安全。那么&#xff0c;MD5应用场景有哪些&#xff0c;我们怎么解密MD5&#xff0c;本文将带大家了解MD5的相关知识&#xff0c;以…

Laravel创建定时任务

创建一个任务&#xff0c;创建成功后会在App/Console/Commands中生成一个以Test命名的文件&#xff0c;我们可以在这里面写我们的任务指令。 php artisan make:command Test 运行这个定时任务 run 是运行一次&#xff0c;我们可以用来测试是否成功&#xff0c;work是一直运行&a…

Jenkins的使用教程

介绍&#xff1a; Jenkins是一个开源软件项目&#xff0c;是基于Java开发的一种持续集成工具&#xff0c;用于监控持续重复的工作&#xff0c;旨在提供一个开放易用的软件平台&#xff0c;使软件的持续集成变成可能。 目的: 最重要目的就是把原来分散在各个机器上繁杂的工作全部…

hadoop高可用搭建

修改计算机名称 //修改计算机名称 [rootlocalhost ~]# hostnamectl set-hostname ant150//快速生效 [rootlocalhost ~]# bash 主机名称映射 [rootant150 ~]# vim /etc/hosts 192.168.153.150 ant150 192.168.153.151 ant151 192.168.153.152 ant152 192.168.153.153 ant153 …

数据分析-深度学习 Tensorflow Day6

我们需要解决的问题&#xff1a;1&#xff1a; 什么是bp 神经网络&#xff1f;2&#xff1a;理解bp神经网络需要哪些数学知识&#xff1f;3&#xff1a;梯度下降的原理4: 激活函数5&#xff1a;bp的推导。1.什么是bp网络&#xff1f;引用百度知道回复&#xff1a;“我们最常用的…

【mmrotate】旋转目标检测之训练DOTA数据集

every blog every motto: You can do more than you think. https://blog.csdn.net/weixin_39190382?typeblog 0. 前言 mmrotate训练DOTA数据集记录 1. 正文 1.1 数据准备 数据介绍部分&#xff0c;参考DOTA数据介绍&#xff0c;官方提供了裁剪工具development kit。这里…

LINUX内核链表

LINUX内核链表 一、传统链表的缺陷 传统的双向循环链表概念简单&#xff0c;操作方便&#xff0c;但存在有致命的缺陷&#xff0c;用一句话来概括就是&#xff1a; 每一条链表都是特殊的&#xff0c;不具有通用性。换句话说&#xff0c;对于每一种不同的数据&#xff0c;所构…

java对象克隆和面向对象的设计原则

java进阶注解内置注解元注解自定义注解对象克隆浅克隆深克隆java设计模式建模语言类之间的关系依赖关系关联关系单向关联双向关联自关联聚合关系组合关系继承关系实现关系面向对象设计原则单一职责开闭原则里氏替换原则依赖倒置接口隔离迪米特原则组合/聚合复用原则注解 java注…

关于ucharts在小程序中的使用

项目添加组件 画图之前&#xff0c;首先需要引入ucharts组件&#xff0c;引入链接https://ext.dcloud.net.cn/plugin?id271。 点击下图中红色方框内容&#xff1a; 导入完成后&#xff0c;与uni其他组件一样&#xff0c;无需引入&#xff0c;直接使用即可。 使用组件 uchar…

jhipster自动生成java代码的方法

一、前言 java springboot后台项目用到了jpa查询数据库&#xff0c;还用到了jhipster&#xff0c;这个东西可以自动生成基础的Controller、Service、Dao、JavaBean等相关代码&#xff0c;减少重复开发。 在此总结下使用方法。 二、jhipster自动生成java代码的方法 1.需要先…