I've written this Wack-a-mole like game on my iPad in the app Pythonista. Keeping that in mind, it uses a different graphic interface from the one I have usually seen used, Tkinter. I'm a beginner and this is my first major project, so any tips on how to code more efficiently and improve this program would be greatly appreciated.
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':
if button2.title == 'off':
if button3.title == 'off':
if button4. title == 'off':
return True
#Turns off all lights
def all_Off():
turn_off(button)
turn_off(button2)
turn_off(button3)
turn_off(button4)
#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')
button.center = (view.width*.2, view.height*.5)
button.flex = 'LRTB'
button.action = button_tapped
view.add_subview(button)
button2 = ui.Button(title = 'off')
button2.center = (view.width*.4, view.height*.5)
button2.flex = 'LRTB'
button2.action = button_tapped
view.add_subview(button2)
button3 = ui.Button(title = 'off')
button3.center = (view.width*.6, view.height*.5)
button3.flex = 'LRTB'
button3.action = button_tapped
view.add_subview(button3)
button4 = ui.Button(title = 'off')
button4.center = (view.width*.8, view.height*.5)
button4.flex = 'LRTB'
button4.action = button_tapped
view.add_subview(button4)
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():
x = random.randint(0,3)
if x == 0:
turn_on(button)
elif x == 1:
turn_on(button2)
elif x == 2:
turn_on(button3)
else:
turn_on(button4)
scoreCount = increase_Score(scoreCount)
sleep(speed)
if speed >= 1.5:
speed-= .07
if game_Over_Alert():
all_Off()
play_Game()
play_Game()
view.close()