Skip to content

Instantly share code, notes, and snippets.

@jglee72
Created March 25, 2017 15:50
Show Gist options
  • Save jglee72/e19b8cfc41c727a2cd18c855c52370a9 to your computer and use it in GitHub Desktop.
Save jglee72/e19b8cfc41c727a2cd18c855c52370a9 to your computer and use it in GitHub Desktop.
WackAMole.py
import random
import ui
from time import sleep
import console
import sys
#turn button to "on" state
def turn_on(sender):
sender.title = 'on'
sender.tint_color = ('yellow')
#reverts button to off state
def turn_off(sender):
sender.title = 'off'
sender.tint_color = 'light blue'
#briefly turns button to "on" state then reverts button to original state
def blink(sender):
turn_on(sender)
sleep(.5)
turn_off(sender)
#When the button is tapped, it reverses the current state of the button
def button_tapped(sender):
turn_off(sender)
#Describes game
def game_Description():
console.alert("Game objective:", 'Click the button to turn the light off before the next light turns on', "Ready")
#Pops up when user loses game
def game_Over_Alert():
play_Again = console.alert('Game Over!','','Play Again?')
return play_Again
#Checks to see if all lights are off
def check_Lights():
if (button.title == 'off' and
button2.title == 'off' and
button3.title == 'off' and
button4.title == 'off' and
button5.title == 'off' and
button6.title == 'off' and
button7.title == 'off' and
button8.title == 'off'):
return True
else:
return False
#Turns off all lights
def all_Off():
turn_off(button)
turn_off(button2)
turn_off(button3)
turn_off(button4)
turn_off(button5)
turn_off(button6)
turn_off(button7)
turn_off(button8)
#Increase score by 1 and display
def increase_Score(x):
# x += 1
score.title = 'Score: %d' % x
return x
#setting UI and buttons
view = ui.View()
view.name = 'Light Panel'
view.background_color = 'black'
button = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button.width=50
button.height=50
button.center = (view.width*.3, view.height* .4)
button.flex = 'LRTB'
button.action = button_tapped
view.add_subview(button)
button2 = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button2.width=50
button2.height=50
button2.center = (view.width*.433, view.height*.4)
button2.flex = 'LRTB'
button2.action = button_tapped
view.add_subview(button2)
button3 = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button3.width=50
button3.height=50
button3.center = (view.width*.5666, view.height*.4)
button3.flex = 'LRTB'
button3.action = button_tapped
view.add_subview(button3)
button4 = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button4.width=50
button4.height=50
button4.center = (view.width*.7, view.height*.4)
button4.flex = 'LRTB'
button4.action = button_tapped
view.add_subview(button4)
button5 = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button5.width=50
button5.height=50
button5.center = (view.width*.3, view.height* .5)
button5.flex = 'LRTB'
button5.action = button_tapped
view.add_subview(button5)
button6 = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button6.width=50
button6.height=50
button6.center = (view.width*.4333, view.height*.5)
button6.flex = 'LRTB'
button6.action = button_tapped
view.add_subview(button6)
button7 = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button7.width=50
button7.height=50
button7.center = (view.width*.5666, view.height*.5)
button7.flex = 'LRTB'
button7.action = button_tapped
view.add_subview(button7)
button8 = ui.Button(title = 'off',font = ('Arial Rounded MT Bold', 20), bg_color = 'red')
button8.flex = 'LRTB'
button8.action = button_tapped
button8.width=50
button8.height=50
button8.center = (view.width*.7, view.height*.5)
view.add_subview(button8)
scoreCount = 0
score = ui.Button(title = 'Score: 0')
score.center = (view.width*.5, view.height*.75)
score.flex = 'LRTB'
view.add_subview(score)
#Set up display
view.present('sheet')
#Runs the game and handles the function
def play_Game():
scoreCount = 0
speed = 2.55
game_Description()
random.seed()
while check_Lights():
# sleep(speed)
scoreCount = increase_Score(scoreCount)
x = random.randint(0,7)
y = random.randint(0,14)
if x == 0 or y == 0:
turn_on(button)
scoreCount+=1
if x == 1 or y == 1:
turn_on(button2)
scoreCount+=1
if x == 2 or y == 2:
turn_on(button3)
scoreCount+=1
if x == 3 or y == 3:
turn_on(button4)
scoreCount+=1
if x == 4 or y == 4:
turn_on(button5)
scoreCount+=1
if x == 5 or y == 5:
turn_on(button6)
scoreCount+=1
if x == 6 or y == 6:
turn_on(button7)
scoreCount+=1
if x == 7 or y == 7:
turn_on(button8)
scoreCount+=1
sleep(speed)
if speed >= 1.5:
speed-= 0.07
if game_Over_Alert():
all_Off()
play_Game()
play_Game()
view.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment