回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>因此,我尝试使用实例对我的游戏进行碰撞检测。代码如下:</p>
<pre><code>class Paddle:
def __init__(self, x, y, width, height, rect):
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.rect.Rect(x, y, width, height)
class Ball:
coordinateX = 600
coordinateY = 300
velocity = [random.randint(0,1),random.randint(-1,1)]
player = (Paddle(1100, 300, 10, 30, (1100, 300, 10, 30)))
enemy = Paddle(100, 300, 10, 30, (100, 30, 10, 30))
ball = (Ball.coordinateX, Ball.coordinateY, 15)
</code></pre>
<p>后来:</p>
<pre><code>ballRect = pygame.rect.Rect(Ball.coordinateX, Ball.coordinateY, 10, 10)
if pygame.sprite.collide_rect(player, ballRect):
bounce()
</code></pre>