1 / 42

XNA

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.

jake
Download Presentation

XNA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XNA An Introduction

  2. 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/

  3. 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

  4. Where is the XNA framework? • C:\Program Files\Microsoft XNA\XNA Game Studio\v3.1\References\Windows\x86\

  5. File > New Project > Windows Game

  6. 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.

  7. Anatomy Lesson 1 • The main entry point is found on Program.cs • You generally do not add code here

  8. Game1.cs (you can change this but if you do you need to change the run code in Program.cs)

  9. 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.

  10. 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

  11. 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.

  12. 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

  13. They are initialized in the constructor (you learn what this is in OOP)

  14. This is what a game looks like initially

  15. 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.

  16. 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();

  17. Here it is in the code

  18. 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();

  19. 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(); }

  20. This is what the code looks like

  21. Now run the code • See if it launches full screen and if you can exit by hitting the escape button.

  22. Change the background color to black

  23. Lets add some spritesRight click on Content > Add > Existing Item > pick the example png

  24. Change the asset name to butterfly in the properties window

  25. Add two fields

  26. Load the texture

  27. Draw it to the screen

  28. Now run it again • Do you have a butterfly in the top left hand corner of a black screen?

  29. Now lets make it move add a sprite speed to the fields

  30. 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; } }

  31. 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

  32. Make the call in the update method

  33. Run the game again • This time the butterfly should be moving around the screen and bouncing when it gets t the edge.

  34. But I want to control the butterfly • Ok – ok • Here we go.

  35. Add 2 new fields

  36. Initialize the oldstate

  37. Add a new method • It is in the posted text file

  38. This is what it looks like in your code

  39. Now comment out the old update sprite method and make a call to your new update input method

  40. Run it again • You should have control over the butterfly

  41. 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

  42. Homework • Finish work of art 4 • Study for the final

More Related