Approaching player scores?

Question:

Looking for help in approaching player scores in my game. How do I assign the scores at the end of each game to the player playing? What would be the best approach to a game like this?

class Player:
    def __init__(self, username, playerscore):
        self.username = username
        self.playerscore = playerscore
       
def question(Player):
    score = 0 
    question = input("what direction is the arrow pointing, left or right? ->")
    if question == "right":
        score += 1
    else:
        score += 0

    question2 = input("What direction is arrow pointing, up or down? ^")
    if question2 == "up":
        score += 1 
    else: 
        score += 0 
    

intro = input("Are you ready to play?")
if intro.lower() in ("yes", "go", "y"):
    user = input("What is your username? ")
    p1 = Player(user, 0)
else:
    exit()

cont = input("Player 2, are you ready to play? ")
if cont.lower() in ("yes", "go", "y"):
    user2 = input("What is your username? ")
    p2 = Player(user2, 0)
else: 
    print(p1, "wins by default.")

if p1.playerscore>p2.playerscore:
    print (p1.username, "is the winner!")
elif p1.playerscore == p2.playerscore:
    print ("No one wins. It's a tie!")
else:
    print (p2.username, "is the winner!")
Asked By: bamb1

||

Answers:

If I am understanding you correctly, you set up two player objects from the "Player" class and initialize their scores to zero. If you want to increment a particular player’s score you should just be able to add a value to the "p1.playerscore" or "p2.playerscore" value. Following is a tweaked version of your code with a simple increment of the player’s score.

class Player:
    def __init__(self, username, playerscore):
        self.username = username
        self.playerscore = playerscore
       
def question(Player):
    #score = 0 
    question = input(Player.username + " what direction is the arrow pointing, left or right? ->")
    if question == "right":
        Player.playerscore += 1
    else:
        Player.playerscore += 0

    question2 = input(Player.username + " what direction is arrow pointing, up or down? ^")
    if question2 == "up":
        Player.playerscore += 1 
    else: 
        Player.playerscore += 0 
    

intro = input("Are you ready to play?")

if intro.lower() in ("yes", "go", "y"):
    user = input("What is your username? ")
    p1 = Player(user, 0)
else:
    exit()

cont = input("Player 2, are you ready to play? ")

if cont.lower() in ("yes", "go", "y"):
    user2 = input("What is your username? ")
    p2 = Player(user2, 0)
else: 
    print(p1.username, "wins by default.")
    quit()                  # Exit if two players are not found
    
question(p1)

question(p2)

if p1.playerscore>p2.playerscore:
    print (p1.username, "is the winner with a score of:", p1.playerscore)
elif p1.playerscore == p2.playerscore:
    print ("No one wins. It's a tie!")
else:
    print (p2.username, "is the winner with a score of:". p2.playerscore)

One could get fancy with creating "setter" functions as is done in other object oriented programs, but this seems to be the most straightforward way to accomplish this with little fuss.

This is a sample of the output with this tweak.

@Una:~/Python_Programs/PlayerScore$ python3 PlayerScore.py 
Are you ready to play?yes
What is your username? Lily
Player 2, are you ready to play? yes
What is your username? Craig
Lily what direction is the arrow pointing, left or right? ->right
Lily what direction is arrow pointing, up or down? ^up
Craig what direction is the arrow pointing, left or right? ->left
Craig what direction is arrow pointing, up or down? ^down
Lily is the winner with a score of: 2

Give that a try.

Answered By: NoDakker
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.