/
hangman.py
129 lines (119 loc) · 3.56 KB
/
hangman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import turtle
import random
t = turtle
screen = turtle.getscreen()
def initialize(): #This sets up the background and settings for the game
global word
t.speed(0) #This creates the settings
screen.tracer(0)
t.hideturtle()
t.setworldcoordinates(20,20,80,80)
t.penup() #This draws a gallow
t.setpos(55,45)
t.pendown()
t.left(90)
t.forward(20)
t.left(90)
t.forward(10)
t.left(90)
t.forward(5)
t.penup()
t.setpos(35,30)
t.left(90)
for i in range(len(word)): #This draws the underscores for letters
t.pendown()
t.forward(2)
t.penup()
t.forward(2)
def drawLetter(word,character): #Called when player correctly guesses. Draws letter
global count
times = [x for x, appearance in enumerate(word) if appearance == character] #Creates list of when 'character' appears in 'word'
for i in range(len(times)): #Draws character based on when it appears in 'word'
t.penup()
t.setpos(36 + 4*(times[i]),31)
t.write(character, move=True, align='center', font=('Arial', 20, 'normal'))
count += len(times)
def drawBody(): #Called when player incorrectly guesses. Draws body part
global n
t.setheading(270)
t.penup()
if n == 0: #draws head
t.setpos(45,60)
t.right(90)
t.pendown()
t.circle(3,360,500)
t.penup()
elif n == 1: #draws torso
t.setpos(45,54)
t.pendown()
t.forward(8)
t.penup()
elif n == 2: #draws left arm
t.setpos(45,51)
t.right(135)
t.pendown()
t.forward(6)
t.penup()
elif n == 3: #draws right arm
t.setpos(45,51)
t.left(135)
t.pendown()
t.forward(6)
t.penup()
elif n == 4: #draws left leg
t.setpos(45,46)
t.right(45)
t.pendown()
t.forward(6)
t.penup()
elif n == 5: #draws right leg
t.setpos(45,46)
t.left(45)
t.pendown()
t.forward(6)
t.penup()
def guess(): #This function asks the user for an input, and either draws a body part or a letter
global word
global n
character = t.textinput('Your Guess','What letter are you thinkin\'')
if character in word: #If letter is in word, draws letter
drawLetter(word,character)
else: #If letter not in word, draws body part
drawBody()
n += 1
def fake_main():
global n
global count
global word
n = 0
count = 0
initialize()
done = False
while not done:
if n > 5:
done = True
t.penup()
t.setpos(50,70)
t.write('You lose.' + 'The word was \'' + word + '\'', move=True, align='center', font=('Arial', 50, 'normal'))
elif count > len(word) - 1:
done = True
t.penup()
t.setpos(50,70)
t.write('Amazing! You guessed the word \'' + word + '\'. You win!', move=True, align='center', font=('Arial', 30, 'normal'))
else:
guess()
return None
def main():
finished = False
while not finished:
global word
wordList = ['lildicky','kendrick','jcole','eminem','kanye','bigsean','travis','childish','vicmensa','drake','weeknd','twochainz','ishdarr','asaprocky','schoolboy','kidcudi','chance','migos','wakaflame','guccimane']
word = wordList[random.randint(0,19)]
fake_main()
repeat = t.textinput('','Would you like to play again? (y/n): ')
if repeat == 'y':
t.reset()
else:
finished = True
return None
main()