A BASIC Hangman Game

jib 0 Tallied Votes 826 Views Share

###############################
# PROLOG SECTION
# hangman.py
# Program to play the traditional game of hangman.
# The computer picks a word and the player
# has to guess it before he has made 7 wrong
# guesses of letters in the word.
# Each time the user makes an incorrect guess
# of a letter, one more body part gets added
# to the text graphic.
# Uses multiple defined functions.
# Uses branching and looping.
# (today's date goes here)
# (programmer names go here)
# (tester names go here)
# Possible future enhancements:
# Unresolved bugs:
############################### This section contains important information about the program.
The comments give an overview of how the hangman game will be played.

FUNCTION DEFINITION SECTION
# Definitions: print_game_rules(),
# display_figure(), prompt_for_letter()

In this program you’ll define three functions: print_game_rules(), which prints the game rules for the user; display_figure(), which displays the appropriate version of the guillotine and “hanging” stick figure; and prompt_for_letter(), which prompts the user for the next letter guess.
These functions are all called from the main processing section of the program

TrustyTony commented: Very unclear post +0
from turtle import *
from random import *
import winsound
def shooter_clicked(x,y):
    global endloop
    
    endloop =True
    return
setup(600,400)


maxx = 300

maxy = 200

minx= -maxx

miny= - maxy

bounce_point = 20
x = randint(minx + bounce_point, maxx + bounce_point)
y = randint(miny + bounce_point, maxy + bounce_point)

dx = 5
dy = 5
ball.goto(x,y)

ball.showturtle()

onscreenclick(stop_loop)



loop_count=0
endloop = False


while not endloop:


    xx = x + dx


    if xx < minx-bounce_point:
        xx = minx + bounce_point
        dx = -dx
        winsound.MessageBeep(-1)

    if xx > maxx - bounce_point:
        xx = maxx- bounce_point
        dx = -dx
        winsound.MessageBeep(-1)



    yy=y+dy
    if yy < miny - bounce_point: 
        yy= miny + bounce_point
        dy = -dy
        winsound.MessageBeep(-1)

    if yy > maxy- bounce_point:   
        yy = maxy - bounce_point
        dy = -dy
        winsound.MessageBeep(-1)


    x = xx
    y = yy
    ball.goto(x,y)
 

bye()
jib -2 Junior Poster

Sorry fellas!! must be overexcitement. That is the wrong code

jib -2 Junior Poster
from random import *

def print_game_rules(max_incorrect,word_len):
    word_len=1
    print"you have only 7 chances to guess the right answer"
    return

def get_letter():
    print
    letter=raw_input("Guess a letter in the mystery word:") 
    letter.strip()
    letter.lower()
    print
    return letter
def display_figure(bad_guesses):
    graphics = [
    """
        +--------+
         |
         |
         |
         |
         |
         |
     ====================
    """,
    """
        +-------
        |      o
        |
        |
        |
        |
    ====================
    """,
    """
        +-------
        |      o
        |   ---+---
        |
        |
        |
    ======================
    """,
    """
        +-------
        |       o
        |    ---+---
        |       |
        |    
        |  
        |   
    =====================
    """,
    """
        +-------
        |  
        |       o
        |    ---+
        |       |
        |      / 
        |     /   
        |
        |
        |
        |
    =====================
    """,
    """
        +-------
        |        o
        |     ---+---
                 |
        |       / 
        |      /   
        |     /     
        |     
    =====================
    """,
      """
        +-------
        |        o
        |     ---+---
                 |
        |       / \
        |      /   \  
        |     /     \    
        |     
    =====================
    """
,]
   
    print graphics[bad_guesses]
    return



animals=["horse","donkey","dinosaur","monkey","cat","aligator","butterfly","buffallo","dragon"]
word=choice(animals)
word_len=len(word)
guesses=word_len * ['_']
max_incorrect=6
alphabet="abcdefghijklmnopqrstuvxyz"
letters_tried=""
number_guesses=0
letters_correct=0
incorrect_guesses=0


print_game_rules(max_incorrect,word_len)
while (incorrect_guesses != max_incorrect) and (letters_correct != word_len):
    letter=get_letter()
    if len(letter)==1 and letter.isalpha():
        if letters_tried.find(letter) != -1:
            print "You already picked", letter
        else:
            letters_tried = letters_tried + letter
            first_index=word.find(letter)
            if  first_index == -1:
                incorrect_guesses= incorrect_guesses +1
                print "the",letter,"is not the mystery_word"
            else:
                print"the",letter,"is in the mystery_word"
                letters_correct=letters_correct+1
                for i in range(word_len):
                    if letter == word[i]:
                        guesses[i] = letter

    else:
        print "Please guess a single letter in the alphabet."
    display_figure(incorrect_guesses)
    
    print ''.join(guesses)
    print "Letters tried so far: ", letters_tried
    if incorrect_guesses == max_incorrect:
        print "Sorry, too many incorrect guesses. You are hanged."
        print "the word was",word
    if letters_correct == word_len:
        print "You guessed all the letters in the word!"
        print "the word was",word
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.