Beispiel #1
0
class IntroScreen():
    def __init__(self, size_ = (0, 0)):
        self.size = size_
        self.bg = Color(69, 139, 116)

        self.ball = Ball()
        self.paddle = Paddle()
        
    def update(self, delta_ms):
        self.ball.update(delta_ms)
        self.paddle.update(delta_ms)

    def render(self):
        from engine.game_env import Game
        Game.get_surface().fill(self.bg)

        self.ball.render()
        self.paddle.render()
        
    def resize(self, new_size):
        self.size = new_size
class IntroScreen():
    def __init__(self, size_=(0, 0)):
        self.size = size_
        self.bg = Color(69, 139, 116)

        self.ball = Ball()
        self.paddle = Paddle()

        brick_size = (50, 25)
        brick_colour = Color(63, 0, 127)
        self.bricks = []

        for x in range(6, self.size[0] - brick_size[0], brick_size[0] + 5):
            for y in range(25, 250, brick_size[1] + 3):
                self.bricks.append(Brick((x, y), brick_size, brick_colour))

    def update(self, delta_ms):
        self.ball.update(delta_ms)
        self.paddle.update(delta_ms)

        self.bound_ball()
        self.bind_paddle()

        self.paddle_bounce()

        for br in self.bricks:
            br.update(delta_ms)

    def render(self):
        from engine.game_env import Game
        Game.get_surface().fill(self.bg)

        for br in self.bricks:
            br.render()

        self.ball.render()
        self.paddle.render()

    def resize(self, new_size):
        self.size = new_size

    def bound_ball(self):
        if self.ball.rect.left < 0:
            self.ball.rect.left = 0
            self.ball.vel[0] *= -1
        if self.ball.rect.right > self.size[0]:
            self.ball.rect.right = self.size[0]
            self.ball.vel[0] *= -1
        if self.ball.rect.top < 0:
            self.ball.rect.top = 0
            self.ball.vel[1] *= -1
        if self.ball.rect.bottom > self.size[1]:
            self.ball.rect.bottom = self.size[1]
            self.ball.vel[1] *= -1

    def bind_paddle(self):
        if self.paddle.rect.left < 0:
            self.paddle.rect.left = 0
        if self.paddle.rect.right > self.size[0]:
            self.paddle.rect.right = self.size[0]

    def paddle_bounce(self):
        tolerance = 0.2929

        overlap = self.paddle.rect.clip(self.ball.rect)

        if overlap:
            if overlap.width < overlap.height:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                self.ball.vel[0] *= -1

                if overlap.height < self.ball.rect.height * tolerance:
                    if self.ball.rect.centery < overlap.centery:
                        self.ball.rect.bottom = overlap.top - 1
                        self.ball.vel[1] *= -1

            elif overlap.width > overlap.height:
                self.ball.rect.bottom = overlap.top - 1
                self.ball.vel[1] *= -1

                if overlap.width < self.ball.rect.width * tolerance:
                    if self.ball.rect.centerx < overlap.centerx:
                        self.ball.rect.right = overlap.left - 1
                    else:
                        self.ball.rect.left = overlap.right + 1
            else:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                if self.ball.rect.centery < overlap.centery:
                    self.ball.rect.bottom = overlap.top - 1
                else:
                    self.ball.rect.top = self.paddle.rect.bottom + 1
                self.ball.vel[0] *= -1
                self.ball.vel[1] *= -1
