贪吃蛇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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值