Пример #1
0
    def update(self):

        self.prevpos = V.vector(self.rect.center).copy()

        self.state.execute(self)

        ## Add a tiny force toward the center of the field
        if False:  #self.state != S.Wait():
            center = V.vector(pygame.display.get_surface().get_rect().center)
            towardcenter = center - self.rect.center
            V.normalize_ip(towardcenter)
            self.steering += 0.5 * towardcenter

        self.velocity += FORCE_PER_TICK * self.steering / (self.mass)
        V.truncate(self.velocity, self.maxspeed)

        speed = V.length(self.velocity)
        if speed > 0.001:
            self.heading = V.normalize(self.velocity)
        self.rect.center += self.velocity
        self.depth = -self.rect.centery

        #determine which image direction to use, based on heading and velocity:
        if speed >= 0.001:
            small = 0.382  # sin(22.5 degrees)
            big = 0.923  # cos(22.5 degrees)
            x, y = self.heading
            if y >= big: self.dir = 's'
            elif small <= y:
                if x > 0: self.dir = 'se'
                else: self.dir = 'sw'
            elif -small <= y:
                if x > 0: self.dir = 'e'
                else: self.dir = 'w'
            elif -big <= y:
                if x > 0: self.dir = 'ne'
                else: self.dir = 'nw'
            else: self.dir = 'n'
        else:
            self.velocity[0] = self.velocity[1] = 0.0

    #image action:  stopped or moving?
        if speed < 0.001:
            self.aspeed = 0.5
            action = 'looking'
        else:
            self.aspeed = 0.2 * speed
            action = 'walking'

        self.images = Images.get(self.name)[self.dir][action]
        self.nframes = len(self.images)

        #advance animation frame
        self.frame = self.aspeed + self.frame
        while self.frame >= self.nframes:
            self.frame -= self.nframes
        self.image = self.images[int(self.frame)]
Пример #2
0
    def update(self):

        self.prevpos = V.vector(self.rect.center).copy()

        self.state.execute(self)
        
        ## Add a tiny force toward the center of the field
        if False:#self.state != S.Wait():
            center = V.vector(pygame.display.get_surface().get_rect().center)
            towardcenter = center - self.rect.center
            V.normalize_ip(towardcenter)
            self.steering += 0.5*towardcenter      
        
        self.velocity += FORCE_PER_TICK*self.steering/(self.mass)
        V.truncate(self.velocity, self.maxspeed)

        speed = V.length(self.velocity)
        if speed > 0.001:
            self.heading = V.normalize(self.velocity)
        self.rect.center += self.velocity
        self.depth = -self.rect.centery
        
    #determine which image direction to use, based on heading and velocity:
        if speed >= 0.001:
            small = 0.382 # sin(22.5 degrees)
            big = 0.923  # cos(22.5 degrees)
            x,y = self.heading
            if y >= big: self.dir = 's'
            elif small <= y:
                if x > 0: self.dir = 'se'
                else: self.dir = 'sw'
            elif -small <= y:
                if x > 0: self.dir = 'e'
                else: self.dir = 'w'
            elif -big <= y:
                if x > 0: self.dir = 'ne'
                else: self.dir = 'nw'
            else: self.dir = 'n'
        else:
            self.velocity[0] = self.velocity[1] = 0.0
        
    #image action:  stopped or moving?
        if speed < 0.001: 
            self.aspeed = 0.5
            action = 'looking'
        else: 
            self.aspeed = 0.2*speed
            action = 'walking'
            
        self.images = Images.get(self.name)[self.dir][action]
        self.nframes = len(self.images)
        
    #advance animation frame
        self.frame = self.aspeed+self.frame
        while self.frame >= self.nframes: self.frame -= self.nframes
        self.image = self.images[int(self.frame)]
Пример #3
0
    def __init__(self, name, pos, maxspeed=PLAYER_MAXSPEED, screen=None):
        AutoObject.__init__(self, pos)

        # Image and rect vars
        self.name = name
        self.images = Images.get(self.name)['e']['looking']
        self.imagesize = self.images[0].get_size()
        w, h = self.imagesize
        self.rect = pygame.Rect(0, 0, Player.wfrac * w, Player.hfrac * h)
        self.rect.center = V.vector(pos)

        # Physics vars
        self.obstacles = []
        self.state = S.Wait()

        self.maxspeed = maxspeed
        self.maxforce = maxspeed * 0.1

        self.velocity = V.vector(0, 0)
        self.steering = V.vector(0, 0)
        randang = random.uniform(0, 2 * math.pi)
        x, y = math.cos(randang), math.sin(randang)
        self.heading = V.vector(x, y)
        self.dir = 's'

        # Animation vars
        self.depth = 0
        self.nframes = len(self.images)
        self.frame = random.randint(0, self.nframes - 1)
        self.mass = 1.0

        # Vars that should be in the state (IMHO):
        wanderangle = random.uniform(0, 2 * math.pi)
        self.wanderpoint = V.vector(math.cos(wanderangle),
                                    math.sin(wanderangle))
        V.normalize_ip(self.wanderpoint)

        self.seektarget = None
        self.seektarget2 = None
