Developing Game-Themed Applications with XNA
Day 1 - Section III: The Block Breaker
Game
f. Including file texture images
back to Day-1 main page
References:
- Pre-requisite: this is the
example (Section IIIe. BlockSet class) we are building off from and here
is the source file we will
begin working with.
- Here is the result source code for this
example.
- For details of file texture and organization of the source code folder,
please refer to the
library documentation.
Goals: In this tutorial we want to experience:
- Working with file textures!
Library function to notice:
-
XNACS1LibBase.World.BackgroundTexture: the
texture (image) to as background of the application window.
-
XNACS1Primitive.Texture:
For both rectangle and circle, we can set their texture at any point
in our program.
Implementation:
-
Including texture resources: Under the Solution Explorer, open the
Content tab,
- Right-mouse-click on the Resources folder:
-
Add new folder: Textures.
-
Add textures: Right-mouse-click over the Textures folder: Add
Existing Items
-
Browse and add the following images (under
TextureImages
folder):
 |
 |
 |
bgTexture.jpg |
blockTexture.jpg |
paddleTexture.jpg |
- The above actions will include these two images as part of the
project. Now, we can use these images in our project.
-
Background Texture: Game1.cs:
- InitializeWorld: include the following line:
- This function will cause our application to use this
image as the background.
- Block Texture: Block.cs
- Constructor: passes in the texture "blockTexture"
to the rectangle constructor.
- Paddle without texture: Paddle.cs:
Recall that the Paddle class is a subclass from the
Block class. The Block constructor assigns the blockTexture by
default. In the Paddle constructor, we change the texture:
Lesson Learned:
There are two things we should keep in mind when working with file textures:
- Inclusion into the project: we must add each texture image into the
project. These images must be added to the Textures
folder under Content/Resources.
- Using the texture image: Notice we use the texture image without
the file name extension.
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.