Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7921939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:46:24+00:00 2026-06-03T16:46:24+00:00

I’m using Python to make a simple evolution simulator. There is a list called

  • 0

I’m using Python to make a simple evolution simulator.

There is a list called engine.All, that stores both the units/animals and food. I iterate through it, and if I run into an animal, I iterate through it once again to find if he is colliding with any of the food pieces.

If so, then I increase his energy, flag the food piece as eaten, and add it to the toRemove list, which I later use to remove the elements from engine.All.

This is the code, but with all redundant things removed:

def remove(l, who): #This should remove all the elements contained in who from the list l
    offset = 0

    for i in who:
        l.pop(i + offset)
        offset -= 1

    return l


for ob in engine.All:
    if ob.skip:
        continue;

    if ob.drawable:
       ob.draw()

    if isinstance(ob, Flatlander): #If it is an animal
        #Do speed stuff
        ob.energy -= decay #Lower its energy

        for i in range(len(engine.All)): #Iterate through the list again
            if collides(ob.pos, ob.r, engine.All[i].pos, engine.All[i].r) and isinstance(engine.All[i], Food) and ob.energy + engine.All[i].r < ob.r**2*3.14 and not engine.All[i].skip: #If it collides with a food piece, the food piece isn't about to be deleted and it can take the energy in (their maximum is defined by their radiuses)
                ob.energy += engine.All[i].r #Increase the his energy
                toRemove.append(i) #Add the food piece to the toRemove list
                engine.All[i].skip = True #Flag it as skipped

        if ob.energy < 0 and not ob.skip: #If its energy is 0 and if it isn't already about to be deleted
            toRemove.append(engine.All.index(ob)) #Add it to the toRemove list
            ob.skip = True #Flag it as skipped


engine.All = remove(engine.All, toRemove) 

I am almost certain that this doesn’t work, and that there is a better way of doing this. The reason I’m so certain is that sometimes, I see things just “flashing” on the screen – suddenly disappearing and appearing again. Also, there seem to be “ghost” animals (referred to as Flatlanders in the code), I conclude this because sometimes the food pieces are disappearing permanently.

Please, recommend a way of doing this that is more efficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:46:27+00:00Added an answer on June 3, 2026 at 4:46 pm

    If the skip attribute is only set for those objects which need to be removed, just make use of that:

    import math
    import itertools
    
    for ob in engine.All:
        if isinstance(ob, Flatlander):
            ob.energy -= decay
    
    for pair in itertools.combinations(engine.All, 2):
        fooditems = [x for x in pair if isinstance(x, Food) and not x.skip]
        animals = [x for x in pair if isinstance(x, Flatlander) and not x.skip]
        if not (fooditems and animals):
            continue
        animal = animals[0]
        food = fooditems[0]
        if collides(animal.pos, animal.r, food.pos, food.r):
            # This seems an odd calculation to me but I think it follows your code.
            if animal.energy + food.r < animal.r ** 2 * math.pi:
                # eating is feasible; the animal always eats the food if it can
                food.skip = True
                animal.energy += food.r # Not the area?
    
    for ob in engine.All:
        if isinstance(ob, Flatlander) and ob.energy < 0:
            ob.skip = True # dead
    
    # Remove dead things
    engine.All = [ob for ob in engine.All if not ob.skip]
    
    # Draw everything (no dead things remain)
    for ob in engine.All:
        if ob.drawable:
          ob.draw()
    

    Generally I would probably prefer to keep the food items and the flatlanders in separate lists.

    There are several technique for doing collision detection more efficiently (for example dividing the world into squares, and checking only those animals in the same or surrounding squares) but if you have only a few (say less than a few hundred) animals/food items this will not be needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.