420 likes | 602 Views
XNA. An Introduction. What XNA is…. Microsoft® XNA™ is composed of industry-leading software, services, resources, and communities focused on enabling game developers to be successful on Microsoft gaming platforms. http://www.xna.com/. The XNA Framework is a library.
E N D
XNA An Introduction
What XNA is… • Microsoft® XNA™ is composed of industry-leading software, services, resources, and communities focused on enabling game developers to be successful on Microsoft gaming platforms. • http://www.xna.com/
The XNA Framework is a library • Now to you get why I was making you make a class library and make calls to it??? • http://msdn.microsoft.com/en-us/aa937791.aspx
Where is the XNA framework? • C:\Program Files\Microsoft XNA\XNA Game Studio\v3.1\References\Windows\x86\
How a game runs • On a loop – it just keeps going and going and going and going • This is different than web, console and forms applications • They are “Event” driven • They will sit and do nothing until the users interacts with them • A game is running on a loop and picks up changes and updates to the state and user input along the way.
Anatomy Lesson 1 • The main entry point is found on Program.cs • You generally do not add code here
Game1.cs (you can change this but if you do you need to change the run code in Program.cs)
Anatomy Lesson 2 • The Initialize method is where you can initialize any assets that do not require a GraphicsDevice to be initialized. • The LoadContent method is where you load any necessary game assets such as models and textures. • The UnloadContent method is where any game assets can be released. Generally, no extra code is required here, as assets will be released automatically when they are no longer needed. • The Update loop is the best place to update your game logic: move objects around, take player input, decide the outcome of collisions between objects, and so on. • The Draw loop is the best place to render all of your objects and backgrounds on the screen.
When do they run? • Initialize runs once on the game load • Load content runs once on the game load • Update and Draw run over and over and over again • Unload content runs once when you exit the game
Without doing anything • If you build and run your game now, the GraphicsDeviceManager will set up your screen size and render a blank screen. Your game will run and update all by itself. It's up to you to insert your own code to make the game more interesting.
What is the GraphicsDeviceManager ? • It is a private property (you learn what this is in OOP) of the XNA Game class • There are 2 • The other one is the ContentManager
They are initialized in the constructor (you learn what this is in OOP)
Lets make it full screen: Step 1 • Add to new private fields to the Game1 class private int height; private int width; • These are declared globally so that they are available in the whole class.
Add the code to the Initialize method to make it full screen height = graphics.GraphicsDevice.Viewport.Height; width = graphics.GraphicsDevice.Viewport.Width; this.graphics.PreferredBackBufferHeight= height; this.graphics.PreferredBackBufferWidth= width; this.graphics.ToggleFullScreen(); this.graphics.ApplyChanges();
Before we run it – lets make sure we can exit • This code assumes that you have a controller hooked up to your computer // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();
Since we don’t have a controller – lets add in an input detection from the keyboard • In the Update method add this code KeyboardStatenewState = Keyboard.GetState(); if (newState.IsKeyDown(Keys.Escape)) { this.Exit(); }
Now run the code • See if it launches full screen and if you can exit by hitting the escape button.
Lets add some spritesRight click on Content > Add > Existing Item > pick the example png
Now run it again • Do you have a butterfly in the top left hand corner of a black screen?
Add this method (it needs to be inside the class) void UpdateSprite(GameTimegameTime) { // Move the sprite by speed, scaled by elapsed time. spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; intMaxX = graphics.GraphicsDevice.Viewport.Width - myTexture.Width; intMinX = 0; intMaxY = graphics.GraphicsDevice.Viewport.Height - myTexture.Height; intMinY = 0; // Check for bounce. if (spritePosition.X > MaxX) { spriteSpeed.X *= -1; spritePosition.X = MaxX; } else if (spritePosition.X < MinX) { spriteSpeed.X *= -1; spritePosition.X = MinX; } if (spritePosition.Y > MaxY) { spriteSpeed.Y *= -1; spritePosition.Y = MaxY; } else if (spritePosition.Y < MinY) { spriteSpeed.Y *= -1; spritePosition.Y = MinY; } }
I put it after Draw but you can put it anywhere so long as it is inside the class but not inside the other methods
Run the game again • This time the butterfly should be moving around the screen and bouncing when it gets t the edge.
But I want to control the butterfly • Ok – ok • Here we go.
Add a new method • It is in the posted text file
Now comment out the old update sprite method and make a call to your new update input method
Run it again • You should have control over the butterfly
Not homework • If you feel like it – try to put boundary detection on the update input method so that it bounces instead of going off the side of the screen • Hint: it is in the update sprite method
Homework • Finish work of art 4 • Study for the final