示例#1
0
    def __init__(self):
        self.duration = random.randint(2, 8)
        if(self.duration < 5):
            self.value = 1000   
            image = 'bug1-small.png'
        else:
            self.value = 500
            image = 'bug2-small.png'

        bug_sprite_sheet = pyglet.resource.image(image)
        bug_grid = pyglet.image.ImageGrid(bug_sprite_sheet, 1, 6)
        animation_period = max(self.duration / 100, 0.05)  # seconds

        animation = bug_grid.get_animation(animation_period)
        super(Bug, self).__init__(animation)

        screen_height = director.get_window_size()[1]

        rect = self.get_rect()

        self.speed = (screen_height + rect.height) / self.duration
        self.cshape = OrientableRectShape(
            euclid.Vector2(rect.center[0], rect.center[1]),
                           rect.width / 2, rect.height / 2, 0)
        self.is_colliding = False
示例#2
0
class Bug(Sprite):

    ''' Characters to be destroyed. '''
    def __init__(self):
        self.duration = random.randint(2, 8)
        if(self.duration < 5):
            self.value = 1000   
            image = 'bug1-small.png'
        else:
            self.value = 500
            image = 'bug2-small.png'

        bug_sprite_sheet = pyglet.resource.image(image)
        bug_grid = pyglet.image.ImageGrid(bug_sprite_sheet, 1, 6)
        animation_period = max(self.duration / 100, 0.05)  # seconds

        animation = bug_grid.get_animation(animation_period)
        super(Bug, self).__init__(animation)

        screen_height = director.get_window_size()[1]

        rect = self.get_rect()

        self.speed = (screen_height + rect.height) / self.duration
        self.cshape = OrientableRectShape(
            euclid.Vector2(rect.center[0], rect.center[1]),
                           rect.width / 2, rect.height / 2, 0)
        self.is_colliding = False

    def start(self):
        ''' places the bug to its start position
            duration: the time in second for the
            bug to go to the bottom of the screen '''
        self.spawn()

        self.is_colliding = self.respawn_on_collision()
        collision_counter = 1 if self.is_colliding else 0
        while self.is_colliding:
            self.is_colliding = self.respawn_on_collision()
            if self.is_colliding:
                collision_counter += 1
            if collision_counter > 3:
                break

        self.rotation = -self.duration
        rotate = RotateBy(self.duration * 2, 1)
        self.do(Repeat(rotate + Reverse(rotate)))

        rect = self.get_rect()
        self.cshape.center = euclid.Vector2(rect.center[0], rect.center[1])

    def spawn(self):
        ''' pops at a random place on top of the screen '''
        rect = self.get_rect()
        half_width = rect.width / 2
        screen_width, screen_height = director.get_window_size()
        spawn_x = random.randint(half_width, screen_width - half_width)
        self.position = (spawn_x, screen_height + rect.height / 2)
        self.cshape.center.x, self.cshape.center.y = self.position
        self.cshape.update_position()
        self.cshape.rotate(self.rotation)

    def respawn_on_collision(self):
        ''' if the bug is colliding with another one,
            redraw it at another random place on top of screen
            returns True when it was colliding
        '''
        is_colliding = False
        for other in GAME_MODEL.active_bug_list:
            if BUG_LAYER.collision_manager.they_collide(self, other):
                self.spawn()
                is_colliding = True
                break
        return is_colliding

    def move_by(self, delta_x, delta_y):
        ''' moves the bug
            delta_x distance in pixels along horizontal axis
            delta_y distance in pixels along vertical axis '''
        self.position = (self.position[0] + delta_x, self.position[1] + delta_y)
        self.cshape.center.x, self.cshape.center.y = self.position
        self.cshape.update_position()
        self.cshape.rotate(self.rotation)