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.
[SOLVED] Moving variable
-
In this code:
from scene import * from random import * 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()) self.cells=Rect(self.x, self.y, 5, 5) global cells cells=self.cells print(cells, "object") def update(self): self.x += self.vx self.y += self.vy self.vx *= 0 self.vy *= 0 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) ellipse(*self.cells) class Intro(Scene): def setup(self): self.psize=16 self.psize=10 global plocx global plocy global newplocx global newplocy plocx=240 plocy=160 newplocx = 0 newplocy = 0 self.player = Rect(plocx, plocy, 20, 20) self.colour = Color(random(), random(), random()) self.particles = [] for p in xrange(100): self.particles.append(Particle(self.size)) def touch_began(self, touch): global x1 global y1 x1=touch.location.x y1=touch.location.y def touch_moved(self, touch): global plocx global plocy global newplocx global newplocy x=touch.location.x y=touch.location.y if x > x1: addx=x-x1 newplocx=plocx+addx if x < x1: subx=x-x1 newplocx=plocx+subx if y > y1: addy=y-y1 newplocy=plocy+addy if y < y1: suby=y-y1 newplocy=plocy+suby while newplocx != plocx and newplocy > plocx: plocx = plocx + 1 self.player = Rect(plocx, plocy, self.psize, self.psize) if plocx == newplocx: self.player = Rect(plocx, plocy, self.psize, self.psize) while newplocx != plocx and newplocx < plocx: plocx = plocx - 1 self.player = Rect(plocx, plocy, self.psize, self.psize) if plocx == newplocx: self.player = Rect(plocx, plocy, self.psize, self.psize) while newplocy != plocy and newplocy > plocy: plocy = plocy + 1 self.player = Rect(plocx, plocy, self.psize, self.psize) if plocy == newplocy: self.player = Rect(plocx, plocy, self.psize, self.psize) while newplocy != plocy and newplocy < plocy: plocy = plocy - 1 self.player = Rect(plocx, plocy, self.psize, self.psize) if plocy == newplocy: self.player = Rect(plocx, plocy, self.psize, self.psize) def draw(self): background(0, 0.05, 0.2) self.player = Rect(plocx, plocy, self.psize, self.psize) if not self.player.intersects(self.player): ellipse(*self.player) if self.player.intersects(self.player): self.newpsize=self.psize+1 self.psize=self.newpsize ellipse(*self.player) for p in self.particles: p.update() p.draw() run(Intro(), LANDSCAPE)
There is an object and a scene.
I need to move self.cells to the scene but when I store it in another variable, in the scene only the last variable shows up..
E.g
Object:
Self.cells;
A
B
C
D
In the scene only D will actually be moved.
When the code is run, you will se what I mean...
There is a print of self.cells and it had 100 values. How can I transfer all of them to the Scene?Thanks in advance!
-
Looking through through the code
At line 116 shoud the player be checking if it intersects with its selfif self.player.intersects(self.player):
or was this a typo?
Iterate overself.particles
and get the cells object from it
egdef draw(self): background(0, 0.05, 0.2) self.player = Rect(plocx, plocy, self.psize, self.psize) if not self.player.intersects(self.player): ellipse(*self.player) if self.player.intersects(self.player): #self.newpsize=self.psize+1 #self.psize=self.newpsize ellipse(*self.player) for p in self.particles: p.update() p.draw() cells = p.cells
EDIT
Only 'D' is being store as this is the only variable you have storedglobal cells cells = self.cells
-
You will be much happier without global variables. You have an ok class structure, just replace global xxx with self.xxx. This will expose some of your logic errors, as you think through how to do that.
You need to decide whether particles are responsible for detecting collisions, or whether the scene does it. If the scene does it, particles dont need plocx plocy, etc, so do not need globals at all. The scene then needs to have a check collisions function which is called per draw cycle, and iterates over the particles.
If your particles are eventually going to have an AI, then they will need access to the game state (ploc, and other particles), but you can do that by either passing a game state variable around, or saving a pointer back to the scene in each Particle.
-
You're a legend. Thanks so much!