Ejemplo n.º 1
0
    def step(self, dt):
        w, h = director.get_window_size()
        self.model.cursor.update(self.mouse_pos[0], self.mouse_pos[1])
        self.model.player.move()
        self.model.scroller.update(dt)
        if self.model.tardis:
            self.model.tardis.move()
            if self.model.tardis.offscreen:
                self.model.tardis = None
        self.player_target.px = self.player_target.x
        self.player_target.py = self.player_target.y
        self.player_target.x = self.model.player.x/2
        self.player_target.y = self.model.player.y/2

        for m in self.model.mobs[:]:
            if m.offscreen:
                self.model.mobs.remove(m)
            else:
                m.move(self.model.player.x, self.model.player.y)
                self.elapsed += dt
                update_speed = 0.25
                if self.elapsed > update_speed:
                    self.elapsed = 0
                    source = bulletml.Bullet.FromDocument(m.doc, x=m.x/2, y=m.y/2, target=self.player_target, rank=0.5, speed=20)
                    source.vanished = True
                    self.model.mob_active_bullets.add(source)

        if self.model.player.shooting:
            self.model.dispatch_event("on_shoot")
            self.model.player.target.x, self.model.player.target.y = self.mouse_pos[0]/2, self.mouse_pos[1]/2
            #self.model.player.target.px, self.model.player.target.py = self.model.player.target.x, self.model.player.target.y 
            source = bulletml.Bullet.FromDocument(self.model.player.doc, x=self.model.player.x/2, y=self.model.player.y/2, target=self.model.player.target, rank=0.5, speed=10)
            source.vanished = True
            self.model.player.active_bullets.add(source)
        w,h = director.get_window_size()
        p_active = list(self.model.player.active_bullets)
        for obj in p_active:
            new = obj.step()
            self.model.player.active_bullets.update(new)
            if (obj.finished 
                or not (0 < obj.x < w)
                or not (0 < obj.y < h)):
                self.model.player.active_bullets.remove(obj)

        m_active = list(self.model.mob_active_bullets)
        for obj in m_active:
            new = obj.step()
            self.model.mob_active_bullets.update(new)
            if (obj.finished 
                or not (0 < obj.x < w)
                or not (0 < obj.y < h)):
                self.model.mob_active_bullets.remove(obj)
        mob_collides = False

        if p_active:
            for m in self.model.mobs[:]:
                for p in p_active:
                    if distance(m, p) < 5:
                        self.model.dispatch_event("on_explode")
                        status.score += m.value
                        p.vanished = True
                        m.offscreen = True

        if m_active:
            mob_collides = collides_all(self.player_target, m_active)

        if mob_collides: # bullet collision check
            self.model.dispatch_event('on_game_over')
            return
        if self.model.tardis:
            if self.model.tardis.contains(self.model.player.x, self.model.player.y):
                status.score += self.model.tardis.value
                self.model.dispatch_event('on_win')
                return

        self.model.mob_spawn_counter += 1
        if self.model.mob_spawn_counter == self.model.mob_spawn_rate:
            self.model.mob_spawn_counter = 0
            if status.score < 1000:
                m = Mob()
                m.position = (random.randint(0, w), h)
            elif status.score < 3000:
                m = random.choice([Mob(), Mob2()])
                m.position = (random.choice([0, w]), random.randint(0, h))
            else:
                m = random.choice([Mob(), Mob2(), Mob3()])
                m.position = (random.choice([0, w]), random.randint(0, h))

            self.model.mobs.append(m)
            if status.score > 5000 and not self.model.tardis:
                self.model.tardis = TARDIS()
                self.model.tardis.position = (random.randint(0, w), h)
                self.model.dispatch_event('on_deploy_tardis')


        # make sure the player isn't off the side of the screen
        if self.model.player.x > w:
            self.model.player.x = w
            self.model.player.moving_right = False
        if self.model.player.y > h:
            self.model.player.y = h
            self.model.player.moving_up = False
        if self.model.player.x < 0:
            self.model.player.x = 0
            self.model.player.moving_left = False
        if self.model.player.y < 0:
            self.model.player.y = 0
            self.model.player.moving_down = False
Ejemplo n.º 2
0
source.vanished = True

while (True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise SystemExit
    lactive = list(active)
    for obj in lactive:
        new = obj.step()
        active.update(new)
        if (obj.finished
            or not (-50 < obj.x < 350)
            or not (-50 < obj.y < 350)):
            active.remove(obj)
    if lactive:
        collides = collides_all(target, lactive)
    screen.fill([0, 0, 64] if collides else [0, 0, 0] )
    for obj in active:
        try:
            x, y = obj.x, obj.y
        except AttributeError:
            pass
        else:
            if not obj.vanished:
                x *= 2
                y *= 2
                x -= 1
                y -= 1
                bullet = bullets.get(obj.appearance, red)
                screen.blit(bullet, [x, 600 - y])
Ejemplo n.º 3
0
 def main(self):
     self.file_idx = 0
     while not self.has_exit:
         filename = self.filenames[self.file_idx % len(self.filenames)]
         print "Processing: " + filename
         self.doc = bulletml.BulletML.FromDocument(open(filename, "rU"))
         source = bulletml.Bullet.FromDocument(self.doc, x=200, y=200, target=self.target, rank=0.5)
         self.active = set([source])
         source.vanished = True
         self.new_file = False
         total = 0
         frames = 0
         blue = (0, 0, 1, 1)
         black = (0, 0, 0 , 1)
         while self.active and not self.new_file:
             self.dispatch_events()
             self.target.x, self.target.y = self.mouse_pos
             self.target.x /= 2
             self.target.y /= 2
             #target.y = 300 - target.y
             self.target.px = self.target.x
             self.target.py = self.target.y
             collides = False
             if not self.paused:
                 lactive = list(self.active)
                 start = time.time()
                 count = len(self.active)
                 for obj in lactive:
                     new = obj.step()
                     total += len(new)
                     self.active.update(new)
                     if (obj.finished
                         or not (-50 < obj.x < 650)
                         or not (-50 < obj.y < 650)):
                         self.active.remove(obj)
                 if lactive:
                     collides = collides_all(self.target, lactive)
                 elapsed = time.time() - start
 
                 frames += 1
                 if frames % 100 == 0:
                     print "  Processing: %04d: %d bullets, %d active." % (
                         frames, total, count)
                     if elapsed:
                         seconds_per_bullet = elapsed / count
                         bullets_per_second = count / elapsed
                         print "  %g seconds per bullet (120Hz max: %g)." % (
                             seconds_per_bullet, bullets_per_second / 120)
             if collides:
                 pyglet.gl.glClearColor(*blue)
             else:
                 pyglet.gl.glClearColor(*black)
             self.clear()
             vert_list = []
             for obj in self.active:
                 try:
                     x, y = obj.x, obj.y
                 except AttributeError:
                     pass
                 else:
                     if not obj.vanished:
                         x *= 2
                         y *= 2
                         x -= 1
                         y -= 1
                         vert_list.append((x, y))
             pyglet.gl.glBegin(pyglet.gl.GL_POINTS)
             for x, y in vert_list:
                 pyglet.gl.gl.glVertex2f( x, y )
             pyglet.gl.glEnd()
             self.flip()
             
         print "  Finished: %04d: %d bullets." % (frames, total)
         self.file_idx += 1
Ejemplo n.º 4
0
 def collides(self, other):
     return collision.collides_all(other, list(self))
Ejemplo n.º 5
0
 def test_cross(self):
     a = Dummy(0, 0, 100, 100, 1)
     b = Dummy(100, 100, 0, 100, 1)
     c = Dummy(0, 100, 100, 0, 1)
     collides = collision.collides_all(a, [a, b, c])
     self.failUnlessEqual(collides, [a, c])
Ejemplo n.º 6
0
source.vanished = True

while (True):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise SystemExit
    lactive = list(active)
    for obj in lactive:
        new = obj.step()
        active.update(new)
        if (obj.finished or not (-50 < obj.x < 350)
                or not (-50 < obj.y < 350)):
            active.remove(obj)
    if lactive:
        collides = collides_all(target, lactive)
    screen.fill([0, 0, 64] if collides else [0, 0, 0])
    for obj in active:
        try:
            x, y = obj.x, obj.y
        except AttributeError:
            pass
        else:
            if not obj.vanished:
                x *= 2
                y *= 2
                x -= 1
                y -= 1
                bullet = bullets.get(obj.appearance, red)
                screen.blit(bullet, [x, 600 - y])
Ejemplo n.º 7
0
 def test_cross(self):
     a = Dummy(0, 0, 100, 100, 1)
     b = Dummy(100, 100, 0, 100, 1)
     c = Dummy(0, 100, 100, 0, 1)
     collides = collision.collides_all(a, [a, b, c])
     self.failUnlessEqual(collides, [a, c])