Skip to content

Instantly share code, notes, and snippets.

@voglster
Created May 19, 2021 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voglster/dcac6313417681d4790d559d5db2b058 to your computer and use it in GitHub Desktop.
Save voglster/dcac6313417681d4790d559d5db2b058 to your computer and use it in GitHub Desktop.
from graphics import *
import time
# The given constants work well in a window that is at least 400 by 600.
# Gallows constants
GALLOWS_UPRIGHT = 150 #The x value of the upright for the gallows
GALLOWS_TOP_Y = 200
GALLOWS_BOTTOM_Y = 400
GALLOWS_BASE_LEFT_X = GALLOWS_UPRIGHT - 50
GALLOWS_BASE_RIGHT_X = GALLOWS_UPRIGHT + 50
ROPE_LENGTH = 20
# Person constants
MIDPERSON = 250 #The x value of the vertical line that runs through the middle of the person
HEAD_RADIUS = 30
NECK_LENGTH = 30
TORSO_LENGTH = 30
ARM_OFFSET_X = 40
ARM_OFFSET_Y = 30
LEG_OFFSET_X = 30
LEG_OFFSET_Y = 40
# Calculated person constants
HEAD_CENTER_Y = GALLOWS_TOP_Y + ROPE_LENGTH + HEAD_RADIUS
NECK_TOP_Y = HEAD_CENTER_Y + HEAD_RADIUS
TORSO_BOTTOM_Y = NECK_TOP_Y + NECK_LENGTH + TORSO_LENGTH
ARM_ATTACH_Y = NECK_TOP_Y + NECK_LENGTH
# Textbox constants
TEXTBOX_CENTERPOINT_X = 200
TEXTBOX1_Y = 40
TEXTBOX2_Y = 80
# Create graphics objects for each body part
def head():
circle = Circle(Point(MIDPERSON, HEAD_CENTER_Y), HEAD_RADIUS)
return circle
def torso():
line = Line(Point(MIDPERSON, NECK_TOP_Y),Point(MIDPERSON, TORSO_BOTTOM_Y))
return line
def rightLeg():
line = Line(Point(MIDPERSON, TORSO_BOTTOM_Y), Point(MIDPERSON-LEG_OFFSET_X, TORSO_BOTTOM_Y+LEG_OFFSET_Y))
return line
def leftLeg():
line = Line(Point(MIDPERSON, TORSO_BOTTOM_Y), Point(MIDPERSON+LEG_OFFSET_X, TORSO_BOTTOM_Y+LEG_OFFSET_Y))
return line
def leftArm():
line = Line(Point(MIDPERSON, ARM_ATTACH_Y), Point(MIDPERSON+ARM_OFFSET_X, ARM_ATTACH_Y-ARM_OFFSET_Y))
return line
def rightArm():
line = Line(Point(MIDPERSON, ARM_ATTACH_Y), Point(MIDPERSON-ARM_OFFSET_X, ARM_ATTACH_Y-ARM_OFFSET_Y))
return line
# Initial setup for the game. Should be called before the main loop starts.
def drawGallows(win):
# Bottom
line = Line(Point(GALLOWS_BASE_LEFT_X, GALLOWS_BOTTOM_Y), Point(GALLOWS_BASE_RIGHT_X, GALLOWS_BOTTOM_Y))
line.draw(win)
# Upright
line = Line(Point(GALLOWS_UPRIGHT, GALLOWS_TOP_Y), Point(GALLOWS_UPRIGHT, GALLOWS_BOTTOM_Y))
line.draw(win)
# Cross piece
line = Line(Point(GALLOWS_UPRIGHT, GALLOWS_TOP_Y), Point(MIDPERSON, GALLOWS_TOP_Y))
line.draw(win)
# Rope
line = Line(Point(MIDPERSON, GALLOWS_TOP_Y), Point(MIDPERSON, GALLOWS_TOP_Y + ROPE_LENGTH))
line.draw(win)
# Draw the parts. In your game track the wrong answer number. Start with wrong answer number 0.
# Then each time there is a wrong answer, draw partsList[i] and increment the wrong answer number.
# Here is a routine that shows
# where the text boxes are with the above constants, and
# what happens when the parts are drawn one at a time:
def main():
mainWindow = GraphWin("Hello graphics!", 400, 600)
outputBox1 = Text(Point(TEXTBOX_CENTERPOINT_X,TEXTBOX1_Y),"Welcome to Hangman!")
outputBox2 = Text(Point(TEXTBOX_CENTERPOINT_X,TEXTBOX2_Y),"")
outputBox1.draw(mainWindow)
outputBox2.draw(mainWindow)
answer = "_ "*7
outputBox2.setText(answer)
drawGallows(mainWindow)
partsList =[head(), torso(), rightArm(), leftArm(), rightLeg(), leftLeg()]
for counter in range(len(partsList)):
time.sleep(1.5)
partsList[counter].draw(mainWindow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment