Skip to content

Handling Input

In this tutorial, you’ll make a blue box that moves across the screen using the arrow keys.

This is your first interactive app, and it’s surprisingly easy!

You’ll learn how to:

  • Detect key presses in real time
  • Use pollevent() and keydown() to check input
  • Move a shape by changing its position

Step 1: Setup

Start by importing gint, clearing the screen, and defining your box’s starting position:

import gint

x = 100
y = 100
speed = 4
  • x, y: position of the box
  • speed: how many pixels to move per key press

Step 2: The Game Loop

This loop will run forever — we’ll:

  • Clear the screen
  • Draw the box
  • Check for input
  • Move the box if needed
  • Refresh the display
while True:
    gint.dclear(gint.C_WHITE)
    gint.drect(x, y, x+20, y+20, gint.C_RGB(0, 0, 31))  # Blue box
    gint.dupdate()

We draw the box every frame at its current position.


Step 3: Polling for Input

We now check for keys using pollevent().

    ev = gint.pollevent()

This returns an object with the type of event (KEYEV_DOWN, KEYEV_UP, etc.).

Then we check if the key is held down using keydown():

    if gint.keydown(gint.KEY_LEFT):
        x -= speed
    if gint.keydown(gint.KEY_RIGHT):
        x += speed
    if gint.keydown(gint.KEY_UP):
        y -= speed
    if gint.keydown(gint.KEY_DOWN):
        y += speed

This makes the box move smoothly in response to arrow keys.


Step 4: Exit with EXIT key

Let’s allow the user to quit with the EXIT key:

    if ev.type == gint.KEYEV_DOWN and ev.key == gint.KEY_EXIT:
        break

If you press [EXIT], the program ends.


Final Code

import gint

x = 100
y = 100
speed = 4

while True:
    gint.dclear(gint.C_WHITE)
    gint.drect(x, y, x+20, y+20, gint.C_RGB(0, 0, 31))
    gint.dupdate()

    ev = gint.pollevent()

    if gint.keydown(gint.KEY_LEFT):
        x -= speed
    if gint.keydown(gint.KEY_RIGHT):
        x += speed
    if gint.keydown(gint.KEY_UP):
        y -= speed
    if gint.keydown(gint.KEY_DOWN):
        y += speed

    if ev.type == gint.KEYEV_DOWN and ev.key == gint.KEY_EXIT:
        break

What’s next?

Now that you can move a rectangle. That’s cool !

As an exercise, you can try to prevent the box from going outside the screen.

Next up: Let’s make it move by itself.

💽 Bouncing Logo Simple screen animation using drawing and input