Developing Game-Themed Applications with XNA
Day 1 - Section III: The Block Breaker
Game
e. The BlockSet class
back to Day-1 main page
References:
-
Pre-requisite: this is
the
example (Section IIId. Block and Paddle classes) 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:
- Breaking blocks with the ball!
Implementation:
-
Create a new file: BlockSet.cs. This is container class
for the blocks hang-off the middle of the application window. Here is the link to
the file.
Interesting features of this class includes:
- Instance variables:
-
m_TheSet: linear list of
Block.
-
m_TotalBreak: Total
number of blocks broken so far.
- Constructor: creates and initializes the array blocks.
- InitializeBlock: Create the array of blocks.
- BreakBlock: go through the array of Block
looking for intersection with the ball. Notice:
- Single intersection: As soon as one intersection is detected, we
stop looking.
- Statistics: updates the number of broken blocks.
- Changes to: Game1.cs
- Instance variable:
-
BlockSet
m_BlockSet: The
blocks!
- InitializeWorld: allocate memory and instantiate
m_BlockSet.
- UpdateWorld:
- Re-Create BlockSet: if button-B is pressed, we will recreate
a new block set.
- Breaking blocks: pass the ball into the BlockSet
class to break blocks.
Lesson Learned:
The only slightly interesting point to note is, w can re-create the block set
in the middle of the ball-flight. This kinds of make the game interesting (or
un-interesting). Try it, send the ball flying, when the ball reach the top of
the application window, press-B. Now the ball is trapped at the top-half of the
application window. Cool? Maybe not?
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.