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

Mitya Sirenef msirenef at lightbird.net
Mon Dec 17 00:11:50 EST 2012


On 12/16/2012 01:07 PM, Mitya Sirenef wrote:
> On 12/16/2012 12:00 PM, Darrien Glasser wrote:
 >> Hey guys, I'm working on a Python rock paper scissors (lizard spock) 
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.
 > >
 > > The problem is that it doesn't seem to actually keep track of 
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.
 > >
 > > Currently using Windows Python 32 bit v2.6.8
 > >
 > > Download of the code here:
 > >
 > > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb
 > >
 > > Copy of the code here:
 > >
 > > http://pastebin.com/MNdgiuSY
 > >
 > > Thanks in advance for your help...
 >
 >
 > 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
-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /