【发布时间】:2021-10-16 06:21:21
【问题描述】:
嘿,我正在尝试使用 pygame 创建一个突破性克隆,我使用了
self.course(180 - self.course) % 360
要反弹桨的球,但是我正在研究矢量 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
【问题讨论】:
-
你究竟想通过使用它来实现什么?
-
我想避免使用 cos 和 sin,以熟悉在 pygame 中使用 vector2。我相信我需要在使用 vector2 时声明我的所有对象,但我不熟悉该过程。希望这回答了你的问题
标签: python vector pygame collision-detection