贪吃蛇Python实现

import random
import turtle
import time
def setbackground():
    global page
    page = turtle.Screen()
    page.title('Snake Game')
    page.setup(width = 660, height = 740)
    page.tracer(0)

def setframe():
    frame = turtle.Turtle()
    frame.speed(0)
    frame.hideturtle()
    frame.penup()
    frame.goto(-250,210)
    frame.pendown()
    frame.goto(-250,-290)
    frame.goto(250,-290)
    frame.goto(250,290)
    frame.goto(-250,290)
    frame.goto(-250,210)
    frame.goto(250,210)

def setsnakehead():
    global head
    global headdir
    head = turtle.Turtle()
    head.speed(0)
    head.shape('square')
    head.color('red')
    head.penup()
    head.goto(0,-40)
    headdir = 'stop'

def setfoodlist(xpositiondomainli,ypositiondomainli):
    for i in range(9):
        foodlist[i].speed(0)
        foodlist[i].hideturtle()
        foodlist[i].shape('square')
        foodlist[i].color('black')
        foodlist[i].up()
        foodlist[i].goto(xpositiondomainli[i],ypositiondomainli[i]-15)
        foodlist[i].write(i+1,align='center',font=('arial',15,'normal'))
        foodlist[i].goto(xpositiondomainli[i],ypositiondomainli[i])

def judgeeatfood(head,segments):
    for i in range(9):
        if foodlist[i]:
            if abs(xpositiondomainli[i] - head.xcor()) < 20 and abs(ypositiondomainli[i] - head.ycor()) < 20:
                foodlist[i].clear()
                foodlist[i] = None
                n=0
                while n <= i:
                    new_tail = turtle.Turtle()
                    new_tail.speed(0)
                    new_tail.shape('square')
                    new_tail.color('black','blue')
                    new_tail.penup()
                    new_tail.goto(1000,1000)
                    segments.append(new_tail)
                    n += 1

def setmonster(xpositiondomainli,ypositiondomainli):
    global monster
    monster = turtle.Turtle()
    monster.speed(5)
    monster.shape('square')
    monster.color('black')
    monster.penup()
    monster.goto(xpositiondomainli[20]+10,ypositiondomainli[20]+10)

def readymove(judgement):
    page.listen()
    page.onkeypress(headup,'Up')
    page.onkeypress(headdown,'Down')
    page.onkeypress(headleft,'Left')
    page.onkeypress(headright,'Right')
    page.onkeypress(headstop,'space')

def headstop():
    global headdir
    headdir = 'stop'
def headup():
    global headdir
    headdir = 'up'
def headdown():
    global headdir
    headdir = 'down'
def headright():
    global headdir
    headdir = 'right'
def headleft():
    global headdir
    headdir = 'left'

def move():
    if headdir == 'up':
        y = head.ycor()
        head.sety(y+20)
    if headdir == 'down':
        y = head.ycor()
        head.sety(y-20)
    if headdir == 'left':
        x = head.xcor()
        head.setx(x-20)
    if headdir == 'right':
        x = head.xcor()
        head.setx(x+20)

def monstermove():
    global judgement
    global headdir
    x = monster.xcor() - head.xcor()
    y = monster.ycor() - head.ycor()
    if abs(x) > 10 or abs(y) > 10:
        if abs(x) > abs(y):
            if x >= 0:
                monster.goto(monster.xcor()-20,monster.ycor())
            else:
                monster.goto(monster.xcor()+20,monster.ycor())
        else:
            if y >= 0:
                monster.goto(monster.xcor(),monster.ycor()-20)
            else:
                monster.goto(monster.xcor(),monster.ycor()+20)
    else:
        monster.write('Game over! ',align='right',font=('Arial',15,'normal'))
        judgement = True
        headdir = 'stop'
    page.update()

def collpsewall():
    global headdir
    if headdir == 'left' and head.xcor() <= -235:
        headdir = 'stop'
    if headdir == 'right' and head.xcor() >= 235:
        headdir = 'stop'
    if headdir == 'up' and head.ycor() >= 200:
        headdir = 'stop'
    if headdir == 'down' and head.ycor() <= -280:
        headdir = 'stop'

