The BFOIT Introduction to Programming Class
Partial Code for the Hangman Program
Following is a good start at the hangman program, including the
procedures in the structure drawing.
; global variables
;-----------------
make "gameOver "false
make "badGuessCount 0
make "guessWord "
make "secretWord "
; general constants
;------------------
to black
output 0
end
to blue
output 1
end
to red
output 4
end
to north
output 0
end
to east
output 90
end
to south
output 180
end
; guess word display stuff
;-------------------------
to charWid
output 20
end
to gapWid
output 5
end
to guessWordWidth
;compute width (in turtlesteps) drawing guessWord takes
output 0
end
to guessWordLeftX
output minus quotient guessWordWidth 2
end
to guessWordBottomY
output -120
end
to drawGuessWord
setlabelheight 30
;draw contents of guessWord
end
; scaffold and feedback for wrong guesses (the man) stuff
;--------------------------------------------------------
to scaffoldLeftX
output -90
end
to scaffoldBottomY
output -80
end
to drawScaffold
penup setxy scaffoldLeftX scaffoldBottomY pendown
setpensize 8
setheading north
forward 200
right 90 forward 130
setpensize 2
right 90 forward 15
end
to bodyTopY
output 60
end
to bodyLength
output 70
end
to bodyCenterX
output 40
end
to neckLength
output 10
end
to arm1
setpensize 8
penup setxy bodyCenterX bodyTopY pendown
setheading south
fd neckLength
right 50
fd 50
end
to arm2
setpensize 8
penup setxy bodyCenterX bodyTopY pendown
setheading south
fd neckLength
left 50
fd 50
end
to body
setpensize 16
penup setxy bodyCenterX bodyTopY pendown
setheading south
forward 80
end
to head
setpensize 6
penup setxy bodyCenterX bodyTopY pendown
setheading 275
repeat 72 [fd 2 rt 5]
end
to leg1
setpensize 8
penup setxy bodyCenterX difference bodyTopY bodyLength pendown
setheading 220
fd 60
end
to leg2
setpensize 10
penup setxy bodyCenterX difference bodyTopY bodyLength pendown
setheading 140
fd 60
end
to lose
penup
setpencolor red
setlabelheight 80
setxy difference scaffoldLeftX 20 50
label "Sorry,
setxy sum scaffoldLeftX 30 -10
label "You
setxy sum scaffoldLeftX 10 -70
label "Lose
make "gameOver "true
end
to badGuessFeedback
;take care of updating graphics to reflect a bad guess
end
to win
penup
setpencolor blue
setlabelheight 80
setxy difference scaffoldLeftX 100 50
label "Congrats!
setxy sum scaffoldLeftX 30 -10
label "You
setxy sum scaffoldLeftX 20 -70
label "Win!
make "gameOver "true
end
to guessChar? :ch
;process the character typed - a guess.
;if guessed character is in the secretWord, update the
;word on the display to include it and output true.
;otherwise, output false
end
to keypressed :chNum
;if game in progress, process the key pressed
end
; initialization stuff
;---------------------
to initSecretWord
;pick a random word from a sentence of all the
;possible words your version of hangman supports
end
to makeEmptyGuessWord
;initialize the global variable guessWord
end
to initGuessStuff
make "badGuessCount 0
makeEmptyGuessWord
drawGuessWord
end
to init
hideturtle home clean setpencolor black
make "gameOver "false
drawScaffold
initSecretWord
initGuessStuff
end
init
Table of Contents