What do you want?

So tell me what do you want?

Introduction-Lets make a game

Yes finally with enough time I am going to be posting tutorials on how to create a game in XNA.

For simplicity we use XNA 3.0 Beta and Visual C# 2008 Express Edition SP1.

This game will show you how to use sprite sheets, use animated sprites, and do basic collision detection with rectangles.

namespace

namespace:Definition: Usually it is your project and where all your code is contained.If your code is not contained in the same namespace then you cannot access the code or classes.
Example
namespace MyNamespace

Go back to home page

Differences between the Xbox 360 and the PC

Okay recently I have been to the XNA Creators Club website while cruising through the forums i noticed there were a few topics about performence issues differences between PC games and Xbox 360 games built with the XNA Framework…So I am going to break it down for you in the simplest way possible.This table displays the most common differences between the two.

Xbox 360 PC
.Net Framework Type Compact version(so it isn’t quite as good as the pc version) Full Version
Number of Processors Three Processors Anywhere from one to four are common
Processor Speed Runs at 3.2Ghz each Variable,laptops commonly have 800mhz to 1.5Ghz, where Desktop computers commonly have 1ghz to 4Ghz(that I have seen)
Shader Profile(Vertex and Pixel Shader profiles) custom 3.0 1.1 to 4.0
Type of Graphic Card ATI Variable,common are Nvidia and ATI
Designed for? Made for Heart pounding hardcore games Made for general common use.

It’s important to remember that the garbage collector(or gc) doesnt always run at regular times, so it is important to get rid of those resource hungry objects in a timely matter.
If you use the “XNA Framework remote performance monitor for Xbox 360″ program included witht the xna framework then look for these common things.

Total Garbage Collections
How fast the Garbage collections are happening.

these are the main things to look for.Remember, sometimes it is not the gc though, so run through some test before blaming the gc for the problem.

Here is a article written by Shawn Hargreeves.It also gives some helpful insight about why your games might be running slow on the xbox 360.
Go back to home page

The using statement

using-Definition1:
used to reference namespaces.These namespaces are often found in seperate DLLs,COM Libraries,or within the very project itsself.
Example: using System.Windows.Forms;
Definition2:Often used with resource hungry objects such as the Pen object or the Brush object,doing so caues the Garbage Collector to dispose of it as soon as possible, Sometimes it can bring about better performance.
Example:

using(Form1 form = new Form1())
{
Application.Run(form);
}

OR

using(Pen pen = new Pen(Color.Black))
{
//Insert your code here that uses the pen object
}

———
Go Back to the Home page..

First C# Application

First things first…congrats on choosing c# as a language, it is a great programming language to learn and master..
From the title you can guess what we will be learning how to do today… and it is not a Hello World example..Hello world does not help. exept to teach you how to output text to a console window..
that really isn’t much help when creating windows applications so we are going to learn how to do it in a Windows application.

This tutorial assumes that you have at least Visual C#  2005 or 2008 Express IDE installed..
(IDE stands for Integrated Development Enviorment)
if you don’t you can get it here at the offical website. It is availble as a free download,just be sure to register it, which is also free.

Here is the basic tutorial outline:

1.Learn about creating window applications easily.
2.Learn about the key features in the IDE
First start up the Visual C# IDE you should see something like this
Photobucket

Now look for a menu that looks like this:

Photobucket
On that menu click File, then New Project.

You should now be presented with something that looks like this.
Photobucket

From the selelction click Windows Form Application, name it whatever you want to name it, for me i named it FirstApp

When the project is done being created feel free to save it.To save everything go to File, then Save All, Click Ok,look at where your project is going to be saved to then click ok again.

After the project had been created you should’ve been presented with something like this.

Photobucket
This is called the Design View.
If you do not somthing like this:
Photobucket
Then go to View, then Click on Solution ExplorerThe solution explorer shows all the code files in your project and allows easy access to them. Another important thing to have at the ready is the
ToolBox which looks like this:
Photobucket
If you don’t see that then go to View, then click on ToolBox.

