XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

The Very Beginners Guide To The XNA Framework: Lesson1

Last post 03/11/2009 12:28 by Jim Perry. 2 replies.
  • 03/11/2009 2:27

    The Very Beginners Guide To The XNA Framework: Lesson1

    Ok, people the goal of this is to give newcomers a foundation that will help them learn more advanced xna programming. This is lesson 1 and it is getting familiar with how the default XNA code layout is set up. this assumes you understand the basics of C#


    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    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.Media; 
    using Microsoft.Xna.Framework.Net; 
    using Microsoft.Xna.Framework.Storage; 
     
    //lets take a run through of this code. just read the comments! 
     
    namespace EmptyProjectCodeOverView 
         
        public class Game1 : Microsoft.Xna.Framework.Game 
        { 
     
            //right here we have the start of our game class ( defaulted to Game1 ) 
     
            //this is a good place for adding your global variables 
     
            // i usually make an area here for my variables in between comments like this 
     
            //--------------------------------------- 
            GraphicsDeviceManager graphics; 
            SpriteBatch spriteBatch; 
     
            // other variables here 
            //--------------------------------------- 
     
             //lets move on 
     
     
            public Game1()  //this is the constructor for our default Game1 class 
            { 
                graphics = new GraphicsDeviceManager(this); 
                Content.RootDirectory = "Content"
     
                //it is mainly used for one time program wide initializations like running the game in full screen mode 
     
                //like this 
                graphics.IsFullScreen = true
     
                //that would run once when the program started and set the program to fullscreen mode 
            } 
     
             
            protected override void Initialize()  //initialize: this function is used to initialize your variables. the ones that we put in the commented region at the start of the class. 
            { 
                base.Initialize(); 
     
     
                MyVariable = new MyVariable( so_on_so_forth ); 
            } 
     
            
            protected override void LoadContent() //load content does exactly name says it does this will load the content that you have declared in the variables section 
            //and it also sets up the SpriteBatch which is used for drawing sprites. which we will cover later 
            { 
                spriteBatch = new SpriteBatch(GraphicsDevice); 
     
                 
            } 
     
            
            protected override void UnloadContent() 
            { 
                //this is used for getting rid of your content when the game exits. 
                //we will discuss this more later 
            } 
     
            
            protected override void Update(GameTime gameTime) //update. this is one of the core functions of the game. 
            { 
                 
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
                    this.Exit(); 
     
     
                 
                //this function is ran many times per second. and is where alot of your core game logic will go. for exampe if you wanted to have a sprite move right. 
               //that would be done in this function. you would use something like 
     
     
               SpritePosition.X += speed; 
     
               //the above code will add the variable 'speed' ( assuming you have created one ) and add it's value to the X coordinate of the variable SpritePosition.X ( assuming there is a variable called SpritePosition ) 
     
                 
                base.Update(gameTime); 
            } 
     
           
     
            protected override void Draw(GameTime gameTime) 
            { 
                GraphicsDevice.Clear(Color.CornflowerBlue); 
     
               
                //and finally this is where you put all of the code to draw things. whether is be models, sprites, text or anything else. it will go in here 
     
     
                base.Draw(gameTime); 
            } 
        } 
     



    I hope this helps people get more familiar with XNA.

    next lesson i will cover some of the really useful classes and data types of XNA

  • 03/11/2009 5:03 In reply to

    Re: The Very Beginners Guide To The XNA Framework: Lesson1

    Please do not post tutorial content here, if you do want to post tutorials start a blog and post it there. Also when posting large code blocks please use one of the code paste sites or your blog.
  • 03/11/2009 12:28 In reply to

    Re: The Very Beginners Guide To The XNA Framework: Lesson1

    Also, I recommend writing code that is actually real-world workable. You've got two variables in the code that are never declared. If you expect people to use the code you post it should at least compile. :|
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
Page 1 of 1 (3 items) Previous Next