Jump to content

why won't this work?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Guest_x42_*

Guest_x42_*
  • Guests
my python interpreter keeps on saying: SyntaxError: ('invalid syntax', ('ants.py', 219, 8, 'entities={}\n'))

the code is:
import pygame
import sys
import random
import math
from pygame.locals import *
pygame.init()
global width
global attk_dist
attk_dist=30
global height
width=500
height=500
global screen
screen=pygame.display.set_mode((width, height), 0, 32)
global search_mode
global scan_mode
global attack_mode
global entities
global id    
global retrieve_mode
retrieve_mode=4
attack_mode=3
scan_mode=2
search_mode=1
class spider(object):
    def __init__(self, x, y):
        self.x=x
        self.health=100
        self.y=y
        self.vec=vector(self.x, self.y, random.randrange(0, width), random.randrange(0, height))
        self.type="spider"
        self.image=pygame.image.load("/media/E6BC9486BC9452C3/Documents and Settings/noah/My Documents/My Pictures/spider.jpg").convert_alpha()
        self.alive=True
        self.speed=1.0
        self.clock=pygame.time.Clock()
    def go(self):
        time=self.clock.tick()
        x,y=self.vec.normalize()
        x=x*time
        y=y*time
        y=y*self.speed
        x=x*self.speed
        self.x=self.x+x
        self.y=self.y+y
        if self.x==self.vec.x_two and self.y==self.vec.y_two:
            self.vec.x_two=random.randrange(0, width)
            self.vec.y_two=random.randrange(0, height)
            self.vec.x_one=self.x
            self.vec.y_one=self.y
    def bite(self):
        self.health=self.health-1
        if self.health<=0:
            self.alive=False
            self.hold=2
    def render(self, surface):
        surface.blit(self.image, (self.x, self.y))
class leaf(object):
    def __init__(self, x, y):
        self.x=x
        self.y=y
        self.alive=True
        self.type="leaf"
        self.image=pygame.image.load("/media/E6BC9486BC9452C3/noah/My Documents/My Pictures/leaf.jpg").convert_alpha()
    def go(self):
        pass
    def render(self, surface):
        surface.blit(self.image, (self.x, self.y))
class vector(object):
    def __init__(self, x_one, x_two, y_one, y_two):
        if x_one>x_two:
            self.x_one=x_two
            self.x_two=x_one
            self.y_two=y_one
            self.y_one=y_two
        else:
            self.x_one=x_one
            self.x_two=x_two
            self.y_one=y_one
            self.y_two=y_two
    def __add__(self, rhs):
        vec=vector(self.x_one+rhs.x_one, self.y_one+rhs.y_one, self.x_two+rhs.x_two, self.y_two+rhs.y_two)
        return vec
    def __sub__(self, rhs):
        vec=vector(self.x_one-rhs.x_one, self.y_one-rhs.y_one, self.x_two-rhs.x_two, self.y_two-rhs.y_two)
        return vec
    def __mul__(self, rhs):
        vec=vector(self.x_one*rhs.x_one, self.y_one*rhs.y_one, self.x_two*rhs.x_two, self.y_two*rhs.y_two)
        return vec
    def __div__(self, rhs):
        vec=vector(self.x_one/rhs.x_one, self.y_one/rhs.y_one, self.x_two/rhs.x_two, self.y_two/rhs.y_two)
        return vec
    def get_magnitude(self):
        dx=self.x_two-self.x_one
        dy=self.y_two-self.y_one
        dy=dy**2
        dx=dx**2
        magnitude=math.sqrt(dx+dy)
        return magnitude
    def normalize():
        magnitude=self.get_magnitude()
        x=self.x_two-self.x_one
        y=self.y_two-self.y_one
        x=x/magnitude
        y=y/magnitude
        return (x,y)
    def get_heading():
        x=self.x_two-self.x_one
        y=self.y_two-self.y_one
        heading=y/x
        return heading
