python实现吃满屏幕的贪吃蛇
```pyimport pygame
from sys import exit
import random, math
pygame.init()
mapW = 300
mapH = 330
ROW = 11
COL = 10
# 0 * 0 ~ 29 * 19
# pixelSize = mapH / ROW
dirs = [,,,[-1,0],]
dirNow = 1
dirPause = 4
window = pygame.display.set_mode((mapW, mapH))
pygame.display.set_caption('Snack')
snack = [(ROW // 2, COL // 2)]
food = [(ROW - 1, COL - 1)]
colors = [(200, 147, 158), (0, 158, 128)]
def drawRect(pos, color):
RecX = pos * mapH / ROW
RecY = pos * mapW / COL
pygame.draw.rect(window, color, (RecX, RecY, mapH / ROW, mapW / COL))
def ex(pos):
if(pos < 0 or pos < 0 or pos > ROW - 1 or pos > COL - 1):
return False
return True
def gen_food():
newFood = (random.randint(0, ROW - 1), random.randint(0, COL - 1))
while(newFood in snack):
newFood = (random.randint(0, ROW - 1), random.randint(0, COL - 1))
food.append(newFood)
def bfs(st, ed, block):
flag = False
queue =
pre =
head = 0
while(head < len(queue)):
newNode = queue
head+=1
rd = random.randint(0,3)
for i in range(4):
i = (i + rd) % 4
ppos = (newNode + dirs, newNode + dirs)
if not ex(ppos):
continue
if ppos == ed:
queue.append(ppos)
pre.append(head - 1)
flag = True
break
if ppos in block:
continue
if ppos not in queue:
queue.append(ppos)
pre.append(head - 1)
if flag:
break
res = []
last = len(queue) - 1
if flag :
while last != pre:
res.append(queue)
last = pre
return res[::-1]
def get_dir(pos1, pos2):
for i in range(4):
if((pos1 + dirs, pos1 + dirs) == pos2):
return i
return 4
quitFlag = True
clock = pygame.time.Clock()
road = []
def finddir(snack):
res = []
for i in range(4):
Node = (snack + dirs, snack + dirs)
if Node in snack:
continue
if not ex(Node):
continue
if len(bfs(Node, snack[-2], + snack[:-2])) > 0:
res.append(Node)
if(len(res) <= 0):
debug = 1
return res
ret = res
for i in res:
if(abs(i - food) + abs(i - food) > abs(ret - food) + abs(ret - food)):
ret = i
return
while quitFlag:
clock.tick(60)
# 获取事件,包括键盘事件和退出事件,也可防止卡死
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitFlag = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
t = dirPause
dirPause = t
dirNow = dirPause
if len(road) == 0:
nextPath = bfs(snack, food, snack)
findTail = bfs(snack, snack[-1], snack)
if len(nextPath) > 0:
if(len(nextPath) > len(snack)):
findTail2 = bfs(nextPath[-1], nextPath[-len(snack) - 1], nextPath[-1:-len(snack) - 2])
else:
findTail2 = bfs(nextPath[-1], snack, nextPath + snack)
if len(findTail2) > 0:
road = nextPath
else:
road = finddir(snack)
else :
road = finddir(snack)
if len(road) == 0:
road = findTail
nextRec = road
road.pop(0)
if ex(nextRec):
if(nextRec not in food):
snack.pop()
else :
food.pop()
gen_food()
snack.insert(0, nextRec)
else:
print("Game over at %d %d"%(nextRec,nextRec))
quitFlag = False
pygame.draw.rect(window, (245, 135, 155), (0, 0, mapW, mapH))
for body in snack:
drawRect(body, colors)
drawRect(snack, (255,255,255))
for body in food:
drawRect(body, colors)
pygame.display.flip()
```
!(data/attachment/forum/202003/14/232951n88duf9eeex9biui.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600 "snk.png")
效果
收藏!{:4_132:}
页:
[1]