如何让球与窗框碰撞,反弹并限制球在矩形区域内?

4
嘿,我正在尝试用Pygame创建一个打砖块的克隆游戏,并且我使用了
self.course(180 - self.course) % 360

要将球从球拍上弹起来,我正在研究Vector2类,但是我不知道如何将我的Ball类转换成它。如果有人能指导我正确的方向,那就太好了。
这是我想用Vector2转换的代码。
import pygame
import math

class Ball(pygame.sprite.Sprite):

    course = 130

    def __init__(self):
        # Calling the parent class (Sprite)
        pygame.sprite.Sprite.__init__(self)

        # Creating the ball and load the ball image
        self.image = pygame.image.load("ball.png").convert()
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 270

    # Creating a bounce function to make the ball bounce of surfaces.
    def bounce(self, diff):
        self.course = (180 - self.course) % 360
        self.course -= diff
        
    # Create the function that will update the ball.
    def update(self):
        course_radianse = math.radians(self.course)
        self.rect.x += 10 * math.sin(course_radianse)
        self.rect.y -= 10 * math.cos(course_radianse)
        self.rect.x = self.rect.x
        self.rect.y = self.rect.y
        
        # Check if ball goes past top
        if self.rect.y <= 0:
            self.bounce(0)
            self.rect.y = 1
            
        # Check if ball goes past left side
        if self.rect.x <= 0:
            self.course = (360 - self.course) % 360
            self.rect.x = 1
            
        # Check if ball goes past right side
        if self.rect.x >= 800:
            self.course = (360 - self.course) % 360
            self.rect.x = 800 - 1
            
        if self.rect.y > 600:
            return True
        else:
            return False

你使用它的目的是什么? - michalwa
我想避免使用cos和sin,在pygame中熟悉使用vector2。 我相信我需要使用vector2来声明所有对象,但我不熟悉这个过程。希望这回答了你的问题。 - MerrinX
1个回答

11
一个向量定义了一个方向和大小。你需要将此向量加到球的位置上。遗憾的是,pygame.Rect 只存储整数,因此对象的位置也必须存储在 pygame.math.Vector2 中。你需要一个向量来表示对象的位置和另一个向量来表示方向。每当位置发生变化时,就需要通过四舍五入的位置设置 .rect 属性。 如果物体撞击了表面,则球会被表面的法向量反射(.reflect())。
最简单的例子: repl.it/@Rabbid76/PyGame-BallBounceOffFrame

import pygame
import random

class Ball(pygame.sprite.Sprite):

    def __init__(self, startpos, velocity, startdir):
        super().__init__()
        self.pos = pygame.math.Vector2(startpos)
        self.velocity = velocity
        self.dir = pygame.math.Vector2(startdir).normalize()
        self.image = pygame.image.load("ball.png").convert_alpha()
        self.rect = self.image.get_rect(center = (round(self.pos.x), round(self.pos.y)))

    def reflect(self, NV):
        self.dir = self.dir.reflect(pygame.math.Vector2(NV))

    def update(self):
        self.pos += self.dir * self.velocity
        self.rect.center = round(self.pos.x), round(self.pos.y)
   
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

all_groups = pygame.sprite.Group()
start, velocity, direction = (250, 250), 5, (random.random(), random.random())
ball = Ball(start, velocity, direction)
all_groups.add(ball)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    all_groups.update()

    if ball.rect.left <= 100:
        ball.reflect((1, 0))
    if ball.rect.right >= 400:
        ball.reflect((-1, 0))
    if ball.rect.top <= 100:
        ball.reflect((0, 1))
    if ball.rect.bottom >= 400:
        ball.reflect((0, -1))

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), (100, 100, 300, 300), 1)
    all_groups.draw(window)
    pygame.display.flip()

假设你有一组块:
block_group = pygame.sprite.Group()

检测球和块组的碰撞。一旦检测到碰撞(pygame.sprite.spritecollide()),就在块上反射球:
block_hit = pygame.sprite.spritecollide(ball, block_group, False)
if block_hit:
    bl = block_hit[0].rect.left  - ball.rect.width/4
    br = block_hit[0].rect.right + ball.rect.width/4
    nv = (0, 1) if bl < ball.rect.centerx < br else (1, 0)
    ball.reflect(nv)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接