Python turtle object wont show in window - python

I am currently trying to make a copy of Atari's breakout using python and turtle. Previously I have created a pong copy which does not use OOP and it worked perfectly fine. However due to the bricks in breakout I decided to use OOP and create an object class for the bricks. Once I run the program, it wont display the brick. Any ideas to why?
import turtle
window = turtle.Screen()
window.title('Atari Breakout')
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)
class Brick(Turtle):
def __init__(self):
super().__init__(shape='square', visible=True)
self.myturtle = turtle.Turtle()
self.color = 'white'
self.shapesize(stretch_wid=10, stretch_len=4)
self.pendown()
self.goto(-350, 200)
board1 = Brick()
window.update()

There are a few problems in this code - firstly, you need turtle.Turtle not just Turtle as otherwise this causes an error. Secondly, the line self.myturtle = turtle.Turtle() is unnecessary as super().__init__(shape='square', visible=True) already creates a turtle, and thirdly self.color = 'white' should be changed to self.color('white'). Also, I'm pretty sure you meant self.penup() not self.pendown() to stop the brick from drawing a line from the center to its position.
Completed code:
import turtle
window = turtle.Screen()
window.title('Atari Breakout')
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)
class Brick(turtle.Turtle):
def __init__(self):
super().__init__(shape='square', visible=True)
self.color('white')
self.shapesize(stretch_wid=10, stretch_len=4)
self.penup()
self.goto(-350, 200)
board1 = Brick()
window.update()

Related

Turtle not displaying on screen - Python

I am trying to recreate the snake game, however when I call the function which is supposed to create the initial body of the snake nothing appears on the screen.
The same problem happens if I simply create a turtle object in the main file.
Snake:
from turtle import Turtle
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
class Snake:
def __init__(self):
self.segments = []
self.create_snake()
def create_snake (self):
for position in STARTING_POSITIONS:
n_segment = Turtle("square")
n_segment.color("white")
n_segment.penup()
print(position)
n_segment.goto(position)
self.segments.append(n_segment)
Main:
from turtle import Turtle, Screen
from snake import Snake
screen = Screen()
screen.setup(width = 600 , height= 600)
screen.bgcolor("black")
screen.title("SNAKE")
screen.tracer(0)
game_on = True
segments = []
snake = Snake()
turtle = Turtle()
In order to see the snake, I just added the following code snippet in main.py after the turtle definition to display a stationary snake with a pointer.
while game_on:
screen.update()
screen.exitonclick()
The result was this image.
Hope that helps.
Regards.
this is a minimum reproducible example of the above code which works and draws a square (segment of the snake body) in the snake class:
import turtle as ttl
class Snake:
def __init__(self):
self.segments = []
self.create_snake()
def create_snake (self):
ttl.shape('square')
ttl.color('white')
screen = ttl.Screen()
screen.setup(width=600, height=600)
screen.bgcolor('black')
snake = Snake()
ttl.done()
A lot of the code from the OP question is removed because there were numerous errors. Also, for the purpose of simplicity the class was added to the code, but the OP could break it into two separate files if desired.
I believe that there error in the question code was that the screen was drawing over itself which is why it was always black.
You can build from here.

Turtle Text Flashing

When I attempt to put text over a turtle in the python turtle module, it flashes. Any solutions?
import turtle
s = turtle.Screen()
s.setup(width=500,height=600)
c = turtle.Turtle()
c.shapesize(stretch_len=5,stretch_wid=5)
c.goto(0,0)
c.shape("square")
pen = turtle.Turtle()
pen.hideturtle()
pen.goto(0,0)
pen.color("red")
while True:
pen.write("hello!")
s.update()
Although I don't see the flashing on my screen, my guess is your problem is related to this bad idiom:
while True:
# ...
s.update()
We need neither the while True: (which has no place in an event-driven environment like turtle) nor the call to update() (which is not needed given no previous call to tracer()). Let's rewrite this as turtle code:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=500, height=600)
turtle = Turtle()
turtle.hideturtle()
turtle.shapesize(5)
turtle.shape('square')
turtle.stamp() # stamp a square so we can reuse turtle
pen = Turtle()
pen.hideturtle()
pen.color("red")
pen.write("Hello!", align='center', font=('Arial', 16, 'normal'))
screen.exitonclick()
Does that solve your problem?

Why my python kept crashing when it tried to run the turtle.tracer() method in turtle module?

