- import os
- import time
- import random
- import copy
- global FIND
- global WAYPOINT
- global WAYPOINT2
- global APPLE
- global SNAKE
- global GRID
- global BACKUP
- global SCREEN
- global NEW
- cols=22
- rows=22
- #蛇标志是4,头在列表尾部
- SNAKE=[(9,10),(9,11),(9,12)]
- GRID=[[0 for x in range(cols)] for y in range(rows)]
- BACKUP=[[0 for x in range(cols)] for y in range(rows)]
- SCREEN=[[' ' for x in range(cols)] for y in range(rows)]
- NEW=1
-
- def game():
- global FIND
- global WAYPOINT
- global WAYPOINT2
- global APPLE
- global SNAKE
- global GRID
- global BACKUP
- global SCREEN
- global NEW
- while True:
- if NEW==1:
- #随机生成苹果的位置,标志是5
- while True:
- APPLE=(random.randint(2,20),random.randint(2,20))
- if GRID[APPLE[0]][APPLE[1]]==0:break
-
- ##########################################
- #这的被#括住的代码反复用到,本来要写成函数的,在函数内也用global声明了,可还是会报名称未定义的错误
- GRID=[[0 for x in range(cols)] for y in range(rows)]
- for x in range(cols):
- GRID[0][x]=1
- GRID[rows-1][x]=1
- for y in range(rows):
- GRID[y][0]=1
- GRID[y][cols-1]=1
- GRID[APPLE[0]][APPLE[1]]=5
- for body in SNAKE:
- GRID[body[0]][body[1]]=4
- ##########################################
- show()
-
- backupGrid()
- WAYPOINT=[]
- FIND=0
- search(SNAKE[-1][0],SNAKE[-1][1])
- sk=copy.deepcopy(SNAKE)
- restoreGrid()
- for point in WAYPOINT:
- SNAKE.append((point[0],point[1]))
- del SNAKE[0]
-
- ##########################################
- GRID=[[0 for x in range(cols)] for y in range(rows)]
- for x in range(cols):
- GRID[0][x]=1
- GRID[rows-1][x]=1
- for y in range(rows):
- GRID[y][0]=1
- GRID[y][cols-1]=1
- GRID[APPLE[0]][APPLE[1]]=5
- for body in SNAKE:
- GRID[body[0]][body[1]]=4
- ##########################################
-
- WAYPOINT2=[]
- FIND=0
- GRID[SNAKE[0][0]][SNAKE[0][1]]=2
- searchTail(SNAKE[-1][0],SNAKE[-1][1])
- restoreGrid()
- SNAKE=copy.deepcopy(sk)
-
- if FIND==1: #这块表示吃完苹果能活着回来,于是去吃
- for point in WAYPOINT:
- SNAKE.append((point[0],point[1]))
- del SNAKE[0]
-
- ##########################################
- GRID=[[0 for x in range(cols)] for y in range(rows)]
- for x in range(cols):
- GRID[0][x]=1
- GRID[rows-1][x]=1
- for y in range(rows):
- GRID[y][0]=1
- GRID[y][cols-1]=1
- GRID[APPLE[0]][APPLE[1]]=5
- for body in SNAKE:
- GRID[body[0]][body[1]]=4
- ##########################################
- show()
-
- if SNAKE[0][0]>SNAKE[1][0]:
- SNAKE.insert(0,(SNAKE[0][0]+1,SNAKE[0][1]))
- if SNAKE[0][1]>SNAKE[1][1]:
- SNAKE.insert(0,(SNAKE[0][0],SNAKE[0][1]+1))
- if SNAKE[0][1]<SNAKE[1][1]:
- SNAKE.insert(0,(SNAKE[0][0],SNAKE[0][1]-1))
- if SNAKE[0][0]<SNAKE[1][0]:
- SNAKE.insert(0,(SNAKE[0][0]-1,SNAKE[0][1]))
- NEW=1
- ##########################################
- GRID=[[0 for x in range(cols)] for y in range(rows)]
- for x in range(cols):
- GRID[0][x]=1
- GRID[rows-1][x]=1
- for y in range(rows):
- GRID[y][0]=1
- GRID[y][cols-1]=1
- GRID[APPLE[0]][APPLE[1]]=5
- for body in SNAKE:
- GRID[body[0]][body[1]]=4
- ##########################################
-
- else: #没找到安全路线就去追自己尾巴
- NEW=0 #下次循环不产生新苹果
- WAYPOINT2=[]
- FIND=0
- GRID[SNAKE[0][0]][SNAKE[0][1]]=2
- searchTail(SNAKE[-1][0],SNAKE[-1][1])
- restoreGrid()
- if FIND==1:
- for point in WAYPOINT2:
- SNAKE.append((point[0],point[1]))
- del SNAKE[0]
- ##########################################
- GRID=[[0 for x in range(cols)] for y in range(rows)]
- for x in range(cols):
- GRID[0][x]=1
- GRID[rows-1][x]=1
- for y in range(rows):
- GRID[y][0]=1
- GRID[y][cols-1]=1
- GRID[APPLE[0]][APPLE[1]]=5
- for body in SNAKE:
- GRID[body[0]][body[1]]=4
- ##########################################
- show()
- else:
- print('analog need improve...')
- os.system('pause >nul')
- exit()
-
- def show():
- global FIND
- global WAYPOINT
- global WAYPOINT2
- global APPLE
- global SNAKE
- global GRID
- global BACKUP
- global SCREEN
- SCREEN=[[' ' for x in range(cols)] for y in range(rows)]
- for y in range(rows):
- for x in range(cols):
- if GRID[y][x]==0:SCREEN[y][x]=' '
- if GRID[y][x]==1:SCREEN[y][x]='*'
- if GRID[y][x]==4:SCREEN[y][x]='.'
- if GRID[y][x]==5:SCREEN[y][x]='o'
- os.system('cls')
- for y in SCREEN:print(' '.join(y))
- time.sleep(0.05)
-
- def backupGrid():
- global FIND
- global WAYPOINT
- global WAYPOINT2
- global APPLE
- global SNAKE
- global GRID
- global BACKUP
- global SCREEN
- for y in range(rows):
- for x in range(cols):
- BACKUP[y][x]=GRID[y][x]
-
- def restoreGrid():
- global FIND
- global WAYPOINT
- global WAYPOINT2
- global APPLE
- global SNAKE
- global GRID
- global BACKUP
- global SCREEN
- for y in range(rows):
- for x in range(cols):
- GRID[y][x]=BACKUP[y][x]
-
- def searchTail(y,x):
- global FIND
- global WAYPOINT
- global WAYPOINT2
- global APPLE
- global SNAKE
- global GRID
- global BACKUP
- global SCREEN
- WAYPOINT2.append((y,x))
- if GRID[y][x]==2:
- FIND=1
- del WAYPOINT2[0]
- return
- GRID[y][x]=3
- if SNAKE[0][1]>x:
- if (GRID[y][x+1]==0 or GRID[y][x+1]==2)and FIND==0:searchTail(y,x+1)
- if (GRID[y+1][x]==0 or GRID[y+1][x]==2)and FIND==0:searchTail(y+1,x)
- if (GRID[y][x-1]==0 or GRID[y][x-1]==2)and FIND==0:searchTail(y,x-1)
- if (GRID[y-1][x]==0 or GRID[y-1][x]==2)and FIND==0:searchTail(y-1,x)
- if SNAKE[0][0]>y:
- if (GRID[y+1][x]==0 or GRID[y+1][x]==2)and FIND==0:searchTail(y+1,x)
- if (GRID[y][x-1]==0 or GRID[y][x-1]==2)and FIND==0:searchTail(y,x-1)
- if (GRID[y-1][x]==0 or GRID[y-1][x]==2)and FIND==0:searchTail(y-1,x)
- if (GRID[y][x+1]==0 or GRID[y][x+1]==2)and FIND==0:searchTail(y,x+1)
- if SNAKE[0][1]<x:
- if (GRID[y][x-1]==0 or GRID[y][x-1]==2)and FIND==0:searchTail(y,x-1)
- if (GRID[y-1][x]==0 or GRID[y-1][x]==2)and FIND==0:searchTail(y-1,x)
- if (GRID[y][x+1]==0 or GRID[y][x+1]==2)and FIND==0:searchTail(y,x+1)
- if (GRID[y+1][x]==0 or GRID[y+1][x]==2)and FIND==0:searchTail(y+1,x)
- if SNAKE[0][0]<y:
- if (GRID[y-1][x]==0 or GRID[y-1][x]==2)and FIND==0:searchTail(y-1,x)
- if (GRID[y][x+1]==0 or GRID[y][x+1]==2)and FIND==0:searchTail(y,x+1)
- if (GRID[y+1][x]==0 or GRID[y+1][x]==2)and FIND==0:searchTail(y+1,x)
- if (GRID[y][x-1]==0 or GRID[y][x-1]==2)and FIND==0:searchTail(y,x-1)
- if FIND==0:
- try:
- del WAYPOINT2[-1]
- except:return
-
- def search(y,x):
- global FIND
- global WAYPOINT
- global WAYPOINT2
- global APPLE
- global SNAKE
- global GRID
- global BACKUP
- global SCREEN
- WAYPOINT.append((y,x))
- if GRID[y][x]==5:
- FIND=1
- del WAYPOINT[0]
- return
- GRID[y][x]=3
- #顺时针搜索
- if APPLE[1]>x:
- if (GRID[y][x+1]==0 or GRID[y][x+1]==5)and FIND==0:search(y,x+1)
- if (GRID[y+1][x]==0 or GRID[y+1][x]==5)and FIND==0:search(y+1,x)
- if (GRID[y][x-1]==0 or GRID[y][x-1]==5)and FIND==0:search(y,x-1)
- if (GRID[y-1][x]==0 or GRID[y-1][x]==5)and FIND==0:search(y-1,x)
- if APPLE[0]>y:
- if (GRID[y+1][x]==0 or GRID[y+1][x]==5)and FIND==0:search(y+1,x)
- if (GRID[y][x-1]==0 or GRID[y][x-1]==5)and FIND==0:search(y,x-1)
- if (GRID[y-1][x]==0 or GRID[y-1][x]==5)and FIND==0:search(y-1,x)
- if (GRID[y][x+1]==0 or GRID[y][x+1]==5)and FIND==0:search(y,x+1)
- if APPLE[1]<x:
- if (GRID[y][x-1]==0 or GRID[y][x-1]==5)and FIND==0:search(y,x-1)
- if (GRID[y-1][x]==0 or GRID[y-1][x]==5)and FIND==0:search(y-1,x)
- if (GRID[y][x+1]==0 or GRID[y][x+1]==5)and FIND==0:search(y,x+1)
- if (GRID[y+1][x]==0 or GRID[y+1][x]==5)and FIND==0:search(y+1,x)
- if APPLE[0]<y:
- if (GRID[y-1][x]==0 or GRID[y-1][x]==5)and FIND==0:search(y-1,x)
- if (GRID[y][x+1]==0 or GRID[y][x+1]==5)and FIND==0:search(y,x+1)
- if (GRID[y+1][x]==0 or GRID[y+1][x]==5)and FIND==0:search(y+1,x)
- if (GRID[y][x-1]==0 or GRID[y][x-1]==5)and FIND==0:search(y,x-1)
- if FIND==0:
- try:
- del WAYPOINT[-1]
- except:
- print('SNAKE can not catch APPLE!')
- os.system('pause >nul')
- exit()
-
- game()
复制代码 全局变量太多了,特别能报错,用“#”括住的本来是个函数的,而且全局变量我都在函数内部用global声明了一遍可仍然会报"name not defined"错。
还有代码本身的问题,我用变量NEW表示game()的该次循环是否产生新的苹果,在代码后面FIND==0的情况我也对NEW进行了操作,可程序运行到后期出现了蛇还没吃到苹果,苹果就满屏幕跳来跳去的情况,望大神慷慨指教! |