Can't seem to apply the concept of arrays and flexible arrays.
Author |
Message |
Luffy123
|
Posted: Thu Jun 07, 2012 5:00 pm Post subject: Can't seem to apply the concept of arrays and flexible arrays. |
|
|
Like I know what both of them do perfectly. I don't know at all how to apply them when making my game. I am trying to make a snake game. Regular snake, and I know you have to use arrays in it but I don't know how to apply my knowledge of arrays in this game. Any help would be appreciated. Thank you. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Thu Jun 07, 2012 5:27 pm Post subject: RE:Can\'t seem to apply the concept of arrays and flexible arrays. |
|
|
You could use arrays to store the location (x,y) of each segment of the snake.
You could use arrays to store the map (wall sections, non-wall sections).
You could use arrays to store high scores. |
|
|
|
|
![](images/spacer.gif) |
Luffy123
|
Posted: Thu Jun 07, 2012 5:38 pm Post subject: Re: RE:Can\'t seem to apply the concept of arrays and flexible arrays. |
|
|
DemonWasp @ Thu Jun 07, 2012 5:27 pm wrote: You could use arrays to store the location (x,y) of each segment of the snake.
You could use arrays to store the map (wall sections, non-wall sections).
You could use arrays to store high scores.
What do you mean use arrays to store the map?
And when storing the snake x'ys. how do I use an array to draw out the each segment of the snake? i mean how do i use each point of a flexible array. I'll keep adding points everytime it eats an object and to make the snake increase ill have to add 1 point to the flexible array but how do i draw the snake with the flexible array is what im confused about |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Thu Jun 07, 2012 7:42 pm Post subject: RE:Can\'t seem to apply the concept of arrays and flexible arrays. |
|
|
Well, the first thing is whether or not you're going to use graphics or make it text based, it'll change it a little bit. |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Thu Jun 07, 2012 8:04 pm Post subject: Re: Can't seem to apply the concept of arrays and flexible arrays. |
|
|
Raknarg wrote: Well, the first thing is whether or not you're going to use graphics or make it text based, it'll change it a little bit.
Text based snake???
Luffy123 wrote: What do you mean use arrays to store the map?
And when storing the snake x'ys. how do I use an array to draw out the each segment of the snake?
For the map, suppose your game area is a 30x30 grid. Then you can have a 30x30 array that tells you which places in the grid are walls.
To draw the snake, you can just go through the array (with a loop) and for each pair of x and y values, draw a snake segment at the appropriate place on the screen.
Note that you can store the map and snake in other ways as well. |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Thu Jun 07, 2012 8:15 pm Post subject: RE:Can\'t seem to apply the concept of arrays and flexible arrays. |
|
|
Lol I mean like the graphics are done as text, I had to do that for one of my projects. |
|
|
|
|
![](images/spacer.gif) |
Luffy123
|
Posted: Thu Jun 07, 2012 9:07 pm Post subject: Re: Can't seem to apply the concept of arrays and flexible arrays. |
|
|
Dreadnought @ Thu Jun 07, 2012 8:04 pm wrote: Raknarg wrote: Well, the first thing is whether or not you're going to use graphics or make it text based, it'll change it a little bit.
Text based snake???
Luffy123 wrote: What do you mean use arrays to store the map?
And when storing the snake x'ys. how do I use an array to draw out the each segment of the snake?
For the map, suppose your game area is a 30x30 grid. Then you can have a 30x30 array that tells you which places in the grid are walls.
To draw the snake, you can just go through the array (with a loop) and for each pair of x and y values, draw a snake segment at the appropriate place on the screen.
Note that you can store the map and snake in other ways as well.
Oh, how would I move each segment of the snake then? When using an array?
Also I have another question. When declaring a multidimensional array and giving it a value.
var a : array 1 .. 10, 1 .. 5 of int
for i : 1 .. 10
for k : 1 .. 5
a(i,k) := 4 // what does this do. What is it assigning the value 4 to? I can't seem to get the picture in my head.. can someone clearly explain it?
end for
end for |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Thu Jun 07, 2012 9:49 pm Post subject: Re: Can't seem to apply the concept of arrays and flexible arrays. |
|
|
We can visualize a 2-dimensional array as a grid of numbers.
Here's a 2x3 array. (array 1..2, 1..3 of int) I've already given it some values
Now let's find the element at (1, 2). This is like saying the element in the first row and second column.
So the value in the array at (1, 2) is 2.
If we run the code Turing: | for i: 1..2
for j: 1..3
myArray (i, j) = 0
end for
end for |
This is what the process might look like at each iteration of the loops (I put the values of i and j above each)
code: | (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3) <- (i, j)
0 2 3 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0
4 5 6 4 5 6 4 5 6 0 5 6 0 0 6 0 0 0 |
As you can see, we are setting each element in the array to zero one at a time.
As for moving the snake, each segment moves to were the segment ahead of it was. So you just have to shift all the values in the array toward one end (which ever is the tail). |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Luffy123
|
Posted: Thu Jun 07, 2012 10:06 pm Post subject: Re: Can't seem to apply the concept of arrays and flexible arrays. |
|
|
Dreadnought @ Thu Jun 07, 2012 9:49 pm wrote: We can visualize a 2-dimensional array as a grid of numbers.
Here's a 2x3 array. (array 1..2, 1..3 of int) I've already given it some values
Now let's find the element at (1, 2). This is like saying the element in the first row and second column.
So the value in the array at (1, 2) is 2.
If we run the code Turing: | for i: 1..2
for j: 1..3
myArray (i, j) = 0
end for
end for |
This is what the process might look like at each iteration of the loops (I put the values of i and j above each)
code: | (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3) <- (i, j)
0 2 3 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0
4 5 6 4 5 6 4 5 6 0 5 6 0 0 6 0 0 0 |
As you can see, we are setting each element in the array to zero one at a time.
As for moving the snake, each segment moves to were the segment ahead of it was. So you just have to shift all the values in the array toward one end (which ever is the tail).
That made a lot of sense. I think I finalyl understand everything I was thinking of it as a cartigian plane graph but now I understand you can set each element a number. |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Fri Jun 08, 2012 10:19 am Post subject: RE:Can\'t seem to apply the concept of arrays and flexible arrays. |
|
|
Exactly. It's more like a row of boxes. If you had an array of 1 .. 3 of int, it would be like having a row of three boxes which all hold an integer. If you have an array 1 .. 3, 1 .. 3 of int then you would have a grid of 9 boxes in total, which all hold an integer. |
|
|
|
|
![](images/spacer.gif) |
|
|