Fun Programming with Visual Studio Rob Miles Microsoft


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# Writing games for Windows Phone Writing Silly Games for Profit The Creators Club and Indie Games 3

The Video Game Business Bigger than the movies? GTA 4 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 4

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 5

demo “Hide the Gamepad” Rob Miles 6

Sample XNA 2 D 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, 2 D, sprite based game 7

demo “Hot Salad Death” Rob Miles 8

Computer Game Constructions 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 9

Starting with our Cheese Texture 2 D cheese. Texture; Rectangle cheese. Rectangle; 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 10

Games and Resources Modern games contain thousands of content items: Pictures Sounds 3 D models Scripts A game has to manage all these items so that the program can find and use them 11 cheese. Texture = Content. Load<Texture 2 D>("Cheese");

Loading the Cheese Texture protected override void Load. Content() { cheese. Texture = Content. Load<Texture 2 D>("Cheese"); cheese. Rectangle = new Rectangle (0, 0, 100); } Load. Content is 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 12

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 13

XNA Game Drawing protected override void Draw(Game. Time game. Time) { graphics. Graphics. Device. Clear(Color. Cornflower. Blue); sprite. Batch. Begin(); sprite. Batch. Draw(cheese. Texture, cheese. Rectangle, Color. White ); sprite. Batch. End(); base. Draw(game. Time); } 14

demo “Drawing Cheese” Rob Miles 15

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 16

Stupid XNA Game Update int cheese. XSpeed = 3; int cheese. YSpeed = 3; protected override void Update() { cheese. Rectangle. X = cheese. Rectangle. X + cheese. XSpeed; cheese. Rectangle. Y = cheese. Rectangle. Y + cheese. YSpeed; } We have two integer variables that hold the speed of our cheese 17

demo “Moving Cheese” Rob Miles 18

Adding some Bounce to the Cheese protected override void Update() { cheese. Rectangle. X = cheese. Rectangle. X + cheese. XSpeed; if (cheese. Rectangle. X < 0 || cheese. Rectangle. Right > Graphics. Device. Viewport. Width) { cheese. XSpeed *= -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 19

demo “Bouncing Cheese” Rob Miles 20

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 21

Using the Thumbstick DPad Game. Pad. State pad. State = Game. Pad. Get. State(Player. Index. One); if (pad. State. Is. Connected) { if (pad. State. DPad. Left == Button. State. Pressed) { bread. Rectangle. X = bread. Rectangle. X - bread. Speed; } } 22 if (pad. State. DPad. Right == Button. State. Pressed) { bread. Rectangle. X = bread. Rectangle. X + bread. Speed; }

Using the Thumbstick DPad int pad. XSpeed = 10; int pad. YSpeed = 10; if (pad. State. Is. Connected) { bread. Rectangle. X += (int) (pad. State. Thumb. Sticks. Left. X * pad. XSpeed); bread. Rectangle. Y -= (int) (pad. State. Thumb. Sticks. Left. Y * pad. YSpeed); } 23

Hitting the Cheese if ( bread. Rectangle. Intersects(cheese. Rectangle)) { cheese. YSpeed *= -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 24

Adding Sound // Sound effect variable Sound. Effect ding; // Load the sound effect content in Load. Content // Sound effects are WAV files which are played from memory ding = Content. Load<Sound. Effect>("Ding"); // Play the sound effect when the bread hits the cheese if ( bread. Rectangle. Intersects(cheese. Rectangle)) { cheese. YSpeed *= -1; ding. Play(); } 25

Playing Music // Song variable Song music; // Load the song content in Load. Content // Music can be an MP 3 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 Media. Player. Play(music); 26

XNA and Windows Phone 7 provides an XNA environment that supports both 2 D and 3 D games These are developed using exactly the same environment Games can be stored and run from the device 27

demo “Windows Phone Starlight” Rob Miles 28

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 2 D XNA and you develop for the platform using Visual Studio 2008 and XNA 3. 1 29

demo “Zune HD Album Juggler” Rob Miles 30

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 Dream. Spark Visit robmiles. com for details on how to get started 31

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 32

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! 33

Resources XNA Creators Club: http: //creators. xna. com/ Dream. Spark https: //downloads. channel 8. 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 34

© 2008 Microsoft Corporation. All rights reserved. Microsoft, 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.
- Slides: 35