Example #1
0
    def __init__(self, hud):
        super(GameLayer, self).__init__()
        # 添加板子和球
        self.is_on_exiting = True
        self.paddle = Paddle('images/paddle.png')
        self.add(self.paddle.sprite)
        self.ball = Ball('images/ball.png')
        self.ball.reset_position = (320, self.paddle.sprite.height + self.ball.sprite.height / 2)
        self.add(self.ball.sprite)
        # hud 用于记录,更新关卡,死亡,金币数据
        self.hud = hud
        # 生成关卡
        self.level = Level()
        self.level.levels = self.hud.levels

        self.add(self.hud.gold_hud)
        self.add(self.hud.level_hud)
        self.add(self.hud.death_hud)

        # 添加按键状态
        self.key_pressed_left = False
        self.key_pressed_right = False
        self.key_pressed_up = False
        self.key_pressed_down = False

        self.reset()

        # 定期调用 self.update 函数
        # FPS frame per second 每秒帧数
        self.schedule(self.update)
Example #2
0
    def join(self, old_state=None):
        if old_state == 'menu':

            self.ball = Ball((100,100,100))
            self.player = Paddle((100,100,100), 20, self.HEIGHT/2, 1)
            self.opponent = Paddle((100,100,100), self.WIDTH - 20, self.HEIGHT/2, 2)

            self.all_sprites = pygame.sprite.Group()
            self.all_sprites.add(self.ball, self.player, self.opponent)
Example #3
0
class PaddleSelectionState(BaseState):
    def enter(self):
        self.paddle = Paddle(settings.VIRTUAL_WIDTH // 2 - 32,
                             settings.VIRTUAL_HEIGHT - 64)
        self.arrows_texture = settings.GAME_TEXTURES['arrows']
        self.gray_scale_surface = pygame.Surface((24, 24), pygame.SRCALPHA)

    def update(self, dt):
        if settings.pressed_keys.get(pygame.K_RETURN):
            self.state_machine.change('serve', level=1, paddle=self.paddle)

        if settings.pressed_keys.get(pygame.K_RIGHT):
            self.paddle.skin = min(3, self.paddle.skin + 1)
        elif settings.pressed_keys.get(pygame.K_LEFT):
            self.paddle.skin = max(0, self.paddle.skin - 1)

    def render(self, surface):

        render_text(surface,
                    'Select a paddle color with left and right',
                    settings.GAME_FONTS['medium'],
                    settings.VIRTUAL_WIDTH // 2,
                    20, (255, 255, 255),
                    center=True)
        render_text(surface,
                    'Press Enter to continue!',
                    settings.GAME_FONTS['medium'],
                    settings.VIRTUAL_WIDTH // 2,
                    50, (255, 255, 255),
                    center=True)

        self.paddle.render(surface)

        surface.blit(self.arrows_texture,
                     (settings.VIRTUAL_WIDTH // 2 - 72 - 24,
                      settings.VIRTUAL_HEIGHT - 68),
                     settings.GAME_FRAMES['arrows'][0])

        if self.paddle.skin == 0:
            pygame.draw.circle(self.gray_scale_surface, (40, 40, 40, 150),
                               (12, 12), 12)
            surface.blit(self.gray_scale_surface,
                         (settings.VIRTUAL_WIDTH // 2 - 72 - 24,
                          settings.VIRTUAL_HEIGHT - 68))

        surface.blit(
            self.arrows_texture,
            (settings.VIRTUAL_WIDTH // 2 + 72, settings.VIRTUAL_HEIGHT - 68),
            settings.GAME_FRAMES['arrows'][1])

        if self.paddle.skin == 3:
            pygame.draw.circle(self.gray_scale_surface, (40, 40, 40, 150),
                               (12, 12), 12)
            surface.blit(self.gray_scale_surface,
                         (settings.VIRTUAL_WIDTH // 2 + 72,
                          settings.VIRTUAL_HEIGHT - 68))
Example #4
0
    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))
Example #5
0
    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]))
        ]