Beispiel #3
0
class ServeState(BaseState):
    def enter(self, **params):
        self.level = params['level']
        self.paddle = params['paddle']
        self.paddle.x = settings.VIRTUAL_WIDTH//2-32
        self.paddle.y = settings.VIRTUAL_HEIGHT-32
        self.ball = Ball(
            self.paddle.x + self.paddle.width//2 - 4, self.paddle.y - 8
        )
        self.bricks = params.get('bricks', create_level(self.level))
        self.score = params.get('score', 0)
        self.lives = params.get('lives', 3)
        self.broken_bricks_counter = params.get('broken_bricks_counter', 0)
        self.live_factor = params.get('live_factor', 1)
        self.points_to_next_live = params.get(
            'points_to_next_live', settings.LIVE_POINTS_BASE
        )

    def update(self, dt):
        if settings.pressed_keys.get(pygame.K_RETURN):
            self.state_machine.change(
                'play',
                level=self.level,
                paddle=self.paddle,
                ball=self.ball,
                score=self.score,
                lives=self.lives,
                bricks=self.bricks,
                broken_bricks_counter=self.broken_bricks_counter,
                points_to_next_live=self.points_to_next_live,
                live_factor=self.live_factor
            )

        keys = pygame.key.get_pressed()
        
        if keys[pygame.K_LEFT]:
            self.paddle.vx = -settings.PADDLE_SPEED
        elif keys[pygame.K_RIGHT]:
            self.paddle.vx = settings.PADDLE_SPEED
        else:
            self.paddle.vx = 0

        self.paddle.update(dt)
        self.ball.x = self.paddle.x + self.paddle.width//2 - 2

    def render(self, surface):
        
        heart_x = settings.VIRTUAL_WIDTH-120

        i = 0
        # Draw filled hearts
        while i < self.lives:
            surface.blit(
                settings.GAME_TEXTURES['hearts'], (heart_x, 5),
                settings.GAME_FRAMES['hearts'][0]
            )
            heart_x += 11
            i += 1
        
        # Draw empty hearts
        while i < 3:
            surface.blit(
                settings.GAME_TEXTURES['hearts'], (heart_x, 5),
                settings.GAME_FRAMES['hearts'][1]
            )
            heart_x += 11
            i += 1

        render_text(
            surface, f'Score: {self.score}', settings.GAME_FONTS['tiny'],
            settings.VIRTUAL_WIDTH-80, 5, (255, 255, 255)
        )

        for brick in self.bricks[0]:
            brick.render(surface)

        self.paddle.render(surface)
        self.ball.render(surface)

        render_text(
            surface, f'Level {self.level}', settings.GAME_FONTS['large'],
             settings.VIRTUAL_WIDTH//2, settings.VIRTUAL_HEIGHT//2-30,
             (255, 255, 255), center=True
        )
        render_text(
            surface, 'Press Enter to serve!', settings.GAME_FONTS['medium'],
            settings.VIRTUAL_WIDTH//2, settings.VIRTUAL_HEIGHT//2,
            (255, 255, 255), center=True
        )
class IntroScreen():
    def __init__(self, size_=(0, 0)):
        self.size = size_
        self.bg = Color(69, 139, 116)

        self.ball = Ball()
        self.paddle = Paddle()

    def update(self, delta_ms):
        self.ball.update(delta_ms)
        self.paddle.update(delta_ms)

        self.bound_ball()
        self.bind_paddle()

        self.paddle_bounce()

    def render(self):
        from engine.game_env import Game
        Game.get_surface().fill(self.bg)

        self.ball.render()
        self.paddle.render()

    def resize(self, new_size):
        self.size = new_size

    def bound_ball(self):
        if self.ball.rect.left < 0:
            self.ball.rect.left = 0
            self.ball.vel[0] *= -1
        if self.ball.rect.right > self.size[0]:
            self.ball.rect.right = self.size[0]
            self.ball.vel[0] *= -1
        if self.ball.rect.top < 0:
            self.ball.rect.top = 0
            self.ball.vel[1] *= -1
        if self.ball.rect.bottom > self.size[1]:
            self.ball.rect.bottom = self.size[1]
            self.ball.vel[1] *= -1

    def bind_paddle(self):
        if self.paddle.rect.left < 0:
            self.paddle.rect.left = 0
        if self.paddle.rect.right > self.size[0]:
            self.paddle.rect.right = self.size[0]

    def paddle_bounce(self):
        tolerance = 0.2929

        overlap = self.paddle.rect.clip(self.ball.rect)

        if overlap:
            if overlap.width < overlap.height:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                self.ball.vel[0] *= -1

                if overlap.height < self.ball.rect.height * tolerance:
                    if self.ball.rect.centery < overlap.centery:
                        self.ball.rect.bottom = overlap.top - 1
                        self.ball.vel[1] *= -1

            elif overlap.width > overlap.height:
                self.ball.rect.bottom = overlap.top - 1
                self.ball.vel[1] *= -1

                if overlap.width < self.ball.rect.width * tolerance:
                    if self.ball.rect.centerx < overlap.centerx:
                        self.ball.rect.right = overlap.left - 1
                    else:
                        self.ball.rect.left = overlap.right + 1
            else:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                if self.ball.rect.centery < overlap.centery:
                    self.ball.rect.bottom = overlap.top - 1
                else:
                    self.ball.rect.top = self.paddle.rect.bottom + 1
                self.ball.vel[0] *= -1
                self.ball.vel[1] *= -1
