Tutorial 11: Structures

So you’re starting to learn more and more about coding for the Wii and you’ve come to the point where you your variables to be more organised. This may be the cause if you have multiple objects on the screen that need to move around, etc. You can grab the PDF of this tutorial here: codemii-tutorial-11

You are actually able to put variables in structures, which mean you can have multiple variables in the one element of the structure.

For example, if you wish to have an object’s x / y co-ordinates as well as if the objects are on the screen, you can use the example below.

struct object_struct {
	int x;
	int y;
	bool on_screen;
};

Then you just assign this structure to a variable (10 elements of x, y and on_screen for this array)

struct object_struct objects[10]; 

And now you can perform the following assignments.

object[0].x = 42;
object[0].y = 78;
object[0].on_screen = true;
object[1].x = 342;
object[1].y  = 123;
object[1].on_screen = true; 

You can then just use a β€œfor” loop which will loop to 10 and assign random values to the x and y co-ordinates and make them move around easily as below.

// Assign random x and y co-ordinates
int i;
for (i = 0; i < 10; i++) {
	object[i].x = rand() % 640 + 1;
	object[i].y = rand() % 480 + 1;
	object[i].on_screen = true;
}

// Make them move around randomly and disappear once they are off the screen
int on_screen_counter = 10;

while (on_screen_counter >= 1) {
	for (i = 0; i < 10; i++) {
		if (object[i].on_screen == true) {
			// Random true or false
			if ((rand() % 1) == 1) {
				object[i].x += rand() % 3 + 1;
				object[i].y += rand() % 3 + 1;
			}
			else {
				object[i].x -= rand() % 5 + 1;
				object[i].y -= rand() % 5 + 1;
			}
			
			// Outside the screen
			if (object[i].y > 480 || object[i].y < 0 || object[i].x > 640 || object[i].x < 0) {
				object[i].on_screen = false;
				on_screen_counter--;
			}

			// Print your image on the screen here E.g:
			// DrawImg (object[i].x, object[i].y, test_img);
		}
	}
}

That wraps up this quick tutorial on using structures in your code. You are also able to have structures inside structures too. See you next time.

6 Responses to “Tutorial 11: Structures”

  1. Pvt Ryan says:

    Mate you have loads of mistakes here..

    1) code segement 2: Should be:
    struct object_struct objects[10];

    2) code segement 3:
    You should declare i at the top instead of x
    The 1st for loop should use “i” as the counter as you use it later and haven’t declared it.

    3) code segement 3: Inside 1st for loop should be
    object[i].x = rand() % 640 + 1;
    object[i].y = rand() % 480 + 1;
    object[i].on_screen = true;

    4) code segment 3: Your outside screen logic is wrong, it should be:
    object[i].y > 480 || object[i].y < 0 || object[i].x > 640 || object[i].x < 0

    I have made the corrections to the PDF versions and will email them to you now.

  2. paulus says:

    In C an expression of the form
    object[0].x = 42;
    is called an assignment, not a call…

  3. mrjon says:

    can you make a tutorial for configure devkitpro and eclipse? with the autocomplete tool? for wii development…thanks! great work!

Leave a Reply