hondros 25 Junior Poster

Okay, I'm creating a game where you are a red circle on a black background, and you can move it around the screen, and you have to pick up the other dots. It's more complex than that, but I seem to be having an issue. Some dots will go away when you're over them, which is what I want. However, sometimes they'll randomly disappear, and they won't disappear if you go over them. Also, none of the dots appear to be moving. I made a similar program that has the dots chase you, and I made this current game off that one. Anyways, here's the code I have, if you need it explained, I will. Can anyone help me debug it?

import pygame
from pygame.locals import *
import random

if not pygame.font:
    print 'Atention, there are no fonts.'

if not pygame.mixer:
    print 'Atention, there is no sound.'

pygame.init()

# The colors used here
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
orange = (255, 100, 0)
purple = (255, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)
colors = [green, blue, orange, purple, white]

# Window code
window_width = 800
window_height = 640
window = pygame.display.set_mode((window_width, window_height))

# How big the circles are
ray_circle = 10

# The player's code
# The list format: [xpos, ypos, color, (movementx, movementy)]
player = [(window_width/2),(window_height/2), red, [3, 3]]
pcircle = pygame.draw.circle(window, player[2], (player[0], player[1]), ray_circle)

# Setup the AI code
# -----------------
# Easy game
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(-4, 5):
            if (player[0] == ai[0] - x):
                if (player[1] == player[1] - x):
                    self.active.pop(self.active.index(ai))
            
    def _run(self, xpos, ypos, color, dif, playerx, playery):
        self.circle = circle = pygame.draw.circle(window, color, (xpos, ypos), ray_circle)
        # 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
            a = random.randint(0, 70)
            # Straight
            if a in range(0, 70):
                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(70, 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:
                xpos = window_width
            if ypos > window_height:
                ypos = window_height
            if xpos < 0:
                xpos = 0
            if ypos < 0:
                ypos = 0



# Initialize the AI
EasyAI = EasyAI()
ainum = 10
for x in range(0, ainum):
    EasyAI._create(random.randint(0, window_width), random.randint(0, window_height), random.choice(colors), [3])
for x in EasyAI.active:
    EasyAI._run(x[0], x[1], x[2], x[3], player[0], player[1])
    #circle = pygame.draw.circle(window, x[2], (x[0], x[1]), ray_circle)
print EasyAI.active
#for x in range(0, 10)
# Starting the code
pygame.display.flip()

pygame.key.set_repeat(1000, 100)

while True:
    print EasyAI.active
    for event in pygame.event.get():
        pass

    key_pressed = pygame.key.get_pressed()

    if key_pressed[K_LEFT]:
        player[0] -= player[3][0]

    if key_pressed[K_RIGHT]:
        player[0] += player[3][0]

    if key_pressed[K_UP]:
        player[1] -= player[3][1]

    if key_pressed[K_DOWN]:
        player[1] += player[3][1]
    
    if key_pressed[K_ESCAPE]:
        quit()

    if player[0] > window_width:
        player[0] = window_width
    if player[1] > window_height:
        player[1] = window_height
    if player[0] < 0:
        player[0] = 0
    if player[1] < 0:
        player[1] = 0

    window.fill(black)
    for ai in EasyAI.active:
        EasyAI._check(player, ai)

    for x in EasyAI.active:
        EasyAI._run(x[0], x[1], x[2], x[3], player[0], player[1])
        #circle = pygame.draw.circle(window, x[2], (x[0], x[1]), ray_circle)
    pcircle = pygame.draw.circle(window, player[2], (player[0], player[1]), ray_circle)
    pygame.display.flip()

EDIT: It appears that when the circles randomly disappear, the x of both are lining up. Hmm...
EDIT2: I have updated a block of code with this:

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

Now all the dots will disappear when I move. Perhaps there's something here?...
EDIT3: I believe I just found my problem. The third player should be ai xD
EDIT4: (Last edit) Okay, so the dots no longer have that problem, but they're still not moving at all. Any thoughts?

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.