在 pygame 中使用 vector2。与窗框碰撞,将球限制在矩形区域内

Mer*_*inX 2 python pygame vector collision-detection

嘿,我正在尝试用 pyame 创建一个突围克隆,我用过

self.course(180 - self.course) % 360
Run Code Online (Sandbox Code Playgroud)

为了反弹桨的球,但是我正在研究矢量 2 类,但我不知道如何使用它来转换我的 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
Run Code Online (Sandbox Code Playgroud)

Rab*_*d76 7

矢量定义了方向和数量。您必须将向量添加到球的位置。遗憾的是pygame.Rect只存储整数,因此对象的位置也必须存储在pygame.math.Vector2. 您需要 1 个向量作为对象的位置,第二个向量作为方向。每次位置发生变化时,.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()
Run Code Online (Sandbox Code Playgroud)

假设您有一组块:

block_group = pygame.sprite.Group()
Run Code Online (Sandbox Code Playgroud)

检测的碰撞ballblock_group。一旦pygame.sprite.spritecollide()检测到碰撞 ( ),reflect块上的球:

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)
Run Code Online (Sandbox Code Playgroud)