Ad

Removing And Deleting Appended Object - Python

- 1 answer

With the code below i can get the appended sprite to delete and remove from the list on the event of a click, but sadly this isnt what i need. I've been mucking around with it for hours trying to get it to automatically delete and remove from the list after it has looped through the movement class 15 times. (i can supply that if you need it to help me with this.

class projectilesGroup(object):

    def __init__(self, canvas):
        self.canvas = canvas
        self.projectiles = list()
        self.moving = True

    def clicked(self, event, projectile):
        print('clicked:', projectile),

        # remove oval from canvas        
        self.canvas.delete(projectile.oval)

        # remove enemy from list
        self.projectiles.remove(projectile)

    def add_new_projectile(self):
        print(len(self.projectiles))
        if len(self.projectiles) < 5:
            e = projectile(self.canvas)
            # stop new enemy if all enemies are stoped
            e.moving = self.moving
            self.canvas.tag_bind(e.oval, '<Button-1>', lambda event:self.clicked(event, e))
            self.projectiles.append(e)
        else:
            print("You have 5 spells active - I can't add more.")

thanks.

def move(self):
    if repcount < 15:
    if self.moving: # to stop root.after
        if self.direction == 1: # up
             self.y1 -= self.radius
             self.y2 -= self.radius
        elif self.direction == 2: # down
            self.y1 += self.radius
            self.y2 += self.radius
        elif self.direction == 3: # left
            self.x1 -= self.radius
            self.x2 -= self.radius
        elif self.direction == 4: # right
            self.x1 += self.radius
            self.x2 += self.radius
        self.canvas.coords(self.oval, self.x1, self.y1, self.x2, self.y2)
        repcount += 1
        root.after(20, self.move)
    else:
        print('done')
Ad

Answer

First, every object needs variable to count movements. When counter counts to 15 then object can remove itself.

Object can easely remove oval from canvas because it has self.oval and self.canvas.

Small problem can be to remove object from projectlist but you can give access to projectlist:

e = projectile(self.canvas, self.projectiles)

EDIT:it could be done this way

class ProjectilesGroup(object):

    # ...

    def clicked(self, event, projectile):
        print('clicked:', projectile),

        self.remove(projectile)

    def remove(self, projectile):

        # remove oval from canvas        
        self.canvas.delete(projectile.oval)

        # remove enemy from list
        self.projectiles.remove(projectile)

    def add_new_projectile(self):
        print(len(self.projectiles))
        if len(self.projectiles) < 5:
            e = Projectile(self, self.canvas) #parent and canvas

        # ...

class Projectile():

    def __init__(self, parent, canvas):
        self.parent = parent
        self.canvas = canvas

        # ... 


    def move(self):

        # .... 

        else:
            print('done')
            self.parent.remove(self)    

BTW: In document PEP8 there is sugesstion to use "CamelCase" class names - like Projectile and ProjectilesGroup. It is easer to recognize class in code. Some editors even use different color for CamelCase names (SO use blue color)

Ad
source: stackoverflow.com
Ad