砖破碎机 - 砖碰撞(上下)

时间:2017-07-31 19:51:27

标签: python pygame

我在pygame(python)中制作一个破砖游戏,问题是当球击中砖块时,砖块消失(更改x和y),

但是当碰撞发生时,球不想下降,反之亦然,如果球击中球的顶部,球需要上升...

这里是主要功能..一堆变量..

    while loop:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    p_x_move = -5
                elif event.key == pygame.K_RIGHT:
                    p_x_move = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    p_x_move = 0
                elif event.key == pygame.K_RIGHT:
                    p_x_move = 0

这是我画画的地方。

        screen.fill(white)

        # platform calling
        platform.draw()
        platform.rules()
        # new variables
        platform_new_x = platform.move(p_x_move)
        ball_new = ball.move(self.y_direction, self.x_direction)

        # ball calling
        ball.draw()
        ball_new = list(ball_new)
        # loop if statements
        # 0 - y, 1 - x
        if ball_new[0] < 5:
            self.y_direction = "down"
        if ball_new[0] > height:
            break #game over

这是一些有效的if语句(忽略这个)

        if ball_new[0]+5 > p_y and ball_new[1] >= platform_new_x and ball_new[1] < platform_new_x+p_width and ball_new[0] <= p_y+p_height:
            self.y_direction = "up"
        # width = 120
        if ball_new[1] < platform_new_x + 40 and ball_new[0] >= p_y:
            self.x_direction = "far_left"
        if ball_new[1] > platform_new_x + 80 and ball_new[0] >= p_y:
            self.x_direction = "far_right"
        if ball_new[1] < platform_new_x + 60 and ball_new[1] > platform_new_x + 40 and ball_new[0] >= p_y:
            self.x_direction = "short_left"
        if ball_new[1] > platform_new_x + 60 and ball_new[1] < platform_new_x + 40and ball_new[0] >= p_y:
            self.x_direction = "short_right"


        if ball_new[1] > width:
            self.x_direction = "far_left"
        elif ball_new[1] < 0:
            self.x_direction = "far_right"

        bricks.draw()

所以在这里我有if语句我希望你能够阅读它们我提前道歉..球与砖碰撞很好,但问题是我想在碰撞时改变它的Y方向发生这不起作用我写的..

        # ball collision with the brick

        if ball_new[1] > bricks_1[0][0] and ball_new[1] - 100 < bricks_1[0][0] and ball_new[0] - 25 < bricks_1[0][1] and ball_new[0] > bricks_1[0][1]:
            bricks_1[0] = (1000, 1000)
            if ball_new[1] <= 210:
                self.y_direction = "down"
                print("down")
            if ball_new[1] >= 225:
                self.y_direction = "up"

        if ball_new[1] > bricks_1[1][0] and ball_new[1] - 100 < bricks_1[1][0] and ball_new[0] - 25 < bricks_1[1][1] and ball_new[0] > bricks_1[1][1]:
            bricks_1[1] = (1000, 1000)
            if ball_new[1] <= 210:
                self.y_direction = "down"
                print("down")
            if ball_new[1] >= 225:
                self.y_direction = "up"

        if ball_new[1] > bricks_1[2][0] and ball_new[1] - 100 < bricks_1[2][0] and ball_new[0] - 25 < bricks_1[2][1] and ball_new[0] > bricks_1[2][1]:
            bricks_1[2] = (1000, 1000)
            if ball_new[1] <= 210:
                self.y_direction = "down"
                print("true")
            if ball_new[1] >= 225:
                self.y_direction = "up"

        if ball_new[1] > bricks_1[3][0] and ball_new[1] - 100 < bricks_1[3][0] and ball_new[0] - 25 < bricks_1[3][1] and ball_new[0] > bricks_1[3][1]:
            bricks_1[3] = (1000, 1000)
            if ball_new[1] <= 210:
                self.y_direction = "down"
                print("down")
            if ball_new[1] >= 225:
                self.y_direction = "up"

        if ball_new[1] > bricks_1[4][0] and ball_new[1] - 100 < bricks_1[4][0] and ball_new[0] - 25 < bricks_1[4][1] and ball_new[0] > bricks_1[4][1]:
            bricks_1[4] = (1000, 1000)
            if ball_new[1] <= 210:
                self.y_direction = "down"
                print("down")
            if ball_new[1] >= 215:
                self.y_direction = "up"

        pygame.display.update()
        clock.tick(FPS)

0 个答案:

没有答案