pyguessgame

In [65]:
import random
import urwid
import os
from pyfiglet import Figlet
import json
import random
import clint

Started as a simple python guessing game to be ran in terminal. Asks for a number between 0-10 and works up to 100 - asking for the next 10 each time. On win a urwid screen shows with 'You Win'. It would be nice if the whole game was inside of urwid. Figlet intergration.

In [39]:
f = Figlet()
In [40]:
opusr = os.listdir('/home/wcmckee/signinlca/usernames/')
In [40]:
 
In [41]:
opusr
Out[41]:
['tnow',
 'signinlca.py',
 'charle',
 'wcm',
 'wmck',
 'wmen',
 'webmck',
 'pjohns',
 'red',
 'wez',
 'checkthis',
 'blah',
 'jchick',
 'poiu',
 'poi',
 'qwe',
 'point',
 'cvb',
 'blag',
 'gerty',
 'jblog',
 'ssung',
 'clittle',
 'yellow']
In [42]:
ranumz = len(opusr)
In [43]:
ranin = random.randint(0, ranumz)
In [44]:
ranin
Out[44]:
15
In [45]:
opza =  opusr[ranin]
In [46]:
opza
Out[46]:
'qwe'
In [47]:
lidte = open('/home/wcmckee/signinlca/usernames/' + opza + '/.signups.json', 'r')
In [48]:
plid = lidte.read()
In [51]:
tlid = json.loads(plid)
In [64]:
firna = tlid['firstname']
In [ ]:
 
In [60]:
tlicha = tlid['lastname']
In [66]:
fullna = firna + ' ' + tlicha
In [67]:
from clint.textui import colored, puts

perf = puts(colored.yellow(fullna))

