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)


Sign In
Create Account

Guest_x42_*
Back to top