Example #6
0
    def __init__(self, hud):
        super(GameLayer, self).__init__()
        # 添加板子和球
        self.is_on_exiting = True
        self.paddle = Paddle('images/paddle.png')
        self.add(self.paddle.sprite)
        self.ball = Ball('images/ball.png')
        self.ball.reset_position = (320, self.paddle.sprite.height + self.ball.sprite.height / 2)
        self.add(self.ball.sprite)
        # hud 用于记录,更新关卡,死亡,金币数据
        self.hud = hud
        # 生成关卡
        self.level = Level()
        self.level.levels = self.hud.levels

        self.add(self.hud.gold_hud)
        self.add(self.hud.level_hud)
        self.add(self.hud.death_hud)

        # 添加按键状态
        self.key_pressed_left = False
        self.key_pressed_right = False
        self.key_pressed_up = False
        self.key_pressed_down = False

        self.reset()

        # 定期调用 self.update 函数
        # FPS frame per second 每秒帧数
        self.schedule(self.update)
Example #7
0
    def __init__(self, line_thickness=10, speed=5):
        self.line_thickness = line_thickness
        self.speed = speed
        self.score = 0

        # Initiate variable and set starting positions
        # any future changes made within rectangles
        ball_x = int(window_width / 2 - self.line_thickness / 2)
        ball_y = int(window_height / 2 - self.line_thickness / 2)
        self.ball = Ball(ball_x, ball_y, self.line_thickness,
                         self.line_thickness, self.speed)
        self.paddles = {}
        paddle_height = 50
        paddle_width = self.line_thickness
        user_paddle_x = 20
        computer_paddle_x = window_width - paddle_width - 20

        # you
        self.paddles['user'] = Paddle(user_paddle_x, paddle_width,
                                      paddle_height)
        # computer
        self.paddles['computer'] = AutoPaddle(computer_paddle_x, paddle_width,
                                              paddle_height, self.ball,
                                              self.speed)
        self.scoreboard = Scoreboard(0)
Example #8
0
    def __init__(self, size_=(0, 0)):
        self.size = size_
        self.bg = Color(69, 139, 116)
        #Week 12 additions
        self.bg_won = Color(50, 200, 255)
        self.bg_lost = Color(255, 0, 0)
        self.game_over = False
        self.ball_out = False
        self.game_won = False
        #Week 12 additions end

        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))
Example #9
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
Example #10
0
    def create_entities(self):
        screen = Screen(self.get_width(), self.get_height())
        screen.fill_screen((0, 0, 0))

        mid_line = pygame.Surface((5, self.get_height()))
        mid_line.fill((255, 255, 255))

        count_down_font = pygame.font.SysFont("Comic Sans MS", 100)
        player_one_score = 0
        player_two_score = 0

        screen = Screen(self.get_width(), self.get_height())
        screen.fill_screen((0, 0, 0))

        mid_line = pygame.Surface((5, self.get_height()))
        mid_line.fill((255, 255, 255))

        ball = Ball(screen.screen, 10)

        paddle_one = Paddle(screen, 20, 50)
        paddle_two = Paddle(screen, 960, 50)

        entities = {
            "screen": screen,
            "paddle_one": paddle_one,
            "paddle_two": paddle_two,
            "ball": ball,
            "player_one_score": player_one_score,
            "player_two_score": player_two_score,
            "mid_line": mid_line,
            "width": self.get_width(),
            "height": self.get_height(),
            "count_down_font": count_down_font
        }

        return entities
Example #11
0
    def __init__(self, levels=1, golds=0, deadcount=0):
        super(GameLayer, self).__init__()
        # 添加板子和球

        self.paddle = Paddle('images/paddle.png')
        self.add(self.paddle.sprite)
        self.ball = Ball('images/ball.png')
        self.ball.reset_position = (320, self.paddle.sprite.height + self.ball.sprite.height/2)
        self.add(self.ball.sprite)
        # 生成关卡
        self.level = Level()
        self.level.levels = levels
        # 添加标签用于记录金币和关卡。死亡次数
        self.gold = golds
        self.deadcount = deadcount

        self.hud = Label('金币: '+str(golds))
        self.level_hud = Label('关卡: '+str(levels))
        self.deadstu = Label('死亡: '+str(deadcount))

        self.hud.position = (0, 460)
        self.level_hud.position = (80, 460)
        self.deadstu.position = (160, 460)
        self.add(self.hud)
        self.add(self.level_hud)
        self.add(self.deadstu)

        # 添加按键状态
        self.key_pressed_left = False
        self.key_pressed_right = False
        self.key_pressed_up = False
        self.key_pressed_down = False

        self.blocks = []
        self.reset()

        # 定期调用 self.update 函数
        # FPS frame per second 每秒帧数
        self.schedule(self.update)
