My first Arduboy game: Super Soldiers


(David Inwood) #1

Preview Small

I got an Arduboy for Christmas, and I’ve been trying to program a game with it for the last few weeks. This is basically a Power-Ranger type of fighting game. You play as the Pink Super Soldier as she learns how to transform, and then teleports to Fort McMurray to fight a giant ant. The file can be downloaded here: https://github.com/DavidInwood/SuperSoldiers.git

I originally wanted the game to be way longer, but I didn’t realize how limited the program memory is. I may make sequels in the future since I’ve got the basic fighting code working now.

I should probably give a heads up that this game uses more than 70% of dynamic memory, so it will likely trigger the memory glitch that prevents you from loading new games. This topic describes how to deal with it: How to Reset Arduboy if it's Bricked.


(Simon) #2

Great work.

You may be able to save some memory using the tip published on the lower half of page 22 of Volume 7 of the magazine. It shows a simple way to extend the Arduboy class and provide text wrapping at a given location (loc 67 in your code below).

You can also grab the code here > https://github.com/filmote/Extend

if(GameState == GordonsFace1){
  arduboy.setCursor(67,0);
  arduboy.print("Use the");
  arduboy.setCursor(67,9);
  arduboy.print("power of");
  arduboy.setCursor(67,18);
  arduboy.print("the");
  arduboy.setCursor(67, 27);
  arduboy.print("ancient");
  arduboy.setCursor(67,36);
  arduboy.print("gems to");
  arduboy.setCursor(67,45);
  arduboy.print("fight the");
  arduboy.setCursor(67,54);
  arduboy.print("monster.");
};

The above could become:

if(GameState == GordonsFace1){
  arduboy.setCursor(67,0);
  arduboy.print("Use the\npower of\nthe\nancient\ngems to\nfight the\nmonster.");
};

Also in the DrawBlue(), DrawPink() and DrawGreen() functions, look to save the coordinates and image into local variables and call the drawBitmap() below the switch.

Also, the code below uses ‘int’ rather than correctly sized variables.

I changed the following code …

struct SuperSoldier{
  int x;
  int8 y;
  int z;
  int Stance;
  int Facing;
  int HP;
  int StanceDelay;
  int GroundLevel;
};

to …

struct SuperSoldier{
  int x;
  uint8_t y;
  int z;
  int Stance;
  int Facing;
  int HP;
  int StanceDelay;
  int GroundLevel;
};

and saved 148 bytes of Progmem and 4 bytes of RAM. I have made the assumption that the Y values is a positive integer between 0 and 255 - maybe this is wrong but the point is I bet not all of your variables need to be 16 bit numbers that allow both positive and negative values.

Do this where possible across the board and you will save a lot! Google uint8_t, int8_t, uint16_t and int16_t and try to resize all of your variables to the lowest size possible.


(Scott) #3

If you’re short on RAM, using the F() macro for printing fixed strings will help. This will store the strings only in program memory.

E.g.:
Change:

arduboy.print("Fort McMurray is");

to:

arduboy.print(F("Fort McMurray is"));

Since you’re only using single tones, you could consider using my ArduboyBeep class instead of ArduboyTones. This will free up some code space. The ArduboyBeep functions will be included in the next release of the Arduboy2 library.


(Scott) #4

You could also just place the text on the left side and the graphics on the right, so that \n in a string, or the println() function, would properly wrap to the start of the next line, without needing a text wrapping extension.


(Simon) #5

He could do that too. Of course, I do not understand why wrapping is not part of the base class.


(Erwin) #6

I added your game to http://arduboy.ried.cl :smiley:


(David Inwood) #7

Thank you! I’ll try these things and see how much memory I save. The “z” and “Facing” variables can definitely be made a lot smaller.


(Simon) #8

I am super careful now with trying to pick the smallest variable types I can. I often run into the reverse problem - my game is doing stupid things and I find that my variable is too small and the value has overflown!