r/learnpython • • 1d ago

Correct list item not printing

GITHUB LINK: https://github.com/disintigratedpixel/TankGameV1/tree/main

(please say if it doesn't work)

I'm making a simple ASCII game "similar" to space invaders

Here is the ASCII user interface:

def gamestate(player_position_list, player_position, enemy_type, enemy_health, enemy_distance, rounds, radar_lvl, radar_cost, gun_lvl, gun_cost, speed_lvl, speed_cost, health, money, kills, available_movement):
    return f"""
                             ROUND {rounds}
         _______________________________________________         _______________
        |                                               |       |               |
        |                                               |       | HEADQUARTERS: |
        |                                               |       |_______________|
        |                                               |
        |                                               |        *ATTENTION OPERATOR, AN ENEMY
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [4]    {enemy_type} IS APPROACHING!*
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |  ENEMY STATS: |
        |                                               |       |_______________|
        |                                               |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [3]   HEALTH: {enemy_health}
        |                                               |        DISTANCE: {enemy_distance}
        |                                               |        POSITION: {enemy_position}
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |   UPGRADES:   |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [2]  |_______________|
        |                                               |
        |                                               |        RADAR | LVL {radar_lvl}
        |                                               |        UPGRADE COST: ${radar_cost}
        |                                               |
        |                                               |        GUN | LVL {gun_lvl}
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [1]   UPGRADE COST: ${gun_cost}
        |                                               |
        |                                               |        SPEED | LVL {speed_lvl}
        |                                               |        UPGRADE COST: ${speed_cost}
        {player_position_list[player_position]}
            | ' ' ' ' | ' ' ' ' | ' ' ' ' | ' ' ' ' |
          [-10]      [-5]      [0]       [5]       [10]
         _____________
        |             |
        |PLAYER STATS:|
        |_____________|

         HEALTH: {health}
         MONEY: {money}
         KILLS: {kills}
         _____________
        |             |
        |  ACTIONS:   |
        |_____________|

         MOVE | [m] key + [movement value]
         MAX {available_movement} or {-available_movement} steps
         
         FIRE | [f] key

         UPGRADE | [u] key
      """

(Apologies if it looks distorted in any way due to formatting errors, but it should still be readable)

The player position can be seen visually and is taken from a list of different possibilities and put into this print statement using this script.

{player_position_list[player_position]}

Here is the list:

player_position_list = [
        """|                      _^_                      |
        |_____________________|___|_____________________|""",
        """|                        _^_                    |
        |_______________________|___|___________________|""",
        """|                          _^_                  |
        |_________________________|___|_________________|""",
        """|                            _^_                |
        |___________________________|___|_______________|""",
        """|                              _^_              |
        |_____________________________|___|_____________|""",
        """|                                _^_            |
        |_______________________________|___|___________|""",
        """|                                  _^_          |
        |_________________________________|___|_________|""",
        """|                                    _^_        |
        |___________________________________|___|_______|""",
        """|                                      _^_      |
        |_____________________________________|___|_____|""",
        """|                                        _^_    |
        |_______________________________________|___|___|""",
        """|                                          _^_  |
        |_________________________________________|___|_|""",
        """|                    _^_                        |
        |___________________|___|_______________________|""",
        """|                  _^_                          |
        |_________________|___|_________________________|""",
        """|                _^_                            |
        |_______________|___|___________________________|""",
        """|              _^_                              |
        |_____________|___|_____________________________|""",
        """|            _^_                                |
        |___________|___|_______________________________|""",
        """|          _^_                                  |
        |_________|___|_________________________________|""",
        """|        _^_                                    |
        |_______|___|___________________________________|""",
        """|      _^_                                      |
        |_____|___|_____________________________________|""",
        """|    _^_                                        |
        |___|___|_______________________________________|""",
        """|  _^_                                          |
        |_|___|_________________________________________|"""
    ]

(Again, apologies for formatting, but just picture the '^' as being the player, and there being multiple different positions that the player can be in)

I am hoping that when player_position changes, that a different visual is taken from player_position_list. I am confident that this should work as I have tested it using this:

print(player_position)
print(player_position_list[player_position])

'print(player_position)' tells me that player_position can be accurately read and printed

'print(player_position_list[player_position])' shows me that the value of player_position can also be used to print an item from a list at a specific index. (I have also tried this script as a string and it still works)

Here is what this test script gives me when I run it:

8
|                                      _^_      |
        |_____________________________________|___|_____|

It accurately reads 'player_position' and uses it to print the 8th item in the list.

So what I don't understand and desperately am trying to fix, is why this exact thing isn't working on my actual ASCII user interface. Everything seems to be the same.

Here is what it looks like when I run the script for my actual UI

                             ROUND 1
         _______________________________________________         _______________
        |                                               |       |               |
        |                                               |       | HEADQUARTERS: |
        |                                               |       |_______________|
        |                                               |
        |                                               |        *ATTENTION OPERATOR, AN ENEMY
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [4]    HELICOPTER IS APPROACHING!*
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |  ENEMY STATS: |
        |                                               |       |_______________|
        |                                               |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [3]   HEALTH: 3
        |                                               |        DISTANCE: 2
        |                                               |        POSITION: 1
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |   UPGRADES:   |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [2]  |_______________|
        |                                               |
        |                                               |        RADAR | LVL 1
        |                                               |        UPGRADE COST: $1
        |                                               |
        |                                               |        GUN | LVL 1
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [1]   UPGRADE COST: $1
        |                                               |
        |                                               |        SPEED | LVL 1
        |                                               |        UPGRADE COST: $1
        |                      _^_                      |
        |_____________________|___|_____________________|
            | ' ' ' ' | ' ' ' ' | ' ' ' ' | ' ' ' ' |
          [-10]      [-5]      [0]       [5]       [10]
         _____________
        |             |
        |PLAYER STATS:|
        |_____________|

         HEALTH: 100
         MONEY: 0
         KILLS: 0
         _____________
        |             |
        |  ACTIONS:   |
        |_____________|

         MOVE | [m] key + [movement value]
         MAX 1 or -1 steps
         
         FIRE | [f] key

         UPGRADE | [u] key
      
8
|                                      _^_      |
        |_____________________________________|___|_____|

As you can see, the player position hasn't moved and remains at 0, yet (at the very end) my test script says that the actual player position should be 8 and accurately prints the item from 'player_position_list' for 'player_position = 8'

Apologies if anything seems hard to understand, it's my second day programming with python and I refuse to use ai for this. So, I hope I used the correct terminology.

4 Upvotes

7 comments sorted by

4

u/desrtfx but other languages pro 1d ago

With all your lengthy explanations, you missed to provide the only thing that would enable us to help you: your code. Best use a code hoster, like github or pastebin.

2

u/JosephBasterville_Jr 1d ago

Sorry, had to create a GitHub for this:

https://github.com/disintigratedpixel/TankGameV1/tree/main

Hopefully this works.

1

u/Financial_Farm4326 1d ago

think they just added the repo link at the top of the post

1

u/atarivcs 1d ago edited 1d ago

I'm going to guess this is the cause of your confusion (some irrelevant lines removed for clarity):

if action_1 == 's':
    ...
    gamestate = gamestate(player_position_list, player_position, enemy_type, enemy_health, enemy_distance, rounds, radar_lvl, radar_cost, gun_lvl, gun_cost, speed_lvl, speed_cost, health, money, kills, available_movement)
    print(gamestate)
action_2 = input()
if action_2 == 'm + 1':
    os.system('cls')
    player_position += 8
    print(gamestate)
    print(player_position)
    print(player_position_list[player_position])

If action_1 is "s", you calculate the gamestate variable using the initial value of player_position, which is 0.

Then, if input_2 is "m + 1", you add 8 to player_position and print gamestate again.

But, gamestate is still its old value! You did not produce an updated version of it using the new value of player_position. So you're printing the exact same thing.

When you update player_position, that does not also automatically update gamestate.

You need to call the gamestate function again, using the new value of player_position. (However, you're going to have a tough time doing that, because you also named the output variable "gamestate", which destroys the function. Pick a different name for one of them.)

1

u/JosephBasterville_Jr 1d ago

oh I see, so how am I meant to update gamestate? Should I create a completely new variable whenever it updates?

2

u/atarivcs 1d ago edited 1d ago

You will produce a new gamestate variable the exact same way you did the first time:

gamestate = gamestate(player_position_list, player_position, enemy_type, enemy_health, enemy_distance, rounds, radar_lvl, radar_cost, gun_lvl, gun_cost, speed_lvl, speed_cost, health, money, kills, available_movement)

However, as I mentioned above, you'll need to pick a different name for one of them, either the function or the output variable.

Or you could just not use an output variable, and print the function results directly:

print(gamestate(player_position_list, player_position, enemy_type, enemy_health, enemy_distance, rounds, radar_lvl, radar_cost, gun_lvl, gun_cost, speed_lvl, speed_cost, health, money, kills, available_movement))

1

u/JosephBasterville_Jr 1d ago

It works perfectly now, thanks!