Arduino Asteroids Game: Info and Credit Screens!

While not very exciting from the gamer standpoint, the information and credits screens are really important for several reasons. The main one being that it tells you how to play the game. It also lets you know how to contact the developer and who made the game. In the event you want to find more great material to play with!

Essentially, I have set up a scrolling screen that scrolls information over and over again. Pretty boring. However, I found a useful gem that I’d like to share with you here.

You see, as the information scrolls by, you might decide to click a button to do something, e.g., go back to the main menu, start the game, or what not. Well, the original method I was using was:

  • Loop through displayed data.
  • Check if they clicked a button.
  • Add 1.5 second delay.

This is a problem because you may have to hold the button for 1.5 seconds before it is picked up by the program. So, how do we include a delay, but constantly check for button presses? Well, with a “while” loop of course! Check it out:

gameDelay = 0;
      while (gameDelay < 2000){
       // Add a delay in the loop.
       delay(1);
       gameDelay++;
     lcd_key = read_LCD_buttons();   // read the buttons
  
       switch (lcd_key){ // depending on which button was pushed, we perform an action
    
           case btnSELECT:{
                 gameState = 0;
                 gameLineNum = 0;
                 break;
           }
       }
       }

So, instead of 2 seconds of delay all at once, I have a while loop that delays 1 ms, checks for button pushes, and then repeats for 2000 times, or roughly 2 seconds. This allows the player to press the button at any time, and only delays by 1 ms before reading it. Not only is this more functional, but it looks cooler in code too!

You can check out the full commit for the credits screen and info screens on my GitLab if you want more information.

Linux – keep it simple.

3 thoughts on “Arduino Asteroids Game: Info and Credit Screens!

  1. I’m guessing you found the time involved in checking the buttons to note affect the approximation of time too much?
    If you were concerned about the total lifetime number of reads of the hardware (if that’s a thing), I think you could afford to delay about 100ms before checking the buttons. Or are hardware interrupts a thing on this platform? Or am I thinking too low-level, based on my old college days…

    Liked by 1 person

    1. I don’t think this supports hardware interrupts on this very primitive device. 😀 The buttons work by checking the analog voltage return (each button creating a different resistance), so it is somewhat cumbersome and slow, generally speaking, and you can actually only use one button at a time, as the lowest resistance will be the one read.

      You are absolutely right though, it could easily be lengthened to 100 ms or even 250 ms and would still work pretty well. It is probably a little overkill to check it 1000 times per second, as my fingers are not that fast, and since you can only press one button at a time.

      If resources were a premium, then I would lower the rate of check, but it is more than able to handle the load since the game itself requires very little to run, even on an Arduino.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s