Programming 2 Introduction

Programming 2 Introduction

XNA for Fun and Profit St Bede’s College Rob Miles Department of Computer Science University of Hull Agenda • Computer Games – How Computer Games work • XNA – What is XNA? – The XNA Framework – Very Silly Games – Making a game in sixty minutes • Why everyone should be writing games! 2 The Video Game Business • Bigger than the movies? – GTA4 sales topped half a billion dollars in its first week of release, 5 times the earnings of the Iron Man movie • Set to grow even more? – It is now easy (and cheap) to write a game in your bedroom and make it available to every Xbox Live subscriber in the world – The games industry is looking for new kind of games 3 • Games that are very silly and sociable • Easy to create and understand • Mainly for multiple players • Some games are “Zero Graphics” • Based on examples in: Learn Programming Now! with Microsoft® XNA™ Game Studio 3.0 by Rob Miles 4 “Hide the Gamepad” Sample XNA 2D game “Bread and Cheese” • We are going to make a simple “casual” game • The player guides the cheese around with the bread, hitting the tomatoes but avoiding the peppers and tangerines • This is a simple, 2D, sprite based game 6 “Bread and Cheese” Computer Game Construction • Every game that has ever been written does these things: 1. Initialise all the resources at the start • This is where the “Loading” screen comes from 2. Repeatedly runs the game loop: a) Update the game world • read the controllers, update the state and position of the things in the game b) Draw the game word for the player to see • Display the game elements on the screen 8 Starting with our Cheese Texture2D cheeseTexture; Rectangle cheeseRectangle; • To begin writing our game we can make some cheese bounce around the screen • To draw some cheese the game needs to remember two things – The picture to draw – The position on the screen to draw it • In programs these are called variables 9 Games and Resources • Modern games contain thousands of content items: – Pictures – Sounds – 3D models – Scripts • A game has to manage all these items so that the program can find and use them cheeseTexture = Content.Load<Texture2D>("Cheese"); 10 Loading the Cheese Texture protected override void LoadContent() { cheeseTexture = Content.Load<Texture2D>("Cheese"); cheeseRectangle = new Rectangle (0, 0, 100,100); } • A game is made up of things that hold information (the variables) and things that do stuff (the methods) • LoadContent is a method that is called when our game starts running • It loads our cheese texture and makes it available for use in the game 11 Games and Rectangles x,y height width • The Rectangle tells us where on the screen the texture is to be drawn • It gives the position and the size of the draw operation 12 Creating a Bank Class Positioning the Cheese Texture protected override void LoadContent() { cheeseTexture = Content.Load<Texture2D>("Cheese"); cheeseRectangle = new Rectangle (0, 0, 100,100); } • This will draw our cheese image at the top left hand corner and the cheese will be 100 pixels wide and 100 pixels high • Unlike graph paper, with computer graphics the position of (0,0) is the top left hand corner of the screen – Increasing the value of Y moves the rectangle down – Increasing the value of X moves the rectangle across 13 Drawing the Game World • Now we have our cheese we want to draw it for the player to see • The game contains a Draw method that is called draw the game display on the screen • We need to add some program code to tell draw to put our cheese on the screen • This code will send commands to the Graphics Card in the computer to make it draw the texture 14 Creating a Bank Class XNA Game Drawing protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(cheeseTexture, cheeseRectangle, Color.White ); spriteBatch.End(); base.Draw(gameTime); } • This version of the Draw method clears the screen to blue and then draws the cheese 15 “Cheese Drawing” Making things move with the Update method • At the moment the cheese is always drawn at the same place • We need to make it move about the screen • Games do this by having an Update behaviour that makes the game world work – In a racing game this means moving all the cars on the track, checking for collisions etc – In a shooting game this means moving all the players, checking to see if any bullets have hit anything etc • At the moment we are just going to make our cheese move Stupid XNA Game Update int cheeseXSpeed = 3; int cheeseYSpeed = 3; protected override void Update() { cheeseRectangle.X = cheeseRectangle.X + cheeseXSpeed; cheeseRectangle.Y = cheeseRectangle.Y + cheeseYSpeed; } • We have two integer variables that hold the speed of our cheese • They are used to update the position of the cheese rectangle when Update is called 18 “Cheese Moving” Adding some Bounce to the Cheese protected override void Update() { cheeseRectangle.X = cheeseRectangle.X + cheeseXSpeed; if (cheeseRectangle.X < 0 || cheeseRectangle.Right > GraphicsDevice.Viewport.Width) { cheeseXSpeed = cheeseXSpeed * -1; } // Repeat for Y } • We want the cheese to bounce off the edge of the screen • We can do this by changing the sign of the speed value 20 “Cheese Bouncing” Creating a bread bat • Now we need to create a bat texture and then allow the player to control it • XNA provides support for keyboard and XBOX 360 gamepad – You can plug a wired gamepad into an PC and it will just work – You can also get a USB adapter so you can use wireless gamepads • The gamepad buttons can be tested during the update method 22 XNA Digital Gamepad Input GamePadState padState = GamePad.GetState(PlayerIndex.One); if (padState.IsConnected) { if (padState.DPad.Left == ButtonState.Pressed) { breadRectangle.X = breadRectangle.X - breadSpeed; } if (padState.DPad.Right == ButtonState.Pressed) { breadRectangle.X = breadRectangle.X + breadSpeed; } /// repeat for bread Y position } 23 XNA Analogue Gamepad Input int padXSpeed = 10; int padYSpeed = 10; if (pad.IsConnected) { breadRectangle.X += (int) (pad.ThumbSticks.Left.X * padXSpeed); breadRectangle.Y -= (int) (pad.ThumbSticks.Left.Y * padYSpeed); } • This code uses the analogue input of the left thumbstick to update the position of the bread rectangle 24 Hitting the Cheese if ( breadRectangle.Intersects(cheeseRectangle)) { cheeseYSpeed = cheeseYSpeed * -1; } • This code reverses the vertical direction of the cheese when it hits the bread • It works by detecting when the cheese and bread rectangles intersect • This is a very simple kind of collision detection 25 Adding Sound // Sound effect variable SoundEffect ding; // Load the sound effect content in LoadContent ding = Content.Load<SoundEffect>("Ding"); // Play the sound effect when the bread hits the cheese if ( breadRectangle.Intersects(cheeseRectangle)) { cheeseYSpeed *= -1; ding.Play(); } • The sound effect is a WAV file 26 Playing Music // Song variable Song music; // Load the song content in LoadContent music = Content.Load<Song>("Music"); // Play the song using the Media Player MediaPlayer.Play(music); • The Music resource is an MP3 file • Can also play WMA files and music from the media on the device 27 “Bread and Cheese” Rob Miles Getting Started with XNA • All the software is free: – Visual Studio 2008 Express Edition – XNA Game Studio 3.1 • Games can be run on the XBOX 360 – You need to join the "Creators Club" in order to do this – Students can get free membership through DreamSpark • Visit robmiles.com for details on how to get started 29 Selling your Games • You can put your XNA games onto Xbox Live • All Xbox Live subscribers are able to download and play XNA games you have written • You can even charge money for them 30 Summary • The XNA Framework provides a very powerful environment for game creation – Write games for your Xbox or PC in C# using Visual Studio • You should all be writing games – It is easy to do – It is fun! – You might make some money! 31 Resources • XNA Creators Club: http://creators.xna.com/ • DreamSpark https://downloads.channel8.msdn.com/ • Microsoft XNA blogs http://blogs.msdn.com/xna/ • All the sample code and resource links: http://www.robmiles.com • Very Silly Games and book links: http://verysillygames.com 32.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    32 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us