Example #12
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)

        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
Example #13
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)

        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
Example #14
0
class GameLayer(Layer):
    is_event_handler = True

    def __init__(self, levels=1, golds=0, deadcount=0):
        super(GameLayer, self).__init__()
        # 添加板子和球

        self.paddle = Paddle('images/paddle.png')
        self.add(self.paddle.sprite)
        self.ball = Ball('images/ball.png')
        self.ball.reset_position = (320, self.paddle.sprite.height + self.ball.sprite.height/2)
        self.add(self.ball.sprite)
        # 生成关卡
        self.level = Level()
        self.level.levels = levels
        # 添加标签用于记录金币和关卡。死亡次数
        self.gold = golds
        self.deadcount = deadcount

        self.hud = Label('金币: '+str(golds))
        self.level_hud = Label('关卡: '+str(levels))
        self.deadstu = Label('死亡: '+str(deadcount))

        self.hud.position = (0, 460)
        self.level_hud.position = (80, 460)
        self.deadstu.position = (160, 460)
        self.add(self.hud)
        self.add(self.level_hud)
        self.add(self.deadstu)

        # 添加按键状态
        self.key_pressed_left = False
        self.key_pressed_right = False
        self.key_pressed_up = False
        self.key_pressed_down = False

        self.blocks = []
        self.reset()

        # 定期调用 self.update 函数
        # FPS frame per second 每秒帧数
        self.schedule(self.update)
    # def on_enter(self):
    #     self.visible = True

    def reset(self):
        self.paddle.reset()
        self.ball.reset()
        # 清空界面上的block
        for b in self.blocks:
            self.remove(b)
        # 再初始化新的砖块
        self.blocks = []
        positions = self.level.blocks_pos
        number_of_blocks = len(positions)
        for i in range(number_of_blocks):
            b = Sprite('images/block.png')
            b.position = positions[i]
            self.add(b)
            self.blocks.append(b)

    def game_over(self):
        self.deadcount += 1
        scene = Scene(GameOver(self.level.levels, self.gold, self.deadcount))
        director.replace(scene)

    def update_hud(self):
        self.hud.element.text = '金币: ' + str(self.gold)
        self.level_hud.element.text = '关卡: ' + str(self.level.levels)

    def update_blocks(self):
        for b in self.blocks:
            if collision(b, self.ball.sprite):
                self.ball.hit()
                self.blocks.remove(b)
                self.remove(b)
                self.gold += 1
                self.update_hud()
                print('金币:', self.gold)
                break

    def update_ball(self):
        if self.ball.fired:
            self.ball.update()
        else:
            bx, by = self.ball.sprite.position
            px, py = self.paddle.sprite.position
            self.ball.sprite.position = (px, by)
        collide = collision(self.ball.sprite, self.paddle.sprite)
        if collide:
            self.ball.hit()
        if self.ball.dead():
            self.game_over()

    def update_paddle(self):
        self.paddle.update()

    def update_input(self):
        self.paddle.move_right = self.key_pressed_right
        self.paddle.move_left = self.key_pressed_left
        if self.key_pressed_up:
            self.ball.fire()

    def update_newlevel(self):
        if len(self.blocks) == 0:
            self.level.next()
            print(self.level.levels)
            # self.reset()
            scene = Scene(GuoCangDongHua(self.level.levels, self.gold, self.deadcount))
            director.replace(scene)
            # director.replace(FadeTransition(Scene(GameLayer(self.level.levels, self.gold))))

    def update(self, dt):
        self.update_newlevel()
        self.update_ball()
        self.update_paddle()
        self.update_input()
        self.update_blocks()
        self.update_hud()

    def on_key_press(self, key, modifiers):
        k = symbol_string(key)
        status = True
        if k == 'LEFT':
            self.key_pressed_left = status
        elif k == 'RIGHT':
            self.key_pressed_right = status
        elif k == 'UP':
            self.key_pressed_up = status

    def on_key_release(self, key, modifiers):
        k = symbol_string(key)

        status = False
        if k == 'LEFT':
            self.key_pressed_left = status
        elif k == 'RIGHT':
            self.key_pressed_right = status
        elif k == 'UP':
            self.key_pressed_up = status
