Question

I have made a hangman game in Python, and stupidly didn't save as before I tried using curses. :| Now, the body of the man is displayed in different spots. The head and arms are before the pole that holds the top up, and the body and the legs are where they should be. Here's my code. It's probably the ugliest code I have written in Python...

import random
import re
import curses

myscreen = curses.initscr()
myscreen.clear()
myscreen.border(0)
myscreen.refresh()

f = open("/usr/share/dict/words", "r")
c = f.read().split('\n')
d = {1:'o', 2:'\\o/', 3:'\n      |     \\o/\n      |      |', 4:'\n      |      \\o/\n      |      |\n      |     / \\'}
w = random.choice(c).lower()
i = 1
j = 6
n = []
t = []
b = ""
l=4
h=2

myscreen.addstr(2,6, "--------")
myscreen.addstr(3,6, "|")
myscreen.addstr(3,13, "|"+b)
for item in range(j+1):
  myscreen.addstr(l, 2, "    |\n")
  l=l+1

myscreen.addstr(9,2,"__________\n")

for char in w:
  n.append('_')

for item in n:
  myscreen.addstr(10, h, item,)
  h=h+2

myscreen.addstr(11, 2, '\n')
myscreen.refresh()

while n != w:
  if not re.findall('_', ''.join(n)):
    myscreen.clear()
    myscreen.addstr(1,1, "You win!!\n\n")
    myscreen.refresh()
    break
  myscreen.addstr(12,2,"Guess: ")
  g = myscreen.getstr(12,9,1)
  if 1:
    if g in ''.join(t):
      print "You already guessed that."
      continue
    if g in w:
     k = [match.start() for match in re.finditer(re.escape(g), w)]
     for item in k:
       n[item]=g
     t.append(g)
    else:
      if i!=5:
        b = d[i]
        i=i+1
        j=j-1
        t.append(g)
      else:
        myscreen.clear()
        myscreen.addstr(1, 1, "You lose!")
        myscreen.addstr(2, 1, "The word was %s.\n\n" % w)
        myscreen.refresh()
        break
    myscreen.clear()
    myscreen.addstr(2,6, "--------")
    myscreen.addstr(3,6, "|")
    myscreen.addstr(3,13, "|\n"+b)
    l=4
    for item in range(j):
      myscreen.addstr(l, 6, "|\n")
      l=l+1

    myscreen.addstr(9,2,"__________\n")
    l=2
    for item in t:
      myscreen.addstr(10, l, item)
      l=l+2
    l=2
    for item in n:
      myscreen.addstr(11, l, item)
      l=l+2
    myscreen.refresh()
  else:
    myscreen.addstr(12, 2, "Please enter a letter.")
    myscreen.refresh()

What is wrong with it?

Était-ce utile?

La solution

D'oh! I was doing the for loop after I had placed the pole that holds it up there already, so it "overwrote" the previous one.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top