#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    10

    My first little big game


    its the hungman game
    in spanish, if anybody is interested i will translate it
    it was a lot of hard thinking
    the worst to do was the "A _ D _ _ _ _" string with the letters the user already guessed

    i dont know why i make this post its just for sharing some code and because i was like 4 hours doing it.

    Code:
    # AhorcadoX 1.0 (build 43)
    # por IvanHope
    # November - 24 - 2004
    
    # Esta clase la creo para emular la funcion cls de MsDOS
    def cls():
     	for i in range(1, 20):
     		print
    cls()
    print"""
      		      AhorcadoX 1.0 (build 43)
      		         Por IvanHope   
                                                                
                                                               
                                                               
                                             
                                                  
                                                       
                                              
                                                     
                                                   
                                                   
       
            #####################################################
            # Dispones de 7 intentos para descubrir la palabra. #
            # Instrucciones:                                    #
            #   * Escribe la letra o grupo de letras que deseas #
            #     saber si forma parte de la palabra.           #
            #   * Si crees saber la palabra escribe la misma.   #
            #####################################################  
            
            
    """
    raw_input("                     ENTER para comenzar...")
     		
    import random
    # Words son las opciones de las que el programa va a elejir
    words = ("loko", "internacional", "representativo", "estereotipo", "megalomaniaco", "altruista")
    guion = "_ "
    seguir = "si"
    output = ""
    
    # mientras el usuario no diga explicitamente que no quiere seguir el juego sigue
    while seguir != "no":
       # elije una palabra del grupo WORDS
       word = random.choice(words)
       word = word.lower()
       # Saca el largo de la palabra elejida
       largo = int(len(word))
       # Resetea todas las variables al default
       buenas = ""
       malas = ""
       intentos = 0
       correcto = "no"
       # crea el tuple letras
       letras = []
       #crea un temp para cuando sea necesario
       temp = ""
       cls ()
       # esto es el loop hasta que el usuario gana o pierde
       while correcto != "si" and intentos < 8:
          
          # crea el string que muestra las letras adivinadas en un grafico y pone "_" para las no adivinadas
          for l in word:
             if l in letras:
                output = output + " " + l
                temp += l
             else:
                output += " _"
          
          
          cls ()
          print "\t\t", output
          # resetea output
          output = ""
          print "\n********************************************************"
          print "\nLa palabra tiene: ", largo, "letras..."
          print "Letras correctas: ", buenas, ", Letras Erradas: ", malas
          print "Llevas ", intentos, " de 7 intentos."
          print "********************************************************\n\n\n"
          # pide al usuario el input de una letra
          letra = raw_input("\nEscribe una letra o arriesga la palabra: ")
          letra = letra.lower()
          # si el input es igual a word se corta el loop
          if letra == word or temp == word:
             correcto = "si"
          # le informa al usuario si la letra se encuentra en la palabra o no
          # agrega la letra al string "buenas" o "malas" dependiendo si es correcta o no
          if letra in word and letra != "":
             print "La letra ", letra, "se encuentra en la palabra."
             letras += letra
             buenas = buenas + " - " + letra
          else:
             print "La letra ", letra, "no se encuentra en la palabra."
             malas = malas + " - " + letra
          # Suma 1 a la cantidad de intentos
          intentos += 1
       # Si se utilizaron los 7 intentos
       if intentos > 7:
          cls ()
          print """
          *-----------------------------------------*
          ] Lo lamento, se te acabaron los intentos [
          *-----------------------------------------*
          """
       # si es correcto antes de cortar el loop imprime el mensaje ganador
       if correcto == "si":
          cls ()
          print """
             &&&      &    &    &    &     &&&&  &&&&& &&&&&  &&&& 
            &   &    & &   &&   &   & &   &    &   &   &     &    &
           &     &   & &   & &  &   & &   &        &   &     &     
           &         & &   & &  &   & &    &&      &   &&&&&  &&   
           &   &&&  &   &  &  & &  &   &     &&    &   &        && 
           &     &  &&&&&  &  & &  &&&&&       &   &   &          &
            &   &  &     & &   && &     & &    &   &   &     &    &
             &&&   &     & &    & &     &  &&&&    &   &&&&&  &&&& 
             
             
             
             
             
             
             
             """
       # Cualquier cosa que no sea "no" reinicia el juego
       seguir = raw_input("Jugar otra vez?")
       seguir = seguir.lower()
    cls ()
    # mensaje del final del juego
    print """
                                     
                                           
                                          
                                    
                                        
                                      
                                          
                                       
       
       
       
       
       
       
       """
    # espera enter antes de terminar
    raw_input("Presione ENTER para salir...")
    Last edited by Scorpions4ever; August 3rd, 2007 at 04:35 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Ivan cld u translate it in English for me I'm curious to know abt the hangman game. It all seems Greek oops Spanish to me

    Pls do it....wld help many to take the plunge into building new games!

    Thanks & Rgds,
    Subha
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    10
    ok i did my best sorry for the english

    these are the current bugs:
    * if you pick all the letters and you actually complete the word letter by letter but you dont write the complete word you dont win, i tried to do something but it doesnt work properly.

    improvements i would like:
    * use a .txt file for the words, so its easier for anyone to add new words
    * more ascii art: for example an ascii picture of the guy when is hunged or a winning picture.

    here is the code
    please let me know any imporvement you make:; like shrinking the code.

    Code:
    # AhorcadoX 1.0 (build 43)
    # por IvanHope
    # November - 24 - 2004
    
    # this class emulates the clearscreen command in MsDOS
    def cls():
     	for i in range(1, 20):
     		print
    cls()
    print"""
      		      Hangman 1.0 (build 43)
      		         By IvanHope
      		         
                                                               
                                                              
                                                             
                                 
                                    
                                        
                                      
                                       
                                   
                                                                       
                                                                       
                                                                      
       
            #####################################################
            # You have 7 chances to guess or you loose.         #
            # Instructions:                                     #
            #   * Write the letter or letters you think are     #
            #     in the word.                                  #
            #   * If you know the answer you can try to guess.  # 
            #   * Each input counts as a channce                #
            #####################################################  
                    
    """
    raw_input("                     ENTER to begin...")
     		
    import random
    # Words are the random elements for the game
    words = ("computer", "international", "congressman", "galaxy", "president", "boeing")
    restart = "yes"
    output = ""
    
    # the only way of terminating the game is to write no (NO, No, nO) when the game asks, otherwise it will restart
    while restart != "no":
       # it will choose a random word from words
       word = random.choice(words)
       word = word.lower()
       # it will determinate the lenght of the chosen word
       length = int(len(word))
       # next few lines reset all the variables
       good = ""
       bad = ""
       tries = 0
       correct = "no"
       # tuple that contains correct guessed letters
       letters = []
       #temp string just in case
       temp = ""
       cls ()
       # this loop will continue until tries goes over 7 or user guess correctly
       while correct != "yes" and tries < 8:
          
          # this creates the string with the correct letters and the missing ones
          for l in word:
             if l in letters:
                output = output + " " + l
                temp += l
             else:
                output += " _"
          
          
          cls ()
          print "\t\t", output
          # reset output string
          output = ""
          print "\n********************************************************"
          print "\nThe word is: ", length, "letters long..."
          print "Correct letters: ", good, ", Wrong letters: ", bad
          print "You have used ", tries, " out 7 possible tries."
          print "********************************************************\n\n\n"
          # ask the user for input
          letter = raw_input("\nAsk a letter or take a guess: ")
          letter = letter.lower()
          # if input eq to word loops stops because correct will be "yes"
          if letter == word or temp == word:
             correct = "yes"
          # user will know if the letter was or wasnt in thhe word
          # the letter will became part of the string good or bad
          # i had to put letter != "" because otherwise a blank space will appear in good string
          if letter in word and letter != "":
             print "The letter/s ", letter, "is in the Word."
             letters += letter
             good = good + " - " + letter
          else:
             print "The letter/s ", letter, "is not in the Word."
             bad = bad + " - " + letter
          # it will sum 1 to tries
          tries += 1
       # if tries more than 7 loop stops
       if tries > 7:
          cls ()
          print """
          *-------------------------------------------*
          ] I am sorry but you used all the 7 chances [
          *-------------------------------------------*
          """
       # if correct eq "yes" will print you Won and then finish the loop
       if correct == "yes":
          cls ()
          print """
                                       
                                   
                                    
                                   
                                     
                                         
                                        
                                          
               
             
             
             
             
             
             
             
             """
       # any other answer but "no"(NO, No, nO) will restart the game
       restart = raw_input("Play Again?")
       restart = restart.lower()
    cls ()
    # game over ASCII
    print """
                                     
                                           
                                          
                                    
                                        
                                      
                                          
                                       
       
       
       
       
       
       
       """
    # it will wait til user press enter key to end game
    raw_input("ENTER to Exit...")
    Last edited by Scorpions4ever; August 3rd, 2007 at 04:35 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Thanks Ivan,
    Am going thru' ur game program....I may not be the right person to ask for improvements in ur code...but will let u know if I do come across any...ur code is great... guess ppl shd take inspiration from u & post away their little inventions.

    Thanks & Rgds,
    Subha
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    10
    Some comments about your code:

    you dont need int(len(word)) - it is the same as len(word) :
    Code:
    >>> type(len("test"))
    <type 'int'>
    >>> type(int(len("test")))
    <type 'int'>
    >>>


    Your function cls() is waste of space.
    if you want to print 20 empty lines, do this instead:
    Code:
    print "\n" * 20
    or

    Code:
    import os
    system("cls")
    if you are on windows

    Dag
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Code:
    import os
    system("cls")
    this won't work for 2 reasons.....
    first, you can't use system("cls").....its got to be
    os.system("cls")

    second thing is there is still no one liner available to emulate the cls of MSDOS.... guess that doesn't clear ur interactive window...does it?

    The rest 2 improvements cld be implemented...they were good!

    Rgds,
    Subha
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    10
    Ooops, thought I wrote os. before the system("cls")

    but, who said you need to emulate the MSDOS cls for the interactive window? I just said you might use this option for windows as you either run it as a .exe or the .py script - the interactive window is mainly used for testing..

    thanks for commenting it out though
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Originally Posted by Dag
    but, who said you need to emulate the MSDOS cls for the interactive window? I just said you might use this option for windows as you either run it as a .exe or the .py script - the interactive window is mainly used for testing..
    Hmmm sorry was presumptous!

    Thanks & Rgds,
    Subha

IMN logo majestic logo threadwatch logo seochat tools logo