Hangman

def redraw_all(app, canvas):
draw_hangman(canvas)
def draw_hangman(canvas):
# Bounds (to be removed later)
canvas.create_rectangle(0, 0, 100, 100, outline='', fill='yellow')
canvas.create_line(00, 100, 40, 100) # Base
canvas.create_line(20, 100, 20, 0) # Pole
canvas.create_line(20, 0, 80, 0) # Bar
canvas.create_line(80, 0, 80, 20) # Rope
canvas.create_oval(70, 20, 90, 40) # Head
canvas.create_line(80, 40, 80, 70) # Body
canvas.create_line(60, 40, 80, 50) # Left arm
canvas.create_line(100, 40, 80, 50) # Right arm
canvas.create_line(65, 90, 80, 70) # Left leg
canvas.create_line(95, 90, 80, 70) # Right leg
# Face
canvas.create_line(75, 25, 78, 28) # Left eye
canvas.create_line(75, 28, 78, 25) # Left eye
canvas.create_line(82, 25, 85, 28) # Right eye
canvas.create_line(85, 25, 82, 28) # Right eye
canvas.create_line(75, 35, 85, 35) # Mouth
# Tounge
canvas.create_arc(78, 32, 82, 38, start=180, extent=180, style='arc')
if __name__ == '__main__':
from uib_inf100_graphics.event_app import run_app
run_app(width=800, height=600)
def app_started(app):
app.secret_word = 'EPLE'
app.guessed_letters = ''
app.incorrectly_guessed_letters = ''
def key_pressed(app, event):
guess = event.key.upper()
if guess not in app.guessed_letters:
app.guessed_letters += guess
if guess not in app.secret_word:
app.incorrectly_guessed_letters += guess
def redraw_all(app, canvas):
# Top left text
canvas.create_text(
20, 20, anchor='nw',
text=f'Du har bommet med: {app.incorrectly_guessed_letters}'
)
# Hangman figure
draw_hangman(
canvas, 100, 100, app.width-100, app.height-100,
len(app.incorrectly_guessed_letters)
)
# Concealed and revealed letters
txt = get_concealed_word(app.secret_word, app.guessed_letters)
canvas.create_text(app.width/2, app.height-50, text=txt, font='Courier 30')
def get_concealed_word(secret_word, guessed_letters):
# F. eks.
# hvis secret_word = 'EPLE'
# og guessed_letters = 'APPL'
#
# så retur-verdi:
# '_ P L _ '
res = ''
for c in secret_word:
if c in guessed_letters:
res += c + ' '
else:
res += '_ '
return res
def draw_hangman(canvas, x1, y1, x2, y2, step):
dx = x1
dy = y1
width = (x2 - x1)
height = (y2 - y1)
sx = width / 100 # skaleringsfaktor i x-retning (der 100 er original bredde)
sy = height / 100 # skaleringsfaktor i y-retning (der 100 er original høyde)
if step >= 1: # Base
canvas.create_line(
dx + sx * 0, dy + sy * 100,
dx + sx * 40, dy + sy * 100
)
if step >= 2: # Pole
canvas.create_line(
dx + sx * 20, dy + sy * 100,
dx + sx * 20, dy + sy * 0
)
if step >= 3: # Bar
canvas.create_line(
dx + sx * 20, dy + sy * 0,
dx + sx * 80, dy + sy * 0
)
if step >= 4: # Rope
canvas.create_line(
dx + sx * 80, dy + sy * 0,
dx + sx * 80, dy + sy * 20
)
if step >= 5: # Head
canvas.create_oval(
dx + sx * 70, dy + sy * 20,
dx + sx * 90, dy + sy * 40
)
if step >= 6: # Body
canvas.create_line(
dx + sx * 80, dy + sy * 40,
dx + sx * 80, dy + sy * 70
)
if step >= 7: # Left arm
canvas.create_line(
dx + sx * 60, dy + sy * 40,
dx + sx * 80, dy + sy * 50
)
if step >= 8: # Right arm
canvas.create_line(
dx + sx * 100, dy + sy * 40,
dx + sx * 80, dy + sy * 50
)
if step >= 9: # Left leg
canvas.create_line(
dx + sx * 65, dy + sy * 90,
dx + sx * 80, dy + sy * 70
)
if step >= 10: # Right leg and face
canvas.create_line(
dx + sx * 95, dy + sy * 90,
dx + sx * 80, dy + sy * 70
)
canvas.create_line(
dx + sx * 75, dy + sy * 25,
dx + sx * 78, dy + sy * 28
)
canvas.create_line(
dx + sx * 75, dy + sy * 28,
dx + sx * 78, dy + sy * 25
)
canvas.create_line(
dx + sx * 82, dy + sy * 25,
dx + sx * 85, dy + sy * 28
)
canvas.create_line(
dx + sx * 85, dy + sy * 25,
dx + sx * 82, dy + sy * 28
)
canvas.create_line(
dx + sx * 75, dy + sy * 35,
dx + sx * 85, dy + sy * 35
)
canvas.create_arc(
dx + sx * 78, dy + sy * 32,
dx + sx * 82, dy + sy * 38,
start=180, extent=180, style='arc'
)
if __name__ == '__main__':
from uib_inf100_graphics.event_app import run_app
run_app(width=800, height=600)
Her er en oversikt over vanlige ord på norsk.