Пример #4
0
    def __init__(self, name, pos, maxspeed=PLAYER_MAXSPEED, screen=None):
        AutoObject.__init__(self, pos)

        # Image and rect vars
        self.name = name
        self.images = Images.get(self.name)['e']['looking']
        self.imagesize = self.images[0].get_size()
        w,h = self.imagesize
        self.rect = pygame.Rect(0, 0, Player.wfrac*w, Player.hfrac*h)
        self.rect.center = V.vector(pos)

        # Physics vars
        self.obstacles = []
        self.state = S.Wait()
        
        self.maxspeed = maxspeed
        self.maxforce = maxspeed*0.1

        self.velocity = V.vector(0,0)
        self.steering = V.vector(0,0)
        randang = random.uniform(0, 2*math.pi)
        x,y = math.cos(randang), math.sin(randang)
        self.heading = V.vector(x,y)
        self.dir = 's'

        # Animation vars
        self.depth = 0
        self.nframes = len(self.images)
        self.frame = random.randint(0,self.nframes-1)
        self.mass = 1.0

        # Vars that should be in the state (IMHO):
        wanderangle = random.uniform(0, 2*math.pi)
        self.wanderpoint = V.vector(math.cos(wanderangle), math.sin(wanderangle))
        V.normalize_ip(self.wanderpoint)

        self.seektarget = None
        self.seektarget2 = None
Пример #5
0
 def __init__(self, pos):
     AutoObject.__init__(self, pos)
     self.image = Images.get('square')
     self.rect = self.image.get_rect()
     self.rect.topleft = pos
     self.imagesize = self.rect.size
Пример #6
0
def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
    global ticks, show_world
    #Initialize Everything
    pygame.init()
    font = pygame.font.Font(None, 36)
    textcolor = (100, 255, 255)
    text = font.render("Autonomous Motion Demo", 1, textcolor)
    screen = pygame.display.set_mode(screen_size)  #, FULLSCREEN)
    pygame.display.set_caption('Autonomous Motion Demo')
    pygame.mouse.set_visible(False)
    world = pygame.Surface(world_size)
    world = world.convert()

    #Create The Backgound
    Images.load_image('grass.jpg')
    background = pygame.Surface(world_size)
    background = background.convert()
    background_image = Images.get('grass')
    for x in range(0, background.get_width(), background_image.get_width()):
        for y in range(0, background.get_height(),
                       background_image.get_height()):
            background.blit(background_image, (x, y))

#Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()

    #Prepare Game Objects
    clock = pygame.time.Clock()

    Images.load_image('square.png')
    Images.load_character('dino', os.path.join("T_dino_red",
                                               "dino red bitmaps"))
    Images.load_character('ogre', os.path.join("T_ogre", "ogre 96x bitmaps"))

    w, h = world_size

    # Blocks
    if (True):
        b = Block((0, 0))
        bw, bh = b.rect.size

        for x in range(0, w, bw):
            blocks.add(Block((x, 0)))
            blocks.add(Block((x, h - bh)))
        for y in range(bh, h - bh, bh):
            blocks.add(Block((0, y)))
            blocks.add(Block((w - bw, y)))

    # Mobs

    for d in range(ndinos):
        add_mob(dinos)
    for o in range(nogres):
        add_mob(ogres)
    mobs.add(dinos)
    mobs.add(ogres)

    everything.add(mobs)
    everything.add(blocks)
    dtext = "Wait"
    for o in ogres:
        o.changeState(S.User())
    print """
c: slow clock
H: show heading
F12: show world
b: show box
0: wait
w: wander
s: seek
p: pursuit
f: flee
e: evasion
"""

    #Main Loop
    while 1:
        clock.tick(ticks)

        #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return
                elif event.key == K_c:
                    if ticks < 30: ticks = 30
                    else: ticks = 10
                elif event.key == K_F12:
                    show_world = not show_world
                    if show_world:
                        screen = pygame.display.set_mode(
                            world_size)  #, FULLSCREEN)
                    else:
                        screen = pygame.display.set_mode(
                            screen_size)  #, FULLSCREEN)
                elif event.key == K_h:
                    for m in mobs:
                        m.showheading = not m.showheading
                elif event.key == K_b:
                    for m in everything:
                        m.showbox = not m.showbox
                elif event.key == K_0:
                    dtext = "Wait"
                    text = font.render(dtext, 1, textcolor)
                    for m in dinos:
                        m.changeState(S.Wait())
                elif event.key == K_w:
                    dtext = "Wander"
                    text = font.render(dtext, 1, textcolor)
                    for m in dinos:
                        m.changeState(S.Wander())
                elif event.key == K_s:
                    dtext = "Seek"
                    text = font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Seek())
                elif event.key == K_p:
                    dtext = "Pursuit"
                    text = font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Pursuit())
                elif event.key == K_f:
                    dtext = "Flee"
                    text = font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Flee())
                elif event.key == K_e:
                    dtext = "Evasion"
                    text = font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Evasion())

    #Update sprites
        mobs.update(everything)

        #Draw Everything
        world.blit(background, (0, 0))
        everything.draw(world)
        screen.fill((0, 0, 0))
        if show_world:
            screen.blit(world, (0, 0))
        else:
            screen.blit(world, (0, 0),
                        ogres.sprites()[0].rect.inflate(screen_size))
        screen.blit(text, (32, 32))
        pygame.display.flip()
