pygame中的嘈杂反弹

3okqufwl  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(311)

我正在从这里分解代码以使球反弹。
下面是代码的精简版本:

import pygame
import random

# Define some colors

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
BALL_SIZE = 25
SPEED = 10

class Ball:
    """
    Class to keep track of a ball's location and vector.
    """
    def __init__(self):
        self.x = 0
        self.y = 0
        self.change_x = 0
        self.change_y = 0

def make_ball():
    """
    Function to make a new, random ball.
    """
    ball = Ball()
    # Starting position of the ball.
    # Take into account the ball size so we don't spawn on the edge.
    ball.x = random.randrange(BALL_SIZE, SCREEN_WIDTH - BALL_SIZE)
    ball.y = random.randrange(BALL_SIZE, SCREEN_HEIGHT - BALL_SIZE)

    # Speed and direction of rectangle
    ball.change_y = SPEED
    ball.change_x = SPEED

    return ball

def main():
    """
    This is our main program.
    """
    pygame.init()

    # Set the height and width of the screen
    size = [SCREEN_WIDTH, SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Bouncing Balls")

    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    ball_list = []

    ball = make_ball()
    ball_list.append(ball)

    # -------- Main Program Loop -----------
    while not done:
        # --- Event Processing
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        # --- Logic
        for ball in ball_list:
            # Move the ball's center
            ball.x += ball.change_x
            ball.y += ball.change_y

            # Bounce the ball if needed: walls
            if ball.y > SCREEN_HEIGHT - BALL_SIZE or ball.y < BALL_SIZE:
                ball.change_y *= -1
            if ball.x > SCREEN_WIDTH - BALL_SIZE or ball.x < BALL_SIZE:
                ball.change_x *= -1

        # --- Drawing
        # Set the screen background
        screen.fill(BLACK)

        # Draw the balls
        for ball in ball_list:
            pygame.draw.circle(screen, WHITE, [ball.x,ball.y], BALL_SIZE)

        # --- Wrap-up
        # Limit to 60 frames per second
        clock.tick(60)

        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()

    # Close everything down
    pygame.quit()

if __name__ == "__main__":
    main()

现在,球总是以45度角从墙上反弹。我想在这里引入一些噪声,使反弹稍微更真实一些。
我试图简单地将随机噪声添加到反弹中:

if ball.y > SCREEN_HEIGHT - BALL_SIZE or ball.y < BALL_SIZE:
                ball.change_y *= -1*random.uniform(0,1)
if ball.x > SCREEN_WIDTH - BALL_SIZE or ball.x < BALL_SIZE:
                ball.change_x *= -1*random.uniform(0,1)

假设发生:球会改变从墙上反弹的Angular 。实际发生的情况是:球减速然后停止。

bis0qfac

bis0qfac1#

当球撞击墙壁时,根据入射角和墙壁的法向量,球会被墙壁反射。这可以使用 pygame.math 模块。

move_vector = pygame.Vector2(ball.change_x, ball.change_y)
reflect_vector = move_vector.reflect(normal_vector)

法向量垂直于墙。如果需要一些随机性,则需要随机旋转法向量:

normal_vector.rotate_ip(random.randint(-10, 10)) # +/- 10 degrees

比较示例:

import pygame
import random

# Define some colors

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
BALL_SIZE = 25
SPEED = 10

class Ball:
    """
    Class to keep track of a ball's location and vector.
    """
    def __init__(self):
        self.x = 0
        self.y = 0
        self.change_x = 0
        self.change_y = 0

def make_ball():
    """
    Function to make a new, random ball.
    """
    ball = Ball()
    # Starting position of the ball.
    # Take into account the ball size so we don't spawn on the edge.
    ball.x = random.randrange(BALL_SIZE, SCREEN_WIDTH - BALL_SIZE)
    ball.y = random.randrange(BALL_SIZE, SCREEN_HEIGHT - BALL_SIZE)

    # Speed and direction of rectangle
    ball.change_y = SPEED
    ball.change_x = SPEED

    return ball

def main():
    """
    This is our main program.
    """
    pygame.init()

    # Set the height and width of the screen
    size = [SCREEN_WIDTH, SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Bouncing Balls")

    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    ball_list = []

    ball = make_ball()
    ball_list.append(ball)

    # -------- Main Program Loop -----------
    while not done:
        # --- Event Processing
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        # --- Logic
        for ball in ball_list:
            # Move the ball's center
            ball.x += ball.change_x
            ball.y += ball.change_y

            # Bounce the ball if needed: walls
            normal_vector = None
            if ball.y > SCREEN_HEIGHT - BALL_SIZE:
                ball.change_y = abs(ball.change_y)
                normal_vector = pygame.Vector2(0, -1)
            elif ball.y < BALL_SIZE:
                ball.change_y = -abs(ball.change_y)
                normal_vector = pygame.Vector2(0, 1)
            elif ball.x > SCREEN_WIDTH - BALL_SIZE:
                ball.change_x = abs(ball.change_x)
                normal_vector = pygame.Vector2(-1, 0)
            elif ball.x < BALL_SIZE:
                ball.change_x = -abs(ball.change_x)
                normal_vector = pygame.Vector2(1, 0)

            if normal_vector:
                normal_vector.rotate_ip(random.randint(-10, 10))
                move_vector = pygame.Vector2(ball.change_x, ball.change_y)
                reflect_vector = move_vector.reflect(normal_vector)
                ball.change_x = reflect_vector.x
                ball.change_y = reflect_vector.y

        # --- Drawing
        # Set the screen background
        screen.fill(BLACK)

        # Draw the balls
        for ball in ball_list:
            pygame.draw.circle(screen, WHITE, [round(ball.x), round(ball.y)], BALL_SIZE)

        # --- Wrap-up
        # Limit to 60 frames per second
        clock.tick(60)

        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()

    # Close everything down
    pygame.quit()

if __name__ == "__main__":
    main()
c9qzyr3d

c9qzyr3d2#

因此,这更像是一个数学/物理问题,而不是python。据我了解,, change_ychange_x 代表球的速度。你希望有一个恒定的速度(我的意思是大小),但有一个嘈杂的Angular 。
就代码而言,它意味着:


# This is the speed of your ball.

    math.sqrt( (ball.change_y)**2 + (ball.change_x)**2 ) = constant

因此,您希望通过以下方式更改代码:

if ball.y > SCREEN_HEIGHT - BALL_SIZE or ball.y < BALL_SIZE:
                ball.change_y *= -1*random.uniform(0,1)
                ball.change_x = math.sqrt(2*(SPEED**2) - ball.change_y**2)
if ball.x > SCREEN_WIDTH - BALL_SIZE or ball.x < BALL_SIZE:
                ball.change_x *= -1*random.uniform(0,1)
                ball.change_y = math.sqrt(2*(SPEED**2) - ball.change_x**2)

因此,速度将是恒定的,但相对x和y分量将是可变的。

相关问题