Python turtle.Screen() freezes and crashes every time

Question:

I’m making a space invader clone just to learn a little Python since I just started with it. I made a turtle.Screen() but everytime I run it, it instantly freezes and crashes. Does anyone know what’s causing this problem?

     import turtle
     from turtle import forward, right, left
     forward(50)
     import os
     import math
     import random
     import shelve

     wn = turtle.Screen()
     wn.bgcolor("black")
     wn.title("Space invaders")

     border_pen = turtle.Turtle()
     border_pen.speed(0)
     border_pen.color("white")
     border_pen.penup()
     border_pen.setposition(-300, -300)
     border_pen.pendown()
     border_pen.pensize(3)
     for side in range(4):
              border_pen.fd(600)
              border_pen.lt(90)
     border_pen.hideturtle()

     delay = input("press enter to finish.")

There are no errors when I debug it, although at the “from turtle import forward, right, left” line, the “forward, right, left” words are marked red for some reason. (I’m also using pycharm community edition if that’s any useful info.)

Asked By: Al1on

||

Answers:

Once (re)indented correctly, it works for me. I suggest you get rid of all the imports you’re not using as well as consolidate down to a single turtle import. Slightly simplified turtle code for debugging:

from turtle import Turtle, Screen

wn = Screen()
wn.bgcolor("black")
wn.title("Space invaders")

border_pen = Turtle()
border_pen.speed("fastest")
border_pen.color("white")
border_pen.pensize(3)

border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()

for side in range(4):
    border_pen.forward(600)
    border_pen.left(90)

border_pen.hideturtle()

wn.exitonclick()

Then check if this works, and if not, supply us with the actual error messages you are getting (eg. edit your original quesion to include any error messages.)

Answered By: cdlane

Hey I know this is old but I was following the same tutorial you were on YouTube for this Space Invaders game and I had this same exact issue. The crash comes from using “delay = input()”

Simply erase that at the bottom and replace it with wn.exitonclick() like cdlane suggested and it’s a complete fix.

Hope this helps anyone else doing this tutorial and getting stuck.

Answered By: James Plinkus