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] Help with erasing a Rect()
-
I'm coming close to the end of making my Agar.io game, which feels great!
Here's the 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 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 xmin=215 xmax=265 ymin=140 ymax=190 while xmax > plocx and newplocx > plocx: plocx = plocx + 1 self.player = Rect(plocx, plocy, 16, 16) while xmin < plocx and newplocx < plocx: plocx = plocx - 1 self.player = Rect(plocx, plocy, 16, 16) while ymax > plocy and newplocy > plocy: plocy = plocy + 1 self.player = Rect(plocx, plocy, 16, 16) while ymin < plocy and newplocy < plocy: plocy = plocy - 1 self.player = Rect(plocx, plocy, 16, 16) def draw(self): background(0, 0.05, 0.2) self.player = Rect(plocx, plocy, self.psize, self.psize) for p in self.particles: p.update() p.draw() cells = p.cells if not self.player.intersects(cells): ellipse(*self.player) if self.player.intersects(cells): self.newpsize=self.psize+1 self.psize=self.newpsize ellipse(*self.player) run(Intro(), LANDSCAPE)
When the player eats a cell, I want the cell to be erased. I don't want it changing location because it might ruin what I have in mind later. Could someone please help with this?
It's a Rect() so I don't know how it will work.Thanks in advance!
-
Look at python lists namely the part on
list.remove
Also as @JonB has stated remove global variables where they are not required
Something to consider as wellif not self.player.intersects(cells): ellipse(*self.player) if self.player.intersects(cells): self.newpsize=self.psize+10 self.psize=self.newpsize ellipse(*self.player)
can become
if self.player.intersects(cells): self.newpsize=self.psize+10 self.psize=self.newpsize ellipse(*self.player)
-
OK, I removed the useless global variables... However, its not a list - it's a Rect(). Maybe you could help me with making it a list instead. If it can't be erased.
-
self.particles
is a list. if an item in it intersects then remove it. You are already checking if items intersect, tweaking the code a bit will allow you to remove it. -
That's what I need help with... @Cethric
Sorry if I'm being a pain.
-
In the iteration of
self.particles
>for p in self.particles
, there is a check for intersectionif self.player.intersects(cells):
wherecells = p.cells
. If this check isTrue
then removep
from the listself.particles
-
So from my code, I just have to fun a way to remove p.cells, or do I have to check something else first? @Cethric
-
This would be a whole lot easier if there was a repo where pull requests could be submitted to fix the code. Everyone needs a common codebase to be fixing rather then English prose corrections to Python code.
-
self.particles
is a list and the variablep
is an item from the list. the functionlist.remove
removes an item from the list. Hence in your code if a cell intersects the remove it from the list.
ieif self.player.intersects(cells): ... self.particles.remove(p)
I apologise in advanced if I come across being annoyed it is not how I intend to come across.
-
No, its fine. Also, thanks a bunch!
-
A different suggestion: try using layers. The advantage of layers is that most of the "hard work" is done for you, such as adding and removing objects, positioning objects relative to their parent, rendering them properly, etc. You also don't need to keep track of all objects manually in a list somewhere, instead you add them as sublayers of another layer, and they will be drawn automatically.
Consider making a backup copy of your script before changing it to use layers, you might run into problems and want to look at your original code.