Using Arrow Keys Input

511 views
Skip to first unread message

Brad Strassburger

unread,
Mar 6, 2016, 5:23:05 PM3/6/16
to VPython-users
I am trying to create a simple program where I use the arrow keys to move an object on the screen. I have been able to figure this out by using other keys on the keyboard (specifically letters and numbers) but cannot figure out how to use the arrow keys to do this. Can anyone help?
Thanks
Message has been deleted

Bruce Sherwood

unread,
Mar 6, 2016, 6:51:27 PM3/6/16
to VPython-users
Here is a little program that lets you explore the keyboard input. You'll find that pressing the arrow keys yields the character strings 'left', 'right', 'up', and 'down'.

from visual import *
prose = label()
while True:
    ev = scene.waitfor('keydown')
    prose.text = ev.key

Brad Strassburger

unread,
Mar 6, 2016, 7:07:31 PM3/6/16
to VPython-users
This is a great little program. Thank you so much, it is very helpful.

Aaron Titus

unread,
Mar 7, 2016, 9:22:31 AM3/7/16
to vpytho...@googlegroups.com
I know that Bruce already gave you a program to use. However, I thought I would add some additional suggestions.

I teach a physics course for non-science majors called Physics for Video Games. This semester, I am converting my course materials from VPython Classic to GlowScript VPython.

If you are using VPython Classic, see Chapter 4 of the pdf at:


If you are using GlowScript VPython, see the updated version of this chapter at:



This chapter/activity in my course teaches students to add keyboard interactions to a simple program where balls move back and forth in the scene and a box (the shooter) shoots “bullets” at the balls. The following chapter, in each case, adds collision detection in order to complete this simple game.

In VPython Classic, there is no event for lifting a key. However, in GlowScript VPython, there is an event called ‘keyup’. To see two different versions of this simple game, go to:


The program "shoot-the-ball" uses the keyup event. Hold down the left/right arrow keys to move the shooter left and right. When you stop pressing the key, the shooter stops. Use the spacebar to shoot a bullet.

The program "shoot-the-ball-v2"  is like the VPython Classic version of the game. In this case, the shooter travels left and right. Pressing an arrow key changes the direction of the velocity and pressing any other key sets the velocity to zero. Again, the spacebar shoots a bullet.

When I first created the course about 5 years ago, GlowScript was in its infancy so I used Classic. Now, in 2016 GlowScript VPython is the preferred choice. It has numerous practical advantages for students writing and submitting their programs. However, the keyboard event ‘keyup’ is a big advantage for games.

Aaron


On Mar 6, 2016, at 5:23 PM, Brad Strassburger <bradstra...@gmail.com> wrote:

I am trying to create a simple program where I use the arrow keys to move an object on the screen. I have been able to figure this out by using other keys on the keyboard (specifically letters and numbers) but cannot figure out how to use the arrow keys to do this. Can anyone help?
Thanks

--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages