UselessProgrammer
UselessProgrammer

Reputation: 11

How do I add points to a total in treasure hunt game?

So Im making a treasure hunt game in which the user is placed on a grid as 'P' and can move around to collect coins from chests ('X') which are shown on the grid. On the same gird, bandits('B') are also present which take away all previously collected coins.

Now, Ive gotten as far as allowing the player to move around the board but dont know how to add coins to the already created coins variable when the player lands on the treasure.

This is the relevant part of the code which randomly placed 5 Bandits and 10 treasure chests on the board:

def bandits(board):
    added_bandits = 0
    while added_bandits < 5:
        x_bandit = r.randint(0,7)
        y_bandit = r.randint(0,7)
        if board[x_bandit][y_bandit] == 'O':
            board[x_bandit][y_bandit] = 'B'
            added_bandits = added_bandits + 1

def treasures(board):
    added_treasure = 0
    while added_treasure < 10:
        x_treasure = r.randint(0,7)
        y_treasure = r.randint(0,7)
        if board[x_treasure][y_treasure] == 'O':
            board[x_treasure][y_treasure] = 'X'
            added_treasure = added_treasure + 1

Upvotes: 1

Views: 222

Answers (1)

Alex
Alex

Reputation: 779

I would create a class Player, where you store this information and that manage the adding/removing of the coins of the player.

class Player(object):

    def __init__(self, name):
        self.playername = name
        self.total_coins = 0

    def add_coins(self, number):
        self.total_coins += number

    def remove_coins(self, number):
        self.total_coins -= number

    def move(self, move_data):
    #implement here the players's move semantic

    def print_total_coins(self):
        print("Total coins %d" % (self.total_coins))

That way you can get the total coins score like this:

 player1 = Player("Player 1")
 player1.print_total_coins()

I would further encapsulate the bandits and the treasures in classes too.

Upvotes: 1

Related Questions