-
Notifications
You must be signed in to change notification settings - Fork 8
/
lcd_4dice.py
100 lines (78 loc) · 2.15 KB
/
lcd_4dice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
import random
from time import sleep
import RPi.GPIO as GPIO
from lcd_display import lcd
# use Pi board pin numbers with GPIO.BOARD
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# see http://elinux.org/Rpi_Low-level_peripherals
# see http://pypi.python.org/pypi/RPi.GPIO
def flash(d):
GPIO.output(11, False)
sleep(d)
GPIO.output(11, True)
sleep(d)
disp = lcd()
disp.display_string("Raspi Dice 4 ", 1)
disp.display_string(" ", 2)
flash(0.2)
flash(0.2)
n = 0
while True:
go = not(GPIO.input(7))
stop = not(GPIO.input(12))
if go:
# wait for button release
while go:
go = not(GPIO.input(7))
disp.display_string("Raspi Dice 4 ", 1)
disp.display_string("Throwing ... ", 2)
# get a random number from 0 to 6 and convert to int for use with range
r = int(random.random()*6)+1
# print (r)
for x in range(r):
flash(0.2)
n+=1
if n==4:
r4 = r
total = r1+r2+r3+r4
if (total<10):
disp.display_string("Too Bad :---( ", 2)
elif (total<14):
disp.display_string("Ok do better :-|", 2)
elif (total==14):
disp.display_string("You're average ~", 2)
elif (total<14):
disp.display_string("Ok do better :-|", 2)
elif (total<14):
disp.display_string("Ok do better :-|", 2)
elif (total<18):
disp.display_string("Above Average:-)", 2)
else:
disp.display_string("Wow! Awesome :-D", 2)
n=0
text = "%d+%d+%d+%d = %d " % (r1, r2, r3, r4, total)
elif n==1:
r1 = r
text = "%d " % (r1)
elif n==2:
r2 = r
text = "%d+%d " % (r1, r2)
elif n==3:
r3 = r
text = "%d+%d+%d " % (r1, r2, r3)
disp.display_string(text, 1)
if n!=0: # 0 = finished game
disp.display_string("Throw again ... ", 2)
if stop:
disp.display_string("Goodbye ", 2)
flash(0.5)
GPIO.cleanup()
# print ("goodbye")
disp.clear()
exit()
sleep(0.1)