Portfolio project: Supermarket - the RPG

Hi,

Sorry - I’m green as grass, and have fallen at the first hurdle. I’m on the portfolio project stage of the Computer Science pathway, and have decided to make a text-based RPG set in a supermarket.

Essentially, the functionality I’m trying to create is to give the player a choice of startup ‘pathways’ (trying not to use the word ‘classes’ in a gamer sense!) that gives them different beginning characteristics.

Based on their input choice, this index value is used to read from a nested list of attributes, and then a createPlayer method is called with these charactersitics.

This is what I have so far:

# Supermarket: the RPG


# start-up message
start_up_message = "###########################################\n\nWelcome to Supermarket: the RPG \n \n Choose a player class\n1: Mum with kids\n2: Sharp-elbowed Granny"

player_choice_of_class = 0

# Player class, influenced by which type of shopper they initially choose

startup_stats = [["Mum with kids", 0.8, 120, 120, 70, 70, 100], ["Sharp-elbowed Granny", 1.1, 70, 70, 100, 100, 80]]

class Player:
  def __init__(self, player_class, time_factor, stamina, max_stamina, patience, max_patience, money):
    self.player_class = player_class
    self.time_factor = time_factor
    self.stamina = stamina
    self.max_stamina = max_stamina
    self.patience = patience
    self.max_patience = max_patience
    self.money = money

  def __repr__(self, player_class, time_factor, stamina, max_stamina, patience, max_patience, money):
    pass

def create_player(player_choice_of_class):
  index_choice = int(player_choice_of_class) - 1
  init_player_class = startup_stats[index_choice][0]
  init_time_factor = startup_stats[index_choice][1]
  init_stamina = startup_stats[index_choice][2]
  init_max_stamina = startup_stats[index_choice][3]
  init_patience = startup_stats[index_choice][4]
  init_max_patience = startup_stats[index_choice][5]
  init_money = startup_stats[index_choice][6]
  # Create player as "new_player" with start-up data as drawn from startup stats list
  new_player = Player(init_player_class, init_time_factor, init_stamina, init_max_stamina, init_patience, init_max_patience, init_money)
  return new_player

  

  # block of code to call that shows character's current stats as a running item
def display_stats():
    stats = "Time Factor: " + str(self.time_factor) + "\n" + "Stamina: " + str(self.stamina) + "/" + str(self.max_stamina) + "\n" + "Patience: " + str(self.patience) + "/" + str(self.max_patience) + "\n" + "Money: £" + str(self.money)
    return stats
   
# ################################# GAME LOOP ########################
# Ask player which player_class they want to play with
player_choice_of_class = input(start_up_message)
#print(player_choice_of_class)
create_player(player_choice_of_class)
#test_player = Player("Mum with kids", 0.8, 120, 120, 70, 70, 100)

print(display_stats())

This throws an error - fundamentally, I’m struggling to refer to the ‘new_player’ object I’ve just created in order to display their stats (or do anything else with them going forwards.

Any help gratefully received!

what error does it throw? need this in order to work out the issue

$ python 3 script.py
python: can't open file '3': [Errno 2] No such file or directory
$ python3 script.py
###########################################

Welcome to Supermarket: the RPG 
 
 Choose a player class
1: Mum with kids
2: Sharp-elbowed Granny2
Traceback (most recent call last):
  File "script.py", line 54, in <module>
    print(display_stats())
  File "script.py", line 44, in display_stats
    stats = "Time Factor: " + str(self.time_factor) + "\n" + "Stamina: " + str(self.stamina) + "/" + str(self.max_stamina) + "\n" + "Patience: " + str(self.patience) + "/" + str(self.max_patience) + "\n" + "Money: £" + str(self.money)
NameError: name 'self' is not defined
$ ^C
$ 

I can’t get it to work whether I refer to it as ‘self’ or as ‘new_player’

  # block of code to call that shows character's current stats as a running item
def display_stats(player):
    stats = "Time Factor: " + str(player.time_factor) + "\n" + "Stamina: " + str(player.stamina) + "/" + str(player.max_stamina) + "\n" + "Patience: " + str(player.patience) + "/" + str(player.max_patience) + "\n" + "Money: £" + str(player.money)
    return stats



# ################################# GAME LOOP ########################
# Ask player which player_class they want to play with
player_choice_of_class = input(start_up_message)
#print(player_choice_of_class)
player = create_player(player_choice_of_class)
#test_player = Player("Mum with kids", 0.8, 120, 120, 70, 70, 100)

print(display_stats(player))

you need to make sure you are referencing your created classes properly. you created a new player class but didnt assign it to a variable, so you couldnt actually access that class. i have assigned it to a ‘player’ variable. so now you can reference it in the altered “display_stats” funciton.

hope this helps :slight_smile:

1 Like

Perfect. Makes complete sense.

Thanks so much!