views:

2550

answers:

7

I just can't figure out what's wrong with this...

#!/usr/bin/env python
#
#       Bugs.py
#       

from __future__ import division

# No Module!
if __name__ != '__main__': 
    print "Bugs.py is not meant to be a module"
    exit()

# App
import pygame, sys, random, math
pygame.init()

# Configuration Vars
conf = {
    "start_energy": 50, 
    "food_energy": 25, 
    "mate_minenergy": 50, 
    "mate_useenergy": 35, 
    "lifespan": 300000
}

class Bugs:
    def __init__(self):
        self.list  = []
        self.timers= {}
        # Names / colors for sexes
        self.sex = ["Male", "Female"]
        self.color = ["#CBCB25", "#A52A2A"]
        # Bug info tracking
        self.bugid = 0
        self.buginfo = {"maxgen":0, "maxspeed":0}

    def new(self, x=False, y=False, sex=2, speed=0, generation=0, genes=[]):
        sex   = sex   if not sex   == 2 else random.randint(0,1)
        speed = speed if not speed == 0 else random.randint(1,3)
        # Create new bug object
        self.bugs.append(BugObj(sex, speed, generation, bugid, pygame.time.get_ticks, genes))
        # Make sure it has a timer
        if not self.timers[speed]:
            self.timers[speed] = 1
            pygame.time.set_timer(25 + speed, 1000 / speed)
        # Update info tracking variables
        if speed      > self.buginfo["maxspeed"]: self.buginfo["maxspeed"] = speed
        if generation > self.buginfo["maxgen"]  : self.buginfo["maxgen"]   = generation
        self.bugid += 1

    def speed_count(self, speed):
        a = 0
        for i in list[:]:
            if i.speed = speed:
                a += 1
        return a

class BugObj:
    def __init__(self, sex, speed, generation, bugid, born, genes):
        global conf
        self.sex        = sex
        self.speed      = speed
        self.generation = generation
        self.id         = bugid
        self.born       = born
        self.genes      = genes
        self.died       = -1
        self.energy     = conf["start_energy"]
        self.target     = "None"

    def update(self):
        global conf
        if self.age() > conf["lifespan"]:
            self.die()
        else:
            f = closest_food()
            m = closest_mate()
            # If there's a potential mate
            if m != 0 and self.energy > conf["mate_minenergy"]:
                if not self.rect.colliderect(m.rect):
                    self.move_toward(m)
                    self.target = "Mate: " + str(m.rect.center)
                else:
                    Bugs.mate(self, m)
                    self.target = "Mate: (Reached)"
            elif f != 0:
                if not self.rect.colliderect(f.rect):
                    self.move_toward(f)
                    self.target = "Food: " + str(f.rect.center)
                else:
                    self.eat(f)
                    self.target = "Food: (Reached)"
            else:
                self.target = "Resting"
            # Use energy
            self.energy -= 0

    def closest_food(self):
        pass

    def closest_mate(self):
        pass

    def age(self):
        if self.died != -1:
            return pygame.time.get_ticks - self.born
        else:
            return self.died - self.born

    def die(self):
        # Remove self from the list
        Bugs.list.remove(self)
        # Turn off timer
        if not Bugs.speed_count(self.speed):
            Bugs.timers[self.speed] = 0
            pygame.time.timers(25 + self.speed, 0)
        # Bye!
        del self

class Food:
    def __init__(self)
        pass

    def update(self)
        pass

# Update Loop
while 1:
    ev = pygame.event.wait()
    speed = ev.type - 25
    if speed > 24:
        for i in Bugs.list[:]:
            if i.speed = speed
                i.update()
                print "Updating bug #" + str(i.id)
    if speed == 0:
        Food.update()

I get the following every time:

  File "Bugs.py" line 53
    def new(self, x=False, y=False, sex=2, speed=0, generation=0, genes=[]):
                                                                           ^
Indentation Error: unindent does not match any outer indentation level
+12  A: 

It's possible that you have mixed tabs and spaces in your file. You can have python help check for such errors with

python -m tabnanny <name of python file>
mbarnett
I've tried that, gotBugs.py 31 ' self.sex = ["Male", "Female"]\n'checked the tabs/spacing and it seems to be fine
Rob
@Rob: If you got an error, then "seems to be fine" is a judgement that might be incorrect. Replaces all tabs with spaces.
S.Lott
Never heard of tabnanny, good tip
Steven Graham
@S.Lott - By "it seems to be fine" I meant I did that about 4 times before posting here on any lines that tabnanny gave me. It never returned any of the lines that actually had the problems.
Rob
+2  A: 

You probably have a mixture of spaces and tabs in your original source file. Replace all the tabs with four spaces (or vice versa) and you should see the problem straight away.

Your code as pasted into your question doesn't have this problem, but I guess your editor (or your web browser, or Stack Overflow itself...) could have done the tabs-to-spaces conversion without your knowledge.

RichieHindle
Rob
And this is just one reason I find significant whitespace a really bad idea.
Svante
I think the problem is allowing tab=n spaces in the language. You're fighting a lost battle if you fight significant whitespace, and there's a good reason for that.
chrispy
+1  A: 

Can you set your text editor to use spaces for tabs and see if that makes anything obvious?

Jim
My editor has been set to use spaces since I started using it
Rob
A: 

Maybe it's this part:

if speed      > self.buginfo["maxspeed"]: self.buginfo["maxspeed"] = speed
if generation > self.buginfo["maxgen"]  : self.buginfo["maxgen"]   = generation

Try to remove the extra space to make it look aligned.

Edit: from pep8

  Yes:

      x = 1
      y = 2
      long_variable = 3

  No:

      x             = 1
      y             = 2
      long_variable = 3

Try to follow that coding style.

Loïc Wolff
Besides, you have a lot of `if i = x`, it should be `if i == x`. And you missed some ":" at the end of some method
Loïc Wolff
I know, I'm somewhat sloppy and just catch it all later when I test :)
Rob
I looked through the code and I don't see `if i = x` anywhere. And `if i = x` shouldn't even work in Python! The compiler won't even let you do that! @dex, which line(s) are you talking about?
steveha
he wasn't putting in the actual variable names, don't worry it's in there...
Rob
A: 

I would recommend checking your indentation levels all the way through. Make sure that you are using either tabs all the way or spaces all the way, with no mixture. I have had odd indentation problems in the past which have been caused by a mixture.

Wayne Koorts
A: 

Don't forget the use of """ comments. These need precise indentation too (a 1/2 hr job for me resolving this damn error too!)

Pete