moving 1 to another position if keyhit is true?
BlitzMax Forums/BlitzMax Beginners Area/moving 1 to another position if keyhit is true?
| ||
Function moveblock() If KeyHit(KEY_LEFT) For Local x:Int = 0 To 9 For Local y:Int = 0 To 9 If array:Int[x,y] = 1 array[x+1,y] EndIf Next Next EndIf End Function if I fill my array with 0's and assign just one position the number one. I can draw a box at that position in the array if it equals 1. Now, If i wanted the box to move position I would set a different position in the array the number 1. Then when I run the code the box is drawn at a different location. I do this by manually assigning the value outside of a loop before I run the code. Looking at the code above I'm trying to move the box from one position in the array to the next available position in the array using keyhit(). It's obviously not working. I'll try some more ideas but a little help would be appreciated. Heres is all the code : Strict Rem Array Movement for game End Rem 'Setup Graphics Mode Graphics 800,600,16,0 'set Alpha SetMaskColor 0,0,0 'load image Global box:TImage = LoadImage("icon.png",MASKEDIMAGE) 'Array Origin Global mapx:Int, mapy:Int mapx = 10 mapy = 10 'Setup array Global array:Int[10,10] ' Fill array with data For Local x:Int = 0 To 9 For Local y:Int = 0 To 9 array:Int[x,y] = 0 Next Next 'set 1 position of array to 1. I do this manually. Change to have the box drawn elsewhere. array:Int[0,0] = 1 Repeat Cls drawblock() moveblock() FlushMem Flip Until KeyHit(KEY_ESCAPE) Function drawblock() For Local x:Int = 0 To 9 For Local y:Int = 0 To 9 If array:Int[x,y] = 1 DrawImage box,Mapx+x*50,mapy+y*50,0 EndIf Next Next End Function Function moveblock() If KeyHit(KEY_LEFT) For Local x:Int = 0 To 9 For Local y:Int = 0 To 9 If array:Int[x,y] = 1 array[x+1,y] EndIf Next Next EndIf End Function the image is here : ![]() |
| ||
Use a counter variable, say "BoxPos", to keep track of where the box currently is. Initially, BoxPos = 0. The algorithm you need would be something like this: (This assumes the array is a 'square', with size ArraySize) If Keyhit(theKey) array[BoxPos / ArraySize, BoxPos Mod ArraySize] = 0 BoxPos = BoxPos + 1 array[BoxPos / ArraySize, BoxPos Mod ArraySize] = 1 Endif Ryan |
| ||
Hi Ryan. I honestly have no clue what your code does or how I'm going to implement it. I'll try though. thanks :) P.S Any other ideas? |
| ||
Haha, that's pretty much the implementation you want - just replace the code in your moveBlock() function with the code I've given you (with small changes). BoxPos starts at 0. From your code, you have a 10 x 10 array, so ArraySize would be 10. I'm assuming your box starts off at array location [0,0], so array[0,0] = 1. When you press the key, array[0,0] becomes 0, boxPos is incremented by 1, and array[0,1] becomes 1. When boxPos is 9, array[0,9] would equal 1. Pressing the key now would make array[0,9] equal 0, boxPos = 10, then array[1,0] equal 1. This is because of the way the / and Mod operators work. This is the sort of thing you want, isn't it? Ryan |
| ||
Haha, that's pretty much the implementation you want - just replace the code in your moveBlock() function with the code I've given you (with small changes). BoxPos starts at 0. From your code, you have a 10 x 10 array, so ArraySize would be 10. I'm assuming your box starts off at array location [0,0], so array[0,0] = 1. When you press the key, array[0,0] becomes 0, boxPos is incremented by 1, and array[0,1] becomes 1. When boxPos is 9, array[0,9] would equal 1. Pressing the key now would make array[0,9] equal 0, boxPos = 10, then array[1,0] equal 1. This is because of the way the / and Mod operators work. This is the sort of thing you want, isn't it? I need a while before the above would start to make sense. :) Watch this space, I may ask you to explain it again as if you were talking to a retard which by the way I'm not far from. ;) |
| ||
All you need are two variables to keep track of where the box is : X and Y. If you move left, reduce X. If you move right, increase X. If you move up, reduce Y. If you move down, increase Y. Initially, the box will be in an arbitrary position in the array, say [5,5]. So X will store 5, Y will store 5 and the array at index [5,5] will be set to 1 at the start. When you make a move, you set the array at index [X,Y] to 0, you make the move, then set the array at the new index to 1. However, you will also need to check that a move will not take you outside of the array, and you will also need to include some sort of delay into your movement routine otherwise the movement will be too fast to see. Come on Amon, you should be able to do this sort of stuff! Ryan |
| ||
Hi Ryan. I have made some progress although with a few problems. Heres my movement code : Function moveblock() If KeyHit(KEY_RIGHT) array[BoxPosx / 10, BoxPosx Mod 10] = 0 BoxPosx = BoxPosx + 10 array[BoxPosx / 10, BoxPosx Mod 10] = 1 EndIf If KeyHit(KEY_LEFT) array[BoxPosx / 10, BoxPosx Mod 10] = 0 BoxPosx = BoxPosx - 10 array[BoxPosx / 10, BoxPosx Mod 10] = 1 EndIf If KeyHit(KEY_DOWN) array[BoxPosx / 10, BoxPosx Mod 10] = 0 BoxPosx = BoxPosx + 1 array[BoxPosx / 10, BoxPosx Mod 10] = 1 EndIf If KeyHit(KEY_UP) array[BoxPosx / 10, BoxPosx Mod 10] = 0 BoxPosx = BoxPosx - 1 array[BoxPosx / 10, BoxPosx Mod 10] = 1 EndIf End Function I've changed ArraySize to 10 and have set up 2 globals "BoxPosx & BoxPosy". Although there doesnt appear to be a need for BoxPosY It does what I want it to do even though I still dont get the maths for it. array[BoxPosx / 10, BoxPosx Mod 10] = 0 BoxPosx = BoxPosx - 1 array[BoxPosx / 10, BoxPosx Mod 10] = 1 Why is it like this? "array[BoxPosx / 10" = Dividing boxposx by 10 to get what value? Then : ",BoxPosx mod 10] = 1" = Does what? I dont get whats happening. |
| ||
No no, I meant do it like this:; Assumes global X and Y ; Remember to check for moving outside of the array, ; and to include some sort of delay. Function moveblock() If KeyHit(KEY_RIGHT) array[X,Y] = 0 X = X + 1 array[X,Y] = 1 EndIf If KeyHit(KEY_LEFT) array[X,Y] = 0 X = X - 1 array[X,Y] = 1 EndIf If KeyHit(KEY_DOWN) array[X,Y] = 0 Y = Y + 1 array[X,Y] = 1 EndIf If KeyHit(KEY_UP) array[X,Y] = 0 Y = Y - 1 array[X,Y] = 1 EndIf End Function Your method requires only one variable. Imagine a 3 x 3 array is numbered like this: 0 1 2 # # # 0 # 0 1 2 1 # 3 4 5 2 # 6 7 8 If BoxPos was say 5, then by the above grid, it is stored in column 2, row 1. The array is a 3 x 3 grid. 5 Mod 3 = 2, the column number, and 5 / 3 = 1, the row number. Try it for other values of BoxPos to see how it works. However, this method is far slower and less natural than the one above - use the one with the X and the Y. Ryan |
| ||
The new version actually makes more sense to me. I didnt get the other one at all. Well it works so now I have to add checks to see if the box goes out the arrays boundaries. I dont need a timer i think because I'm using keyhit() instead of keydown() so the box only moves if the key has been hit. |
| ||
Success! |
| ||
Hoe would I check if the box has exceeded the boundaries of the array? I cant seem to get it to work. |
| ||
Oh come on! If X = 0, prevent movement to the left. If Y = 0, prevent movement upwards. If X = ArraySize - 1, prevent movement to the right. If Y = ArraySize - 1, prevent movement downwards. Easy peasy! Ryan |