Example #15
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
Example #16
0
class GameLayer(Layer):
    is_event_handler = True

    def __init__(self, hud):
        super(GameLayer, self).__init__()
        # 添加板子和球
        self.is_on_exiting = True
        self.paddle = Paddle('images/paddle.png')
        self.add(self.paddle.sprite)
        self.ball = Ball('images/ball.png')
        self.ball.reset_position = (320, self.paddle.sprite.height + self.ball.sprite.height / 2)
        self.add(self.ball.sprite)
        # hud 用于记录,更新关卡,死亡,金币数据
        self.hud = hud
        # 生成关卡
        self.level = Level()
        self.level.levels = self.hud.levels

        self.add(self.hud.gold_hud)
        self.add(self.hud.level_hud)
        self.add(self.hud.death_hud)

        # 添加按键状态
        self.key_pressed_left = False
        self.key_pressed_right = False
        self.key_pressed_up = False
        self.key_pressed_down = False

        self.reset()

        # 定期调用 self.update 函数
        # FPS frame per second 每秒帧数
        self.schedule(self.update)

    def reset(self):
        self.paddle.reset()
        self.ball.reset()
        # 清空界面上的block
        for b in self.level.blocks:
            self.remove(b.sprite)
        # 再初始化新的砖块
        self.level.reset()
        for b in self.level.blocks:
            self.add(b.sprite)

    def game_over(self):
        self.hud.death += 1
        self.is_on_exiting = False
        scene = create_scene(GameOver(self.hud))
        director.replace(scene)

    def update_hud(self):
        self.hud.update()

    def update_blocks(self):
        for b in self.level.blocks:
            if collised(b.sprite, self.ball.sprite):
                print(1)
                self.ball.hit()
                b.live -= 1
                if b.live < 0:
                    self.level.blocks.remove(b)
                    self.remove(b.sprite)
                else:
                    b.reset()
                self.hud.gold += 1
                self.update_hud()
                print('金币:', self.hud.gold)
                break

    def update_ball(self):
        if self.ball.fired:
            self.ball.update()
        else:
            bx, by = self.ball.sprite.position
            px, py = self.paddle.sprite.position
            self.ball.sprite.position = (px, by)
        collide = collised(self.ball.sprite, self.paddle.sprite)
        if collide:
            xisu = 1
            bx, by = self.ball.sprite.position
            px, py = self.paddle.sprite.position
            offect_scale = (bx - px) / (self.paddle.sprite.width / 2)
            ball_speedx_change = offect_scale * xisu
            self.ball.speedx += ball_speedx_change
            self.ball.hit()
        if self.ball.dead():
            self.game_over()

    def update_paddle(self):
        self.paddle.update()

    def update_input(self):
        self.paddle.move_right = self.key_pressed_right
        self.paddle.move_left = self.key_pressed_left
        if self.key_pressed_up:
            self.ball.fire()

    def update_newlevel(self):
        if len(self.level.blocks) == 0:
            self.is_on_exiting = False
            if self.level.next():
                print(self.level.levels)
                self.hud.levels += 1
                scene = create_scene(GuoCangDongHua(self.hud))
            else:
                scene = create_scene(GameComplite(self.hud))
            director.replace(scene)

    def update(self, dt):
        if self.is_on_exiting:
            self.update_newlevel()
            self.update_ball()
            self.update_paddle()
            self.update_input()
            self.update_blocks()
            self.update_hud()

    def on_key_press(self, key, modifiers):
        k = symbol_string(key)
        status = True
        if k == 'LEFT':
            self.key_pressed_left = status
        elif k == 'RIGHT':
            self.key_pressed_right = status
        elif k == 'UP':
            self.key_pressed_up = status

    def on_key_release(self, key, modifiers):
        k = symbol_string(key)

        status = False
        if k == 'LEFT':
            self.key_pressed_left = status
        elif k == 'RIGHT':
            self.key_pressed_right = status
        elif k == 'UP':
            self.key_pressed_up = status
