Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello ā I'm @olemoritz on Twitter.
Help with Magic8Ball (scenes)
-
Magic8Ball is a game that gives you advice.
I've already made the game with 3 scenes.
The first scene (MyScene), says the name of the game.
The second scene (Help), gives instructions.
The last scene (Advice), gives the advice
However, I need help with making the code pick another random advice (answer).Thank you in advance!
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:
answer="Go for it!"
elif choice==2:
answer="No way, Jose!"
elif choice==3:
answer="I'm not sure.\nAsk me again."
elif choice==4:
answer="Fear of the\nunknown is\nwhat imprisons\nus."
elif choice==5:
answer="It would be\nmadness to\ndo that!"
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 = Trueclass 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): run(Advice())
import motion, scene
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()) def touch_ended(self, touch): run(Help())
run(MyScene())
</code></pre> -
Without changing to much, I would write this...
import ... answers = ["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 Advice(scene.Scene) ... def setup(self): self.choice=randint(0,6) ... def draw(self): ... scene.text(answers[self.choice].format(x, y, z), font_size=45, x=self.center.x, y=self.center.y)
You have to remove the global lines (choice and answer) and replace it with the list.
-
@brumm thanks I'll try it out, and let you know how it turns out xD!!
-
@brumm wouldn't
random.choice
make things simpler?import random . . . answers = [. . .] . . . ## where ever you find this useful def update_answer(self): . . . scene.text(random.choice(answers), . . .
This may not work for your code, but it is a more pythonic way.
-
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 usesrandom.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!", "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())
-
I had that prepared 3 hours ago but couldn't access wifi.
-
OMG THANK YOU SOOO MUCH!!!
-
That's an awesome effect you have going with the particles.
-
I also love the subtle color effect when tilting the device. Nicely done!
-
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.
-
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. -
@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. - a
-
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. -
I have a refactored 8ball that I will submit once the repo is ready.
-
Cool, can't wait to see it!
-
Add an empty Magic8Ball.py to the repo and I will submit a pull request.