1
I’m with a program, in which there are some objects, which in this case are "blobs" or round beings, which through ids(identification numbers) can kill each other, but I can’t make the death actually happen, I tried to use in the class of blobs:
def death(self):
del self
Calling in the code:
if hunter_pos == target_pos and hunter_id != target_id:
blob.death()
Being hunter_pos the position of the "hunter", and hunter_id the identification number of the "hunter", the same for the target, or "target".
But that method didn’t work for me, so far it was the only method of killing objects I tried, so any other dirt is worth.
Obs:"did not work", in the sense that the object was not deleted/dead, ie the blob continues to appear on the screen.
Code :
import pygame
import random
import time
from blob import Blob
game_display = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Blob World')
clock = pygame.time.Clock()
def draw_environment(blob_list):
game_display.fill((255, 255, 255))
for blob_dict in blob_list:
for blob_id in blob_dict:
blob = blob_dict[blob_id]
hunter_pos = blob.position()
hunter_id = blob_id
for blob_id in blob_dict:
blob = blob_dict[blob_id]
target_pos = blob.position()
target_id = blob_id
if hunter_pos == target_pos and hunter_id != target_id:
blob.death()
pygame.draw.circle(game_display, blob.color, [blob.x, blob.y], blob.size)
blob.move()
blob.check_boundaries()
pygame.display.update()
def main():
blue_blobs = dict(enumerate([Blob((0, 0, 255), 800, 600, 8, 4, 5, -5) for i in range(6)]))
red_blobs = dict(enumerate([Blob((255, 0, 0), 800, 600, 8, 4, 5, -5) for i in range(5)]))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment([blue_blobs, red_blobs])
clock.tick(60)
if __name__ == '__main__':
main()
Class of blobs:
import random
class Blob:
def __init__(self, color, x_boundary, y_boundary, size_max, size_min, mov_max, mov_min):
self.x = random.randrange(0, x_boundary)
self.y = random.randrange(0, y_boundary)
self.size = random.randrange(size_min, size_max)
self.color = color
self.x_boundary = x_boundary
self.y_boundary = y_boundary
self.mov_min = mov_min
self.mov_max = mov_max
def move(self):
self.x += random.randrange(self.mov_min, self.mov_max)
self.y += random.randrange(self.mov_min, self.mov_max)
def check_boundaries(self):
if self.x < 0: self.x = 0
elif self.x > self.x_boundary: self.x = self.x_boundary
if self.y < 0: self.y = 0
elif self.y > self.y_boundary: self.y = self.y_boundary
def position(self):
position_total = self.x, self.y
return position_total
def size(self):
return self.size
def death(self):
del self
What do you mean "it didn’t work"? Also, put a [mcve] for us, because you can’t understand what is happening without context.
– Pablo Almeida
Enter your complete code, please. How is the object defined
blob
? Wouldn’t it be something liketarget.death()
in that case?– Woss
You are confusing
del
with thedelete
C++ (object destroyer).– Klaider