Leçon 1, Chapitre 1
En cours

Jeu Tire sur la chauve souris – Etape 4 : Coder le sprite viseur

Yann KIDSHAKER 18 mars 2026

Now that you have created your own aim sprite, it’s time to bring in the Joystick, and write the script to control the sprite using the joystick.

Let’s begin by connecting the Joystick to evive. Oh, and yes, the joystick we’re talking about is the Joystick available in your treasure chest, i.e. your starter kit.

At the base of the Joystick you’ll notice the following 5 pins:

  1. GND (Ground Connection)
  2. +5V (5V Power Supply)
  3. VRX (X-axis potentiometer output)
  4. VRY (Y-axis potentiometer output)
  5. SW (Switch Output)

Connect these 5 pins as follows (refer the diagram to avoid confusion):

  1. Connect the GND pin to evive’s GND pin.
  2. Connect the +5V pin to evive’s 5V pin.
  3. Connect the VRX (X-axis output) pin to the A0 pin on evive.
  4. Connect the VRY (Y-axis output) pin to the A1 pin on evive.
evive Notes Icon
In this activity we only need to move the joystick in four directions. Hence, we won’t be connecting the pin SW pin.

Joystick_connection

 The pins that we’re using to read the value of the movement in the X-axis and Y-axis are, as you must have noticed, analog pins; and for each analog input, you already know that the number of possible unique values that evive can work with are 1024 (0-1023). Therefore, the mean, or the central, or the neutral position of the Joystick would correspond to the middle value of the range, i.e. 512 (≈ 1023/2). However, because nothing is ever 100% accurate, some fluctuations and inaccuracies may creep in; for that we’re going to ignore values between 400 and 600 and accept values only above 600 and below 400. The difference between the values and 512 will give the position of the Aim sprite. To slow down its motion and keep it within the boundaries of the stage, we’ll divide the result by 20.

Now, it’s time to write the script. But first, we need variables to store the X-axis and Y-axis values that evive will read from the Joystick; therefore, we’re going to create two variables, namely XPosition and YPosition to store the X-axis and Y-axis values respectively. By now, you have become an expert of creating variables; so, go ahead, open the Variables palette, and give a demonstration of your expertise!

Done? Great! Now let’s begin writing the script. DON’T forget to select the Aim sprite before writing the script. When you select it, you’ll see that the scripting area becomes empty.

  1. The first step, like every other script for this game, is to drag and drop the when () key pressed block, and select the key you’ve chosen in the previous scripts.
  2. The next step is to set the values of the two variables that you just created to 0; if you don’t, any further changes in its value may lead to an incorrect value being stored in them. For that, go to the Variables palette, and drag and drop set () to () block below the when () key pressed block, and set the value of XPosition to 0. Repeat the step to set the value of YPosition as well to 0.
  3. Next, drag and drop the forever block below the set () to () block.
  4. Now, to check whether the values that evive reads from the Joystick are below 400 and above 600, we’re going to use the if block instead of the if-else block; why? Because we don’t want to take any action in case the condition isn’t satisfied. So, drop the if block inside the forever block. Now, go to the Operators palette, and in the diamond-like shape, drag and drop the () or () block.
  5. To check the condition, we must compare the input. Therefore, in the first space of the () or () block, drag and drop the () < () block, and in the second space, drag and drop the () > () Next, inside the first space of the () < () block and drop the read analog pin () block; you’ll find this block in the evive palette. Set the pin to A0. Repeat the step for the () > () block. Next, in the second space of the () < () block write 400, and in that of the () > () block write 600.
  6. The next step is to define the action that should be taken if the condition is true, and that action is to change the value of XPosition according to the input at pin A0. For that, go to the Variables palette and drag and drop the change () by () block. Before beginning the script we discussed about subtracting 512 from the input and then dividing it by 20 to determine the position of the Aim sprite as the Joystick moves; we’re going to apply this concept while writing the script in the following manner: first, drag and drop the XPosition variable block inside the first space of the change () by () block; then, drag and drop the () / () block inside its second space. Next, in the first space of the () / () block drag and drop the () – () block. In the first of the () – () block write 512 and in the second space drag and drop read analog pin () block and set the pin as A0. Now, in the second space of the () / () block, write 20.
  7. Repeat steps 5 and 6 for checking whether the input at A1 pin satisfies the condition, and changing the value of YPosition. All you have to do is to change the pin from A0 to A1; the rest is the same.
  8. Here, the range for the x coordinate is -240 to 240, and for the y coordinate is -180 to 180. If YPosition is greater than 180, then we will set it as 180; if it is less than -180, then, we will set it as -180. Go to the Control palette and drag and drop another if block. Go to the Operators palette and drag and drop the () > () block in the diamond-shaped space of the if block. inside the first space of the () > () block, drag and drop the YPosition variable block; in the second space, write 180.
  9. Go to the Control palette and drag and drop another if block. Go to the Operators palette and this time, drag and drop the () < () block in the diamond-shaped space of the if block. inside the first space of the () < () block, drag and drop the YPosition variable block; in the second space, write -180.
  10. We will follow the same process to keep XPosition between -240 and 240. Repeat step 8; this time, write 240 instead of 180.
  11. Repeat step 9; this time, write -240 instead of -180.
  12. Lastly, go to the Motion palette and drag and drop the glide () secs to x: () y: () block, and set the value of x as XPosition and of y as YPosition; do this by dragging and dropping the XPosition variable block in the space next to x and YPostion variable block in the space next to y.

And you’re done! The aim script is ready! You can test the script by connecting evive to your computer and moving the Joystick in every direction. In the top left corner of the stage, you’ll see the values of the variables XPosition and YPosition; you can verify that the Joystick is properly connected, and the script is working by observing the change in the values of these variables.

Control Aim Sprite