Search code examples
pythonturtle-graphicspython-turtle

Turtle screen closes almost immediately after opening


I writed some code from a python book and the book shows that if you run the code there will be no problem

but when I ran the code there was a problem

This is my code:

import turtle
import time

turtle.pensize(5)
turtle.bgcolor("black")
turtle.pencolor("white")
turtle.turtlesize(2,2,2)

def ileri():
    turtle.fd(5)
def geri():
    turtle.backward(5)
def sag():
    turtle.right(90)
    turtle.fd(5)
    turtle.left(90)
def sol():
    turtle.left(90)
    turtle.fd(5)
    turtle.right(90)
def sagdon():
    turtle.right(10)
def soldon():
    turtle.left(10)
def siyah():
    turtle.pencolor("black")
def yesil():
    turtle.pencolor("green")
def acikyesil():
    turtle.pencolor("lightgreen")
def mavi():
    turtle.pencolor("blue")
def acikmavi():
    turtle.pencolor("lightblue")
def sari():
    turtle.pencolor("yellow")
def kirmizigul():
    turtle.pencolor("red")

turtle.onkeypress(ileri, "w" or "Up")
turtle.onkeypress(geri, "s" or "Down")
turtle.onkeypress(sag, "d")
turtle.onkeypress(sol, "a")
turtle.onkeypress(sagdon, "Right")
turtle.onkeypress(soldon, "Left")
turtle.onkeypress(siyah, "0")
turtle.onkeypress(yesil, "1")
turtle.onkeypress(acikyesil, "2")
turtle.onkeypress(mavi, "3")
turtle.onkeypress(acikmavi, "4")
turtle.onkeypress(sari, "5")
turtle.onkeypress(kirmizigul, "6")
turtle.listen()

when I try to run this game every hecking time a screen opens for like 0.1 second and closes

if you try to help it will be so good for me

I was expecting the screen was going to be remain open because it was in the books image but I was not expecting to see the screen for just 0.1 seconds


Solution

  • i believe this is what you tried to do all you had to do is import screen and set the screen setup and update it:

    import turtle
    from turtle import  Screen
    import time
    
    turtle.pensize(5)
    turtle.bgcolor("black")
    turtle.pencolor("white")
    turtle.turtlesize(2,2,2)
    
    screen= Screen()
    screen.setup(600,600)
    
    def ileri():
        turtle.fd(5)
    def geri():
        turtle.backward(5)
    def sag():
        turtle.right(90)
        turtle.fd(5)
        turtle.left(90)
    def sol():
        turtle.left(90)
        turtle.fd(5)
        turtle.right(90)
    def sagdon():
        turtle.right(10)
    def soldon():
        turtle.left(10)
    def siyah():
        turtle.pencolor("black")
    def yesil():
        turtle.pencolor("green")
    def acikyesil():
        turtle.pencolor("lightgreen")
    def mavi():
        turtle.pencolor("blue")
    def acikmavi():
        turtle.pencolor("lightblue")
    def sari():
        turtle.pencolor("yellow")
    def kirmizigul():
        turtle.pencolor("red")
    while(True):
        screen.update()
        time.sleep(0.07)
        turtle.onkeypress(ileri, "w" or "Up")
        turtle.onkeypress(geri, "s" or "Down")
        turtle.onkeypress(sag, "d")
        turtle.onkeypress(sol, "a")
        turtle.onkeypress(sagdon, "Right")
        turtle.onkeypress(soldon, "Left")
        turtle.onkeypress(siyah, "0")
        turtle.onkeypress(yesil, "1")
        turtle.onkeypress(acikyesil, "2")
        turtle.onkeypress(mavi, "3")
        turtle.onkeypress(acikmavi, "4")
        turtle.onkeypress(sari, "5")
        turtle.onkeypress(kirmizigul, "6")
        turtle.listen()
        screen.exitonclick()
    

    hope it was usefull to you and goodluck.