我如何反转矩形的运动方向?

2024-09-24 00:27:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编程很陌生,因为它只是作为一门学科引入我的学校,我需要一些帮助。我被赋予了一个任务,有三个球(矩形图像)在屏幕上弹跳并相互分离的动画。我有三个球,墙上的弹跳都很好,但我不知道如何让它们互相弹跳。如有任何帮助将不胜感激,我目前的代码如下:

import pygame
import random
import sys

if __name__ =='__main__':

    ball_image1 = 'beachball1.jpg'
    ball_image2 = 'beachball2.jpg'
    ball_image3 = 'beachball3.jpg'
    bounce_sound = 'thump.wav'
    width = 800
    top = 600
    x = 0
    y = 0
    background_colour = 0,0,0
    caption= 'Bouncing Ball animation'
    velocity1 = [-1,-1]
    velocity2 = [-1,1]
    velocity3 = [1,-1]
    pygame.init ()
    frame = pygame.display.set_mode ((width, top))
    pygame.display.set_caption (caption)
    ball1= pygame.image.load (ball_image1). convert()
    ball2= pygame.image.load (ball_image2). convert()
    ball3= pygame.image.load (ball_image3). convert()
    ball_boundary_1 = ball2.get_rect (center=(random.randint(50, 750),random.randint(50, 550)))
    ball_boundary_2 = ball2.get_rect (center=(random.randint(50, 750),random.randint(50, 550)))
    ball_boundary_3 = ball2.get_rect (center=(random.randint(50, 750),random.randint(50, 550)))
    sound = pygame.mixer.Sound (bounce_sound)
    while True:
        for event in pygame.event.get():
            print event 
            if event.type == pygame.QUIT: sys.exit(0)
        if ball_boundary_1.left < 0 or ball_boundary_1.right > width:
            sound.play()
            velocity1[0] = -velocity1[0]
        if ball_boundary_1.top < 0 or ball_boundary_1.bottom > top:
            sound.play()
            velocity1[1] = -velocity1[1]
        if ball_boundary_2.left < 0 or ball_boundary_2.right > width:
            sound.play()
            velocity2[0] = -velocity2[0]
        if ball_boundary_2.top < 0 or ball_boundary_2.bottom > top:
            sound.play()
            velocity2[1] = -velocity2[1]
        if ball_boundary_3.left < 0 or ball_boundary_3.right > width:
            sound.play()
            velocity3[0] = -velocity3[0]
        if ball_boundary_3.top < 0 or ball_boundary_3.bottom > top:
            sound.play()
            velocity3[1] = -velocity3[1]

        ball_boundary_1 = ball_boundary_1.move (velocity1)
        ball_boundary_2 = ball_boundary_2.move (velocity2)
        ball_boundary_3 = ball_boundary_3.move (velocity3)
        frame.fill (background_colour)
        frame.blit (ball1, ball_boundary_1)
        frame.blit (ball2, ball_boundary_2)
        frame.blit (ball3, ball_boundary_3)
        pygame.display.flip() 

Tags: orplayiftoprandomwidthframepygame
3条回答

实际上,在你提供的代码中你有一些线索。检测球是否在墙上反弹的方法是检查球的顶部边界是否小于0,这意味着它已接触到顶部边界,需要反弹,以便更改方向。在

所以代码:

if ball_boundary_1.top < 0 or ball_boundary_1.bottom > top:
        sound.play()
        velocity1[1] = -velocity1[1]

如果球在地板或天花板上反弹,基本上会改变为垂直运动。在

所以要修改这个,你需要问问你自己,如果两个球互相弹跳意味着什么…这意味着它们的边界以多种方式重叠。例如,你可以检查球1的左边或右边在球2的水平尺寸内,球1的顶部或底部在球2的垂直尺寸内,然后两个球接触,所以改变两个球的速度。在

正如我的讲师在我对这个问题的特殊评估中所说的

Once a second ball is bouncing around the frame, add some code to change the directions of the balls if they collide. PyGame provides a function that allows you to detect if a collision has occurred between two Rect objects. The function is named colliderect and is called in the following manner. If two Rect objects are named ball_boundary_1 and ball_boundary_2, you could check for a collision with:

if ball_boundary_1.colliderect(ball_boundary_2):

    # alter the direction of the balls with the usual method

所以,这将被用来检查是否发生了碰撞,返回True,并运行一些代码来反转速度。祝你好运,我自己还没有真正做到这一点,我正准备试试

当两个物体“碰撞”时,它们基本上存在于同一个物理空间中,因此你必须对此进行检查。在二维这是很好和容易,特别是矩形形状。基本上,编写一个名为“overlap”的函数,如果两个球发生碰撞,则返回一个真值。例如:

for (i = 0; i < numberOfBalls; i++) {
    for (j = i+1; j < numberOfBalls; j++) {
        if (overlap (ball_rect[i], ball_rect[j])) {
            // you will need to write this reverse velocity code differently
            // to comply with the code above but I recommend that the balls 
            // go in an array with three elements so that you can index 
            // them. (Useful in for loops as seen above)
            ball_xvelocity[i] *= -1;
            ball_xvelocity[j] *= -1;
            ball_yvelocity[i] *= -1;
            ball_yvelocity[j] *= -1;
        }
    }
}

我将把重叠功能留给你,但你应该谷歌'矩形碰撞检测',你会发现如何。你会发现这真的没有那么可怕,会让你相信你可以研究和学习编程概念。在

N.B.特别没有给出python代码。既然是家庭作业,你的工作就是写答案。对不起,这只是一个普通的政策。希望这有帮助。:)

相关问题 更多 >