Пример #7
0
def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
    global ticks, show_world
#Initialize Everything
    pygame.init()
    font = pygame.font.Font(None, 36)
    textcolor = (100, 255, 255)
    text = font.render("Autonomous Motion Demo", 1, textcolor)
    screen = pygame.display.set_mode(screen_size)#, FULLSCREEN)
    pygame.display.set_caption('Autonomous Motion Demo')
    pygame.mouse.set_visible(False)
    world = pygame.Surface(world_size)
    world = world.convert()
    
#Create The Backgound
    Images.load_image('grass.jpg')
    background = pygame.Surface(world_size)
    background = background.convert()
    background_image = Images.get('grass')
    for x in range(0,background.get_width(),background_image.get_width()):
        for y in range(0,background.get_height(),background_image.get_height()):
            background.blit(background_image,(x,y))

#Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()
    
#Prepare Game Objects
    clock = pygame.time.Clock()

    Images.load_image('square.png')
    Images.load_character('dino', os.path.join("T_dino_red", "dino red bitmaps"))
    Images.load_character('ogre', os.path.join("T_ogre", "ogre 96x bitmaps"))

    w,h = world_size

    # Blocks
    if (True):
        b = Block((0,0))
        bw, bh = b.rect.size

        for x in range(0,w,bw):
            blocks.add(Block((x,0)))
            blocks.add(Block((x,h-bh)))
        for y in range(bh,h-bh,bh):
            blocks.add(Block((0,y)))
            blocks.add(Block((w-bw,y)))

    # Mobs
    
    for d in range(ndinos):  add_mob(dinos)
    for o in range(nogres):  add_mob(ogres)
    mobs.add(dinos)
    mobs.add(ogres)

    everything.add(mobs)
    everything.add(blocks)
    dtext = "Wait"
    for o in ogres:
        o.changeState(S.User())

#Main Loop
    while 1:
        clock.tick(ticks)

    #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return
                elif event.key == K_c:
                    if ticks < 30 : ticks = 30
                    else: ticks = 10
                elif event.key == K_F12:
                    show_world = not show_world
                    if show_world:
                        screen = pygame.display.set_mode(world_size)#, FULLSCREEN)
                    else:
                        screen = pygame.display.set_mode(screen_size)#, FULLSCREEN)
                elif event.key == K_h:
                    for m in mobs:
                        m.showheading = not m.showheading
                elif event.key == K_b:
                    for m in everything:
                        m.showbox = not m.showbox
                elif event.key == K_0:
                    dtext = "Wait"
                    text = font.render(dtext, 1, textcolor)
                    for m in dinos:
                        m.changeState(S.Wait())
                elif event.key == K_w:
                    dtext = "Wander"
                    text = font.render(dtext, 1, textcolor)                    
                    for m in dinos:
                        m.changeState(S.Wander())
                elif event.key == K_s:
                    dtext = "Seek"
                    text = font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Seek())
                elif event.key == K_p:
                    dtext = "Pursuit"
                    text = font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Pursuit())
                elif event.key == K_f:
                    dtext = "Flee"
                    text = font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Flee())
                elif event.key == K_e:
                    dtext = "Evasion"
                    text= font.render(dtext, 1, textcolor)
                    pick_on(ogres, dinos, S.Evasion())
                                               
    #Update sprites
        mobs.update(everything)

    #Draw Everything
        world.blit(background, (0,0))
        everything.draw(world)
        screen.fill((0,0,0))
        if show_world:
            screen.blit(world, (0,0))
        else:
            screen.blit(world, (0,0), ogres.sprites()[0].rect.inflate(screen_size))
        screen.blit(text, (32,32))
        pygame.display.flip()
