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 :)