# Help with Magic8Ball (scenes) browsing

• Magic8Ball is a game that gives you advice.
The first scene (MyScene), says the name of the game.
The second scene (Help), gives instructions.
However, I need help with making the code pick another random advice (answer).

Here's the code:

<pre><code>
#coding: utf-8
from random import *
import random
from scene import *
from random import *

choice=randint(1,7)
if choice==1:
elif choice==2:
elif choice==3:
elif choice==4:
elif choice==5:
elif choice==6:
answer="Makes no\ndifference to\nme, do or don't\n- whatever."
else:
answer="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 = randint(0, self.w)
self.y = randint(0, self.h)
self.vx = randint(-10, 20)
self.vy = randint(-10, 20)
self.colour = Color(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())

``````    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())
``````

import motion, scene
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())

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):
``````

import motion, scene

use_motion = True

def init(self):
scene.run(self, frame_interval=0)

``````def setup(self):
self.center = self.bounds.center()
if use_motion:
self.particles = []
for p in xrange(200):
self.particles.append(Particle(self.size))

def stop(self):
if use_motion:

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())

def touch_ended(self, touch):
run(Help())
``````

run(MyScene())
</code></pre>

• posted
0

I cleaned up your code just a bit, and solved the problem. Instead of your if statements in the beginning, I stored your answers in a list. Then, every time before `run(Advice`, it uses `random.choice()` to pick a random answer from the list.

``````#coding: utf-8
import random, motion, scene
from scene import *

choices = [
"Go for it!",
"No way, Jose!",
"Fear of the\nunknown is\nwhat imprisons\nus.",
"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):

use_motion = True

def __init__(self):
scene.run(self, frame_interval=0)

def setup(self):
self.center = self.bounds.center()
if use_motion:
self.particles = []
for p in xrange(200):
self.particles.append(Particle(self.size))

def stop(self):
if use_motion:

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())``````

• posted
0

I had that prepared 3 hours ago but couldn't access wifi.

• OMG THANK YOU SOOO MUCH!!!

• posted
0

That's an awesome effect you have going with the particles.

posted
0

I also love the subtle color effect when tilting the device. Nicely done!

• posted
0

You seem like you have past experience in programming, just not in Python. I could never have pulled off that particle effect, but your code looks to me like a beginner wrote it. There's a lot of redundancy.

• Yes, I had past experience with python and I'm actually really good at it but I'm still getting used to Pythonista's scenes, layers and everything.

• posted
0

Great! Good luck :)

• @Webmaster4o @omz @blmacbeth
I'm about to make another post on a small code I need help with. It would be nice if you guys could look at it.

• posted
0

@Pythonistapro777 It would be great if you could post this code into a GitHub repo.

To do so, go to `https://github.com/Pythonistapro777/Magic8Ball` and look for a line that has:

• a `green icon of arrows` on the left followed by
• a `Branch: master` button followed by
• `Magic8Ball / +`

If you click on that `+` you can add a new file to the repo (that part of the UI is not super intuitive!!)

Call the new file `Magic8Ball.py` and paste in your code. We can then send you code suggestions ("pull requests") which you are free to accept, reject, or ignore until you have the code for a perfect eight ball.

• Sure, and thanks for the instructions, because I couldn't seem to be able to upload anything I made. :)

• I'm gonna Finnish my current projects and upload them
with the Magic8Ball. Should be up by the end of this week.

• posted
0

I have a refactored 8ball that I will submit once the repo is ready.

• Cool, can't wait to see it!

• posted
0

Add an empty Magic8Ball.py to the repo and I will submit a pull request.

Internal error.

Oops! Looks like something went wrong!