Getting and storing player information

Our first task is to figure out how we are going to get and store information from those who play our game. There are a few steps we need to take, including asking the player for their name, and then storing the player's name. We will also perform some code in the background to store information about the player that we have not yet asked for. This is a sneaky bit of coding that is quite fun and will let you expand your game if you want to. Let's walk through each step.

Making a players list

The first thing that we will do is make an empty list to store information about each player. We are going to name the list players, but we are not going to put anything in our list yet. Why not? Well, our players might be different in each game, and they will have different information too, so we need to allow our game to store this information as our players enter it into the computer. Here is what the players list looks like:

players = []

Now that we have made this list, we can add players to this list. Recall that we will also make a profile to store information about the players. In fact, the profile will be stored in some tiny dictionaries that we make inside the lists!

Making a players list

New skill! Putting one item inside another item. This is called nesting. Next, we will learn how to nest a dictionary inside of a list.

Player profiles

In this next step, we are going to make a dictionary for each player. The dictionary that we make will have placeholders for the player name, the player's backpack items, and the player's score:

Player profiles

Imagine that all of the information in the dictionary is a player profile. The player profile will be filled in by information that we get from the player's interactions with our game. The code in the following screenshot is what the completed code for our players will look like:

Player profiles

Before you write any code, let's read and break down the code. The first two lines are comments to remind us of what we are doing, and line 5 is where we make an empty list. The code in line 7, which is the first line of code that the computer cares about, allows us to do the following:

  • Set the number of players with the range() function: Since counting in Python starts at zero, and the range() function does not include the last number, we are creating profiles for player 0 and player 1 (refer to Chapter 6, Working with Data – Lists and Dictionaries, where we spent time printing and counting lists, to refresh your memory about how lists items are counted).
  • The for loop to make profile for each player: For player 0 and player 1, we will make a player profile with information.
  • The player.append() function: This adds an information type to each player profile. In this case, name is string, score is int, and backpack is an empty list.

The backpack dictionary key is special because it is a list that will store all of the backpack items inside of the profile. It allows the user to have many items stored in the same place:

Player profiles

Player profiles – how do they work?

Now, let's think about all the information in a player profile. We have a list called players. Inside the players list, we have a dictionary for each player. The dictionary is where the player profile information is stored. Inside the dictionary for each player, we have made room for an item list. The item list is called backpack, and its job is to remember all the list items in the player profile. Try to imagine the profile like a tree that has more leaves as it breaks away from the trunk and branch:

Player profiles – how do they work?

The name for what we have done is called nesting. Nesting is when we put one thing inside something else. Here, we have nested one datatype (a dictionary) inside another datatype (players list).

Tip

Save your code if you have not done so already!

Add players to profile

So, we have set up a data structure, called the player profile, as a way to store the information about each player. Now, we need to write the code that will prompt the players to enter their information into our program. We will use the raw_input() function to get information from the players and store this information in the user profile. Our request for user information will continue inside the for loop.

First, read through the code from lines 15-20 in this screenshot:

Add players to profile

In line 15 of this code, you will notice our raw_input() command, which asks the player to enter their name. Did you notice that the name dictionary key is used? Did you notice that before the name key, players[i] is used? This means that the answer to the Enter your name prompt will be stored in the dictionary under the name key. A player profile will be created, and it will be waiting for information about the backpack items and the game score.

The player number is being set by i. The lowercase i represents one player. So, line 15 asks us for the name of player i. How does it know what number to choose? Where is i getting that information? If you go back up to the for loop, you will notice for i in range(2). This means for the first player of two players, do all the things in the loop. When line 15 runs the for loop the first time, it asks for input from player 1; when the for loop runs the second time, it asks for input from player 2. A for loop with a range(2) only runs twice, so after getting and storing input from player 2, the for loop stops looping.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.254.231