def updateinformation():
    global contact
    global headdir
    global information
    information = turtle.Turtle()
    information.penup()
    information.goto(0,230)
    information.hideturtle()
    txt = 'Contact:     ' + str(contact) + '     Time:'+ str(time1) +'      Motion:'+ headdir
    information.write(txt,align='center',font=('Arial',20,'normal'))

def ui():
    global description
    description = turtle.Turtle()
    description.penup()
    description.hideturtle()
    description.setpos(-50,0)
    txt = '''\tWelcome to snake game ... \n
               You are going to use Up, Down, Left, Right keys to move the snake \n
               around the screen, trying to consume all the food items \n
               before the moster catches you ... \n
               Click anywhere on the screen to start the game, have fun!!!'''
    description.write(txt,align ='center',font = ('Arial',11,'normal'))

def main():
    global xpositiondomainli
    global ypositiondomainli
    global foodlist
    global food1
    global food2
    global food3
    global food4
    global food5
    global food6
    global food7
    global food8
    global food9
    global segments
    global judgement
    global contact
    global time1
    global headdir
    judgement = False
    time1 = 0
    segments = []
    contact = 0
    xpositiondomainli = [i for i in range(-240,241,20)]
    ypositiondomainli = [i for i in range(-280,201,20)]
    random.shuffle(xpositiondomainli)
    random.shuffle(ypositiondomainli)
    food1 = turtle.Turtle()
    food2 = turtle.Turtle()
    food3 = turtle.Turtle()
    food4 = turtle.Turtle()
    food5 = turtle.Turtle()
    food6 = turtle.Turtle()
    food7 = turtle.Turtle()
    food8 = turtle.Turtle()
    food9 = turtle.Turtle()
    foodlist = [food1,food2,food3,food4,food5,food6,food7,food8,food9]
    headdir = 'stop'
    delay = 0.2
    n = 0
    delay2 = 1

    setbackground()
    page.update()

    setframe()
    setsnakehead()
    setmonster(xpositiondomainli,ypositiondomainli)
    setbackground()
    setframe()
    readymove(judgement)

    while n < 4:
        information2 = turtle.Turtle()
        information2.hideturtle()
        information2.penup()
        information2.goto(0,230)
        txt2 = 'Contact:    ' + str(contact) + '    Time:'+ str(time1) +'     Motion:'+ headdir
        information2.write(txt2,align='center',font=('Arial',20,'normal'))
        ui()
        description.clear()
        information2.clear()  
        time.sleep(delay2)
        n+=1

    setfoodlist(xpositiondomainli,ypositiondomainli)

    while not judgement:
        time.sleep(delay)
        page.update()

        updateinformation()
        collpsewall()
        judgeeatfood(head,segments)

        if len(segments) >= 45:
            judgement = True
            headdir = 'stop'
        if not judgement:
            if headdir == 'up' or headdir == 'down' or headdir == 'right' or headdir == 'left':
                for i in range(len(segments)-1,0,-1):
                    x = segments[i-1].xcor()
                    y = segments[i-1].ycor()
                    segments[i].goto(x,y)
                    if monster.xcor() == x+10 and monster.ycor() == y+10:
                        contact += 1
                if len(segments)>0:
                    x = head.xcor()
                    y = head.ycor()
                    segments[0].goto(x,y)
        
        time1 += 1
        monstermove()
        move()
        page.update()
        information.clear()

    information1 = turtle.Turtle()
    information1.penup()
    information1.goto(0,230)
    information1.hideturtle()
    txt1 = 'Contact:    ' + str(contact) + '    Time:'+ str(time1) +'     Motion:'+ headdir
    information1.write(txt1,align='center',font=('Arial',20,'normal'))
    if len(segments) >= 45:
        information1.goto(0,-40)
        information1.write('Winner!',align='center',font=('Arial',40,'normal'))

    page.mainloop()

main()