python 迷宫生成路线

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
import sys
import os
import time
import turtle

turtle.screensize(800,600, "seashell")
turtle.pensize(16)
turtle.pencolor('tomato')

points = []

for i in range(16):
    points.append([])
    for j in range(16):
        points[i].append(' ')

points[0][0] = '↓'
turtle.goto(2,2)
turtle.pendown()
turtle.speed(10)
aPath = [{'x': 0, 'y': 0, 'd': 0, 't':'↓'}]
while True:

    if len(aPath) == 0:
        break
        
    item = aPath.pop( random.randint(max(0,len(aPath)-3), len(aPath)-1))
    tD = []

    turtle.goto(item['x']*20+2,item['y']*20+2)
    turtle.pendown()

    if item['x']+1 < 16 and points[item['x']+1][item['y']] == ' ':
        tD.append({'x':item['x']+1, 'y':item['y'],'t':'→'})
        if item['x'] == 0:
            tD.append({'x':item['x']+1, 'y':item['y'],'t':'→'})

    if item['x']-1 >= 0 and points[item['x']-1][item['y']] == ' ':
        tD.append({'x':item['x']-1, 'y':item['y'],'t':'←'})
        if item['x'] == 2:
            tD.append({'x':item['x']-1, 'y':item['y'],'t':'←'})

    if item['y']+1 < 16 and points[item['x']][item['y']+1] == ' ':
        tD.append({'x':item['x'], 'y':item['y']+1,'t':'↓'})
        if item['y'] == 0:
            tD.append({'x':item['x'], 'y':item['y']+1,'t':'↓'})

    if item['y']-1 >= 0 and points[item['x']][item['y']-1] == ' ':
        tD.append({'x':item['x'], 'y':item['y']-1,'t':'↑'})
        if item['y'] == 3:
           tD.append({'x':item['x'], 'y':item['y']-1,'t':'↑'})

    if len(tD) > 0:
        aPath.append(item)
        
        r = random.randint(0, len(tD)-1)
       
        points[tD[r]['x']][tD[r]['y']] = tD[r]['t']

        if tD[r]['t'] == '→':
            turtle.setheading(0)
        if tD[r]['t'] == '←':
            turtle.setheading(180)
        if tD[r]['t'] == '↓':
            turtle.setheading(90)
        if tD[r]['t'] == '↑':
            turtle.setheading(270)

        turtle.forward(22)

        aPath.append({'x': tD[r]['x'], 'y': tD[r]['y'], 'd': r})        
    turtle.penup()

sys.stdin.readline()

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值