I am making a snake game in pygame and each cube in the snake needs to be represented by a variable. This variable will be run through a class to move the cube. This will be repeated for each cube, so that the snake moves. An extra cube needs to be added every time the score increases, so this means a new variable must be created. How can I make it so that a new variable is assigned to the new cube?
Asked
Active
Viewed 49 times
0
-
1A `list` would probably suit your purposes better. Dynamically creating and naming variables is the wrong thing to do *most of the time* and the results can often be replicated by using `list`s or `dict`s. – SyntaxVoid Nov 12 '19 at 20:36
-
*"An extra cube needs to be added every time the score increases, so this means a new variable must be created."* Why not just have one variable with a list of the cubes? – kaya3 Nov 12 '19 at 20:36
-
[Tutorial on creating Snake Game](https://techwithtim.net/tutorials/game-development-with-python/snake-pygame/tutorial-1/) shows that multiple cubes are stored in a list which can be appended (see Snake class). – DarrylG Nov 12 '19 at 20:39
-
Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – G. Anderson Nov 12 '19 at 20:47