Trying to make a basic Python score counter in a game... will not count.

M

Mitya Sirenef

game, and the beginning is
complete. After I finished it, I thought, "You know what? I think I
can make this even better, and add a score counter." And so I did.score. In fact it always keeps the score at 0 for both players. It's
fully functional otherwise, but it's bothering me that I can't get it to
work.
I was actually thinking of making a simple rock paper scissors game so I
went ahead and cobbled it together, using a design with a class and
generally a structured approach.. It keeps the scores, too, and you can
set both players to be AI, or one to be AI, or both to be humans:

https://github.com/pythonbyexample/PBE/blob/master/code/rockpaper.py

(it needs python3 but can be easily changed to work with python2.x)


- mitya


If anyone's interested, I've simplified the code further by making use
of my TextInput utility class; I've also made the game into a tutorial,
it can be found here:

http://lightbird.net/larks/rockpaper.html


The updated full code is really tiny (it requires utils.py which is
linked at the top of tutorial):


import sys
from random import choice as randchoice
from time import sleep

from utils import TextInput

players = 'XY'
ai_players = 'Y'
moves = "rps"
wins = ("rp", "sr", "ps") # choice on the right side wins
status = "%5s %3d %5s %3d moves: %s %s"
pause_time = 0.3


class RockPaperScissors(object):
def run(self):
self.textinput = TextInput("(r|p|s)")
scores = [0, 0]

while True:
choice1, choice2 = (self.get_move(p) for p in players)

if choice1 != choice2:
winplayer = 1 if (choice1+choice2 in wins) else 0
scores[winplayer] += 1

print(status % (players[0], scores[0], players[1],
scores[1], choice1, choice2))
sleep(pause_time)

def get_move(self, player):
if player in ai_players : return randchoice(moves)
else : return self.textinput.getval()


if __name__ == "__main__":
try : RockPaperScissors().run()
except KeyboardInterrupt : pass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,073
Messages
2,570,537
Members
47,195
Latest member
RedaMahuri

Latest Threads

Top