Okay, Im using Python 2.6, and the latest version of pygame. In my game, I have circles moving away from you, you have to catch them, and if you catch them, they disappear. The amount of circles can vary, and that is the issue I am having. As of now, I have the program create a bunch of circles in EasyAI().active, then in the loop, check if your circle is close to the AI circle, if so, then pop it from the EasyAI().active, then move the AI. Here's an excerpt of my code:

class EasyAI():

    active = []

    def _create(self, xpos, ypos, color, dif):
        self.active.append([xpos, ypos, color, dif])

    def _check(self, player, ai):
        for x in range(-8, 9):
            for y in range(-8, 9):
                if (player[0] == (ai[0] - x)):
                    if (player[1] == (ai[1] - y)):
                        self.active.remove(ai)

    def _run(self, xpos, ypos, color, dif, playerx, playery):
        self.circle = circle = pygame.draw.circle(window, color, (xpos, ypos), ray_circle)
        self.active.remove([xpos, ypos, color, dif])
        # If the circle is in the same position as the player:
        if (xpos != playerx) or (ypos != playery):
            # Sometimes, we don't want it just going in a straight line. It has a 2/3 chance of going straight, 1/3 random, 1/100 nowhere
            #### REALLY GOOD MEDIUM SETTING:
            # 0, 60; 60, 99
            a = random.randint(0, 100)
            # Straight
            if a in range(0, 60):
                nopx = (xpos - playerx)
                nopy = (ypos - playery)
                if nopx > 0:
                    xpos += random.choice(dif)
                if nopx <= 0:
                    xpos -= random.choice(dif)
                if nopy > 0:
                    ypos += random.choice(dif)
                if nopy <= 0:
                    ypos -= random.choice(dif)
            # Random location
            if a in range(10, 99):
                b = random.randint(0, 2)
                if b == 0:
                    xpos += random.choice(dif)
                if b == 1:
                    xpos -= random.choice(dif)
                c = random.randint(0, 2)
                if c == 0:
                    ypos += random.choice(dif)
                if c == 1:
                    ypos -= random.choice(dif)
            # It does nothing if other
            if xpos > window_width-5:
                xpos = window_width-5
            if ypos > window_height-5:
                ypos = window_height-5
            if xpos < 5:
                xpos = 5
            if ypos < 5:
                ypos = 5
            self.active.append([xpos, ypos, color, dif])


# Initialize the AI
Easy_AI = EasyAI()
ainum = 10
for x in range(0, ainum):
    Easy_AI._create(random.randint(0, window_width), random.randint(0, window_height), random.choice(colors), [2, 2])
for x in Easy_AI.active:
    Easy_AI._run(x[0], x[1], x[2], x[3], player[0], player[1])

Here's my issue. It runs fine, but most of the time, the circles are somewhat transparent, showing the black background. Sometimes, the circles will stay completely filled. If there are a large amount of circles, the game runs slower, and the mass of colors keep them from being transparent. So I guess what I am asking is if anyone has any other ideas on how to create variable AI's? and how to store them?
Any critique on my code would be nice :)

Are you blitting the whole screen or only the objects? I think there is an option in pygame not to redraw the whole screen, but only partially, this could help with the speed. A part of your problem could also be how you draw to the screen, so I’m curious about that.

About the transparency. You pick your colors from a random list called colors, right? A way colors could be defined is by using a rgba value (red, green, blue, alpha) this last value is the key. Alpha is a parameter that sets the transparence of an object and if this is picked at random you will have some transparent objects. (I don’t know what is in your colors and how pygame does the drawing of circles exactly, but you might check this.)

It might be a good idea to use more pygame functions because then you use code that has been improved looked at for many hours instead of trying to build from scratch. For example, to check if you player catches the circle in the _check function you could actually use Rect.collidepoint, because that is basically what you’re doing. (You would draw a rectangle around the players position and pass that to active, this rectangle object contains position, width and height and you can check with collidepoint if a certain coordinate is within this rectangle). But this is of course not critical, it will only speed up your development once you know these functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.