Reputation: 25
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.
Upvotes: 2
Views: 337
Reputation: 56925
The collision check is only happening one time when the game starts, not on every frame.
You can put the logic into a function and call it in each of your movement loops on every step:
def eat_food_if_close():
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)
Call for each direction like:
def derecha():
while head.direction != 'left':
head.direction = 'right'
x = head.xcor()
head.setx(x + 20)
eat_food_if_close() # <---
Another possible solution is to use ontimer
to trigger the test to run every, say, 30 milliseconds:
# ...
def tick():
camara.ontimer(tick, 30)
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)
tick()
camara.mainloop() # blocks forever, don't add code below this
The app might run smoother if you disable the internal turtle loop and use the ontimer
loop to run everything alongside turtle.update()
. An example is here, but this might be too large a refactor to thrust you into just yet, so I'll leave it as a concept.
Upvotes: 1