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 getting the variable from one scene to the other [SOLVED!]
-
My little brother pointed out that in the restart scene I should display their score and he Hughes score on the device. So I did. But there's an error when I try to get the self.pts variable (variable for score) in restart scene. Please help with that.
Thanks in advance!
Here's the code:
#coding: utf-8 from scene import * from random import * import time import sys 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 Intro(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 = 37 if self.size.w > 100 else 7 text('Welcome to\nGravity Block\n\n\n\n', 'Futura', s, *self.bounds.center().as_tuple()) t = 65 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\nBy: Adedayo Ogunnoiki', 'Futura', s, *self.bounds.center().as_tuple()) def touch_ended(self, touch): run(Help1()) import motion, scene use_motion = True class Help1(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 = 30 if self.size.w > 100 else 7 text('Tap the screen to flip gravity.\nAvoid being hit by any objects.\nEvery 500 points it will gradually\nspeed up. You must reach 10,000\npoints to win! Also, you can click\nthe ❎ at the top right of the screen\nto exit.', 'Futura', s, *self.bounds.center().as_tuple()) def touch_ended(self, touch): run(MyScene(), LANDSCAPE) class MyScene (Scene): def setup(self): self.x = 100 self.y = 0 self.scr_h = 510 self.scr_w = 218 self.flr = 101 #floor self.g = 0.5 #gravity self.etimer = 0.0 self.acel = 0 self.grav = True self.lw = self.scr_h self.ms = 6 #max speed self.n = 6 self.ex = self.scr_h - 32 self.ey = self.flr self.box_h = 32 self.box_2 = 0 self.button = Button(Rect(self.size.w/2-250, self.size.h/2--58, 500, 35)) self.button.background = Color(0,0.05,0.2) self.button.stroke = Color(0,0.05,0.2) self.button1 = Button(Rect(self.size.w/2-250, self.size.h/2-92, 500, 35)) self.button1.background = Color(0,0.05,0.2) self.button1.stroke = Color(0,0.05,0.2) #reset self.pts = 0 self.rtimer = 0 #collisions self.dead = False pass def draw(self): background(0, 0.5, 1) stroke(255,0,0) self.button.draw() self.button1.draw() #text tint(0,0,0,1) if self.dead == True: run(lose()) if self.dead == False: self.pts += 1 tint(1,1,1) text('Points: {}'.format(self.pts), x=self.size.w/3, y=self.size.h/3.5*3, font_size=40) text('You can click the ❎ at the\ntop right of the screen to exit.', x=self.size.w/2, y=self.size.h/11, font_size=20) #character self.player=Rect(self.x-40,self.y,32,32) fill(1,1,1) rect(*self.player) # Touch input: if not self.grav: self.acel += 1 self.obstacle=Rect(self.ex,self.ey,32,self.box_h) fill(1,1,1) rect(*self.obstacle) if self.pts < 500: self.spd = 0.1 if self.pts > 500 and self.pts < 1000: self.spd = 0.15 if self.pts > 1000 and self.pts < 1500: self.spd = 0.2 if self.pts > 1500 and self.pts < 2000: self.spd = 0.25 if self.pts > 2000 and self.pts < 2500: self.spd = 0.3 if self.pts > 2500 and self.pts < 3000: self.spd = 0.35 if self.pts > 3000 and self.pts < 3500: self.spd = 0.4 if self.pts > 3500 and self.pts < 4000: self.spd = 0.45 if self.pts > 4000 and self.pts < 4500: self.spd = 0.5 if self.pts > 4500 and self.pts < 5000: self.spd = 0.55 if self.pts > 5500 and self.pts < 6000: self.spd = 0.6 if self.pts > 6000 and self.pts < 6500: self.spd = 0.65 if self.pts > 6500 and self.pts < 6000: self.spd = 0.7 if self.pts > 6000 and self.pts < 6500: self.spd = 0.75 if self.pts > 6500 and self.pts < 7000: self.spd = 0.8 if self.pts > 7000 and self.pts < 7500: self.spd = 0.85 if self.pts > 7500 and self.pts < 8000: self.spd = 0.9 if self.pts > 8000 and self.pts < 8500: self.spd = 0.95 if self.pts > 8500 and self.pts < 9000: self.spd = 1 if self.pts > 9000 and self.pts < 9500: self.spd = 1.05 if self.pts > 9500 and self.pts < 10000: self.spd = 1.1 if self.pts > 10000: run(win()) if self.dead == False: self.etimer += self.spd self.ex -= self.etimer if self.ex < 0: self.ex = self.scr_h self.rand = randint(1,3) if self.rand==1: self.scr_h = 510 self.scr_w = 218 self.ex = self.scr_h - 32 self.ey = self.flr self.box_h = 32 self.box_2 = 0 if self.rand==2: self.scr_h = 510 self.scr_w = 218 self.ex = self.scr_h - 32 self.ey = self.flr self.box_h = 32 self.box_2 = 0 self.ey=self.ey+86 self.box_2--32 if self.rand==3: self.scr_h = 510 self.scr_w = 218 self.ex = self.scr_h - 32 self.ey = self.flr self.box_h = 32 self.box_2 = 0 self.ey=self.ey+43 self.box_2--32 self.dead = False self.etimer = 0 # Physics if self.dead == False: self.y += self.acel if self.y < self.flr: self.y = self.flr self.acel = 0 else: self.acel -= self.g if self.y > self.scr_w - 32: self.y = self.scr_w - 32 self.acel = 0 if self.acel > self.ms: self.acel = self.ms if self.acel < -self.ms: self.acel = -self.ms if self.player.intersects(self.obstacle): self.dead=True def touch_began(self, touch): self.grav = not self.grav class lose(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 = 90 if self.size.w > 100 else 7 text('You Lose!', 'Futura', s, *self.bounds.center().as_tuple()) def touch_ended(self, touch): run(help2()) class help2(Scene): def setup(self): self.particles = [] for p in xrange(200): self.particles.append(Particle(self.size)) def draw(self): background(0,0.05,0.2) s = 45 if self.size.w > 100 else 7 text('You can click the ❎\nat the top right of the\nscreen to exit.', 'Futura', s, *self.bounds.center().as_tuple()) 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(restart()) class restart(Scene): def setup(self): self.button = Button(Rect(self.size.w/2--30, self.size.h/2-80, 150, 150), 'Restart') self.button.background = Color(0,0.05,0.2) self.button.stroke = Color(0,0.05,0.2) self.button.image = 'White_Square' self.button.action = self.add_clicks self.add_layer(self.button) self.particles = [] for p in xrange(200): self.particles.append(Particle(self.size)) def add_clicks( sender): run(MyScene()) def draw(self): text('Points:\n{}'.format(self.pts), x=self.size.w/3, y=self.size.h/3.5*3, font_size=40) highscore=open('gblockhighscore.txt', 'r+') high=fhand.read() if self.pts>int(high): highscore.seek(0) highscore.write(self.pts) text('Best:\n{}'.format(int(highscore)), x=self.size.w/3, y=self.size.h/3.5*3, font_size=40) background(0,0.05,0.2) self.button.background = Color(0,0.05,0.2) self.button.draw() s = 45 if self.size.w > 100 else 7 tint(255,255,255) 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()) class Win(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 = 90 if self.size.w > 100 else 7 text('You Win!', 'Futura', s, *self.bounds.center().as_tuple()) def touch_ended(self, touch): run(restart()) run(Intro(), LANDSCAPE)
Sorry if the code's massive.
-
This is because self.pts is an attribute of MyScene, and therefore not in the namespace of restart. You can change this by storing points in a global variable upon death, that is read in the restart scene.
-
I understand what you mean but I'm unsure on where to add it. I tried:
if self.dead = True: global self.pts
Global can't have self and you said upon dead, but I don't know where that is.
I know I'm not being much help @Webmaster4o -
see https://github.com/tjferry14/Custom-Python-Modules/blob/master/HighScores.py. for ideas. if you want the high scores to persist, you need to write them to a file, then load them when you open the view. this module does most of what you want.
-
I've already got that part. I've got a different problem...
-
if you are just trying to transfer a variable, and want to use a glbal(rather than, say, a method of your new scene that is called by the old scene), you would say
global last_score last_score= self.pts
when the game ends, and then just use
last_score
directly in the new scene.