Beispiel #5
0
class IntroScreen():
    def __init__(self, size_=(0, 0)):
        self.size = size_
        self.bg = Color(69, 139, 116)

        self.ball = Ball()
        self.paddle = Paddle()

        brick_size = (50, 25)
        brick_colour = Color(63, 0, 127)
        self.bricks = []

        for x in range(6, self.size[0] - brick_size[0], brick_size[0] + 5):
            for y in range(25, 250, brick_size[1] + 3):
                self.bricks.append(Brick((x, y), brick_size, brick_colour))

    def update(self, delta_ms):
        self.ball.update(delta_ms)
        self.paddle.update(delta_ms)

        if not self.ball.launched:
            self.ball.rect.centerx = self.paddle.rect.centerx
            self.ball.rect.bottom = self.paddle.rect.top
            self.launch_ball()

        for br in self.bricks:
            br.update(delta_ms)

        self.brick_bounce()
        self.bound_ball()
        self.bind_paddle()

        self.paddle_bounce()

    def render(self):
        from engine.game_env import Game
        Game.get_surface().fill(self.bg)

        for br in self.bricks:
            br.render()

        self.ball.render()
        self.paddle.render()

    def resize(self, new_size):
        self.size = new_size

    def bound_ball(self):
        if self.ball.rect.left < 0:
            self.ball.rect.left = 0
            self.ball.vel[0] *= -1
        if self.ball.rect.right > self.size[0]:
            self.ball.rect.right = self.size[0]
            self.ball.vel[0] *= -1
        if self.ball.rect.top < 0:
            self.ball.rect.top = 0
            self.ball.vel[1] *= -1
        if self.ball.rect.bottom > self.size[1]:
            self.ball.rect.bottom = self.size[1]
            self.ball.vel[1] *= -1

    def bind_paddle(self):
        if self.paddle.rect.left < 0:
            self.paddle.rect.left = 0
        if self.paddle.rect.right > self.size[0]:
            self.paddle.rect.right = self.size[0]

    def paddle_bounce(self):
        tolerance = 0.2929

        overlap = self.paddle.rect.clip(self.ball.rect)

        if overlap:
            if overlap.width < overlap.height:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                self.ball.vel[0] *= -1

                if overlap.height < self.ball.rect.height * tolerance:
                    if self.ball.rect.centery < overlap.centery:
                        self.ball.rect.bottom = overlap.top - 1
                        self.ball.vel[1] *= -1

            elif overlap.width > overlap.height:
                self.ball.rect.bottom = overlap.top - 1
                self.ball.vel[1] *= -1

                if overlap.width < self.ball.rect.width * tolerance:
                    if self.ball.rect.centerx < overlap.centerx:
                        self.ball.rect.right = overlap.left - 1
                    else:
                        self.ball.rect.left = overlap.right + 1
            else:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                if self.ball.rect.centery < overlap.centery:
                    self.ball.rect.bottom = overlap.top - 1
                else:
                    self.ball.rect.top = self.paddle.rect.bottom + 1
                self.ball.vel[0] *= -1
                self.ball.vel[1] *= -1

    def brick_bounce(self):
        for brick in self.bricks:
            if brick.active:
                test_x = self.ball.rect.centerx
                test_y = self.ball.rect.centery

                if self.ball.rect.centerx < brick.rect.left:
                    test_x = brick.rect.left
                elif self.ball.rect.centerx > brick.rect.right:
                    test_x = brick.rect.right

                if self.ball.rect.centery < brick.rect.top:
                    test_y = brick.rect.top
                elif self.ball.rect.centery > brick.rect.bottom:
                    test_y = brick.rect.bottom

                dist_x = self.ball.rect.centerx - test_x
                dist_y = self.ball.rect.centery - test_y

                dist_x *= dist_x
                dist_y *= dist_y

                distance = (dist_x + dist_y)**0.5

                if distance <= self.ball.radius:
                    brick.active = False
                    overlap = self.ball.rect.clip(brick.rect)

                    if overlap.width < overlap.height:
                        self.ball.vel[0] *= -1
                    elif overlap.width > overlap.height:
                        self.ball.vel[1] *= -1
                    else:
                        self.ball.vel[0] *= -1
                        self.ball.vel[1] *= -1

    def launch_ball(self):
        from pygame.locals import K_SPACE
        from engine.game_env import Game
        if Game.get_key(K_SPACE):
            self.ball.launch()
