You must first complete Making a Basic Function before viewing this Lesson

Screen Movement Using Touchpads

Introduction

We will now learn how the most basic of video game movement works. We will use a controller (touchpads) to move a sprite or character (the pixel).

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

1

Importing some more libraries

Drag the import lp_sensors and the from time import sleep blocks to the workspace

2

Setting up the touchpads

Get the 4 blocks to set up the touchpads in the Setup category.

3

Building the main loop

First up, we’ll drag 4 if condition blocks inside a while True block and a sleep (1) block. Change the (1) to (0.1). 

4

The first condition

We’ll start by building the code for the L1 Button. We will set this button as an Down button. Upon pressing it the pixel on our screen should move DOWN.

y+=5 means that every time the L1 is pressed, y increases by 5 and the we call the movePixel function to draw the pixel on a new part of the screen. 

5

The second condition

Afterwards, we will make the code for the L2 Button. We will set this button as the Up button. Upon pressing it the pixel on our screen should move UP.

y-=5 means that every time the L2 is pressed, y decreases by 5 and the we call the movePixel function to draw the pixel on a lower part of the screen.

6

The third and fourth conditions

Finally, we can apply the same principle on the last 2 conditions. This time, we just have to change the y to x. We will set R1 as Left and R2 as Right. 

Every time R1 is pressed, x is reduced by 5. Moving the pixel to the left.

Every time R2 is pressed, x is incremented by 5. Moving the pixel to the right.

The Complete Code

Next up, we’ll add a simple code that will keep the pixel within the boundaries of our screen

Back to: Intermediate > Game Controller

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account