📄 tutorial.txt
字号:
Adding the ball
***************
After what we did when adding the bat, this is very simple.
Because of the objects module's structure, all we need to do is
add a new module to handle the ball, and tell the objects
module to track it.
The ball module
===============
This is very similar to the bat module, externally. It should
be fairly self-explanatory. It doesn't need to know about
keyboard input, obviously; it just bounces around on its own.
Note that the ball module itself is handling the bounces off
the walls, but it doesn't know about the bat, so it can't
bounce off that. Again, this module uses `layout.h' to find
out the size of the arena. The major advantage of this is that
we can change the arena size if we need to by simply adjusting
the settings in `global.c'. If we'd put numbers directly into
all of these source files then they'd all need changing, which
would be rather awkward and prone to error.
Changes to the objects module
=============================
Again, these are fairly simple. Everything we were doing for
the bat we are now also doing for the ball. That's about it.
Making the ball bounce
======================
Since the walls are all vertical or horizontal, this is very
simple. If the ball is bouncing off a vertical wall, we just
need to flip the sign of the X component of its velocity. If
it's bouncing off a horizontal wall then we flip the sign of
the Y component of the velocity.
Detecting the bounces is also easy. If the X coordinate has
dropped past the left wall, or increased past the right wall,
then we need to do a vertical-wall bounce; similarly, if the Y
coordinate has dropped past the ceiling or increased past the
floor then we do a horizontal-wall bounce. Normally in this
game, of course, the ball doesn't bounce off the floor, but
we'll make it do so for now, to check that it's bouncing around
properly.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -