350 likes | 523 Vues
SESSION CODE: #####. Fun Programming with Visual Studio. Rob Miles Microsoft MVP University of Hull. Agenda Fun Programming with Visual Studio. Writing Silly Games for Fun A Silly Game Demo Writing Silly Games using Visual Studio The XNA Framework Creating games in C#
E N D
SESSION CODE: ##### Fun Programming with Visual Studio Rob Miles Microsoft MVP University of Hull
AgendaFun Programming with Visual Studio • Writing Silly Games for Fun • A Silly Game Demo • Writing Silly Games using Visual Studio • The XNA Framework • Creating games in C# • Writing games for Windows Phone • Writing Silly Games for Profit • The Creators Club and Indie Games
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
Games that are very silly and sociable • Easy to create and understand • Based on examples in: Learn Programming Now! with Microsoft® XNA™ Game Studio 3.0 by Rob Miles
“Hide the Gamepad” demo Rob Miles
Sample XNA 2D game: “Hot Salad Death” • We are going to consider 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
“Hot Salad Death” demo Rob Miles
Computer Game Constructions • Every game that has ever been written does these things: • Initialise all the resources at the start • This is where the “Loading” screen comes from • Repeatedly runs the game loop: • Update the game world • read the controllers, update the state and position of the things in the game • Draw the game word for the player to see • Display the game elements on the screen
Starting with our Cheese • 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 • This is the basis of a sprite Texture2DcheeseTexture; RectanglecheeseRectangle;
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");
Loading the Cheese Texture protectedoverridevoidLoadContent() { cheeseTexture = Content.Load<Texture2D>("Cheese"); cheeseRectangle = newRectangle(0, 0, 100,100); } • LoadContentis called when the game starts • It loads the content and makes it available for use in the game • It loads the cheese texture from the Content Manager • It creates a rectangle to bound and position the sprite • It should scale the rectangle to match the display dimensions
Creating a Bank Class 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 to draw the game display on the screen • We need to add some code to tell draw to put the cheese on the screen • The draw commands are batched up before being sent to the graphics hardware
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); }
“Drawing Cheese” demo Rob Miles
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 • In a racing game this would mean moving all the cars on the track, checking for collisions etc • In a shooting game this would mean moving all the players, checking to see if any bullets have hit anything etc
Stupid XNA Game Update intcheeseXSpeed = 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
“Moving Cheese” demo Rob Miles
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 *= -1; } • We want the cheese to bounce off the edge of the screen • We can do this by changing the sign of the speed value
“Bouncing Cheese” demo Rob Miles
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
Using the ThumbstickDPad 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; }}
Using the ThumbstickDPad int padXSpeed = 10; int padYSpeed = 10; if (padState.IsConnected) { breadRectangle.X += (int) (padState.ThumbSticks.Left.X * padXSpeed); breadRectangle.Y -= (int) (padState.ThumbSticks.Left.Y * padYSpeed); }
Hitting the Cheese if ( breadRectangle.Intersects(cheeseRectangle)) { 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
Adding Sound // Sound effect variableSoundEffect ding;// Load the sound effect content in LoadContent // Sound effects are WAV files which are played from memoryding = Content.Load<SoundEffect>("Ding"); // Play the sound effect when the bread hits the cheeseif ( breadRectangle.Intersects(cheeseRectangle)) { cheeseYSpeed *= -1; ding.Play(); }
Playing Music // Song variableSong music;// Load the song content in LoadContent// Music can be an MP3 or WMA file// Also have access to the media content on the device music = Content.Load<Song>("Music"); // Play the song using the Media Player MediaPlayer.Play(music);
XNA and Windows Phone • Windows Phone 7 provides an XNA environment that supports both 2D and 3D games • These are developed using exactly the same environment • Games can be stored and run from the device
“Windows Phone Starlight” demo Rob Miles
Windows Phone 7 Game Development • Games are controlled using the Accelerometer and the multi-touch input • There is no physical controller as such • Windows Phone provides X, Y and Z values for acceleration and four points of multi-touch • In this respect it is similar to the Zune HD device • Although Zune HD only supports 2D XNA and you develop for the platform using Visual Studio 2008 and XNA 3.1
“Zune HD Album Juggler” demo Rob Miles
Getting Started with XNA • All the software is free: • Visual Studio 2010 Express Edition • XNA Game Studio 4.0 (when it is released) – use the Windows Phone SDK for now • 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
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 • Windows Phone will have its own marketplace
Summary • The XNA Framework provides a very powerful environment for game creation • Write games for your Xbox or PC in C# using Visual Studio – can also target Windows Phone • You should all be writing games • It is easy to do • It is fun! • You might make some money!
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
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.