I'm trying to figure out a way to make it to when I hold down a key the player will constantly move, or just have the player move forward constantly with just turtle graphics, (I do have pygame installed also)
import turtle
from turtle import *
#Setup Screen
wn = turtle.Screen()
wn.setup(700,700)
wn.title("white")
wn.bgcolor("black")
#Create Player
player = turtle.Turtle()
player.penup()
player.shape("triangle")
player.color("white")
def forward():
player.forward(20)
def lef():
player.left(90)
def forward():
player.right(90)
onkey(forward,"Up")
onkey(left,"Left")
onkey(right,"Right")
listen()
You can fix it simply by adding wn to start of
wn.onkey(forward, 'Up')
wn.onkey(left, 'Left')
wn.onkey(right, 'Right')
wn.listen()
wn.mainloop()
I hope this helps!
I recommend you read this post on repeating key events and first determine whether your operating system provides key repeat and whether you can/want to adjust that and/or how to turn it off to implement your own. That link includes code to implement your own key repeat behavior in turtle.
I've reworked your code below and the keys repeat fine for me because my operating system (OSX) implements key repeat:
from turtle import Turtle, Screen
# Setup Screen
wn = Screen()
wn.setup(700, 700)
wn.title('white')
wn.bgcolor('black')
# Create Player
player = Turtle('triangle')
player.speed('fastest')
player.color('white')
player.penup()
def forward():
player.forward(20)
def left():
player.left(90)
def right():
player.right(90)
wn.onkey(forward, 'Up')
wn.onkey(left, 'Left')
wn.onkey(right, 'Right')
wn.listen()
wn.mainloop()
In OSX I can control the rate (and turn it off) in the Keyboard panel of Systems Preferences. Look into what your OS provides.
Some programming notes: avoid importing the same module two different ways, this always leads to confusion. If you find you're getting interference between keyboard events at high repetition rates consider the following for all three event handlers:
def forward():
wn.onkey(None, 'Up') # disable event in handler
player.forward(20)
wn.onkey(forward, 'Up') # reenable event
If you are using python 3 then you should use wn.onkeypress(). The function onkey() is triggered when the key is pressed and then released but the onkeypress() function is triggered immediately when a key is pressed down.
Related
I have used the onkey function for the right and left:
sc.onkey(user_left,"left")
sc.onkey(user_right,"right")
I have also set the screen after setting the turtle and importing turtle:
sc=Screen()
But when I use the same format for up and down:
sc.onkey(user_up,"up")
sc.onkey(user_down,"down")
It does nothing. I also have my functions:
def user_right:
t3.forward(5)
def user_left:
t3.backward(5)
t3 is my user turtle, and it is sideways, the shape is turtle, and its head is facing right. t3 is automatically set to make its head facing the right side when the code runs. By the way, I import from turtle import*
I see several issues here. First, this can't work as you claim:
def user_right:
t3.forward(5)
def user_left:
t3.backward(5)
It would need to be:
def user_right():
t3.forward(5)
def user_left():
t3.backward(5)
Next, this won't work in turtle in standard Python:
sc.onkey(user_left,"left")
sc.onkey(user_right,"right")
These keys would have to be "Left" and "Right". Are you using a non-standard turtle implementation? (E.g. the one on repl.it) You show your two event handlers that work, but fail to show the two that don't which would be of more interest when trying to debug your code.
Finally, your missing a call to the screen's listen() method, so your keystrokes are ignored. Here's how I might implement the functionality your code is hinting at:
from turtle import Screen, Turtle
def user_right():
turtle.forward(5)
def user_left():
turtle.backward(5)
def user_up():
turtle.sety(turtle.ycor() + 5)
def user_down():
turtle.sety(turtle.ycor() - 5)
turtle = Turtle()
turtle.shape('turtle')
screen = Screen()
screen.onkey(user_left, 'Left')
screen.onkey(user_right, 'Right')
screen.onkey(user_up, 'Up')
screen.onkey(user_down, 'Down')
screen.listen()
screen.mainloop()
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 write this code to make a pong game. Everything works fine except for the left paddle.
When I press the buttons that control the movements and I keep them pressed, the paddles moves constantly until key release.
Except for the "w" key. This key makes the paddle move for just 1 step and then stops. If I want to move, I have to release and press again.
The onkey events uses the same function of the "down" button, so I assume it's correct.
I tried to change onkey with onkeypressed but it doesn't work.
Below is the code: the Paddle module contains Paddle, a class that inherits from the Turtle class, and the functions go_up and go_down.
from turtle import Screen, Turtle
from paddle import Paddle
screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.title("Pong")
screen.tracer(0)
l_paddle = Paddle((-350, 0))
r_paddle = Paddle((350, 0))
screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "s")
screen.onkey(l_paddle.go_down, "w")
game_is_on = True
while game_is_on:
screen.update()
screen.exitonclick()
You can try using screen.onkeypress() instead of screen.onkey(); I had this issue before, and switching to onkeypress() solved it.
I am using Python 2.7 turtle graphics to make a simple mouse sensor that draws whenever you hold down the mouse button. Since my function is based on the user, it crashes right when you run it. I found out that turtle.exitonclick() will help keep the interface on, but I can't draw because my draw is bound to the left mouse button click. Is there any solution to get around this or is it possible to rebind turtle.exitonclick() to something else? Here's my code:
import turtle
wn = turtle.Screen()
wn.bgcolor("lightblue")
#wn.screensize(400,300)
turtle.setup(400,300)
draw = turtle.Turtle()
def gothere(event):
draw.penup()
draw.goto(event.x-360, 340-event.y)
draw.pendown()
#draw.color(blue)
def movearound(event):
draw.goto(event.x-360, 340-event.y)
draw.color(blue)
def release(event):
draw.penup()
def reset(event):
draw.clear()
cavset = turtle.getcanvas()
cavset.bind("<Button-2>", gothere)
cavset.bind("<B1-Motion>", movearound)
cavset.bind("<ButtonRelease-2>", release)
cavset.bind("<Escape>", reset)
wn.listen()
wn.exitonclick()
(I am running through command prompt, BTW.)
What you most likely want is:
turtle.mainloop()
instead of:
wn.exitonclick()
as that's what exitonclick() calls after setting up a handler for the "onclick" part.
However, there are other issues. You seem to be mixing three different coding models: functional interface to turtle; object-oriented interface to turtle; calling into turtle's Tkinter underpinnings.
Let's simplify and just use the object-oriented interface to turtle:
from turtle import Turtle, Screen, mainloop
def gothere(x, y):
draw.penup()
draw.goto(x, y)
draw.pendown()
def movearound(x, y):
draw.ondrag(None) # disable handler in handler!
draw.color("blue")
draw.goto(x, y)
draw.ondrag(movearound)
def release(x, y):
draw.penup()
def reset():
draw.clear()
wn = Screen()
wn.setup(400, 300)
wn.bgcolor("lightblue")
draw = Turtle()
wn.onclick(gothere, 2)
draw.ondrag(movearound)
draw.onrelease(release, 2)
wn.onkey(reset, "Escape")
wn.listen()
mainloop()
I believe Python 3 turtle tweaks the turtle event interface to add some methods and use clearer names for others.
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