python如何修改程序使蛇碰到墙壁结束进程

代码如下

import turtle as t
import random as r

class Snake():
    def __init__(self,long,color,walls):#成员函数
        self.walls=walls
        t.pendown()
        t.pensize(5)
        self.color=color
        t.pencolor(self.color)
        x0=0
        y0=0
        p0=(x0,y0)
        t.penup()
        t.goto(p0[0],p0[1])
        t.pendown()
        body=[]
        body.append(p0)
        t.hideturtle()
        for i in range(long):#****    
            arc=r.randint(-90,90)
            print(arc)
            t.seth(arc)
            t.forward(20)
            snake_point=t.pos()
            body.append(snake_point)
        self.body=body
        self.heading=t.heading()

    def erase_tail(self):
        t.penup()
        t.goto(self.body[0][0],self.body[0][1])
        t.pendown()
        t.pencolor('white')
        t.goto(self.body[1][0],self.body[1][1])
        for i in range(len(self.body)-1):#三节蛇,列表有四个元素
            self.body[i]=self.body[i+1]
        t.penup() 
        
    def go_forward(self):
        t.setheading(self.heading)
        long=len(self.body)-1        
        while True:    
            t.goto(self.body[long][0],self.body[long][1])               
            direction=r.randint(0,2)
            arc=r.randint(0,90)
            if direction==0:    
                t.left(arc)#往前走一步
            elif direction==1:
                pass
            elif direction==2:
                t.right(arc)#往前走一步
            t.forward(10)
            snake_point=t.pos()
            if self.walls.not_collision(snake_point):
                break
        t.goto(self.body[long][0],self.body[long][1])
        t.pendown()
        t.pencolor(self.color)
        t.goto(snake_point[0],snake_point[1])
        self.body[long]=snake_point
        self.heading=t.heading()
        
    def go(self):
        self.erase_tail()
        self.go_forward()
        
        
class Walls():
    def __init__(self,width,height,startx,starty):
        self.width=width
        self.height=height
        self.startx=startx
        self.starty=starty
        
    def not_collision(self,snake_point):
        if -self.width/2<snake_point[0]<self.width/2 and -self.height/2<snake_point[1]<self.height/2:
            return True 
        
    def drawWindow(self):
        t.delay(delay=0)
        t.setup(self.width,self.height, self.startx, self.starty)
        
    def drawWalls(self):
        drawRect(self.width,self.height,-self.width/2,self.height/2)        
    

def drawRect(width,height, startx, starty):
    t.penup()
    t.goto(startx, starty)
    t.setheading(0)
    t.pencolor('black')
    t.pendown()
    t.forward(width)
    t.right(90)
    t.forward(height)
    t.right(90)
    t.forward(width)
    t.right(90)
    t.forward(height)


walls=Walls(400,300,0,0)
walls.drawWindow()
walls.drawWalls()

snake1=Snake(4,'red',walls)
snake2=Snake(6,'green',walls)
snake3=Snake(8,'green',walls)

for _ in range(500):    
        
    snake1.go()          
    
    snake2.go()
    snake3.go()
    

t.done()

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答

本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。

​​​​因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

sys.exit(0)