简体   繁体   中英

Why isn't my turtle keybinding responding?

I am in the process of modulating my code for a game that I wrote. When I modulated the player class, I add all the additional parameters/arguments so it can be separated. I kept the keybinding within the the main game module.

The keybinding code look like this


turtle.listen()
a=turtle.onkey(player.go_left(walls), "Left")
b=turtle.onkey(player.go_right(walls), "Right")
c=turtle.onkey(player.go_up(walls), "Up")
d=turtle.onkey(player.go_down(walls), "Down")
e=turtle.onkey(player.headright(missile,lives), "d")
f=turtle.onkey(player.headleft(missile,lives), "a")
g=turtle.onkey(player.headdown(missile,lives),"s")
h=turtle.onkey(player.headup(missile,lives),"w")
i=turtle.onkey(player.headright(missile,lives),"D")
j=turtle.onkey(player.headleft(missile,lives), "A")
k=turtle.onkey(player.headdown(missile,lives),"S")
l=turtle.onkey(player.headup(missile,lives),"W")
o=turtle.onkey(player.drink(info),"space")
m=turtle.onkey(player.fireball(missile2,info,lives),"z")
n=turtle.onkey(player.fireball(missile2,info,lives),"Z")

The game is running without error but the keys aren't responding to the action.

You can find the code in

https://github.com/Ninedeadeyes/7-Dungeons-Deep/tree/master

The keybinding code is in game(mod version)

All functions are within 'player.py'

Any help would be great.

The onkey function only allows a function with no arguments hence it is not possible to use functions with arguments hence why it doesn't work.

https://docs.python.org/2/library/turtle.html#turtle.onkey

But... If you use

turtle.onkey((lambda:player.go_left(walls)), "Left")

it will work because you are creating a function within a function which will express the output as if it had no arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM