📄 tutorial.txt
字号:
Making the bat and ball work together
*************************************
Now we need to make the ball bounce off the bat when it hits
it. As mentioned earlier, the ball and bat modules don't know
anything about each other at the moment. The objects module,
though, does know about both, and contains the variables with
their positions -- so it's a good place to do the collision
detection.
Detecting the collision
=======================
When has the ball hit the bat? Well, it must have crossed the
line in which the bat lies. It must also have passed through
the bat, rather than missing it to the right or left.
To check for the first condition we test whether:
a. the ball was previously above the bat but is now on or
below it, or
b. the ball was previously below the bat, but is now on or
above it
For the second test we must check that the ball's X coordinate
is greater than (or equal to) the bat's left coordinate, and the
ball's Y coordinate is less than (or equal to) the bat's right
coordinate. Strictly we should be interpolating between the new
and old positions, and checking the X coordinate of the ball as
it crossed the line of the bat, but since our ball is moving in
such small steps (one pixel at a time) it doesn't matter.
Making the ball bounce
======================
This is simple; the bat is horizontal, so we can use the same
technique as we did in the ball module itself for bouncing off
horizontal walls.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -