Snake game in python(Resolved) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Snake game in python(Resolved)

Hello, I am creating the snake video game in python with the turtle module, my problem is that when I am testing the game I cannot turn to the right because when I do it the head of the snake stops This is the code: import turtle import time Posponer = 0.1 #Configuracion de la ventana Ventana = turtle.Screen() Ventana.title("Snake") Ventana.bgcolor("black") Ventana.setup(width = 600, height= 600) #Dimesiones de la ventana Ventana.tracer(0) #Mejora las animaciones #Cabeza serpiente Cabeza = turtle.Turtle() #Crear un elemento turtle Cabeza.speed(0) Cabeza.shape("square") #Define la forma del elemento Cabeza.color("white") Cabeza.penup() #El elemento no deja rastros Cabeza.goto(0,0) #Define la posicion del elemento Cabeza.direction = "stop" #Define la direccion del elemento #Funciones def arriba(): Cabeza.direction = "up" def abajo(): Cabeza.direction = "down" def derecha(): Cabeza.direction = "rigth" def izquierda(): Cabeza.direction = "left" def movimiento(): if Cabeza.direction == 'up': y = Cabeza.ycor() Cabeza.sety(y + 20) elif Cabeza.direction == 'down': y = Cabeza.ycor() Cabeza.sety(y - 20) elif Cabeza.direction == 'left': x = Cabeza.xcor() Cabeza.setx(x - 20) elif Cabeza.direction == 'right': x = Cabeza.xcor() Cabeza.setx(x + 20) #Teclado Ventana.listen() #Capta las acciones en el teclado Ventana.onkeypress(arriba, "w") #Si se presiona una tecla activa los parametros asignados Ventana.onkeypress(abajo,"s") Ventana.onkeypress(izquierda,"a") Ventana.onkeypress(derecha,"d") while True: Ventana.update() movimiento() time.sleep(Posponer) Ventana.mainloop() #Esto va a hacer que la ventana no se cierre hasta que le hagas click en cerrar. Practicamente entra en un bucle infinito hasta que tu mismo cierras la ventana.

3rd Jan 2021, 11:25 PM
ISA
ISA - avatar
1 Answer
+ 3
Maybe the issue is the spelling right here in derecha() function. # Try to change: def derecha(): Cabeza.direction = "rigth" # to: def derecha(): Cabeza.direction = "right"
4th Jan 2021, 1:29 AM
noteve
noteve - avatar