Nwagyu!Nwagyu!
App installer
  • English
  • Français
GitHub
App installer
  • English
  • Français
GitHub
  • Documentation Index
  • Tutorial
    • Part 1: Project creation
    • Part 2: Cleaning up the template
    • Part 3: Moving the snake
    • Part 4: Handling snake length
    • Part 5: Eating fruits
    • Part 6: Game over
    • Part 7: Map edge handling
    • Part 8: Frame limiter
    • Part 9: Progressive fruits spawning
    • Part 10: Score calculator and saving
    • Part 11: Map support
    • Part 12: App icon
    • Part 13: Increasing speed
    • Part 14: Conclusion
  • External apps
    • Creating your own application
    • Accessing storage
    • On/Off and Home keys
    • Syscalls
  • Firmware
    • Boot process
    • Slots
    • Bootloader
    • Kernel
    • Userland
    • Addresses and structures
  • Others
    • Communication with the computer (DFU)
    • Downloading Epsilon from NumWorks' website
  • Developers tips
    • [Rust] Using a heap allocator when developing apps
  • User documentation

Part 13: Increasing speed

The snake game gets progressively harder over time due to the snake tail length increasing, but once you have some strategies, you can manage to almost never die. To reduce this problem, we will add a small difficulty touch: increasing the snake speed when a fruit is eaten

First, we need to decide what are the difficulty parameters, our constants:

  • Initial speed, which I've set to 100ms between each frame
  • Minimal speed, at 40ms between each frame
  • Speed change when a fruit is eaten, at -2ms/fruit

In main.h, we can declare them:

#define SNAKE_INITIAL_SPEED 100
#define SNAKE_MIN_SPEED 40
#define SNAKE_SPEED_CHANGE 2

We now need to decrease the frame duration when a fruit is eaten, which is easy given we already have a check increasing the score.

After display_score in main.c, we just need to add the following code:

target_frame_duration -= SNAKE_SPEED_CHANGE;
if (target_frame_duration < SNAKE_MIN_SPEED) {
    target_frame_duration = SNAKE_MIN_SPEED;
}

Our game is now more difficult.

As the game is now faster, an issue can be noticed: if you are pressing two arrows at the same time, only one will be taken into account. This can be improved by ignoring the current direction key in addition to the U-turn prevention.

In snake.c, we just need to add the check

void up() {
    if (last_direction != SNAKE_DIRECTION_DOWN && last_direction != SNAKE_DIRECTION_UP) {
        direction = SNAKE_DIRECTION_UP;
    }
}

void right() {
    if (last_direction != SNAKE_DIRECTION_LEFT && last_direction != SNAKE_DIRECTION_RIGHT) {
        direction = SNAKE_DIRECTION_RIGHT;
    }
}

void down() {
    if (last_direction != SNAKE_DIRECTION_UP && last_direction != SNAKE_DIRECTION_DOWN) {
        direction = SNAKE_DIRECTION_DOWN;
    }
}

void left() {
    if (last_direction != SNAKE_DIRECTION_RIGHT && last_direction != SNAKE_DIRECTION_LEFT) {
        direction = SNAKE_DIRECTION_LEFT;
    }
}

This now allows you to press two keys at the same time, and the snake will alternatively choose one or another depending on the current direction. You can use this behavior to move diagonally, for example.

That's it for this part

Don't forget to commit your work!

Edit this page
Last Updated: 3/6/26, 10:31 PM
Contributors: Yaya-Cout
Prev
Part 12: App icon
Next
Part 14: Conclusion