0

I'm trying to make a function and keybind so whenever I press "w" the object moves up 15 pixels, but when not only does the keybind not work, the object appears to have already moved up those 15 pixels from it's original position:

from turtle import *

# Screen

win = turtle.Screen()
win.title("Runner")
win.bgcolor("white")
win.setup(width=800, height=600)
win.tracer(0)

# Floor
square1 = turtle.Turtle()
square1.speed(0)
square1.shape("square")
square1.shapesize(stretch_wid=0.3, stretch_len=50)
square1.color("black")
square1.penup()
square1.goto((0, -150))
win.update()

# Player

player1 = turtle.Turtle()
player1.shape("circle")
player1.color("red")
player1.speed(30)
player1.penup()
player1.goto(0,-136)
win.update()

# bindings and functions

def playermove():
    y = player1.ycor()
    y += 15
    player1.sety(y)
win.onkeypress(playermove(), "w")
win.listen()


while True:


    win.update()```

1 Answers1

0

As @jasonharper mentioned, you need to provide the function itself. Here is the fixed code:

from turtle import *

# Screen

win = turtle.Screen()
win.title("Runner")
win.bgcolor("white")
win.setup(width=800, height=600)
win.tracer(0)

# Floor
square1 = turtle.Turtle()
square1.speed(0)
square1.shape("square")
square1.shapesize(stretch_wid=0.3, stretch_len=50)
square1.color("black")
square1.penup()
square1.goto((0, -150))
win.update()

# Player

player1 = turtle.Turtle()
player1.shape("circle")
player1.color("red")
player1.speed(30)
player1.penup()
player1.goto(0,-136)
win.update()

# bindings and functions

def playermove():
    y = player1.ycor()
    y += 15
    player1.sety(y)

win.onkeypress(playermove, "w")
win.listen()


while True:
    win.update()
Epic Programmer
  • 383
  • 3
  • 12