Example #17
0
class GameLayer(Layer):
    is_event_handler = True

    def __init__(self, hud):
        super(GameLayer, self).__init__()
        # 添加板子和球
        self.is_on_exiting = True
        self.paddle = Paddle('images/paddle.png')
        self.add(self.paddle.sprite)
        self.ball = Ball('images/ball.png')
        self.ball.reset_position = (320, self.paddle.sprite.height + self.ball.sprite.height / 2)
        self.add(self.ball.sprite)
        # hud 用于记录,更新关卡,死亡,金币数据
        self.hud = hud
        # 生成关卡
        self.level = Level()
        self.level.levels = self.hud.levels

        self.add(self.hud.gold_hud)
        self.add(self.hud.level_hud)
        self.add(self.hud.death_hud)

        # 添加按键状态
        self.key_pressed_left = False
        self.key_pressed_right = False
        self.key_pressed_up = False
        self.key_pressed_down = False

        self.reset()

        # 定期调用 self.update 函数
        # FPS frame per second 每秒帧数
        self.schedule(self.update)

    def reset(self):
        self.paddle.reset()
        self.ball.reset()
        # 清空界面上的block
        for b in self.level.blocks:
            self.remove(b.sprite)
        # 再初始化新的砖块
        self.level.reset()
        for b in self.level.blocks:
            self.add(b.sprite)

    def game_over(self):
        self.hud.death += 1
        self.is_on_exiting = False
        scene = create_scene(GameOver(self.hud))
        director.replace(scene)

    def update_hud(self):
        self.hud.update()

    def update_blocks(self):
        for b in self.level.blocks:
            if collised(b.sprite, self.ball.sprite):
                print(1)
                self.ball.hit()
                b.live -= 1
                if b.live < 0:
                    self.level.blocks.remove(b)
                    self.remove(b.sprite)
                else:
                    b.reset()
                self.hud.gold += 1
                self.update_hud()
                print('金币:', self.hud.gold)
                break

    def update_ball(self):
        if self.ball.fired:
            self.ball.update()
        else:
            bx, by = self.ball.sprite.position
            px, py = self.paddle.sprite.position
            self.ball.sprite.position = (px, by)
        collide = collised(self.ball.sprite, self.paddle.sprite)
        if collide:
            xisu = 1
            bx, by = self.ball.sprite.position
            px, py = self.paddle.sprite.position
            offect_scale = (bx - px) / (self.paddle.sprite.width / 2)
            ball_speedx_change = offect_scale * xisu
            self.ball.speedx += ball_speedx_change
            self.ball.hit()
        if self.ball.dead():
            self.game_over()

    def update_paddle(self):
        self.paddle.update()

    def update_input(self):
        self.paddle.move_right = self.key_pressed_right
        self.paddle.move_left = self.key_pressed_left
        if self.key_pressed_up:
            self.ball.fire()

    def update_newlevel(self):
        if len(self.level.blocks) == 0:
            self.is_on_exiting = False
            if self.level.next():
                print(self.level.levels)
                self.hud.levels += 1
                scene = create_scene(GuoCangDongHua(self.hud))
            else:
                scene = create_scene(GameComplite(self.hud))
            director.replace(scene)

    def update(self, dt):
        if self.is_on_exiting:
            self.update_newlevel()
            self.update_ball()
            self.update_paddle()
            self.update_input()
            self.update_blocks()
            self.update_hud()

    def on_key_press(self, key, modifiers):
        k = symbol_string(key)
        status = True
        if k == 'LEFT':
            self.key_pressed_left = status
        elif k == 'RIGHT':
            self.key_pressed_right = status
        elif k == 'UP':
            self.key_pressed_up = status

    def on_key_release(self, key, modifiers):
        k = symbol_string(key)

        status = False
        if k == 'LEFT':
            self.key_pressed_left = status
        elif k == 'RIGHT':
            self.key_pressed_right = status
        elif k == 'UP':
            self.key_pressed_up = status
Example #18
0
    def __init__(self, size_=(0, 0)):
        self.size = size_
        self.bg = Color(69, 139, 116)

        self.ball = Ball()
        self.paddle = Paddle()
Example #19
0
 def enter(self):
     self.paddle = Paddle(settings.VIRTUAL_WIDTH // 2 - 32,
                          settings.VIRTUAL_HEIGHT - 64)
     self.arrows_texture = settings.GAME_TEXTURES['arrows']
     self.gray_scale_surface = pygame.Surface((24, 24), pygame.SRCALPHA)
Example #20
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()