Skip to content

Instantly share code, notes, and snippets.

@ProgDan
Created June 12, 2020 19:33
Show Gist options
  • Save ProgDan/e191f087208690ec78da520f3def2239 to your computer and use it in GitHub Desktop.
Save ProgDan/e191f087208690ec78da520f3def2239 to your computer and use it in GitHub Desktop.
import turtle
import time
import random
delay = 0.1
segments = []
# placar
score = 0
high_score = 0
# Configura a Tela
win = turtle.Screen()
win.title("Snake Game")
win.bgcolor("blue")
win.setup(width=600,height=600)
win.tracer(0)
# Cabeça da Cobra
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 100)
head.direction = "stop"
# Comida para a Cobra
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.shapesize(0.50, 0.50)
food.goto(0,0)
# exibir o placar
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: {}".format(high_score), align="center", font=("Courier", 24, "normal"))
# Funcao de Movimentacao
def move():
# move o segmento 0 onde a cabeça da cobra está
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
if head.direction == "up":
y = head.ycor() # coordenada y da turtle
head.sety(y + 20)
if head.direction == "down":
y = head.ycor() # coordenada y da turtle
head.sety(y - 20)
if head.direction == "right":
x = head.xcor() # coordenada x da turtle
head.setx(x + 20)
if head.direction == "left":
x = head.xcor() # coordenada y da turtle
head.setx(x - 20)
# move os segmentos da cobra
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
def go_up():
if head.direction != "down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_right():
if head.direction != "left":
head.direction = "right"
def go_left():
if head.direction != "right":
head.direction = "left"
# controle do teclado
win.listen()
win.onkey(go_up, "w")
win.onkey(go_down, "s")
win.onkey(go_right, "d")
win.onkey(go_left, "a")
# Loop Principal
while True:
win.update()
move()
time.sleep(delay)
if head.distance(food) < 15:
y = random.randint(-290,290)
x = random.randint(-290,290)
food.goto(x,y)
# aumenta a pontuacao
score = score+10
if score > high_score:
high_score = score
# inclui um segmento
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
# Checa por colisões
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"
score = 0
# remove os segmentos
for segment in segments:
segment.goto(1000, 1000)
# limpa a lista de segmentos
segments = []
# Checa por colisões no corpo
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
score = 0
# remove os segmentos
for segment in segments:
segment.goto(1000, 1000)
# limpa a lista de segmentos
segments = []
# atualiza a pontuacao
pen.clear()
pen.write("score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment