Python turtle.Screen() freezes and crashes every time - python

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.)

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.)

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.

Related

Space Invaders Syntax error on last line of code and random letter on python 3.7 [duplicate]

This question already has answers here:
Syntax error when defining a function on the Python command line
(4 answers)
Closed 4 years ago.
beginner here, I was trying to create a space invaders program in Python 3.7, but when I try to run this script in the terminal, it gives me this error:
border_pen.hideturtle()
^
SyntaxError: invalid syntax
Full script:
import turtle
import os
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")
delay = input("Press enter to finish.")
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pensize(3)
border_pen.pendown()
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
I ran your code locally and it gives none syntax error. But there is an issue in your code. If you are willing to see the white borders you should place the input() the end because when it is in the middle it stays there till enter is pressed and not read the rest of the code and when entered just run the rest but you are not able to see any white borders because it is too fast.
import turtle
import os
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")
delay = input("Delay") # this one is for delay
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pensize(3)
border_pen.pendown()
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
input('Press enter to exit') # for exiting
Take a look at I screened from my local machine.

Draw a square using for loops in python?

import turtle
def Draw_turtle(my_turtle):
for i in range(1,5):
my_turtle.turtle.forward(100)
my_turtle.turtle.right(90)
window = turtle.Screen()
window.bgcolor('green')
Alex = turtle.Turtle()
Alex.color('black')
Alex.speed(2)
Alex.shape('triangle')
Draw_turtle(Alex)
I know nothing of coding yet, when I run the above code nothing happens. Can anyone help me, please.
You have most of the right pieces and roughly in the right order. We just need to fine tune your code a little bit as far as indentation, usage and style:
import turtle
def draw_square(my_turtle):
for i in range(4):
my_turtle.forward(100)
my_turtle.right(90)
window = turtle.Screen()
window.bgcolor('green')
alex = turtle.Turtle()
alex.color('black')
alex.speed('slow')
alex.shape('triangle')
draw_square(alex)
window.mainloop()

Python Turtle - change displayed background image

I am writing a Python 3 turtle example program, and am trying to use bgpic() to change the displayed background image.
Here's a simplified version of my program:
import turtle
import time
screen = turtle.Screen()
screen.setup(600,400)
screen.bgpic('image1.gif')
time.sleep(2)
screen.bgpic('image2.gif')
When I run this program, I'd like to see the initial image, and then see the image change after 2 seconds. Instead, the screen stays blank until after the second image is drawn.
Any help appreciated,
Thanks!
Add screen.update() after the first screen.bgpic('image1.gif').
Adding a turtle and moving it made it work for me
import turtle
t = turtle.Turtle()
s = turtle.Screen()
s.setup(300, 300)
s.bgpic('image1.gif')
for i in range(180):
t.color("black")
t.fd(20)

python turtle graphics window won't open

I have little piece of code from a tutorial which should work fine but I don't get the turtle graphics window to show (I'm on Windows 10 using python 2.7.10). The code looks like this
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.forward(100)
window.exitonclick()
However, when I execute it nothing happens, I don't even get an error message. Instead, the shell just says
================================ RESTART ================================
and displays the little Windows circle (indicating it is working on something) but the turtle graphics window does not pop up.
I have tried repairing my python installation and additionally installing the x86 version, but I get the same outcome on the other installation, too.
Does anyone please know how to fix this?
Thank you,
Tomislav
Functions don't do anything unless you call them. Try:
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.forward(100)
window.exitonclick()
draw_square()

Python turtle.ondrag not working

I've been trying to make a paint program in Python Turtle and for some reason it won't work. I'm using the pen() tool and my code looks like this
from turtle import *
import random
pen()
bgcolor('black')
pencolor('white')
pen.ondrag(pen.goto)
listen()
mainloop()
I've look at this http://docs.python.org/2/library/turtle.html and it says to type turtle.ondrag(turtle.goto) but since I'm using the pen it should work as pen.ondrag but it doesn't, so can someone please clear this up.
Thanks Jellominer
I will simplify and clarify the code given by the questionner:
from turtle import *
ts = Screen(); tu = Turtle()
ts.listen()
ondrag(tu.goto)
mainloop()
This works. You have to click on the turtle and drag it.
First, pen() is not the function you want. Second, although Pen is a synonym for Turtle, pen is not a synonym for turtle. Here's how to go about using ondrag() if you'd like to use Pen instead of Turtle:
from turtle import Pen, Screen, mainloop
def ondrag_handler(x, y):
pen.ondrag(None) # disable handler inside handler
pen.setheading(pen.towards(x, y)) # turn toward cursor
pen.goto(x, y) # move toward cursor
pen.ondrag(ondrag_handler)
screen = Screen()
screen.bgcolor('black')
pen = Pen()
pen.color('white')
pen.shapesize(2) # make it larger so it's easier to drag
pen.ondrag(ondrag_handler)
screen.listen()
mainloop() # screen.mainloop() preferred but not in Python 2
The turtle.ondrag(turtle.goto) makes for a nice short example in the documentation but in reality isn't practical. You want to disable the event handler while handling the event otherwise the events stack up against you. And it's nice to turn the mouse towards your cursor as you drag it.
from turtle import *
ts = Screen()
ondrag(goto)
shapesize(10)
pensize(40)
speed(0)
mainloop()
I think this will surely work.
You can change the size and other things
In here you are using the default turtle .
Sorry but you'll need to take care of the indentation

Categories