I was coding a Snake Game using turtle module but when I add this line into my code the turtle screen and python crashed:
turtle.tracer(0)
can somebody help me so I can complete the game? Thanks a lot
my code:
from turtle import Turtle, Screen, colormode
screen = Screen()
screen.bgcolor("black")
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)
x = 0
segments = []
for turtle in range(3):
turtle = Turtle("square")
turtle.color("white")
turtle.penup()
turtle.goto(0-x, 0)
x += 20
segments.append(turtle)
game_is_on = True
screen.update()
while game_is_on:
for segment in segments:
segment.forward(20)
screen.exitonclick()
I think we need to know more about what you mean by 'crashed'. If you mean everything froze, that's the code you wrote. Once you introduce tracer() you need to provide an update() for every change you want the user to see. But you don't have any update() calls in your loop so everything visually remains as it was before the loop. If you want to see the segments move, you need to do something like:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor('black')
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)
x = 0
segments = []
for turtle in range(3):
turtle = Turtle('square')
turtle.color('white')
turtle.penup()
turtle.setx(x)
x -= 20
segments.append(turtle)
screen.update()
game_is_on = True
while game_is_on:
for segment in segments:
segment.forward(20)
screen.update()
screen.exitonclick() # never reached
If you mean by 'crashed' that Python quit back to the operating system, then you need to describe the environment under which you're running this code.

Python (Turtle) Remove turtle / arrow

I am trying to make a basic Pong and I don't understand why the arrow/turtle in the middle of the screen displays.
The paddle on the screen consists of 6x turtle objects stored in self.paddle.
I know the problem has something to do with the p = Paddle() but I don't understand where the arrow-object is since it seems to not be in the list mentioned above (print(self.paddle)).
Can someone enlighten me?
from turtle import Turtle, Screen
screen = Screen()
screen.setup(width=1000, height=700)
class Paddle(Turtle):
def __init__(self):
super().__init__()
self.paddle = []
self.create_player_paddle()
def create_player_paddle(self):
for pad in range(0, 6):
x = -480
y = 30
p = Turtle(shape="square")
p.turtlesize(0.5)
p.penup()
p.goto(x, y)
self.paddle.append(p)
for part in range(len(self.paddle)):
self.paddle[part].goto(x, y)
y -= 10
p = Paddle()
screen.update()
screen.exitonclick()
I don't understand where the arrow-object is since it seems to not be
in the list mentioned above ... Can someone enlighten me?
Normally, a class like Paddle() either isa Turtle or contains one or more instances of Turtle. But you've done both, it is both a Turtle (the source of your mystery arrow) and it contains a list of turtles. The simple fix is to remove Turtle as its superclass and remove the super().__init__() code:
class Paddle():
def __init__(self):
self.paddle = []
self.create_player_paddle()
Below is my rework of your code addressing the above and other issues I saw:
from turtle import Turtle, Screen
class Paddle():
def __init__(self):
self.segments = []
self.create_player_paddle()
def create_player_paddle(self):
x = -480
y = 30
for _ in range(6):
segment = Turtle(shape='square')
segment.turtlesize(0.5)
segment.penup()
segment.goto(x, y)
self.segments.append(segment)
y -= 10
screen = Screen()
screen.setup(width=1000, height=700)
paddle = Paddle()
screen.exitonclick()
The usual approach to a Pong game in turtle is to make the paddle a subclass of Turtle and then use shapesize() to stretch the turtle to be the rectangle you desire. But this has issues with collision detection. Your approach makes the paddle somewhat more complicated, but might work better collision-detection-wise as you can test each segment.
if you need to hide the Turtle pointer, put Turtle.hideturtle(self) in your constructor init function

I am using Turtle and when I open a window with Turtle, it does not respond

I am using Turtle and when I open a window with Turtle, it does not respond. I haven't finished the code yet. I am still a beginner and I am trying to make a simple pong game. Thanks!
import turtle
# Paddles
pa = turtle.Turtle()
pa.speed(0)
pa.shape("square")
pa.color("white")
pa.penup()
pa.goto(-350, 0)
pa.shapesize(stretch_wid=5, stretch_len=1)
pb = turtle.Turtle()
pb.speed(0)
pb.shape("square")
pb.color("white")
pb.penup()
pb.goto(350, 0)
pb.shapesize(stretch_wid=5, stretch_len=1)
# ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
# Screen
win = turtle.Screen()
win.title("Stankity Stank Stank Stank")
win.bgcolor("black")
win.setup(width=800, height=600)
win.tracer(0)
while True:
win.update
# Controls
def paup():
y = pa.ycor()
y += 20
pa.sety(y)
win.listen()
win.onkeypress(paup, "w")
Stack Overflow said my post was mostly code, so here's some extra text.
You did not ask a question. I presume it is "Why don't I see anything?" Answers:
A white pen on a white background is invisible.
A pen that is up does not draw.
Fix those two things and the following draws a black line with a square at the end.
import turtle
pb = turtle.Turtle()
pb.speed(0)
pb.shape("square")
pb.color("black")
pb.pendown()
pb.goto(350, 0)
I recommend that you look at the turtle doc, starting with the sample on the upper right, and the turtledemo module (python -m turtledemo or import turtledemo). Also, get a few lines right before writing so much.

Categories