Ask coding questions

← Back to all posts
Hangman code
aadyagupta1 (0)

Can you pls help me out? This is my code for hangman but for some reason I have a lot of errors. Pls post the correct code

#functions
import pygame
import math

#setup
pygame.init()
WIDTH, HEIGHT = 800,500
win=pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('hangman')

FPS=60
clock=pygame.time.Clock()

#load images
images= []
for i in range(7):
image= pygame.images.load('hangman'+str(i)+'.png')
image.append

#game variables
hangman_status=0
WHITE=(255,225,225)
BLACK=(0,0,0)
A=65
word='DEVOLOPER'
guessed= []

#button variables
RADIUS=20
GAP=15
letters=[]
startx=round((WIDTH-(RADIUS2+GAP)13)/2)
starty=400
for i in range(27):
x=startx+GAP2+((RADIUS2+GAP)(i%13))
y=starty+((i//13)
(GAP+RADIUS*2))
letters.append([x,y,chr(A+i), True])

#button space
def draw():
win.fill(WHITE)
pygame.display.update()
pygame.blit(images[hangman_status]), (150,100)

#draw buttons
for letter in letters:
x,y,ltr,visible=letter
pygame.draw.circle(win,BLACK,(x,y), RADIUS,3)
text=LETTER_FONT.render(ltr,1,BLACK)
win.blit(x-text.get_width()/2,y-text.get_height()/2)

#display letters
for letter in word:
if letter in guessed:
displayword += letter + ''
else:
display_word += '
'

#font
LETTER_FONT=pygame.font.SysFont('comicsans', 40)
WORD_FONT=pygame.font.SysFont('comicsans', 60)

#game loop
run=True
while run:
clock.tick(FPS)
draw()

for event in pygame.event.get():
if event.type()==pygame.QUIT():
run=False
if event.type()==MOUSEBUTTONDOWN:
m_x, m_y=pygame.mouse.get_pos()
for letter in letters:
x,y,ltr,visible=letter

   if visible:
     distance=math.sqrt((x-m_x**2)+(y-m_y**2))
     if distance<RADIUS:
       letter[3]=False
     guessed.append(ltr)
     if ltr not in word:
       hangman_status+=1

win=True
for ltr in word:
if ltr not in guessed:
win=False
break

if win:
win.fill(WHITE)
text=WORD_FONT.render('You won! Congrats',1, BLACK)
win.blit(text,WIDTH/2-text.get_width()/2,HEIGHT/2-text.get_height()/2)
pygame.display.update()
pygame.time.delay(3000)
break

if hangman_status==6:
win.fill(WHITE)
text=WORD_FONT.render('You lost! the phrase was DEVELOPER',1, BLACK)
win.blit(text,WIDTH/2-text.get_width()/2,HEIGHT/2-text.get_height()/2)
pygame.display.update()
pygame.time.delay(3000)
break

pygame.quit()

Comments
hotnewtop
AloegelhiPlaysR (127)

Line 17, change pygame.images to pygame.image

aadyagupta1 (0)

@AloegelhiPlaysR , I did do that however for the variable 'images' it is saying no such file or dictionary

Wumi4 (498)

Can you send us the error?

Also, when I run the code in the Repl, it doesn't have too much error, only on line 17, you need to rewrite pygame.images to pygame.image.