Пример #1
0
    def _setup_sprites(self):
        """
        Initializes all sprites

        """
        from sprite import Pad, Ball
        self.pad_left = Pad(relative_to=self.playarea_rect)
        self.pad_right = Pad(relative_to=self.playarea_rect, align=1)
        self.ball = Ball(relative_to=self.playarea_rect)
        self.allsprites = pygame.sprite.RenderUpdates((
                    self.pad_left,
                    self.pad_right,
                    self.ball
                    ))
        self.pads = pygame.sprite.Group((self.pad_left, self.pad_right))
Пример #2
0
class GameView(View):
    def __init__(self, size=(800,600)):
        self.surfaces = []
        self.paused = False
        self.stopped = False
        self._setup_screen(size)
        self._setup_background()
        self._setup_sprites()
        self._setup_score()

    def reset(self):
        """
        Resets score and sprites

        """

        self.allsprites.empty()
        self._setup_background()
        self._setup_sprites()
        self._setup_score()
        self.stopped = False

    def _setup_score(self):
        """
        Sets up the score object

        """
        from score import Score
        self.score = Score(self.background)

    def _setup_sprites(self):
        """
        Initializes all sprites

        """
        from sprite import Pad, Ball
        self.pad_left = Pad(relative_to=self.playarea_rect)
        self.pad_right = Pad(relative_to=self.playarea_rect, align=1)
        self.ball = Ball(relative_to=self.playarea_rect)
        self.allsprites = pygame.sprite.RenderUpdates((
                    self.pad_left,
                    self.pad_right,
                    self.ball
                    ))
        self.pads = pygame.sprite.Group((self.pad_left, self.pad_right))

    def _setup_background(self):
        self.background = pygame.Surface((self.screen.get_size()))
        self.background = self.background.convert()
        self.background.fill((0,0,0))

        self.playarea = pygame.image.load(os.path.join("data", "playfield.png"))
        self.playarea.convert_alpha()
        self.playarea_rect = self.playarea.get_rect()
        self.playarea_rect.top = 50
        self.background.blit(self.playarea, self.playarea_rect)

    def kill(self):
        self.allsprites.empty()
        self.stopped = True

    def game_over(self, win=True):
        """
        What do you think it does??

        """
        self.ball.kill()
        if win:
            image = pygame.image.load(os.path.join("data", "fuckyea.jpg"))
        else:
            image = pygame.image.load(os.path.join("data", "fuuu.jpg"))
        image = image.convert()
        image_rect = image.get_rect()
        image_rect.center = [400, 300]
        self.background.blit(image, image_rect)
        pygame.display.flip()
        self.stopped = True
        #self.event_manager.unregister_listener(self)

    def handle_collisions(self):
        """
        Detect ball - sprite collisions and handle them

        """
        collisions = pygame.sprite.spritecollide(self.ball, self.pads, False)
        if len(collisions):
            # in the future there will be generic collision test
            # so the ball will bounce of extra walls etc
            self.ball.movement_vector[0] = -self.ball.movement_vector[0] 
            self.ball.image = pygame.transform.flip(self.ball.image, 1, 0)
            ball_mid = self.ball.rect.midright[1]
            pad = collisions[0]
            pad_mid = pad.rect.midright[1]
            angle_speed = (pad_mid - ball_mid)/10
            self.ball.movement_vector[1] -= angle_speed

    def notify(self, event):
        """
        Recieve events

        """
        if isinstance(event, events.TickEvent) and not self.stopped:
            if not self.paused:
                self.handle_collisions()
                self.allsprites.update()
                if self.surfaces:
                    self._blit_registered_surfaces()
                self.screen.blit(self.background, (0,0))
                self.allsprites.draw(self.screen)
                pygame.display.flip()
        elif isinstance(event, events.RegisterSurfaceEvent):
            self.register_surface(event.surface)
        elif isinstance(event, events.PauseEvent):
            self.paused = not self.paused
        elif isinstance(event, events.BlitRequestEvent):
            surface = getattr(self, event.surface_name)
            surface.blit(event.element, event.element_rect)
        elif isinstance(event, events.ModifyScoreEvent):
            self.score.modify_score(event)
        elif isinstance(event, events.GameOverEvent):
            self.game_over(event.win)
        elif isinstance(event, events.ControlPadEvent):
            if event.left:
                self.pad_left.pos =  event.pos
            else:
                self.pad_right.pos =  event.pos