Okay now that we have that set up we can start to create a simple user interface.

First click on the Common Controls Tab that is on the tool box, you should see things such as buttons,
picture boxes, and a webbrowser(which will be in the next tutorial if enough feedback is sent).These are known as Controls,now if the controls in the toolbox you can always create your own, once again this will be on another tutorial.
For now though the toolbox controls are more than enough to create a good application.

Back to business.

Either Click and drag a button control from the toolbox onto the form(should say Form1)or double click the button control from the toolbox, feel free to place it anyway on the form it really doesnt matter right now.

Now double click the button control that is on the form.

You should now see something like this:

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FirstAppTut
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
}
}
}
What the project has just done is add a event handler to the button so it knows what to do when it is clicked otherwise it will do nothing.

between the curly brackets under private void button1_Click(object sender,EventArgs e)
type this.

this.Close();
This will tell the window to close if the user should click on it.
Now we are going back to the design view, what you just saw was the code view, to go
back to the Design view, go to View and click on Designer.To go back click the form once then View and click on Code(or press F7)

Now put a textbox control onto the form,it doesnt matter where.

Now put a label control, this time put it below anywhere below the textbox.

Now double click the textbox control on the form.

you will see that
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
has been added to your code.
Now between the curly brackets of the newly added code type this.
label1.Text = textBox1.Text;
What this does it set the label(that you put onto the form)’s text to whatever is the textbox control’s text is.
To see this in action hit the F5 button on your keyboard or go to Debug and click Start Debugging.
you should see something like this.By Debuging your app you are compiling it into an actual program.
Photobucket
If you’ve done everything right you should be able to close the program with the button(or the button with the X )and when you put text into the textbox, the label should change to what you put in the textbox..

So what have we learned so far..?
1.A few key features of the IDE
2.How to run and Debug our Application.
3.How to take user input and show it on a label control
4.The tool box
5.How to make a program close with a click of a button.

What we’re going to learn now is about the Properites of a control.This will literally take a minute or two to learn.
First if you don’t see the this:
Photobucket
Then go to View and click on Property Window
With the property window You can set the text, add an image to a control, or add more event handlers to a control.
O i almost forgot if you click on the tab next to form1.cs(it is in your solution explorer) you should see Form1.Designer.cs double click it, you should now see all the code that was added in automatically for you, just don’t modify the code as it could mess up your project..

Here is what your code should’ve looked liked(that you added)

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
}

As always if you have anyquestions feel free to ask them.
Go back to home page

MessageBoxes in XNA 1.0 & 2.0 & 3.0(Windows only)

Recently I was making a small 2d game. Here was the basic(very basic)  design of it.

1.  Shoot the most targets in under 60 seconds.

2. Once the time runs out a message box pops up and tells the user that he/she is out of time.

Not to elaborate, but simple
———————————————————————————

I was able to get everything up and running. Everything but the messagebox.Now my first thought was to add a reference to the System.Windows.Forms namespace but that caused an error. Now I know of a way around that error but i wanted to try anotherway.

To figure this out i decided to go back to my c++ roots and dig around some of my old win32 app code.

Once i figured that out it was a simple thing to import it to c#.  And i choose to share the code with you!

first add this to your using referernce

using System.Runtime.InteropServices;

then put this above your Game1() method

[DllImport("user32.dll",CharSet = CharSet.Auto)]
public static extern uint MessageBox(IntPtr hWndle, String text, String caption, int buttons);

First we import the dll that contains the messagebox function(which is found on all windows operating systems), then we make declare the messagebox as public,static and extern(external) because the Messagebox method its self is not in our project.Then for the variables we tell it the handle, what text to display, and the button numbers.

So for example to use the messagebox, try this.

MessageBox(new IntPtr(),”messagebox”,”message”,0);