Beispiel #6
0
class IntroScreen():
    def __init__(self, size_=(0, 0)):
        self.size = size_
        self.bg = Color(69, 139, 116)

        self.ball = Ball()
        self.paddle = Paddle()

        brick_size = (int(self.size[0] / 9), 25)
        self.bricks = [
            Brick((x, y), brick_size)
            for x in range(int(0.15 * brick_size[0]), self.size[0] -
                           brick_size[0], int(1.1 * brick_size[0]))
            for y in range(brick_size[1], 9 *
                           brick_size[1], int(1.05 * brick_size[1]))
        ]

    def update(self, delta_ms):
        self.ball.update(delta_ms)
        self.paddle.update(delta_ms)

        self.bound_ball()
        self.bind_paddle()

        self.paddle_bounce()
        self.brick_bounce()

        for br in self.bricks:
            br.update(delta_ms)

    def render(self):
        from engine.game_env import Game
        Game.get_surface().fill(self.bg)

        for br in self.bricks:
            br.render()

        self.ball.render()
        self.paddle.render()

    def resize(self, new_size):
        self.size = new_size

    def bound_ball(self):
        if self.ball.rect.left < 0:
            self.ball.rect.left = 0
            self.ball.vel[0] *= -1
        if self.ball.rect.right > self.size[0]:
            self.ball.rect.right = self.size[0]
            self.ball.vel[0] *= -1
        if self.ball.rect.top < 0:
            self.ball.rect.top = 0
            self.ball.vel[1] *= -1
        if self.ball.rect.bottom > self.size[1]:
            self.ball.rect.bottom = self.size[1]
            self.ball.vel[1] *= -1

    def bind_paddle(self):
        if self.paddle.rect.left < 0:
            self.paddle.rect.left = 0
        if self.paddle.rect.right > self.size[0]:
            self.paddle.rect.right = self.size[0]

    def paddle_bounce(self):
        tolerance = 0.2929

        overlap = self.paddle.rect.clip(self.ball.rect)

        if overlap:
            if overlap.width < overlap.height:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                self.ball.vel[0] *= -1

                if overlap.height < self.ball.rect.height * tolerance:
                    if self.ball.rect.centery < overlap.centery:
                        self.ball.rect.bottom = overlap.top - 1
                        self.ball.vel[1] *= -1

            elif overlap.width > overlap.height:
                self.ball.rect.bottom = overlap.top - 1
                self.ball.vel[1] *= -1

                if overlap.width < self.ball.rect.width * tolerance:
                    if self.ball.rect.centerx < overlap.centerx:
                        self.ball.rect.right = overlap.left - 1
                    else:
                        self.ball.rect.left = overlap.right + 1
            else:
                if self.ball.rect.centerx < overlap.centerx:
                    self.ball.rect.right = overlap.left - 1
                else:
                    self.ball.rect.left = overlap.right + 1
                if self.ball.rect.centery < overlap.centery:
                    self.ball.rect.bottom = overlap.top - 1
                else:
                    self.ball.rect.top = self.paddle.rect.bottom + 1
                self.ball.vel[0] *= -1
                self.ball.vel[1] *= -1

    def brick_bounce(self):
        for br in self.bricks:
            if br.active:
                test_x = self.ball.rect.centerx
                test_y = self.ball.rect.centery

                if self.ball.rect.centerx < br.rect.left:
                    test_x = br.rect.left
                elif self.ball.rect.centerx > br.rect.right:
                    test_x = br.rect.right

                if self.ball.rect.centery < br.rect.top:
                    test_y = br.rect.top
                elif self.ball.rect.centery > br.rect.bottom:
                    test_y = br.rect.bottom

                dist_x = self.ball.rect.centerx - test_x
                dist_y = self.ball.rect.centery - test_y

                dist_x *= dist_x
                dist_y *= dist_y

                dist = (dist_x + dist_y)**0.5

                if dist < self.ball.radius:
                    br.active = False
                    if dist_x < dist_y:
                        self.ball.vel[1] *= -1
                    elif dist_y < dist_x:
                        self.ball.vel[0] *= -1
                    else:
                        self.ball.vel[0] *= -1
                        self.ball.vel[1] *= -1