Пример #8
0
def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
    #Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode(screen_size, FULLSCREEN)
    pygame.display.set_caption('Wait!')
    pygame.mouse.set_visible(True)

    #Create The Backgound
    Images.load_image('grass.jpg')
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background_image = Images.get('grass')
    for x in range(0, background.get_width(), background_image.get_width()):
        for y in range(0, background.get_height(),
                       background_image.get_height()):
            background.blit(background_image, (x, y))

#Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()

    #Prepare Game Objects
    clock = pygame.time.Clock()

    Images.load_character('dino', os.path.join("T_dino_red",
                                               "dino red bitmaps"))
    Images.load_character('ogre', os.path.join("T_ogre", "ogre 96x bitmaps"))

    w, h = screen.get_size()

    for d in range(ndinos):
        dinos.add(Dino(random_pos()))
    for o in range(nogres):
        ogres.add(Ogre(random_pos()))

    everybody.add(dinos)
    everybody.add(ogres)

    for e in everybody:
        e.changeState(S.Wander())

    #Main Loop
    while 1:
        clock.tick(ticks)

        #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return
            elif event.type == MOUSEBUTTONDOWN and event.button == 1:
                for e in everybody:
                    e.showheading = not e.showheading
            elif event.type == MOUSEBUTTONDOWN and event.button == 2:
                for e in everybody:
                    e.showbox = not e.showbox
            elif event.type == MOUSEBUTTONDOWN and event.button == 3:
                d = dinos.sprites()[0]
                if d.state == S.Wander():
                    randomogre = random.choice(ogres.sprites())
                    for d in dinos:
                        d.changeState(S.Pursuit(), target=randomogre)
                else:
                    for d in dinos:
                        d.changeState(S.Wander())

    #Update sprites
        everybody.update()

        #Draw Everything
        screen.blit(background, (0, 0))
        everybody.draw(screen)
        pygame.display.flip()
Пример #9
0
 def __init__(self, pos):
     AutoObject.__init__(self, pos)
     self.image = Images.get('square')
     self.rect = self.image.get_rect()
     self.rect.topleft = pos
     self.imagesize = self.rect.size
Пример #10
0
def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
#Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode(screen_size, FULLSCREEN)
    pygame.display.set_caption('Wait!')
    pygame.mouse.set_visible(True)
    
#Create The Backgound
    Images.load_image('grass.jpg')
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background_image = Images.get('grass')
    for x in range(0,background.get_width(),background_image.get_width()):
        for y in range(0,background.get_height(),background_image.get_height()):
            background.blit(background_image,(x,y))

#Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()
    
#Prepare Game Objects
    clock = pygame.time.Clock()
    
    Images.load_character('dino', os.path.join("T_dino_red", "dino red bitmaps"))
    Images.load_character('ogre', os.path.join("T_ogre", "ogre 96x bitmaps"))

    w,h = screen.get_size()
    
    for d in range(ndinos):  dinos.add(Dino(random_pos()))
    for o in range(nogres):  ogres.add(Ogre(random_pos()))
    
    everybody.add(dinos)
    everybody.add(ogres)

    for e in everybody: e.changeState(S.Wander())

#Main Loop
    while 1:
        clock.tick(ticks)

    #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return
            elif event.type == MOUSEBUTTONDOWN and event.button == 1:
                for e in everybody:
                    e.showheading = not e.showheading
            elif event.type == MOUSEBUTTONDOWN and event.button == 2:
                for e in everybody:
                    e.showbox = not e.showbox
            elif event.type == MOUSEBUTTONDOWN and event.button == 3:
                d = dinos.sprites()[0]
                if d.state == S.Wander():
                    randomogre = random.choice(ogres.sprites())
                    for d in dinos:
                        d.changeState(S.Pursuit(), target=randomogre)
                else:
                    for d in dinos:
                        d.changeState(S.Wander())
                        
    #Update sprites
        everybody.update()

    #Draw Everything
        screen.blit(background, (0, 0))
        everybody.draw(screen)
        pygame.display.flip()
Пример #11
0
 def __init__(self, pos):
     pygame.sprite.Sprite.__init__(self)
     self.image = Images.get('square')
     self.rect = self.image.get_rect()
     self.rect.topleft = pos
Пример #12
0
 def __init__(self, pos):
     pygame.sprite.Sprite.__init__(self)
     self.image = Images.get('square')
     self.rect = self.image.get_rect()
     self.rect.topleft = pos