示例#1
0
    def __init__(self, name, space):
        self.name = name
        self.animation = AnimationController()
        self.body = pymunk.Body(1,1)  # Create a Body with mass and moment
        self.body.position = 0, 0      # Set the position of the body

        self.poly = pymunk.Poly.create_box(self.body) # Create a box shape and attach to body
        space.add(self.body, self.poly)       # Add both body and shape to the simulation
示例#2
0
    def __init__(self, name, space):
        self.name = name
        self.animation = AnimationController()
        self.body = pymunk.Body(1, 1)  # Create a Body with mass and moment
        self.body.position = 0, 0  # Set the position of the body
        self.velocity_scale = 10.0

        shape = pymunk.Circle(self.body, 10, (0, 0))
        shape.friction = 0.5
        shape.collision_type = 2

        space.add(self.body,
                  shape)  # Add both body and shape to the simulation
示例#3
0
class Player:
    def __init__(self, name, space):
        self.name = name
        self.animation = AnimationController()
        self.body = pymunk.Body(1, 1)  # Create a Body with mass and moment
        self.body.position = 0, 0  # Set the position of the body
        self.velocity_scale = 10.0

        shape = pymunk.Circle(self.body, 10, (0, 0))
        shape.friction = 0.5
        shape.collision_type = 2

        space.add(self.body,
                  shape)  # Add both body and shape to the simulation

    def update_velocity(self, vx, vy):
        scale = self.velocity_scale

        self.body.velocity = (vx, vy)

    def to_json(self):
        obj = {}

        obj['name'] = self.name
        obj['position'] = {
            'x': self.body.position.x,
            'y': self.body.position.y
        }
        obj['animation'] = self.animation.to_json()
        obj['velocity'] = {
            'vx': self.body.velocity.x,
            'vy': self.body.velocity.y
        }

        return obj
示例#4
0
def main():
    pixels = NeoRings(pin_number=27,
                      rings=(1, 8, 16),
                      brightness=1.,
                      auto_write=False)

    animations = Animations(pixels)
    animation_controller = AnimationController(animations)
    _thread.start_new_thread(animation_controller.run, ())

    sensor_controller = SensorController(14)
    _thread.start_new_thread(sensor_controller.run, ())

    animation_controller.set_animation("random_blink",
                                       colors=("random", ),
                                       pause=1000,
                                       smooth=True)
示例#5
0
 def __init__(self, display):
     self.display = display
     self.animations_mgr = AnimationController()
     self.surface = pygame.Surface(DISPLAY_SIZE, pygame.SRCALPHA, 32)
     self.field = Field(self.surface)
     self.scoreboard = ScoreBoard(self.surface, SCOREBOARD_POS)
     self.pause_menu = MenuPause(self.display, self)
     self.menu_pos = ((self.field.rect.width - self.pause_menu.width) / 2,
                      (self.field.rect.height - self.pause_menu.height) / 2)
     self.menu_end = None
     self.score = (0, 0)
     self.sprites = pygame.sprite.RenderPlain()
     self.sliders = []
     self.goals = {}
     self.balls = []
     self.state = 'need_wait_put_ball'
     self.pressing_escape = False
     self.present()
示例#6
0
    def set_value(self, value, animated=False):
        import sys
        if not animated:
            self._set_value(value)
            sys.stdout.write(str(self._progress_bar))
        else:
            from animation_controller import \
                AnimationController, AnimationTimingQuad
            start_value = self.value()
            delta = value - start_value

            def step_hook(progress):
                self._set_value(start_value + (delta * progress))
                self.redraw()

            def complete_hook():
                self._set_value(value)
                self.redraw()
                sys.stdout.write(ESC + str(len(str(self._progress_bar))) + "C")

            anim = AnimationController(step=step_hook,
                                       timing=AnimationTimingQuad,
                                       complete=complete_hook)
            anim.begin()
