Theolito Caramba
Theolito Caramba

Reputation: 39

Turtle.onkeypress isn't firing

I'm very new to Python and have made a couple small games during a Python Learning course but never at home. So recently I began making a game, but after just 10 minutes I stumbled upon a problem:

Nothing happened when I pressed "W" although I had writen onkeypress in the code. See for your self:

(It's designed for full screen)

import turtle

s = turtle.Screen()    
g = turtle.Turtle()
t = turtle.Turtle()

#Ground
t.speed(0)
t.up()
t.goto(-1000,-200)
t.down()
t.goto(1000,-200)

#Player
g.speed(0)
PlayerX = -600

def moveX():
    g.clear()
    global PlayerX
    g.up()
    g.goto(PlayerX,-99)
    g.down()
    g.color("Slate Gray")
    g.begin_fill()
    g.circle(-50)
    g.end_fill()
    PlayerX = PlayerX - 1

turtle.onkeypress(moveX, "w")
moveX()

I'm fully aware I haven't made a go backwards button.

Upvotes: 3

Views: 28212

Answers (5)

Isaac Tippetts
Isaac Tippetts

Reputation: 1

#This is a short code I made using space as down and w as up, feel free to 
#extract from it what you can. 
import turtle
player = turtle.Turtle()
y = 0
wn = turtle.Screen()
def p_up():
  global y,up
  up = True
  while(up==True):
    y += 10
    player.sety(y)
def p_down():
  global y,down
  down = True
  while(down==True):
    y -= 10
    player.sety(y)
def up_stop():
  global up
  up = False
def down_stop():
  global down
  down = False
wn.listen()
wn.onkeypress(p_up,"w")
wn.onkeypress(p_down,"space")
wn.onkeyrelease(up_stop,"w")
wn.onkeyrelease(down_stop,"space")
wn.mainloop()

Upvotes: -1

Hackernoob666
Hackernoob666

Reputation: 21

with my version of python none of the others are actually correct, here is the modified code that works for me:

from turtle import Turtle, Screen, setpos, hideturtle

screen = Screen()
screen.setup(500, 500)

#Ground
t = Turtle()
t.speed(0)
t.up()
t.goto(-1000,-200)
t.down()
t.goto(1000,-200)

#Player
player = Turtle()
hideturtle()
player.speed(0)
setpos(0,0)
PlayerX = 0

def moveX():
    player.clear()
    global PlayerX
    player.up()
    player.goto(PlayerX,0)
    player.down()
    player.color("Slate Gray")
    player.begin_fill()
    player.circle(-50)
    player.end_fill()
    PlayerX = PlayerX - 1




screen.onkey(moveX, "w")

screen.listen()

(this can definitely be improved on)

Upvotes: 0

Sachin Cholkar
Sachin Cholkar

Reputation: 89

Not sure if the change is with Python3. But onkey function seems to be dragged under Screen().

turtle.Screen().onkey(movex, "w")

Upvotes: -1

cdlane
cdlane

Reputation: 41872

Along with @doctorlove's spot on correction (+1) of adding listen() to allow the window to receive keyboard events, a couple of comments:

First, click on the window with your mouse to make it active otherwise it won't respond to the keyboard. Second, it can be helpful to deactivate the event handler while in the event hander, and reactivate it on the way out, to avoid problems if someone repeatedly presses the key very fast.

Here's the second comment along with some other code suggestions:

from turtle import Turtle, Screen

screen = Screen()
screen.setup(1200, 500)

# Ground

ground = Turtle()
ground.speed('fastest')

ground.penup()
ground.goto(-1000, -200)
ground.pendown()
ground.forward(2000)

# Player

player = Turtle()
player.speed('fastest')

PlayerX = -600

def moveX():
    global PlayerX

    screen.onkeypress(None, "w")  # disable handler in handler
    player.clear()
    player.penup()
    player.goto(PlayerX, -99)
    player.pendown()
    player.color("Slate Gray")
    player.begin_fill()
    player.circle(-50)
    player.end_fill()

    PlayerX -= 1

    screen.onkeypress(moveX, "w")  # reenable handler

screen.listen()

moveX()

screen.mainloop()  # change import & use turtle.mainloop() if Python 2

mainloop() isn't required to run but the program will exit after your initial moveX() call without it. mainloop() turns control over to the Tk event handler so some events may not fire without it.

You'll need to change onkeypress() to onkey() if this is Python 2 as well as change the way that mainloop() is invoked.

Upvotes: 2

doctorlove
doctorlove

Reputation: 19232

I think it's called onkey not onkeypress. Also I think you need to listen (and add a mainloop if you want it to run):

turtle.onkey(moveX, "w")
turtle.listen()
moveX() # draw things first
turtle.mainloop()

You may need to revisit the numbers you are using to make sure the shape is on the window.

Upvotes: 1

Related Questions