Python Turtle - change displayed background image - python

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)

Related

I'm not sure how to add this image to the screen (turtle)

I entered this turtle code to print a cookie on the screen, but the cookie doesn't actually show up.
import turtle
wn = turtle.Screen()
wn.title("Cookie Clicker")
wn.bgcolor("black")
wn.mainloop()
cookie = "cookie.gif"
turtle.register_shape(cookie)
turtle.shape(cookie)
Anyone know how to fix this? By the way I'm using PyCharm and 'cookie.gif' is in the project folder.
Assuming this isn't an animated GIF, the problem is you've put wn.mainloop() in the wrong place in your code. Generally, it's the last thing you do in a turtle program:
from turtle import Screen, Turtle
cookie = "cookie.gif"
screen = Screen()
screen.title("Cookie Clicker")
screen.bgcolor('black')
screen.register_shape(cookie)
turtle = Turtle()
turtle.shape(cookie)
screen.mainloop()

How do I change the Hitbox size of a turtle in python turtle graphics?

I dont know how to change the size of a turtle hitbox in python turtle graphics
I haven't tried anything yet because I'm new, and know very little about this. I've tried googling it, though, but nothing popped up.
from turtle import *
import turtle
from random import randint
import time
screen = turtle.Screen()
screen.setup(1920, 1080)
player = turtle.Turtle()
I want to add a button that you have to click to start right here
The game starts right here :
wn = turtle.Screen()
last_pressed = 'up'
def setup(col, x, y, w, s, shape):
player.penup()
player.up()
player.goto(x,y)
player.width(w)
player.turtlesize(s)
player.color(col)
player.lt(90)
player.down()
wn.onkey(up, "s")
wn.onkey(left, "d")
wn.onkey(right, "a")
wn.onkey(back, "w")
wn.onkey(quitTurtles, "Escape")
wn.listen()
wn.mainloop()
This may not be exactly what you are looking for, but this might work in your situation.
Detecting collision in Python turtle game
This is a thread on collision detection between objects and with some tweaking of numbers you could increase the hitbox of the turtle using the abs() function
I dont know how to change the size of a turtle hitbox in python turtle graphics
What do you mean by "hitbox"? I'm not sure what you mean by that (and neither does Google, apparently).
Do you mean that you want a rectangular button to click on? If that's the case, you could use the tkinter module together with the turtle module to create a button to click. (But be aware that it's not always easy to get the tkinter and turtle modules to work together to do what you want.)
If you want a button to click on, but don't need a Tkinter button, you could just try creating a new turtle in the shape of a rectangle that intercepts mouse clicks with onclick(). You can see an example of this if you run:
python3 -m turtledemo
and select Examples >> colormixer from the main menubar.
Or, if by "hitbox" you mean how to detect when one turtle has intercepted another turtle (as in, one has come close enough to the other to be considered a "hit"), I suggest querying each turtle's location, then using the Pythagorean Theorem to calculate the distance from each other. If this distance is within a predetermined threshold, consider the hitbox as being "hit."
You can see an example of this by typing:
python3 -m turtle
(Pay attention to the yellow turtle as he tries to catch up to the other turtle.)
I apologize if this answer isn't quite what you're looking for, but I'm just not sure what you mean by "hitbox." Maybe you could clarify?
I just saw this question today (2 years too late i know), and was having a similar problem / question.
What I ended up doing was running 3 distance checks (as i had increased my object size by 3) which differed along the x-axis (width). So it would check the distance between the ball(turtle) and the paddle (any 3 points and if it was shorter than X it would trigger the collision mechanic.
so it was something like :
check the ball class' ball object, and see how far away it is from the paddle,
and if its either in the center, or 30 pixels to the left or right of the center then hit.
if
ball.ball.distance(pad.paddle(), pad.paddle.ycor()) < 30 or
ball.ball.distance(pad.paddle.xcor() - 30, pad.paddle.ycor()) < 30 or
ball.ball.distance(pad.paddle.xcor() + 30, pad.paddle.ycor()) < 30

Clear an image from turtle graphics in Python?

I wrote the following code:
from turtle import *
ts = Screen(); tu = Turtle()
image = "apple.gif"
ts.addshape(image)
tu.shape(image) # The image is displayed
sleep(2); tu.clear # This doesn't clear the image
done()
tu.clear() doesn't clear the image, although it it belongs to tu turtle. If I use ts.clear(), I clear the screen, i.e. the graphics are cleared, but also all events up to this point (key and mouse) are cleared! (I have not included the events here to keep the code simple, but they are simple key events that have been tested and work fine.)
Is there a way to clear an image -- as one does with drawings -- w/o also clearing events set?
I believe what you're describing is hideturtle(). For example:
from time import sleep
from turtle import Turtle, Screen
IMAGE = "apple.gif"
screen = Screen()
screen.addshape(IMAGE)
turtle = Turtle()
turtle.shape(IMAGE) # The image is displayed
sleep(2)
turtle.hideturtle()
screen.exitonclick()
An alternative approach in place of turtle.hideturtle() above is:
turtle.shape("blank")

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

Object oriented programming turtle (python)

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.

Categories