I have a program, in which there are some objects, which in the case are "blobs" or round beings, that through ids (ID numbers) can kill themselves, but I can not do that death actually happens, I tried to use it in the blobs class:
def death(self):
del self
Calling on 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 this method did not work for me, so far it has been the only method of killing objects I have tried, so any other issue is worth it.
Note: "did not work" in the sense that the object was not deleted / killed, ie the blob keeps appearing 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()
Blobs class:
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