hangman.py

Created by laigna

Created on March 14, 2026

3.54 KB

Classic hangman. Enter a word for P2 or press ENTER for random. 7 lives, skips duplicate guesses.


# Hangman - NumWorks
# Created by Alvar Laigna - https://alvarlaigna.com
# Arrows:select letter OK:guess Back:quit
from kandinsky import fill_rect as F,draw_string as D
from ion import keydown as K
from time import sleep as Z
from random import choice
WL=[
 "PYTHON","NUMWORKS","CALCULATOR","GRAPH",
 "SNAKE","MEMORY","ROCKET","PLANET","OCEAN",
 "BRIDGE","FOREST","CASTLE","DRAGON","TOWER",
 "MUSIC","HAMMER","SILVER","GOLDEN","STORM",
 "CLOUD","FLAME","MAGIC","ROBOT","PIXEL",
 "QUEST","BLADE","FROST","LIGHT","NIGHT",
 "BRAVE","CHESS","CROWN","EAGLE","GHOST",
 "JEWEL","LEMON","MEDAL","ORBIT","PRISM",
 "RADAR","SCOUT","TIGER","UNITY","VITAL",
 "WHEAT","ATLAS","DELTA","LUNAR","SOLAR"]
BK=(0,)*3;WH=(255,)*3
WD=(150,90,40);RE=(225,50,50)
GR=(0,190,0);BL=(50,110,230)
YL=(240,210,60);DG=(50,)*3
HX,HY=82,55
NC=9;RX=128;RY=125;GS=19;RS=24

okv=1 if K(4)or K(52) else 0

def okd():
 return K(4)or K(52)

def okp():
 global okv
 d=okd()
 if d and not okv:
  okv=1
  return True
 if not d:okv=0
 return False

def wup():
 while okd():Z(0.02)
 Z(0.12)

def gallows():
 F(10,162,105,5,WD)
 F(25,25,6,137,WD)
 F(25,25,60,5,WD)
 F(82,30,2,16,(120,90,50))
 F(30,50,3,10,WD)
 F(33,47,10,3,WD)

def body(n):
 if n==1:
  for y in range(-9,10):
   r=int(max(0,81-y*y)**0.5)
   if r:F(HX-r,HY+y,r*2,1,WH)
 elif n==2:F(HX-1,HY+10,3,30,WH)
 elif n==3:
  for i in range(14):F(HX-3-i,HY+14+i,2,1,WH)
 elif n==4:
  for i in range(14):F(HX+2+i,HY+14+i,2,1,WH)
 elif n==5:
  for i in range(18):F(HX-3-i,HY+40+i,2,1,WH)
 elif n==6:
  for i in range(18):F(HX+2+i,HY+40+i,2,1,WH)
 elif n==7:
  F(HX-5,HY-3,3,2,RE);F(HX+3,HY-3,3,2,RE)
  F(HX-4,HY+5,9,2,RE)

def smiley():
 F(HX-5,HY-3,2,2,GR);F(HX+4,HY-3,2,2,GR)
 F(HX-4,HY+4,1,1,GR);F(HX+4,HY+4,1,1,GR)
 F(HX-3,HY+5,7,1,GR)

def dw(fd):
 s=" ".join(fd)
 px=RX+(190-len(s)*10)//2
 F(RX,14,190,20,BK)
 D(s,max(RX,px),16,YL,BK)

def di(hp,bad):
 F(RX,40,190,18,BK)
 if bad:
  D("Bad: "+" ".join(bad),RX,42,RE,BK)
 F(RX,63,190,18,BK)
 c=GR if hp>3 else YL if hp>1 else RE
 D("Lives: "+("O "*hp),RX,65,c,BK)

def dl(cx,cy,used):
 for r in range(3):
  for c in range(NC):
   idx=r*NC+c
   x,y=RX+c*GS,RY+r*RS
   if idx>=26:
    F(x-2,y-2,17,19,BK);continue
   ch=chr(65+idx)
   if ch in used:
    F(x-2,y-2,17,19,BK)
    D(ch,x,y,GR if used[ch]else RE,BK)
   elif r==cy and c==cx:
    F(x-3,y-3,19,21,BL)
    D(ch,x,y,WH,BL)
   else:
    F(x-2,y-2,17,19,BK)
    D(ch,x,y,WH,BK)

def game():
 while True:
  print("\n"*20+"HANGMAN\n")
  w=input("Word (ENTER=random): ")
  w=w.upper().strip()or choice(WL)
  fd=["_"]*len(w);bad=[];hp=7
  used={};cx=cy=0
  F(0,0,320,222,BK)
  gallows()
  F(RX,95,190,2,(30,)*3)
  D("HANGMAN",RX+52,100,DG,BK)
  dw(fd);di(hp,bad);dl(cx,cy,used)
  while hp>0 and "_" in fd:
   mv=False
   if K(0):
    cx=max(0,cx-1);mv=True
   elif K(3):
    mx=7 if cy==2 else 8
    cx=min(mx,cx+1);mv=True
   elif K(1):
    cy=max(0,cy-1);mv=True
   elif K(2):
    cy=min(2,cy+1)
    if cy==2 and cx>7:cx=7
    mv=True
   elif okp():
    idx=cy*NC+cx
    if idx<26:
     ch=chr(65+idx)
     if ch not in used:
      if ch in w:
       used[ch]=True
       for i in range(len(w)):
        if w[i]==ch:fd[i]=ch
      else:
       used[ch]=False
       bad.append(ch);hp-=1
       body(7-hp)
      dw(fd);di(hp,bad);dl(cx,cy,used)
    Z(0.18)
   elif K(17):return
   if mv:dl(cx,cy,used);Z(0.12)
   Z(0.03)
  Z(0.5)
  F(RX,95,190,25,BK)
  if "_" not in fd:
   smiley()
   D("YOU WIN!",RX+45,100,GR,BK)
  else:
   body(7)
   t="Was: "+w
   D(t,RX+(190-len(t)*10)//2,100,RE,BK)
  D("OK=again",RX+50,210,DG,BK)
  while True:
   if okp():wup();break
   if K(17):return
   Z(0.05)

game()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.