Make a Snake Game in Python | Turtle | Python Project

preview_player
Показать описание
How to make a snake game using python. Here we will make the classic snake game with python.

------------------------------------------
Music Credit: Lakeyinspired
------------------------------------------
Рекомендации по теме
Комментарии
Автор

if somebody has trouble that the snake is not running just run it again

rekhNZ
Автор

i love this channel .this channel deserves more subs

thelmaperera
Автор

i use pycharm but when i start plying the snake does not moved

alimouricppcoding
Автор

Bro i tried alot but snake doesn't moved from his place....now what did i do?

mannisharma
Автор

#imports
import turtle
import time
import random

delay = 0.1

#scores
score = 0
high_score = 0

#set up screen
wn = turtle.screen()
wn.title("snake game")
wn.bgcolor('yellow')
wn.setup(width=600, hight=600)
wn.tracer(0)

#snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto()
head.direction = "stop"

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

segments = []

#scoreboards
sc = turtle.Turtle()
sc.speed(0)
sc.shape("square")
sc.color("black")
sc.penup()
sc.hideturtle()
sc.goto(0, 260)
sc.write("score: 0 High score: 0", align = "center", font=("ds-digital", 24, "normal"))

#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":
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y+20)
if head.direction == "down":
y = head.ycor()
head.sety(y-20)
if head.direction == "left":
x = head.xcor()
head.setx(x-20)
if head.direction == "right":
x = head.xcor()
head.setx(x+20)

#keyboard bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")

#MainLoop
while True:
wn.update()

#check collision with border area
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 = "stop"

#hide the segments of body
for segment in segments:
segment.goto(1000, 1000) #out of range
#clear the segments
segments.clear()

#reset score
score = 0

#reset delay
delay = 0.1

sc.clear()
sc.write("score: {} High score: {}" .format(score, high_score), align="center", font=("ds-digital", 24, "normal"))

#check collision with food
if head.distance(food) <20:
#move the food to random place
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)

#add a new segment to the head
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("black")
new_segment.penup()
segments.append(new_segment)

#shorten the delay
delay -= 0.001
#increase the score
score += 10

if score > high_score:
high_score = score
sc.clear()
sc.write("score: {} High Score: {}" .format(score, high_score), align="center", font=("ds-digital", 24, "normal"))

#move the segments in reverse order
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
#move segment 0 to head
if len(segments)>0
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)

move()

#check for collision with body
for segment in segments:
if segment.distance(head)<20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"

#hide segments
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1

#update the score
sc.clear()
sc.write("score: {} High Score: {}" .format(score, high_score), align="center", font=("ds-digital", 24, "normal"))
time.sleep(delay)
wn.mainloop()

AQD_Aziz
Автор

I Made a snake game and it worked! BIG TIME.

citosworld
Автор

Please help me, can someone tell me why the snake game what I made doesn't move? I had press (W A S D) but still can move😭

musyarofahhumaira
Автор

After finishing the code and getting the interface of the game which key I have to press to start the game?

mohammadadnan
Автор

Thanks bro it was entertainment day for making snake game
Thank you so much

dhiyaneshwarchessPro
Автор

in order to run this program i just need to download python turtle right?

zach
Автор

how can i add a property which makes this snake faster as long as i keep holding the button

berbatsuleiman
Автор

Hello im having a problem in the line wn.update() it says exception has occurred: terminator X

apollonmegara
Автор

Bro I need help with line 134 on the code the x has a squiggly red line under it and don’t know how to fix

keirathatwal
Автор

I’m getting the turtle.terminator error with wn.update(). Is there a way to fix this?

deviant
Автор

Hello I made this game in pydriod in my android phone and the bug is that the Snake is not moving could you tell me hove to solve this ??

HavkerjNsnsndjw
Автор

everything work find but one thing that I feel almost give up why can't I start to move? I follow ever single code you write and double check it and nothing wring but I just can't move how to fix it

darkwolf
Автор

Oh my God BRO!, I think you are tired of this video !! Really cool !! I've just put you a "like" when I arrive in the middle of the video !! OK, I will but " subscribe "

mmoonnaa
Автор

all perfet but my snake is not movig i am pressing W A S D Keywords but no change please help

RajBaghel
Автор

Can you please tell me what should I do it says "AttributeError: 'int' object has no attribute ' speed'

alexutz
Автор

Could somebody explain these lines, please?:
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 len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)

razvan