sad*_*jfh 2 c arrays struct roguelike
我正在做一个roguelike游戏.我想将地图表示为结构数组,例如在数组中有256个结构.地图是一个16*16的图块网格,每个图块都有属性,例如它上面是否有项目.
所以说我想要一个256个结构的数组tiles:
struct tiles {
char type; /* e.g. dirt, door, wall, etc... */
char item; /* item on top of it, if any */
char enty; /* entity on top of it, e.g. player, orc if any */
}
Run Code Online (Sandbox Code Playgroud)
然后,我需要访问这样的结构数组:
int main(void)
{
unsigned short int i;
struct tiles[256];
for (i = 1; i <= 256; i++) {
struct tiles[i].type = stuff;
struct tiles[i].item = morestuff;
struct tiles[i].enty = evenmorestuff;
}
}
Run Code Online (Sandbox Code Playgroud)
您需要为数组命名.如果int变量如下所示:
int my_int
Run Code Online (Sandbox Code Playgroud)
一系列的ints看起来像:
int my_ints[256]
Run Code Online (Sandbox Code Playgroud)
然后是一系列struct tiles看起来像:
struct tiles my_tiles[256]
Run Code Online (Sandbox Code Playgroud)
声明一个数组struct tiles只是将它放在变量之前,就像处理其他类型一样.对于10的数组int
int arr[10];
Run Code Online (Sandbox Code Playgroud)
同样,声明一个256的数组 struct tiles
struct tiles arr[256];
Run Code Online (Sandbox Code Playgroud)
要访问任何成员,说type,的元素arr,你需要.操作员 arr[i].type
小智 5
数组是一个变量,就像一个整数,所以你需要给它一个名字来访问它.
注意:数组的索引最低,索引0最高255,因此for循环应该是:for (i = 0; i < 256; ++i)而不是.
int main(void)
{
unsigned short int i;
struct tiles t_array[256];
for (i = 0; i < 256; ++i) {
t_array[i].type = stuff;
t_array[i].item = morestuff;
t_array[i].enty = evenmorestuff;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16048 次 |
| 最近记录: |