import turtle
import time
import random
camara = turtle.Screen()
camara.title('bienvenido al juego de la serpiente.')
camara.setup(width=600, height=600)
camara.bgcolor("blue")
score = 0
total_score = 0
#texto
text = turtle.Turtle()
text.penup()
text.hideturtle()
text.color('red')
text.speed(0)
text.setpos(0, 250)
text.write(f'actual score {score} total score {total_score}', move=False, align='center', font=('candara', 24, 'bold'))
#cabeza
head = turtle.Turtle()
head.speed(1.4)
head.direction = 'stop'
head.shape('square')
head.shapesize(stretch_wid=1.1, stretch_len=1.1, outline=None)
head.color('red')
head.penup()
head.setpos(0, 0)
def arriba():
while head.direction != 'down':
head.direction = 'up'
y = head.ycor()
head.sety(y + 20)
def abajo():
while head.direction != 'up':
head.direction = 'down'
y = head.ycor()
head.sety(y - 20)
def izquierda():
while head.direction != 'right':
head.direction = 'left'
x = head.xcor()
head.setx(x - 20)
def derecha():
while head.direction != 'left':
head.direction = 'right'
x = head.xcor()
head.setx(x + 20)
camara.listen()
camara.onkeypress(arriba, 'w')
camara.onkeypress(izquierda, 'a')
camara.onkeypress(abajo, 's')
camara.onkeypress(derecha, 'd')
#food
food = turtle.Turtle()
food.penup()
food.shape('square')
food.speed(0)
x1 = random.randint(-270, 270)
y1 = random.randint(-270, 270)
food.setpos(x1, y1)
if food.distance(head) < 20:
food.penup()
food.shape('square')
food.speed(0)
x1 = random.randint(-270, 270)
y1 = random.randint(-270, 270)
food.setpos(x1, y1)
camara.mainloop()
hello guys, I'm trying to make the "snake game", I did an algorithm, when the snakes touches the food, then the food disappears and appears again in a random position in the map, but in somehow, it doesn't seems it's working, since when the snakes touches the food, the food still keeps in the same position, so after that, I made this algorithm (adding a while True):
food = turtle.Turtle()
while True:
food.penup()
food.shape('square')
food.speed(0)
x1 = random.randint(-270, 270)
y1 = random.randint(-270, 270)
food.setpos(x1, y1)
to figure it out if the food was appearing randomly well enough, so it did, but when I started to move the snake head, the loop stopped running, (the snakes keeps moving), and the food stays put. I mean... when I execute these lines of code:
def arriba():
while head.direction != 'down':
head.direction = 'up'
y = head.ycor()
head.sety(y + 20)
def abajo():
while head.direction != 'up':
head.direction = 'down'
y = head.ycor()
head.sety(y - 20)
def izquierda():
while head.direction != 'right':
head.direction = 'left'
x = head.xcor()
head.setx(x - 20)
def derecha():
while head.direction != 'left':
head.direction = 'right'
x = head.xcor()
head.setx(x + 20)
camara.listen()
camara.onkeypress(arriba, 'w')
camara.onkeypress(izquierda, 'a')
camara.onkeypress(abajo, 's')
camara.onkeypress(derecha, 'd')
the rest of the code stops running in a manner of speaking. so what's being the problem here? what should I do to make food appearing randomly each time the snake touches the food? thank you.