I started to program a few days ago and today I've tried to do the game 'Pong' using python.
But since I am a beginner I can't find what is wrong with the code or something.
When I try to create a turtle.Turtle, the Pycharm says the code is unreachable.
# Game Pong
import turtle
wn = turtle.Screen()
wn.title("Quarentena 21/03")
wn.setup(width=800, height=600)
wn.bgcolor("black")
wn.tracer(0)
# Main Game Loop
while True:
wn.update()
# Paddle A
paddle_a = turtle.Turtle()
paddle_a.shape("square")
paddle_a.speed(0)
paddle_a.color('green')
paddle_a.penup()
Yes, the code is unreachable. Unless you put something to break the loop, 'while True' continues forever because the default condition is always True unless you set it to false. So you can't reach the code below the loop. Happy programming! :)
Related
In the snake program (Python), the line my_screen.onkey(my_snake.turn_up(), 'Up') is always executing. It triggers this 'Up' keystroke event irrespective of the my_screen.listen() statement or if i have pressed any keys ! Can someone help please as I am unable to get this to run properly, ie it was always run the onkey event 'Up'.
from turtle import Turtle
from turtle import Screen
from snake import Snake
import time
my_screen = Screen()
my_screen.setup(width=600, height=600)
my_screen.bgcolor("black")
my_screen.title("My Snake Game")
'''this causes the screen to stop updating, until it gets the 'Update' function call'''
my_screen.tracer(0)
'''create turtle objects'''
my_snake = Snake()
print(my_snake.segments)
my_screen.listen()
my_screen.onkey(my_snake.turn_up(), 'Up')
game_is_on = True
while game_is_on:
my_screen.update()
time.sleep(.1)
my_snake.move()
my_screen.exitonclick()
Actually, the onkey() command must have a function without braces (). So you could have written as
my_screen.onkey(my_snake.turn_up, 'Up')
instead of
my_screen.onkey(my_snake.turn_up(), 'Up')
I just wrote a program based on turtle module and after last line of code I used turtle.done() but when again I want to use my turtle I will get Terminator error and I should close and again open jupyter or even commond line to be able to run my code again please tell me what's wrong? Is it a bug inside turtle?
my code is something like this:
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Here is my code(quite long!)
....
turtle.done()
The error occurs when a turtle program is interrupted during its main loop. If the way you close your turtle program is by clicking the X and there's a while loop that never ends, then the turtle.done() statement will never be met.
You can detect a turtle.Terminator error inside your while loop, so that instead of crashing the program, let the program continue to parse outside the while loop.
Something like this:
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
while True:
try:
# Your code for the while loop
except turtle.Terminator:
break
turtle.done
Or use ontimer().
The turtle module uses a class variable _RUNNING which remains true between executions when running in jupyter instead of running it as a self contained script. I have requested for the module to be updated with a pull request.
Meanwhile, work around/working example
1)
import importlib
import turtle
importlib.reload(turtle)
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Your code
....
turtle.done()
import turtle
my_screen = turtle.Screen()
turtle.TurtleScreen._RUNNING=True
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Your code
....
turtle.done()
I have a small program where I would like to pop up a Python turtle window, display something, close on user click, then pop up a next window. An example program for this is
import turtle
window = turtle.Screen()
t1 = turtle.Turtle()
t1.forward(100)
window.exitonclick()
window2 = turtle.Screen()
t2 = turtle.Turtle()
t2.forward(100)
window2.exitonclick()
The program crashes when trying to open the second window. This is with Python3.6 on a Mac OS X machine. I have seen some reference to crashes when re-running a program twice, and seen advice about adding a bye() call after the first window close, but I haven't managed to work around this yet.
Any suggestions?
It seems like you should probably using a different function then exitonclick to exit out of the screen or clear it. But I was able to get your code working by running turtle.bye() and doing a try/except around it in case of errors.
It's definitely not an elegant solution but is a workaround to get the second window to appear properly.
import turtle
window = turtle.Screen()
t1 = turtle.Turtle()
t1.forward(100)
window.exitonclick()
try:
turtle.bye()
except Exception:
pass
window2 = turtle.Screen()
t2 = turtle.Turtle()
t2.forward(100)
window2.exitonclick()
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.
I'm trying to get two turtles on a screen. So far I have this:
import turtle
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
But nothing is happening :(
Im using the Turtle IDE downloaded from:http://pythonturtle.org/
All i want to do is illustrate some object orientation but if that code wont work, i cant
Anyone got any suggestions (ive tried using turtles API but its hard to follow)
Thanks
You didn't take an object from the class Screen
You should do that to make sure that the window will not close immediately, plus you should make the program loop.
import turtle
wn = turtle.Screen() # taking window object from Screen class
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
while True: # Making the program loop to make the program continue running.
wn.update()
The main loop also you can make it this way
wn.mainloop()
I recommend you follow the first solution.