#include "gbaio.h" #define O 0x01, #define _ 0x00, char sf1[] = { _ O _ _ _ O _ _ O O _ O _ O _ _ _ O O _ O O O _ O _ _ O O _ _ _ O _ O _ O _ O _ _ _ O _ O O _ _ _ O O _ O _ O _ _ _ _ _ _ _ _ _ }; char sf2[] = { _ _ _ _ O _ _ _ _ _ O _ _ _ O _ _ O _ O O O _ _ _ O O O _ O _ O _ _ _ O O O _ _ _ _ O _ _ O _ O _ O _ O _ O _ _ _ _ _ _ _ _ _ _ }; char sf3[] = { _ _ _ _ _ _ _ _ _ _ _ O _ O _ _ _ _ _ O _ _ _ _ _ O _ O O O _ O _ _ O O O _ O _ _ O _ _ _ O O _ _ O _ O _ O _ O _ _ O _ _ _ O _ }; char sf4[] = { _ _ _ _ _ _ _ _ _ _ _ O _ _ _ _ _ O O _ O _ _ _ _ O _ O _ O _ _ O _ O O O O O _ _ O _ O _ O _ _ O _ O _ O _ O _ _ _ _ O _ O _ _ }; #undef O #undef _ //these chars are loaded into memory before the main function even begins int main(void) { start_game(); load_image(1, sf1, IMAGE_8x8); //image number 1,2,3, and 4 are each associated with the chars above, load_image(2, sf2, IMAGE_8x8); //so these 4 image numbers point to the images in the sf1,sf2,sf3, and sf4 images respectively. load_image(3, sf3, IMAGE_8x8); load_image(4, sf4, IMAGE_8x8); int sf[30], i, sfx[30], sfy[30]; float speed=1; //speed is a float, not an int, so it can be changed by .01 for (i=0; i<30; i++) { //this for loop counter sets i to 0, runs through the following "body", adds one to i, and //then, if i is still less than 30, it goes through the following body again, adds one to i, and //then, if i is still less than 30, it goes through the following body again, adds one to i, and //so on until i is 30. sf[i]=i; //assign initial number to each of the variables in this array of all the sprite numbers sfx[i]=rand(0,300); //assign initial number to each of the variables in this array of all the sprite x's sfy[i]=-8; //assign initial number to each of the variables in this array of all the sprite y's } for (i=0; i<30; i++) set_sprite_image(sf[i], rand(1,4)); //each of the sprite numbers in the sf[30] array, which are numbered 0-29, //are taken one at a time and a random image number from 1 to 4 is associated with those sprite numbers while (1) { start_input(); // wake up and smell the buttons, GBA if (button_held(A_BUTTON)) // press the a or b button to change the snowflake speed speed = speed+0.01; if (button_held(B_BUTTON)) speed = speed-0.01; for (i=0; i<30; i++) { //run through the following, but each time increase the variable i by one. //i starts at 0, and end this loop when i hits 30. sfx[i]=sfx[i]+rand(-3*speed, 3*speed); //Take the snowflake x in the sfx[30] array that corrisponds to what i is for this run through //the loop and add a random number between -3s and 3s where s is how fast my snowflakes are going. sfy[i]=sfy[i]+rand(1*speed, 3*speed); //Do the same thing with y, but instead of adding a random number between -3s and 3s, add a random //number between s and 3s. This makes the snowflakes go down and not up, but randomly side to side. if (sfy[i]>270) { //On each run through this for loop, if the snowflake y in the sfy[i] array that corrisponds to what //i is for this run through the loop is at the bottom, then set_sprite_image(sf[i], rand(1,4)); //change that snowflake's image to a random snowflake image and sfy[i] = -8; //move it to the top, or, more accurately, above the top of the screen, just off screen. }; }; start_drawing(); //wait until the right moment, so it doesn't look like a broken TV, and then show the changes made above for (i=0; i<30; i++) //another counter from 0 to 29. It runs through the body of this for loop, then adds 1 to i, then starts over //until adding 1 to i makes it 30 draw_sprite(i, sfx[i], sfy[i]);//for each run through this loop, the sprite at sprite number i is drawn //at the snowflake x in the sfx[30] array that is equal to i and //at the snowflake y in the sfy[30] array that is equal to i. //You have to understand how this draws all the sprites, one on each trip through this loop, //in order to understand what you're changing in the code above. //In other words, the manipulated arrays don't mean anything until they're drawn, //so you have to know how they're being used in the draw_sprite function to know how to manipulate them. } return 0; }