forked from dayoogu/Python-Scripts
/
Magic8Ball.py
146 lines (126 loc) · 3.77 KB
/
Magic8Ball.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#coding: utf-8
import random, motion, scene
from scene import *
choices = [
"Go for it!",
"No way, Jose!",
"I'm not sure.\nAsk me again.",
"Fear of the\nunknown is\nwhat imprisons\nus.",
"It would be\nmadness to\ndo that!",
"Makes no\ndifference to\nme, do or don't\n- whatever.",
"Yes, I think on\nbalance, that is\nthe right choice.",
]
class Particle(object):
def __init__(self, wh):
self.w = wh.w
self.h = wh.h
self.x = random.randint(0, self.w)
self.y = random.randint(0, self.h)
self.vx = random.randint(-10, 20)
self.vy = random.randint(-10, 20)
self.colour = Color(random.random(), random.random(), random.random())
def update(self):
self.x += self.vx
self.y += self.vy
self.vx *= 0.98
self.vy *= 0.98
if self.x > self.w:
self.x = self.w
self.vx *= -1
if self.x < 0:
self.x = 0
self.vx *= -1
if self.y > self.h:
self.y = self.h
self.vy *= -1
if self.y < 0:
self.y = 0
self.vy *= -1
def draw(self):
fill(*self.colour)
rect(self.x, self.y, 8, 8)
class MyScene(Scene):
def setup(self):
self.particles = []
for p in xrange(200):
self.particles.append(Particle(self.size))
def draw(self):
background(0.00, 0.05, 0.20)
for p in self.particles:
p.update()
p.draw()
for t in self.touches.values():
for p in self.particles:
tx, ty = t.location.x, t.location.y
d = (p.x - tx)*(p.x - tx)+(p.y - ty)*(p.y - ty)
d = sqrt(d)
p.vx = p.vx - 5/d*(p.x-tx)
p.vy = p.vy - 5/d*(p.y-ty)
p.colour = Color(random.random(), random.random(), random.random())
s = 45 if self.size.w > 100 else 7
text('Welcome to\nMyMagic8Ball\n\n\n', 'Futura', s, *self.bounds.center().as_tuple())
t = 100 if self.size.w > 100 else 7
text('\n🎱', 'Futura', t, *self.bounds.center().as_tuple())
s = 27 if self.size.w > 100 else 7
text('\n\n\n\n\n\n\n\n\n\n\n\nBy: Adedayo Ogunnoiki', 'Futura', s, *self.bounds.center().as_tuple())
def touch_ended(self, touch):
run(Help())
use_motion = True
class Help(Scene):
def setup(self):
self.particles = []
for p in xrange(200):
self.particles.append(Particle(self.size))
def draw(self):
background(0.00, 0.05, 0.20)
for p in self.particles:
p.update()
p.draw()
for t in self.touches.values():
for p in self.particles:
tx, ty = t.location.x, t.location.y
d = (p.x - tx)*(p.x - tx)+(p.y - ty)*(p.y - ty)
d = sqrt(d)
p.vx = p.vx - 5/d*(p.x-tx)
p.vy = p.vy - 5/d*(p.y-ty)
p.colour = Color(random.random(), random.random(), random.random())
s = 45 if self.size.w > 100 else 7
text('Ask me for\nadvice, then\ntap the screen.\nOr click the ❎\nat the top\nright of the \nscreen to exit.', 'Futura', s, *self.bounds.center().as_tuple())
def touch_ended(self, touch):
global answer
answer = random.choice(choices)
run(Advice())
use_motion = True
class Advice(scene.Scene):
def __init__(self):
scene.run(self, frame_interval=0)
def setup(self):
self.center = self.bounds.center()
if use_motion:
motion.start_updates()
self.particles = []
for p in xrange(200):
self.particles.append(Particle(self.size))
def stop(self):
if use_motion:
motion.stop_updates()
def draw(self):
x,y,z = motion.get_attitude() if use_motion else scene.gravity()
r,g,b = abs(x), abs(y), abs(z)
scene.background(r, g, b)
scene.tint(1-r, 1-g, 1-b)
scene.text(answer.format(x, y, z), font_size=45, x=self.center.x, y=self.center.y)
for p in self.particles:
p.update()
p.draw()
for t in self.touches.values():
for p in self.particles:
tx, ty = t.location.x, t.location.y
d = (p.x - tx)*(p.x - tx)+(p.y - ty)*(p.y - ty)
d = sqrt(d)
p.vx = p.vx - 5/d*(p.x-tx)
p.vy = p.vy - 5/d*(p.y-ty)
p.colour = Color(random.random(), random.random(), random.random())
def touch_ended(self, touch):
run(Help())
run(MyScene())