Arrays and changing them mid-game?
-
Yes, I'm here asking for help yet again, sorry.
I have been tinkering with something and I am really stuck on how to get this working as I want it.
Here is my code so far
It lays out some sprites on the screen, picture boulderdash if you can recall it, and although this works fine and does display the correct sprites, I want to modify it on the fly. So say the player passes through a dirt block, that index in the array should turn from a 1 to a 0 and if there is a 2 above it (boulder) that should drop down.
But even though I can draw these onscreen like it is now, I cannot get into my head how to change the array entries and I'm utterly lost on this.
but then Arrays and I have hated each other since the day I bought Fuze, we just don't see eye to eye and anything with them is a royal pain in the rear end.
-
@SteveZX81 it sounds like you're trying to make something similar to Boulder Dash. This Wireframe Article explains how it's done in pygame zero, so might be useful to you.
-
Not sure if I understand you correctly, but if you want to change say, this "1" into a "0", you would just do:data[3][7] = 0
-
A function to change a particular element would be useful here.
function changeRowElement( row, column, value ) data[row][column] = value return void
There are more fancy ways of doing this, but I think this would help you here. Assuming that you know "where" your player is in the array, you could do like
changeRowElement( playerRow, playerColumn + 1, 0 )
To change the tile to the right of the player into a 0 value.
This function will only work with the
data
array, because it references the global data array inside it. If you wanted to reference any array, you'd have to pass that array to the function. Currently, this is a little dodgy in Fuze. It can totally be worked around, but it's about to change slightly in an update and there will be a notice about this soon. Because of this, I went with the simpler, global versions. Let me know if you want to see the way in which you would pass the array to the function. -
-
I don't know if this was Daves motivation, but in general it's a good idea to have a "something" between the data and the logic. With what Fuze currently supports, that "something" would be a function.
This way, if you change the structure of your data, you only need to update your function, not the game logic that consumes the data. So it's a good preparation to make it easier later to improve your code. Basically what you want in code, is
separation of concerns
, so separating data access from the game logic would be a good practice here. -
There is always a trade off between calling code directly and using functions.
In this case the function method is 3 x slower than accessing the array directly.
But as 1 million operations still only took 6.5 seconds this is probably not a problem.
-
It's not a big difference. Just a good idea to get in to the habit of writing functions to do specific things, rather than lumping it into your game logic, as @PB____ has said.
Really, the benefit comes when you pass it an array. For example:
function changeElement( list, row, column, value ) var newList = list newList[row][column] = value return newList myArray = [ [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1] ] // change row 1 column 3 of myArray to value 10 myArray = changeElement( myArray, 1, 3, 10 )
Now you could pass any old two dimensional array into that function and it will give you back a modified version of that array with the new value. You could update the old one with this, as in the example, or you could create a copy, etc.
This way you wouldn't need to bother writing the array access every time you wanted to change something. Just call your function.
If you're going to do the same type of thing more than once, it's generally best practise to make a function to do it, and pass the things you need into it.
Of course there's always more than one way to skin the proverbial cat. Sometimes it won't be worth bothering with!