Snake Program in Python

Post your programming questions here regarding Microsoft Agent or related topics. Recommended languages: Visual Basic 5.0/6.0, Visual Basic .NET
Post Reply
User avatar
united123
Posts: 105
Joined: Tue Feb 10, 2026 7:28 am
Location: India
Favorite Microsoft Agent: Bonzi
Operating System: Windows 11 Pro 25H2
Contact:

Snake Program in Python

Post by united123 »

I know it's quite a basic program, but still, Here's the code:

Code: Select all

import turtle as t
import random
import time

d = 0.1
s = 0
hs = 0

# screen
sc = t.Screen()
sc.title("Snake Game")
sc.bgcolor("black")
sc.setup(width=600, height=600)
sc.tracer(0)

# snake head
h = t.Turtle()
h.shape("square")
h.color("white")
h.penup()
h.goto(0, 0)
h.direction = "Stop"

# food
f = t.Turtle()
f.speed(0)
f.shape(random.choice(['square', 'triangle', 'circle']))
f.color(random.choice(['red', 'green', 'blue']))
f.penup()
f.goto(0, 100)

# score display
p = t.Turtle()
p.speed(0)
p.shape("square")
p.color("red")
p.penup()
p.hideturtle()
p.goto(0, 250)
p.write("Score : 0  High Score : 0", align="center", font=("candara", 24, "bold"))

# game over display
g = t.Turtle()
g.speed(0)
g.color("yellow")
g.penup()
g.hideturtle()

# Direction functions
def up():
    if h.direction != "down":
        h.direction = "up"

def down():
    if h.direction != "up":
        h.direction = "down"

def left():
    if h.direction != "right":
        h.direction = "left"

def right():
    if h.direction != "left":
        h.direction = "right"

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

def reset_game():
    global s, d, seg
    time.sleep(1)
    h.goto(0, 0)
    h.direction = "Stop"
    f.goto(random.randint(-270, 270), random.randint(-270, 270))
    for segment in seg:
        segment.goto(1000, 1000)
    seg.clear()
    s = 0
    d = 0.1
    p.clear()
    p.write(f"Score : {s} High Score : {hs}", align="center", font=("candara", 24, "bold"))
    g.clear()
    g.goto(0, 0)
    g.write("Game Over\nPress Enter to restart", align="center", font=("candara", 24, "bold"))
    sc.listen()
    sc.onkeypress(start_game, "Return")

def start_game():
    g.clear()
    sc.onkeypress(up, "Up")
    sc.onkeypress(down, "Down")
    sc.onkeypress(left, "Left")
    sc.onkeypress(right, "Right")

# Key bindings
sc.listen()
sc.onkeypress(up, "Up")
sc.onkeypress(down, "Down")
sc.onkeypress(left, "Left")
sc.onkeypress(right, "Right")

# Snake body segments
seg = []

# Main Gameplay
try:
    while True:
        sc.update()

        # Boundary check
        if h.xcor() > 290 or h.xcor() < -290 or h.ycor() > 290 or h.ycor() < -290:
            reset_game()

        # Food collision
        if h.distance(f) < 20:
            f.goto(random.randint(-270, 270), random.randint(-270, 270))
            n_seg = t.Turtle()
            n_seg.speed(0)
            n_seg.shape("square")
            n_seg.color("orange")
            n_seg.penup()
            seg.append(n_seg)
            d -= 0.001
            s += 1
            if s > hs:
                hs = s
            p.clear()
            p.write(f"Score : {s} High Score : {hs}", align="center", font=("candara", 24, "bold"))

        # Move segments
        for i in range(len(seg)-1, 0, -1):
            x, y = seg[i-1].xcor(), seg[i-1].ycor()
            seg[i].goto(x, y)

        if len(seg) > 0:
            x, y = h.xcor(), h.ycor()
            seg[0].goto(x, y)

        move()

        # Self-collision
        for segment in seg:
            if segment.distance(h) < 20:
                reset_game()

        time.sleep(d)

except t.Terminator:
    print("Game closed by user.")
Hey there!

I am United World, who is a Techy, Mapper, Ident uploader and an MS Agent Tuber.

Image
User avatar
united123
Posts: 105
Joined: Tue Feb 10, 2026 7:28 am
Location: India
Favorite Microsoft Agent: Bonzi
Operating System: Windows 11 Pro 25H2
Contact:

Re: Snake Program in Python

Post by united123 »

Here's the video about it:

Hey there!

I am United World, who is a Techy, Mapper, Ident uploader and an MS Agent Tuber.

Image
Post Reply