Reputation: 808
My code currently reads in a string (made of numbers 0
-9
) and uses that value to blit a tile to the screen in a corresponding array. Since I use numbers it's easy to slice the string to get one character and cast that value to an int to use it as a index, e.g.:
display.blit(tiles[int(slicedString), rect])
Since I use this method, I can only have 10 elements in the tiles array (because I cant slice a number like 10
). Can anyone think of a way around this to get, let's say, 20 elements in the tile array?
Upvotes: 2
Views: 27
Reputation: 122097
In the same way that e.g. hexadecimal uses 'a'
for 10
, 'b'
for 11
, and so on, just use letters for numbers beyond 9
:
0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j
One advantage of this is that int
in Python already implements this for base
s beyond 10
:
>>> int('a', 20)
10
>>> int('j', 20)
19
>>> int('hi', 20)
358
which simplifies your implementation.
Upvotes: 1