Thursday, November 25, 2010

Videogames Part 4: The First Game

This final part will be a quick tutorial on a basic program in XNA. The program will have an image bouncing of the edges of the window.

To start you'll need to create a new XNA windows project, should with Xbox, or WP7 but I'm focusing on the windows one right now. Once the project is created in the content section in the solution explorer right-click and add an existing item. Browse to any image (jpg, png or bmp preferably) and load it. This will be the image that will bounce in the program.
We add some variable:

        Texture2D theImage;                                           // our image (*.jpg, *.png *.bmp)
        Vector2 posImage = Vector2.Zero;                      // initial position of the image
        Vector2 velImage = new Vector2(150.0f, 150.0f); // velocity vector
        int MaxX, MinX, MaxY, MinY;                             //limits
Then in load content we need to load our image:

            theSprites = new SpriteBatch(GraphicsDevice);
            // load the images
            theImage = Content.Load("imageName");//note: no extension in the image name
            MaxX = device.Viewport.Width - theImage.Width;//calculate the horizontal limit
            MinX = 0;
            MaxY = device.Viewport.Height - theImage.Height;//calculate the vertical limit
            MinY = 0;
Now for the update, where we update our variables:

GamePadState theXbox = GamePad.GetState(PlayerIndex.One);
            KeyboardState theKeyboard = Keyboard.GetState();
            //a way for the user to exit the program with the keyboard or xbox controller.
            if (theXbox.Buttons.Back == ButtonState.Pressed || theKeyboard.IsKeyDown(Keys.Escape))
            // modify the image position with our velocity vector and the time.
            posImage += velImage * (float)gameTime.ElapsedGameTime.TotalSeconds;
            //chech for horizontal bounce
            if (posImage.X > MaxX)
                velImage.X *= -1;
                posImage.X = MaxX;
            else if (posImage.X < MinX)
                velImage.X *= -1;
                posImage.X = MinX;
            // check for vertical bounce
            if (posImage.Y > MaxY)
                velImage.Y *= -1;
                posImage.Y = MaxY;
            else if (posImage.Y < MinY)
                velImage.Y *= -1;
                posImage.Y = MinY;
 Finally in the draw, we tell the program what to draw in the screen:

            // draw the image
            theSprites.Draw(theImage, posImage, Color.White); //here white acts like transparent
You can download the full solution here. It was made in VS2010 so you'll need VS2010 to open it. Also check a friend's blog for another XNA tutorial here. Note his blog is in spanish, but the code is universal :D

Videogames Part 3: Starting with XNA

The first thing you'll need to get started with XNA will be to get the tools for the job. You can get them here at App Hub or you can also get the at Dreamspark if you are a student. At Dreamspark is important to note you can get a free year of XNA Creators Club. With it you can test/run your games on your Xbox directly. Once you have all the tools for the job you'll need to learn the basics for XNA.

To get started I recommend getting a book on the subject. The book I recommend is Learning XNA 4.0 which includes many lessons on the latest version of XNA. But don't just use a book when you have the internet as a resource. The first page I recommend to anybody interested in XNA has to be Microsoft's App Hub. There you will find tons of tutorials for the PC, Xbox, and Windows Phone 7. From basic 2D stuff to more complex things in 3D. The other site I recommend is GameDev. This site is for anybody interested in game development and is great to interact with other game developers. The purpose of GameDev according to their about, " was created with the sole purpose of providing a public site where game developers could freely exchange information." Here you'll be able to ask fellow game developers for tips and tricks. You can also check the many articles the staff puts out for gamers to learn more about game development. Do note this site is for game development in general and not just XNA.

Wednesday, November 24, 2010

Videogames Part 2: The Technologies

X= -..-  N= -.  A= .-
When it comes to videogames there are many tools in which one can develop a game. You can use tool(s) like DirectX, OpenGL, Unreal Engine, Source Engine, Havok Engine, Flash, and XNA to name a few. But the one that really excited me was XNA due to the fact that you can publish your game to Xbox Live for other people to play. It's also pretty easy to use (compared to the others) when your just beginning to learn.

The XNA framework makes it simple to make games for Xbox, PC, and Windows Phone 7 without the hassles of worrying for compatibility issues. This is because XNA wraps all the low-level details, such as DirectX settings, so the user doesn't have to worry. The only real differences when developing for different platforms are the controls. Even these for the PC and Xbox can be the same as they a both compatible with the Xbox controller. Another good reason XNA is great to start developing games is that it has many predefine methods which simplify tedious tasks. This is why I would recommend any one interested in games to start with XNA which is great way to understand how games differ from any other application.

Videogames Part 1: My Passion explained

Videogames: One of the youngest, if not the youngest, forms of entertainment. They began to gain popularity during the 1980's. But they got my interest during the 1990's with NES, SNES & N64. I can't remember the first time I played a game, but I can't remember a time without them in my life. By the time the N64 came out (1997) I had already decided that I wanted to dedicate myself to creating games. Of course by then I didn't know any of the specifics about creating games, but I knew I would like it. Finally years later (high school) I learned the specifics and loved what they were. So with videogames being my main goal I set out to study Computer Science. So with videogames being this influential in my life, I have a great passion to make my own.