5. The Block and Paddle Classes
back to Tutorial main page
References:
Goals: In this tutorial we want to experience:
- Interaction of objects: the ball and the paddle
Library functions to notice:
Implementation:
-
Create a new file: Block.cs. This is a simple
rectangular block that knows how to bounce a Ball. Here is the link to
the file.
Interesting features of this class includes:
- Instance variables:
-
m_Position: center
position of a rectangular block.
- Constructor: sets the center position of the block.
- Position and Bounds: access the position
and returns the left/right/top/bottom bounds of the block.
- CollideWithBall: determines if a collision has
occurred. If so, computes a new velocity for the ball.
- Bound-to-Bound check: collides the the ball by checking the x/y
bounds of the block with the x/y bounds of the ball. Notice this is
only a pretty good approximation of actually intersecting a
circle with a rectangle.
- Velocity of Ball: If a collision has occurred, check, if
collision at the x-boundaries: flip the X-component of the velocity
otherwise, flip the Y component.
- Update the ball's bounces
- Draw: draws a rectangle.
- Create a new file: Paddle.cs. This will be the paddle at the
bottom of the application window. Notice that the paddle is a special type of
rectangular Block, so subclass we will subclass from the Block
class. Here is the link to the
source file. Interesting features of this class include:
- Instance variable:
we do not need any new instance variables.
- MovePaddle:
allow horizontal movement of the paddle. We check to make sure the
center of the paddle does not move off the application window.
- BounceBall: Calls super class's CollideWithBall function. If a collision indeed
occurs:
- Ball velocity: make sure the ball travel's upwards
after the collision.
- Ball position: make sure the ball is above the paddle
right after collision.
- Changes to: Game1.cs
- Instance variable:
-
Paddle
m_Paddle: The paddle!
- InitializeWorld: allocate memory and instantiate
m_Paddle.
- UpdateWorld:
- Move paddle: pass along the left-thumbstick horizontal
movement as controls of the paddle horizontal position.
- Paddle bounces ball: if there is a ball, compute the
interaction between the ball and the paddle by calling the
paddle's BounceBall() function.
- DrawWorld:
- Paddle: always draws the paddle.
- Ball: if present draws the ball, and echo the number of
bounces.
Lesson Learned:
We worked with two objects in the application: the ball and the paddle.
During the Game1::UpdateWorld() function we:
- Update the ball: create or update the ball position. Notice, the
ball is updated independent from the paddle, we don't need to know anything
about the paddle, we can create the ball and collide the ball against the
application window boundary.
- Update the paddle: here we move the paddle horizontally. Similar to the
ball update, the paddle's update can be carried out independent from the
ball.
- Interaction of the objects: after both objects are properly updated, we
then interact the two. In this case, we call the paddle's bounce ball
function to bounce the ball.
In general, when our application has multiple objects, we can usually update
each object independently, and then be concern about interaction between the
objects.
This document and the related materials are developed with support from
Microsoft Research Computer Gaming Initiative under the Computer Gaming
Curriculum in Computer Science RFP, Award Number 15871.