Developing Game-Themed Applications with XNA
Day 1 - Section III: The Block Breaker
Game
d. The Block and Paddle Class
back to Day-1 main page
References:
- Pre-requisite: this is the
example (Section IIIc. The Ball class) we are building off from and here
is the source file we will
begin working with.
- Here is the result source code from this
example.
Goals: In this tutorial we want to experience:
- Verify our observation from before, for any object, we define its
appearance in the constructor, and define a update function to response to
user input.
- Understand that after all objects have been updated, we can then let
the objects interact (in UpdateWorld()).
Library function 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_Rec: the rectangular block.
- Constructor: sets the center position of the block.
- Position : access the center position of the block.
- CollideWithBall: determines if a collision has
occurred. If so, computes a new velocity for the ball.
- Calls the XNACS1Primitive.Collided function to collide between
the Rectangle and the Circle.
- 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
- 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 we 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 call ClampAtWorldBound()
to make sure 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.
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 update
each object independently, and then be concerned with the 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, and 16531.