This may not seem simple(due to my explanations)  but it this will prevent building errors in the most simplest way( in my opinion)

Go back to home page

Paint .NET 3.31

Paint .NET is a high quality paint program that is made avalible for free.

You can download it here at my virtual drivePaint.NET.3.31.zip or here at the official site

I highly recommend this program if your in need of creating 2d art or editing some textures or images.
Go back to home page

Making a sprite transparent(XNA 2.0 & 3.0)

OpacityTut.zip
SmileFace.PNG

Hello and welcome to one of my tutorials.
Now in this tutorial I am going to show you how to make your sprites(or textures if you want to call them that) transparent. I assume that you at least know the basics of c# and xna.

Lets First add a image to your project.(If you don’t know how to do this then, right click on Content (this can be found in the solution explorer)and then click on add existing, now browse to your image.Select it and click add.).I recommend using a .png image because parts of the image can be transparent with out any code needed. For this example i used a image that I created.If you want to use this image then you can download it from the above link along with the source.
Now in the Game1 class add this code above the constructor.

//For the sprite texture
Texture2D spriteTex;
//For scaling the sprite to fill the screen
Rectangle spriteArea;
//To set the alpha value of the image
//Set it to 255 so we first see the image.
byte alpha = 255;
//Will deterimine if the sprite is fully transparent or not
bool isTransparent = false;

Now put this code in the Initialize() method or the LoadContent() method

spriteArea = new Rectangle(GraphicsDevice.Viewport.X, GraphicsDevice.Viewport.Y, GraphicsDevice.Viewport.Width,GraphicsDevice.Viewport.Height);

And put this in the LoadContent() method

//Replace the "smile_face" with your own image's asset name
spriteTex = Content.Load("smile_face");

Okay now that our sprite is loaded we can start drawing it now. So in the Draw(GameTime gameTime) method insert this code.


spriteBatch.Begin();
spriteBatch.Draw(spriteTex, spriteArea, new Color(255, 255, 255, alpha));
spriteBatch.End();
alpha--;

If you run the project now you should see your sprite fading out over time. But you’ll also see another problem. Eventually the sprite will sort of pop back up fully non transparent. So lets fix this shall we.
First remove alpha--;(this is below spriteBatch.End();)
Now below the the spriteBatch.End();
add this code:


if (isTransparent == false)
{
if (alpha = 0)
{
//Increment the alpha value by one as long as it is greater than or equal to 0;
alpha++;
}
if (alpha == 255) //IF the alpha value is equal to 255 then we must now decrease it.
{
isTransparent = false;//set the isTransparent to false
//return;//This return is optional
}
}
alpha = (byte)MathHelper.Clamp(alpha, 0, 255);

That should fix the problem now.It is also important to know that the code above(that uses the isTransparent bool) can be put in the update method(which may bring better results).I just felt like putting it in the drawing code.

Below is the full code in case you got lost somewhere and don’t want to download the source project.


using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace OpacityTut
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D spriteTex;
Rectangle spriteArea;
bool isTransparent = false;
byte alpha = 255;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteTex = Content.Load(“smile_face”);
//This code could also go into the initialze method of this class
spriteArea = new Rectangle(GraphicsDevice.Viewport.X, GraphicsDevice.Viewport.Y, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.Draw(spriteTex, spriteArea, new Color(255, 255, 255, alpha));
spriteBatch.End();
if (isTransparent == false)
{
if (alpha = 0)
{
//Increment the alpha value by one as long as it is greater than or equal to 0;
alpha++;
}
if (alpha == 255) //IF the alpha value is equal to 255 then we must now decrease it.
{
isTransparent = false;//set the isTransparent bool to false to decrement the alpha value
//return;//This return is optional
}
}
alpha = (byte)MathHelper.Clamp(alpha, 0, 255);
base.Draw(gameTime);
}
}
}

Go back to home page

Follow

Get every new post delivered to your Inbox.