示例#7
0
class Objects:

    def __init__(self, name, space):
        self.name = name
        self.animation = AnimationController()
        self.body = pymunk.Body(1,1)  # Create a Body with mass and moment
        self.body.position = 0, 0      # Set the position of the body

        self.poly = pymunk.Poly.create_box(self.body) # Create a box shape and attach to body
        space.add(self.body, self.poly)       # Add both body and shape to the simulation

    def to_json(self):
        obj = {}

        obj['name'] = self.name
        obj['position'] = {'x': self.body.position.x, 'y': self.body.position.y }
        obj['animation'] = self.animation.to_json()
    
        return obj
示例#8
0
class Battle:
    '''
        Core of game
    '''
    goals_target = 10

    def __init__(self, display):
        self.display = display
        self.animations_mgr = AnimationController()
        self.surface = pygame.Surface(DISPLAY_SIZE, pygame.SRCALPHA, 32)
        self.field = Field(self.surface)
        self.scoreboard = ScoreBoard(self.surface, SCOREBOARD_POS)
        self.pause_menu = MenuPause(self.display, self)
        self.menu_pos = ((self.field.rect.width - self.pause_menu.width) / 2,
                         (self.field.rect.height - self.pause_menu.height) / 2)
        self.menu_end = None
        self.score = (0, 0)
        self.sprites = pygame.sprite.RenderPlain()
        self.sliders = []
        self.goals = {}
        self.balls = []
        self.state = 'need_wait_put_ball'
        self.pressing_escape = False
        self.present()

    def present(self):
        '''
            Present field on screen, create all objects
        '''
        self.field.present()
        self.fill_goals()
        self.create_sliders()
        self.add_ball_to_battle()
        self.bot_player = NormalBot(self, self.right_slider, self.right_goal)
        self.update_score()
        pygame.time.set_timer(EV_PUT_BALL, 1500)
        self.display.blit(self.surface, ((0, 0), DISPLAY_SIZE))
        pygame.display.update(((0, 0), DISPLAY_SIZE))

    def reset(self):
        '''
            Restart for next game
        '''
        self.score = (0, 0)
        self.update_score()
        self.menu_end.hide()
        self.menu_end = None
        for slider in self.sliders:
            slider.put()
        self.update_state('need_wait_put_ball')

    def update(self, main_events_loop, ticks):
        '''
            Main cycle of battle, processing all objects and actions
        '''
        for sprite in self.sprites:
            sprite.clear(self.surface)

        main_events_loop(self)
        self.animations_mgr.update(ticks)

        if self.check_state('play'):
            self.on_play_state()
        elif self.check_state('need_wait_put_ball'):
            self.on_need_wait_put_ball()
        if self.check_state('pause'):
            self.on_pause_state()
            return
        elif self.check_state('end'):
            return

        for sprite in self.sprites:
            sprite.draw(self.surface)
        self.display.blit(self.surface, (0, 0))
        pygame.display.update(self.field.rect)

    def handle_event(self, event):
        '''
            Handling keyboard and user events
        '''
        if event.type == KEYDOWN:
            self.on_key_down()
        elif event.type == KEYUP:
            self.on_key_up()
        elif event.type == USEREVENT:
            self.handle_user_event(event)
        elif event.type == EV_PUT_BALL:
            self.update_state('play')
            pygame.time.set_timer(EV_PUT_BALL, 0)

    def on_key_down(self):
        is_pressed = pygame.key.get_pressed()

        if not self.check_state('pause'):
            if is_pressed[K_ESCAPE]:
                self.pressing_escape = True
                self.pause_game()
                return

        if is_pressed[K_w]:
            self.left_slider.process(UP)
        if is_pressed[K_s]:
            self.left_slider.process(DOWN)
        if is_pressed[K_UP]:
            self.right_slider.process(UP)
        if is_pressed[K_DOWN]:
            self.right_slider.process(DOWN)

    def on_key_up(self):
        is_pressed = pygame.key.get_pressed()
        if not (is_pressed[K_w] and is_pressed[K_s]):
            self.left_slider.on_change_direction()
        if not (is_pressed[K_UP] and is_pressed[K_DOWN]):
            self.right_slider.on_change_direction()

        self.pressing_escape = False

    def handle_user_event(self, event):
        event_info = event.__dict__
        for key in event_info:
            if key == 'goal':
                self.on_goal_scored(event_info)

            if key == 'pause':
                self.update_state('need_wait_put_ball')

    def update_score(self, d_score=(0, 0)):
        self.score = (self.score[0] + d_score[0], self.score[1] + d_score[1])
        self.scoreboard.set_score(self.score)
        self.display.blit(self.surface, (0, 0))
        pygame.display.update(self.scoreboard.rect)

    def update_state(self, state):
        self.state = state

    def check_state(self, state):
        return self.state == state

    def add_ball_to_battle(self):
        collision_objects = {
            'sliders': self.sliders,
            'goals': self.field.goals.sprites(),
            'posts': self.field.posts.sprites(),
            'balls': self.balls
        }
        ball = Ball(self.field.surface, self.field.rect.center, self.sprites,
                    collision_objects, self.animations_mgr)
        self.balls.append(ball)

    def fill_goals(self):
        Goal = namedtuple('Goal', ['pos_x', 'top', 'bottom'])
        left_goal = self.field.left_goal_line
        self.left_goal = Goal(left_goal.rect.centerx, left_goal.rect.top,
                              left_goal.rect.bottom)

        right_goal = self.field.right_goal_line
        self.right_goal = Goal(right_goal.rect.centerx, right_goal.rect.top,
                               right_goal.rect.bottom)

    def get_ball(self):
        return self.balls[0]

    def create_sliders(self):
        left_pos = (L_GOAL_LINE + SLIDER_DISTX, self.field.rect.centery)
        self.left_slider = Slider(self.field.surface,
                                  left_pos,
                                  group=self.sprites,
                                  color='red')
        self.sliders.append(self.left_slider)

        right_pos = (R_GOAL_LINE - SLIDER_DISTX, self.field.rect.centery)
        self.right_slider = Slider(self.field.surface,
                                   right_pos,
                                   group=self.sprites,
                                   color='blue')
        self.sliders.append(self.right_slider)

    def on_play_state(self):
        self.process_objects()
        self.bot_player.process()

    def on_pause_state(self):
        active_menu = self.menu_end or self.pause_menu
        active_menu.update()

    def process_objects(self):
        for ball in self.balls:
            ball.update()

    def on_goal_scored(self, scored_info):
        if scored_info['goal'] == 1:
            self.update_score((1, 0))
        else:
            self.update_score((0, 1))
        if max(self.score) == self.goals_target:
            self.end_game()
        else:
            self.update_state('need_wait_put_ball')

    def on_need_wait_put_ball(self):
        self.update_state('waiting_put_ball')
        pygame.time.set_timer(EV_PUT_BALL, 500)

    def pause_game(self):
        '''
            Show pause menu
        '''
        self.update_state('pause')
        self.pause_menu.set_position(self.menu_pos)
        self.pause_menu.show()
        continue_btn = self.pause_menu.get_widget('continue_btn')
        continue_btn.set_onpressed(self.continue_game)
        quit_btn = self.pause_menu.get_widget('quit_btn')
        quit_btn.set_onpressed(self.quit_game)

    def continue_game(self):
        self.update_state('play')
        self.pause_menu.hide()

    def end_game(self):
        '''
            Show menu with game result and choice: restart game or quit
        '''
        self.update_state('pause')
        menu = MenuEnd(self.display, self)
        menu.set_position(self.menu_pos)
        menu.load_layout()
        win_icon = menu.get_widget('win_icon')
        lose_icon = menu.get_widget('lose_icon')
        if self.score[0] > self.score[1]:
            win_icon.visible = True
            lose_icon.visible = False
        else:
            win_icon.visible = False
            lose_icon.visible = True
        menu.show()
        self.menu_end = menu
        continue_btn = menu.get_widget('continue_btn')
        continue_btn.set_onpressed(self.reset)
        quit_btn = menu.get_widget('quit_btn')
        quit_btn.set_onpressed(self.quit_game)

    def quit_game(self):
        pygame.event.clear()
        ev = pygame.event.Event(QUIT)
        pygame.event.post(ev)