Skip to content

Instantly share code, notes, and snippets.

@SpaceVoyager
Created November 12, 2016 19:04
Show Gist options
  • Save SpaceVoyager/02a3b21b55de1b73c063d8245dbb2daa to your computer and use it in GitHub Desktop.
Save SpaceVoyager/02a3b21b55de1b73c063d8245dbb2daa to your computer and use it in GitHub Desktop.
hangman.py
# coding: utf-8
from scene import *
from ui import *
import speech
import dialogs
def is_point_inside_rect(point, rect):
x = rect[0]
y = rect[1]
w = rect[2]
h = rect[3]
if point.x >= x-w/2 and point.x <= x+w/2 and point.y >= y-h/2 and point.y <= y+h/2:
return True
else:
return False
class MyScene (Scene):
def setup(self):
self.background_color = 'white'
keyboard_font = ('Futura', 40)
self.letters = 'QWERTYUIOPASDFGHJKLZXCVBNM'
i = 0
keyboard_y = 220
key_height = 80
key_width = 100
self.button_rects = []
self.buttons = []
for letter in self.letters:
x = i*(key_width+2)+ 54
i += 1
if i < 11:
y = keyboard_y
elif i < 20:
x = (i-11)*(key_width+2) + 104
y = keyboard_y - key_height -2
else:
y= keyboard_y-2*key_height-4
x = (i-21)*(key_width+2) + 308
self.button_rect = (x, y, key_width, key_height)
self.button_rects.append(self.button_rect)
self.button_shape = ShapeNode(path=Path.rounded_rect(100, 250, self.button_rect[2], self.button_rect[3], 5), fill_color='blue', stroke_color='black', parent=self)
self.button_shape.position = (self.button_rect[0], self.button_rect[1])
self.buttons.append(self.button_shape)
self.button_label = LabelNode(letter, keyboard_font, parent=self)
self.button_label.position = (self.button_rect[0], self.button_rect[1])
self.button_label.color = 'white'
self.button_label.z_position = 1
self.display_phrase = ''
for x in secret_phrase:
if x == ' ' or x == '\n':
self.display_phrase += x
else:
self.display_phrase += '🎃'
self.text_label = LabelNode(self.display_phrase, ('Courier', 50), parent=self)
self.text_label.position = (self.size.w/2, 300)
#self.text_label.color = 'black'
self.stand_path = Path()
self.stand_path.move_to(0,0)
self.stand_path.line_to(300,0)
self.stand_path.move_to(150,0)
self.stand_path.line_to(150,-400)
self.stand_path.line_to(250, -400)
self.stand_path.line_to(250, -350)
self.stand_path.line_width = 5
self.stand_shape = ShapeNode(path=self.stand_path, stroke_color='black', parent=self)
self.stand_shape.anchor_point = (0,0)
self.stand_shape.position = (100, 350)
self.head_path = Path.oval(0,0, 50, 50)
self.head_path.line_width = 5
self.head_shape = ShapeNode(path=self.head_path, stroke_color='black', parent=self)
self.head_shape.anchor_point = (0,0)
self.head_shape.position = (325, 650)
path = Path.oval(0,0, 8, 8)
path.line_width = 3
self.left_eye_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.left_eye_shape.anchor_point = (0,0)
self.left_eye_shape.position = (340, 680)
path = Path.oval(0,0, 8, 8)
path.line_width = 3
self.right_eye_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.right_eye_shape.anchor_point = (0,0)
self.right_eye_shape.position = (355, 680)
path = Path()
path.add_arc(8,0, 8, math.pi, 2*math.pi)
path.line_width = 3
self.mouth_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.mouth_shape.anchor_point = (0,0)
self.mouth_shape.position = (342, 660)
path = Path()
path.move_to(0,0)
path.line_to(0, 100)
path.line_width = 8
self.torso_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.torso_shape.anchor_point = (0,0)
self.torso_shape.position = (350, 550)
path = Path()
path.move_to(0,0)
path.line_to(40, 80)
path.line_width = 5
self.left_leg_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.left_leg_shape.anchor_point = (0,0)
self.left_leg_shape.position = (352, 472)
path = Path()
path.move_to(0,0)
path.line_to(-40, 80)
path.line_width = 5
self.right_leg_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.right_leg_shape.anchor_point = (0,0)
self.right_leg_shape.position = (311, 472)
path = Path()
path.move_to(0,0)
path.line_to(60, 60)
path.line_width = 5
self.left_arm_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.left_arm_shape.anchor_point = (0,0)
self.left_arm_shape.position = (350, 580)
path = Path()
path.move_to(0,0)
path.line_to(-60, 60)
path.line_width = 5
self.right_arm_shape = ShapeNode(path=path, stroke_color='black', parent=self)
self.right_arm_shape.anchor_point = (0,0)
self.right_arm_shape.position = (295, 580)
def touch_began(self, touch):
button_index = 0
for button_rect in self.button_rects:
if is_point_inside_rect(touch.location, button_rect):
button_pressed = self.buttons[button_index]
if button_pressed.fill_color != 'gray':
button_pressed.fill_color = 'gray'
speech.say(self.letters[button_index].lower())
button_index += 1
secret_phrase = dialogs.text_dialog(title='Please enter a secret phrase', text='', font=('<system>', 32), autocorrection=None, autocapitalization=ui.AUTOCAPITALIZE_SENTENCES)
run(MyScene())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment