Hungry_Anaconda

import turtle
import time
import random

d = 0.1
score = 0
hscore = 0

win = turtle.Screen()
win.title(" Hungry AnaConDa @By Sallu@@")
win.bgcolor("light blue")
win.setup(width=600, height=600)
win.tracer(0)

an = turtle.Turtle()
an.speed(0)
an.shape("square")
an.color("dark green")
an.penup()
an.goto(0,0)
an.direction = "stop"

#food
meat = turtle.Turtle()
meat.speed(0)
meat.shape("circle")
meat.color("red")
meat.penup()
meat.goto(0,100)

body =[]

view = turtle.Turtle()
view.speed(0)
view.shape("square")
view.color("yellow")
view.penup()
view.hideturtle()
view.goto(0, 260)
view.write("Score : 0  High Score : 0",align="center",font=("Showcard Gothic",24,"bold"))


def up():
    if an.direction != "down" :
        an.direction = "up"
def down():
    if an.direction != "up" :
        an.direction = "down"
def left():
    if an.direction != "right" :
        an.direction = "left"
def right():
    if an.direction != "left" :
        an.direction = "right"

def move():
    if an.direction == "up":
        y = an.ycor()
        an.sety(y + 20)
        
    if an.direction == "down":
        y = an.ycor()
        an.sety(y - 20)
        
    if an.direction == "left":
        x = an.xcor()
        an.setx(x - 20)
        
    if an.direction == "right":
        x = an.xcor()
        an.setx(x + 20)

win.listen()
win.onkeypress(up,"Up")
win.onkeypress(down,"Down")
win.onkeypress(left,"Left")
win.onkeypress(right,"Right")

while True:
    win.update()
    #Checking is it under the border
    if an.xcor() > 290 or an.xcor() < -290 or an.ycor() > 290 or an.ycor() < -290:
        time.sleep(0.5)
        an.goto(0,0)
        an.direction = "stop"
        for s in body:
            s.goto(4000,4000)
        body.clear()
        #reset score and delay
        d =0.1
        score = 0
        view.clear()
        view.goto(0,0)
        view.color("dark blue")
        view.write("!! GAME OVER !!",align="center",font=("Snap ITC",30,"bold"))
        time.sleep(1)
        view.clear()
        view.goto(0,260)
        view.color("yellow")
        view.write("Score : {}  High Score : {}".format(score,hscore),align="center",font=("Showcard Gothic",24,"bold"))
        
    #move the food
        
    if an.distance(meat) < 20 :

        x = random.randint(-245,290)
        y = random.randint(-245,290)
        meat.goto(x,y)

    #grow
        nbody = turtle.Turtle()
        nbody.speed(0)
        nbody.shape("square")
        nbody.color("green")
        nbody.penup()
        body.append(nbody)

        #shorten the delay
        d -= 0.001
        

        #increse the score

        score += 10

        if score > hscore :
            hscore = score

        view.clear()
        view.write("Score : {}  High Score : {}".format(score,hscore),align="center",font=("Showcard Gothic",24,"bold"))
        

    #move from the end
    for i in range(len(body)-1 ,0, -1):
        x = body[i-1].xcor()
        y = body[i-1].ycor()
        body[i].goto(x,y)

    if len(body) > 0 :
        x = an.xcor()
        y = an.ycor()
        body[0].goto(x,y)
        
    move()

    #check is it touches its body after move
    for s in body:
        if s.distance(an) < 20 :
            time.sleep(0.5)
            an.goto(0,0)
            an.direction = "stop"

            for sg in body:
                sg.goto(4000,4000)
            body.clear()
            #reset the delay
            d = 0.1
            score = 0
            view.clear()
            view.goto(0,0)
            view.color("dark blue")
            view.write("!! GAME OVER !!",align="center",font=("Snap ITC",30,"bold"))
            time.sleep(1)
            view.clear()
            view.goto(0, 260)
            view.color("yellow")
            view.write("Score : {}  High Score : {}".format(score,hscore),align="center",font=("Showcard Gothic",24,"bold"))

            
    time.sleep(d)

win.mainloop()

Comments