Created
April 10, 2018 14:41
-
-
Save jsbain/55695af0195c686f580f1d1faa04eaf6 to your computer and use it in GitHub Desktop.
Untitled.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ui, random | |
from functools import partial | |
colors=['salmon','orange','yellow','turquoise','gray','white'] | |
N=4 #size if grid | |
def animate_button(button,num_rolls): | |
''' returns a callable, which when called will ui.animate to change title and color, with a recursive completion for a total of num_rolls''' | |
def ani(): | |
i=random.randrange(6) | |
button.title=str(i+1) | |
button.bg_color=colors[i] | |
def finish(): | |
pass | |
#button.bg_color='white' | |
# we need to call ui.animate(ani,duration,completion=ani) | |
if num_rolls==0: | |
return finish | |
else: | |
#dur=1 #all dice roll together | |
dur=0.5+random.randrange(4)/8. # just some variation in dice roll duration | |
return partial(ui.animate,ani,duration=dur,completion=animate_button(button,num_rolls-1)) | |
def button_pressed(sender): | |
'''roll all dice. this could be changed to only re-roll sender, or do something else based on sender.loc, etc ''' | |
for button in [b[j][i] for i in range(N) for j in range(N)]: | |
animate_button(button, 3)() #extra () actually calls the animation | |
#set up grid | |
v=ui.View(frame=(0,0,N*50,N*50)) | |
b=[[ui.Button(frame=(i*50,j*50,45,45)) for i in range(N)] for j in range(N)] | |
for i in range(N): | |
for j in range(N): | |
b[j][i].action=button_pressed | |
b[j][i].loc=(i,j) | |
value=random.randrange(6) | |
b[j][i].title=str(value+1) | |
b[j][i].bg_color=colors[value] | |
b[j][i].corner_radius=5 | |
b[j][i].line_width=1 | |
b[j][i].tint_color='blue' | |
v.add_subview(b[j][i]) | |
v.present() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment