Posted: Fri Jun 01, 2012 12:00 pm Post subject: Need help with arrays
I'm programming a space invaders game inside Turing, and I've been trying to get all the aliens to move properly.
Whenever I tried to move the aliens, they move perfectly fine left to right, however, when I added the code for the game to move down, the aliens no longer move side to side, and they have a very odd animation when moving down.
I think arrays would solve the issue, however, I know absolutely nothing about arrays and the Turing help isn't very helpful.
Posted: Fri Jun 01, 2012 12:52 pm Post subject: Re: Need help with arrays
Okay, so I'll leave figuring out how to move up to you - or at least let you give it a shot before I tell you how.
However, I will help you clean up your monsterous code. At the end, it should look like this:
Turing:
drawfillbox(0, 0, 640, 400, black) type aliens : record
width, height, color_, x, y :int endrecord var alien :array0.. 6, 0.. 3of aliens
for x :lower(alien, 1).. upper(alien, 1) for y :lower(alien, 2).. upper(alien, 2)
alien (x, y).width :=10
alien (x, y).height :=10
alien (x, y).color_ :=white
alien (x, y).x := alien (x, y).width + x * alien(x,y).width *2
alien (x, y).y := alien (x, y).height + y * alien(x,y).height *2 endfor endfor
for x :lower(alien, 1).. upper(alien, 1) for y :lower(alien, 2).. upper(alien, 2) drawfillbox(alien (x, y).x, alien (x, y).y, alien (x, y).x + alien (x, y).width, alien (x, y).y + alien (x, y).height, alien (x, y).color_) endfor endfor
But I'm not going to just throw a whole bunch of code at you and expect you to understand it. Let's go through it piece by piece.
Turing:
type aliens : record
width, height, color_, x, y :int endrecord
Now this is called a record in turing. Basically it lets us create a variable with all these variables inside it. So here, we name the record 'aliens'. And between 'record' and 'end record' we insert the variables we want aliens to have. In this case, we want every alien to have a width, a height, a color, an x coordinate, and a y coordinate.
Turing:
var alien :array0.. 6, 0.. 3of aliens
This is a multidimensional array. But don't let the name scare you. All it means is this array is like a grid of variables, instead of a row of variables. So the way we set up out grid was 0 .. 6, 0 .. 3. Which would be like having a grid that starts at 0, and goes all the way to 6 on the X-axis and starts at 0 and goes all the way to 3 on the Y axis. The cool thing about this, is that just how in math class, an (x, y) coordinate matches a point, in multidimensional arrays, an (x,y) coordinate matches a variable.
The last part of this grid of variables, is telling Turing which type of variable is at each coordinate 'of aliens'. Now we are using two powerful concepts together (three, really) to solve our problem. Now each coordinate point holds the 'aliens' variable we made, which from looking above we know it holds 'width, height, color_, x, y'. Sweet! So now every point on our grid has those variables.
The same way we reference a point on a grid in math, is how we reference a point on a grid in a multidimensional array. (0,2) in math would represent the point that is +0 on the X-Axis and +2 on the Y axis. We reference the multidimensional array the same way (x,y).
So when we say alien (0, 0) it returns the value of the varriable at (0,0) in the grid/array. Now since we used our own variable (aliens), we have to tell it which variable to get. So we get the position the same way alien(0, 0) as before, but now since we have a record we add the variable name we want. It comes out like alien(0,0).width or alien(0,0).x.
Turing:
for x :lower(alien, 1).. upper(alien, 1) for y :lower(alien, 2).. upper(alien, 2)
Now I take it you know for loops. If you don't then let me explain it:
A for loop just loops over a piece of code how many ever times you tell it to. That's it. Yup, it's that simple. Oh, and it also has an added bonus. It will store how many times it has gone through a loop into a variable for you. The way you tell it how many times is you tell it where_to_start .. where_to_end. Simple. If I want a for loop to iterate 5 times I'd say "start at 1 and go until 5" or in turing language 1 .. 5. You tell it what variable to put it in when you say 'for x'. In that scenario, it stores it in a variable named 'x'. If you wanted to store it in a variable named 'my_first_for_loop' you could say 'for my_first_for_loop'.
In this piece of code, we tell it to start at lower(alien, 1). What's that mean? It means to look at the alien grid, on the 1st (x) axis, and find the lower bound. Remember how we said our x axis was starting at 0 and going out until 6? lower(alien, 1) will return 0, because that's where we started our grid at. upper(alien, 1) is the same, as you could guess, except it returns the upper bound of our grid. In this case we went out until 6, so it will return 6. Likewise, lower(alien, 2) will do the same thing as before, but instead of the 1st (x) axis, it will use the 2nd (y) axis.
Now this hopefully you will understand. Our for loops, if you didn't notice, go through the entire grid. Here, we just assign each point in the grid a value.
Turing:
for x :lower(alien, 1).. upper(alien, 1) for y :lower(alien, 2).. upper(alien, 2) drawfillbox(alien (x, y).x, alien (x, y).y, alien (x, y).x + alien (x, y).width, alien (x, y).y + alien (x, y).height, alien (x, y).color_) endfor endfor
Now that you know what the two for loops at the top do, and how they work, this should be self explanatory. The only thing left to talk about is the drawfillbox () function. Now all we are doing is getting the value from the variable at each point that we want, and putting it into the function.
**HINT** You can change the size of the boxes by changing the height and width!
Varsteil
Posted: Mon Jun 04, 2012 11:42 am Post subject: Re: Need help with arrays
Thanks for the help, I tried getting the space invaders to move by changing the constants
line to variables, then putting it inside a loop to increase/decrease those amounts to have the aliens move left and right, and up and down.
However, whenever I did this, only one of the boxes draws and moves.
I don't want you to write the code for me because its better if I write and understand the code. But can you give any tips on how I would resolve the issue? I would also like a hint on how to get collision detection working (The program for the shooting is attached). I usually do whatdotcolor for collision detection, however, with this code, it looks very unlikely. Which command would be the equivalent of whatdotcolor for arrays?
Posted: Mon Jun 04, 2012 2:03 pm Post subject: RE:Need help with arrays
Okay, well the first problem with the code you gave me is:
Turing:
fork controls
And all of your other processes. You shouldn't need any processes, only procedures.
Hmm I'm not exactly sure what your issue is given I can't see the code, but it sounds like your only changing the coordinates for one box.
Instead of adding the +xx to the drawfillbox command, you should change the value of the alien's x and y position. Then it will automatically be done. It will also allow you to easily change how much it is moved. For instance
Turing:
alien (x,y).alienx +=35
would move the alien at position (x,y) on your grid 35 pixles to the right.
For detecting collision, there is a great tutorial here.
I suggest you code the collision yourself, so that you can get a greater understanding of it. But after you get it, I'd suggest using the collision library. (User written librarys = http://compsci.ca/v3/viewtopic.php?t=30381)
Like I said, you should understand what your doing before using other people's library (most of the time, at least in Turing), but once you know what you're doing sometimes it is beneficial to use other library because the developers have had plenty of time to optimize the code. It just so happens I wrote the collision detection module on compsci.
Varsteil
Posted: Tue Jun 05, 2012 11:41 am Post subject: Re: Need help with arrays
Thanks for your help, I'm trying to use the command "alien (x, y).alienx += 10"
However, I'm running into the same problem when I used the +xx strategy
I'm putting the "+=10" line under the "alien (x, y).alienx := alien (x, y).width + x * alien (x, y).width * 3" line. I also replaced the .alienx with .alieny and put that one under the equivalent for the alieny line.
When I try to run it with the command, the aliens appear +10 to the right and +10 to the top. However, when I run it as part of a loop to get the aliens to constantly move, what happens instead is, only 1 alien is drawn, the program is executing, however the alien does not move.
Any tips to resolve this issue?
Raknarg
Posted: Tue Jun 05, 2012 1:12 pm Post subject: RE:Need help with arrays
Could you post the snippet? I dont feel like looking at the whole thing.
Also, as a sidenote... You might want to rename your records a bit. If your variable name is alien, there's no point in calling x in the record "alienx". Simply call it x.
It makes more sense if you're calling alien.x rather than alien.alienx
Aange10
Posted: Tue Jun 05, 2012 4:40 pm Post subject: RE:Need help with arrays
As Raknarg said. I also second the motion to show a bit of the code.
If it is only moving +10, and not constantly moving, it's probably because you're not constantly updating the position.
Varsteil
Posted: Wed Jun 06, 2012 11:11 am Post subject: Re: Need help with arrays
The bolded portion is the portion I'm using to make my aliens move. EDIT: This didn't work inside the code, so its just the part inside the (b)(/b). I understand that I need to put it in a loop to get it to move. When I have no loop, my program just shifts the aliens to the right like I'd expect. However, when I do put it in a loop, depending on where I put it, it draws only 1 box at the bottom left, and either draws that 1 box slowly 10 to the left very slowly, or, it doesn't draw at all.
Sponsor Sponsor
Raknarg
Posted: Wed Jun 06, 2012 12:49 pm Post subject: RE:Need help with arrays
That is how you make an object move. Update the x/y position.
Varsteil
Posted: Thu Jun 07, 2012 11:13 am Post subject: Re: Need help with arrays
@Rak Based on what happens when I change the code, I think that those lines control the distance between each alien.
@Aange, I'm sorry but, I don't exactly understand what I'm supposed to be doing with that code. Can you explain it a bit more to me?
Aange10
Posted: Thu Jun 07, 2012 11:26 am Post subject: RE:Need help with arrays
Sure thing.
It didn't glance at your code before, I only looked at Raknarg's post. So lets look at your code. This is your code:
1) The width of the aliens, I add this onto the parameters for the aliens' x variable to determine the width.
2) The height of the aliens, I add this onto the paramaters for the aliens' y variable to determine the height.
3) I use this to make the aliens green
4) This is the space in between each alien horizontally
5) This pushes the aliens 10 pixels to the right, I assumed that inside a loop, this would cause the aliens to constantly move 10 pixels to the right, however this just caused only the bottom left alien to draw and move 10 to the right.
6) This is the space in between each alien vertically
I don't know what 7 or 8 do, but I know my program doesn't work if I change the numbers
9) alien (x, y).alienx + 35 is the left of the alien, alien (x, y).alieny + 180 is the bottom of the alien, the two codes following it are the right and top of the aliens respectively
Aange10
Posted: Thu Jun 07, 2012 12:27 pm Post subject: RE:Need help with arrays
Okay well you have a pretty good grasp of what's going on.
You know the syntax for drawfillbox right? It's (left point, bottom point, right point, top point, color) and it connects the lines and fills it in. with that known, x is the left point, x + width is the right point.
1) Almost, you add the width to the x variable to determine the right point.
2) Almost again, you add the height to the y variable to determine the top point.
3) Correct
4) Not quite. Though that might be what you're thinking, that's not what you're telling the computer. All you are doing here is MOVING the left point over by alien(x,y).width + x * alien (x, y).width * 3. You're not defining any kind of spacing, your simply moving the x to that spot.
5)You're correct on its use, it adds 10 pixles to its left point.
6) Sadly this is the same thing as on #4, but with the y position. All your doing is moving the bottom point by (the stuff on the right). Your not making any kind of spacing, you're just moving the aliens that way.
7) & 8) These go through every alien. If you don't understand what's happening, try clicking on the tutorial I linked for for loops in my inital post.
9) Your right on this one. However there is no reason to +35 and +180. If you want to do that, then add it to when you define the alien's x variables. That's why we have variables is so we don't have to hard code constants in there.
However, where are the x and y variables coming from on line 4 and 6?
Once you read the for loops tutorial, you might realize why the loops are there.
But if you look at the example i had just posted, all you do is move the x or y position. You don't need to change the width or the height, because people don't get taller or fatter when they move. They just change positions. Likewise, when your drawing the aliens, the right and top points are going to automatically adjust because your x and y points changed. You don't need to worry about them.
alien (x, y).alienx += 10
^ That will move the alien at whatever x and y is on the grid by 10 pixles to the right. If you loop over, it'll keep moving.
Raknarg
Posted: Thu Jun 07, 2012 1:11 pm Post subject: RE:Need help with arrays
Out of curiosity, what was your reasoning behind having the aliens in a multidimensional array?