Developing Game-Themed Applications with XNA Game Studio
Section III: The Block Breaker
Game
d. The SetOfBlock class
back to workshop main page
References:
Goals: In this tutorial we want to experience:
- Breaking blocks with the ball!
Implementation:
-
Create a new file: SetOfBlocks.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.
- Draw: draws all the blocks and update status of number
broken and number left.
- Changes to: Game1.cs
- Instance variable:
-
SetOfBlocks
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 SetOfBlock
class to break blocks.
- DrawWorld:
- Paddle: always draws the paddle.
- Ball: if present draws the ball, and echo the number of
bounces.
- Block set: draws all the 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.