perf
qwe qwe
In [63]:
print f.renderText(opza)
print fullna
                    
  __ ___      _____ 
 / _` \ \ /\ / / _ \
| (_| |\ V  V /  __/
 \__, | \_/\_/ \___|
    |_|             

qwe qwe
In [ ]:
 
In [38]:
def exitq(key):
    if key in ('enter', 'return'):
        raise urwid.ExitMainLoop()
In [39]:
pallette = [
    ('banner', 'dark red', 'white'),
    ('streak', 'dark red', 'white'),
    ('bg', 'dark red', 'white'),]
In [40]:
numchez = 0
In [48]:
for guesz in range(10):
    farchez = (numchez)
    numchez = (numchez + 10)
    
    def GetNum():
        return random.randint(farchez,numchez)
    
    randnum = GetNum()
    
    lownumz = (numchez)
    
    innumz = ('Enter a number between ' + str(farchez) + ' and ' + str(lownumz) + ': ')
    
    guessnum = raw_input(innumz)
    
    guesintz = int(guessnum)
    
    print ('Guess was: ' + str(guesintz))
    #colomod
    print ('Correct was: ' + str(randnum))
    
    if guesintz == randnum:
        txt = urwid.Text(f.renderText( guessnum + ' ' + str(randnum) + ' You Win!'))
        numpor = urwid.Text(f.renderText(guessnum))
        map1 = urwid.AttrMap(txt, 'streak')
        mep = urwid.AttrMap(numpor, 'streak')
        fil = urwid.Filler(map1)
        fel = urwid.Filler(mep)
        map2 = urwid.AttrMap(fil, 'bg')
        loopa = urwid.AttrMap(fel, 'bg')
        looena = urwid.MainLoop(loopa, pallette, unhandled_input=exitq)
        looena.run()
        loop = urwid.MainLoop(fil, pallette, unhandled_input=exitq)
        loop.run()
        print f.renderText(guessnum + ' ' + str(randnum) + ' You Win!')
    else:
        txt = urwid.Text(f.renderText(guessnum + ' ' + str(randnum) + ' You Lose!'))
        map1 = urwid.AttrMap(txt, 'streak')
        fil = urwid.Filler(map1)
        map2 = urwid.AttrMap(fil, 'bg')
        loop = urwid.MainLoop(fil, pallette, unhandled_input=exitq)
        loop.run()
        print f.renderText(guessnum + ' ' + str(randnum) + ' You lose!')
        
Enter a number between 70 and 80: 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-48-ff8888390e77> in <module>()
     14     guessnum = raw_input(innumz)
     15 
---> 16     guesintz = int(guessnum)
     17 
     18     print ('Guess was: ' + str(guesintz))

ValueError: invalid literal for int() with base 10: ''
In [35]:
txt = urwid.Text(f.renderText(guessnum + ' ' + str(randnum)))
#numpor = urwid.Text(f.renderText(guessnum))
map1 = urwid.AttrMap(txt, 'streak')
#mep = urwid.AttrMap(numpor, 'streak')
fil = urwid.Filler(map1)
#fel = urwid.Filler(mep)
map2 = urwid.AttrMap(fil, 'bg')
#loopa = urwid.AttrMap(fel, 'bg')
#looena = urwid.MainLoop(loopa, pallette, unhandled_input=exitq)
#looena.run()
loop = urwid.MainLoop(fil, pallette, unhandled_input=exitq)
loop.run()
7[?47h[?1002l[?1000l[?1000h[?1002h
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-0a8c9e47ba8d> in <module>()
     10 #looena.run()
     11 loop = urwid.MainLoop(fil, pallette, unhandled_input=exitq)
---> 12 loop.run()
     13 print f.renderText('You Win!')

/usr/local/lib/python2.7/dist-packages/urwid/main_loop.pyc in run(self)
    276         """
    277         try:
--> 278             self._run()
    279         except ExitMainLoop:
    280             pass

/usr/local/lib/python2.7/dist-packages/urwid/main_loop.pyc in _run(self)
    373                 self.screen.stop()
    374 
--> 375         self.event_loop.run()
    376         self.stop()
    377 

/usr/local/lib/python2.7/dist-packages/urwid/main_loop.pyc in run(self)
    676             while True:
    677                 try:
--> 678                     self._loop()
    679                 except select.error as e:
    680                     if e.args[0] != 4:

/usr/local/lib/python2.7/dist-packages/urwid/main_loop.pyc in _loop(self)
    713 
    714         for fd in ready:
--> 715             self._watch_files[fd]()
    716             self._did_something = True
    717 

/usr/local/lib/python2.7/dist-packages/urwid/raw_display.pyc in <lambda>()
    390         else:
    391             wrapper = lambda: self.parse_input(
--> 392                 event_loop, callback, self.get_available_raw_input())
    393         fds = self.get_input_descriptors()
    394         handles = []

/usr/local/lib/python2.7/dist-packages/urwid/raw_display.pyc in get_available_raw_input(self)
    424         implementation; you can safely ignore it if you implement your own.
    425         """
--> 426         codes = self._get_gpm_codes() + self._get_keyboard_codes()
    427 
    428         if self._partial_codes:

/usr/local/lib/python2.7/dist-packages/urwid/raw_display.pyc in _get_keyboard_codes(self)
    498         codes = []
    499         while True:
--> 500             code = self._getch_nodelay()
    501             if code < 0:
    502                 break

/usr/local/lib/python2.7/dist-packages/urwid/raw_display.pyc in _getch_nodelay(self)
    632 
    633     def _getch_nodelay(self):
--> 634         return self._getch(0)
    635 
    636 

/usr/local/lib/python2.7/dist-packages/urwid/raw_display.pyc in _getch(self, timeout)
    542                 self.gpm_event_pending = True
    543         if self._term_input_file.fileno() in ready:
--> 544             return ord(os.read(self._term_input_file.fileno(), 1))
    545         return -1
    546 

TypeError: ord() expected a character, but string of length 0 found
In [ ]:
print 'The End'

Comments

Comments powered by Disqus