Coder Social home page Coder Social logo

import turtle import time import random

delay = 0.1 score = 0

Create window

wn = turtle.Screen() wn.title("Snake Game") wn.bgcolor("black") wn.setup(width=600, height=600) wn.tracer(0)

Create border

border = turtle.Turtle() border.speed(0) border.color("white") border.penup() border.hideturtle() border.goto(-290, -290) border.pendown() for _ in range(4): border.forward(580) border.left(90)

Create snake head

head = turtle.Turtle() head.speed(0) head.shape("square") head.color("white") head.penup() head.goto(0, 0) head.direction = "Right"

Create fruit

fruit = turtle.Turtle() fruit.speed(0) fruit.shape("circle") fruit.color("red") fruit.penup() fruit.goto(random.randint(-270, 270), random.randint(-270, 270))

Create score display

score_display = turtle.Turtle() score_display.speed(0) score_display.color("white") score_display.penup() score_display.hideturtle() score_display.goto(0, 260) score_display.write("Score: {}".format(score), align="center", font=("Courier", 24, "normal"))

Movement functions

def go_up(): if head.direction != "Down": head.direction = "Up"

def go_down(): if head.direction != "Up": head.direction = "Down"

def go_left(): if head.direction != "Right": head.direction = "Left"

def go_right(): if head.direction != "Left": head.direction = "Right"

Set up key bindings

wn.listen() wn.onkey(go_up, "w") wn.onkey(go_down, "s") wn.onkey(go_left, "a") wn.onkey(go_right, "d")

Main game loop

while True: wn.update()

# Move the snake head
if head.direction == "Up":
    head.sety(head.ycor() + 20)
elif head.direction == "Down":
    head.sety(head.ycor() - 20)
elif head.direction == "Left":
    head.setx(head.xcor() - 20)
elif head.direction == "Right":
    head.setx(head.xcor() + 20)

# Check for collision with the borders
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
    time.sleep(1)
    head.goto(0, 0)
    head.direction = "Right"

# Check for collision with the fruit
if (
    fruit.xcor() - 15 < head.xcor() < fruit.xcor() + 15
    and fruit.ycor() - 15 < head.ycor() < fruit.ycor() + 15
):
    score += 1
    score_display.clear()
    score_display.write("Score: {}".format(score), align="center", font=("Courier", 24, "normal"))
    fruit.goto(random.randint(-270, 270), random.randint(-270, 270))

# Delay for smooth movement
time.sleep(delay)

somayehzhr's Projects

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.