## 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; `

And now you can perform the following assignments.

```object.x = 42;
object.y = 78;
object.on_screen = true;
object.x = 342;
object.y  = 123;
object.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;

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

• teknecal says:

Thanks for those corrections, I didn’t actually test or compile this 😉

• Pvt Ryan says:

Just noticed I have typo’d the 1st word in the pdf.. DOH!!!

2. paulus says:

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

• teknecal says:

You know what I mean :P. Thanks.

3. mrjon says:

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