3

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Theolito Caramba
  • 39
  • 1
  • 1
  • 2

5 Answers5

2

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.

cdlane
  • 40,441
  • 5
  • 32
  • 81
1

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.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • 1
    Python 3 introduces `onkeypress` and `onkeyrelease` where `onkey` is a synonym for the latter. In Python 2, there is only `onkey`. – cdlane Aug 07 '17 at 19:39
0

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)

  • 3
    "With my version of python"... could you precise which version that is? At least 2 or 3 would be a good indication. – Chris Maes Oct 14 '20 at 06:29
  • done on trinket, using python 3 (don't know the exact version), I have since got a python program downloaded but haven't tried it on that. – Hackernoob666 Oct 28 '20 at 22:51
-1

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

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

-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()
  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '22 at 04:48