class ant(object):
    def __init__(self, x, y):
        self.vec=vector(x, y, radnom.randrange(0, width), random.randrange(0, height))
        self.speed=5
        self.type="ant"
        self.alive=True
        self.clock=pygame.time.Clock()
        self.mode=scan_mode
        self.image=pygame.image.load("/media/E6BC9486BC9452C3/Documents and Settings/noah/My Documents/My Pictures/ant.gif").convert_alpha()
    def go(self):
        if self.mode==search_mode:
            self.search()
        elif self.mode==scan_mode:
            self.scan()
        elif self.mode==attack_mode:
            self.attack()
        elif self.mode==retrieve_mode:
            self.retrieve()
        elif self.mode==return_mode:
            self.return_home()
    def scan(self):
        close=get_close_entities(self)
        for entity_id in close:
            entity=close[entity_id]
            if entity.type=="spider":
                vec2=vector(entity.x, entity.y, self.x, self.y)
                vec=vector(entity.x, entity.y, height/2, width/2)
                if vec.get_magnitude()<=attck_dist and vec2.get_magnitude()<=attck_dist and entity.alive:
                    self.mode=attack_mode
                    self.target=entity
                    return
            if entity.type=="leaf":
                vec=vector(entity.x, entity.y, self.x, self.y)
                if vec.get_magnitude<=attck_dist and entity.not_taken:
                    self.target=entity
                    self.mode=retrieve_mode
                    return
            else:
                self.mode=search_mode
    def attack(self):
        vec=vector(self.target.x, self.target.y, self.x, self.y)
        if vec.get_magnitude()>attk_dist:
            self.mode=scan_mode
            return
        else:
            x,y=vec.normalize()
            time=self.clock.tick()
            x=x*time
            y=y*time
            y=y*self.speed
            x=x*self.speed
            self.x=self.x+x
            self.y=self.y+y
            if self.x==self.target.x and self.y==self.target.y:
                self.target.bite()
            if self.target.health<=0:
                self.mode=scan_mode
                return
    def retrieve(self):
        vec=vector(self.target.x, self.target.y, self.x, self.y)
        if self.target.not_taken:
            x,y=vec.normalize()
            time=self.clock.tick()
            x=x*time
            y=y*time
            y=y*self.speed
            x=x*self.speed
            self.x=self.x+x
            self.y=self.y+y
            if self.x==self.target.x and self.y==self.target.y:
                self.target.not_taken=False
                self.mode=return_mode
                return
    def return_home(self):
        vec=vector(self.x, self.y, width/2, height/2)
        x,y=vec.normalize
        time=self.clock.tick()
        x=x*time
        y=y*time
        x=x*self.speed
        y=y*self.speed
        self.x=self.x+x
        self.y=self.y+y
        self.target.x=self.x
        self.target.y=self.y
        if self.x==width/2 and self.y==height/2:
            self.mode=scan_mode
            return
    def search(self):
        time=self.clock.tick()
        x,y=self.vec.normalize()
        x=x*time
        y=y*time
        y=y*speed
        x=x*speed
        self.x=self.x+x
        self.y=self.y+y
        vect=vector(self.x, self.y, self.vec.x_two, self.vec.y_two)
        if vect.get_magnitude<=0:
            self.vec.x_one=self.x
            self.vec.y_one=self.y
            self.vec.x_two=random.randrange(0, width)
            self.vec.y_two=random.randrange(0, height)
        self.mode=scan_mode
        return
    def render(self, surface):
        surface.blit(self.image, (self.x, self.y)
entities={}#THE PROBLEM IS RIGHT HERE
id=0
def add_entity(type, x=random.randrange(0, width), y=random.randrange(0, height)):
    if type=="leaf":
        new=leaf(x, y)
        entities[id]=new
        id=id+1
    if type=="spider":
        new=spider(x, y)
        entities[id]=new
        id=id+1
    if type=="ant":
        new=ant(x, y)
        entities[id]=new
        id=id+1
def get_close_entities(client):
    close={}
    num=0
    for entity_id in entities:
        entity=entities[entity_id]
        dx=0
        if client.x<entity.x:
            dx=entity.x-client.x
        else:
            dx=client.x-entity.x
        if client.y<entity.y:
            dy=entity.y-client.y
        else:
            dy=client.y-entity.y
        if dx<=attck_dist and dy<=attck_dist:
            close[num]=entity
            num=num+1
    return close
count=0
while count<=12:
    add_entity("leaf")
    count=count+1
count=0
while count<=20:
    add_entity("ant")
    count=count+1
count=0
while count<=15:
    add_entity("spider")
    count=count+1
delete=[]
while True:
    for event in pygame.events.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
        if event.type==MOUSEBUTTONDOWN:
            x,y=pygame.mouse.get_pos()
            a,b,c=pygame.mouse.get_pressed()
            if a:
                add_entity("ant", x, y)
            if b:
                add_entity("leaf", x, y)
            if c:
                add_entity("spider", x, y)
    for entity_id in entities:
        entity=entities[entity_id]
        entity.go()
    for entity_id in entities:
        entity=entities[entity_id]
        if not entity.alive:
            entity.hold=entity.hold-1
            if entity.hold<=0:
                delete.append(entity_id)
    for deletable in delete:
        del entities[deletable]
    screen.fill((0, 0, 0))
    screen.lock()
    pygame.draw.circle(screen, (0, 0, 255), (width/2, height/2), attck_dist)
    pygame.draw.circle(screen, (0, 0, 0), (width/2, height/2), attck_dist)
    screen.unlock()
    for entity_id in entities:
        entity=entities[entity_id]
        entity.render(screen)
    pygame.display.update()

Edited by WingedPanther, 06 June 2010 - 03:33 AM.
add code tags (the # button)


#2
Guest_x42_*

Guest_x42_*
  • Guests
ugghhh, this is so f*cking stupid!, it should have put the spacing in!

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

x42 said:

ugghhh, this is so f*cking stupid!, it should have put the spacing in!

Edit your original post and add the code between CODE tags. What are the specific line(s) which is failing the program to run?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

x42 said:

ugghhh, this is so f*cking stupid!, it should have put the spacing in!
It only preserves spaces if you use the code tags (the # button). I've added them for you.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
make sure that the line of entities is spaced to under the def of the method, since python doesn;t use {} for blocks it uses spaces and dont forget the return i guess
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#6
Somelauw

Somelauw

    Newbie

  • Members
  • PipPip
  • 18 posts
    def render(self, surface):
        surface.blit(self.image, (self.x, self.y) # (missing bracket)
entities={}#THE PROBLEM IS RIGHT HERE # (no, it's not)

It's the line above the error. You forgot a bracket!

#7
Guest_x42_*

Guest_x42_*
  • Guests
wha?, how?, but it wasn't!?, huh?, how did this happen?, the last I saw it, it was all to the left because stupid HTML doesn't let you put more than one space!

#8
Guest_x42_*

Guest_x42_*
  • Guests
thanks

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I added the code tags for you.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
Guest_x42_*

Guest_x42_*
  • Guests
but I am the one who put it up, so doesn't that mean that I am the only one can edit it?, hmmm, that isn't exactly how I would have made things if I were the one who made the forum, but I never made a forum, so I guess there must be some sort of reason for having it like that

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
High level admins/mods can edit other people's posts as well.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
It was meant as a transparent fix on formatting due to limitations of the forum's ability to preserve space, there's no need to worry about it, it was for integrity of the question as you would get less answers otherwise if it were not formatted in a runnable state (Being python).
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.