Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

35

Transcript of Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

Page 1: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.
Page 2: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

Rob MilesMicrosoft MVPUniversity of Hull

Fun Programming with Visual Studio

Page 3: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

3

AgendaFun Programming with Visual Studio

Writing Silly Games for FunA Silly Game Demo

Writing Silly Games using Visual Studio The XNA FrameworkCreating games in C#Writing games for Windows Phone

Writing Silly Games for ProfitThe Creators Club and Indie Games

Page 4: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

4

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 movieSet 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

Page 5: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

5

Games that are very silly and sociableEasy to create and understandBased on examples in:

Learn Programming Now! with Microsoft® XNA™ Game Studio 3.0

byRob Miles

Page 6: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

6

“Hide the Gamepad”Rob Miles

demo

Page 7: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

7

Sample XNA 2D game: “Hot Salad Death”

We are going to consider a simple “casual” gameThe player guides the cheese around with the bread, hitting the tomatoes but avoiding the peppers and tangerinesThis is a simple, 2D, sprite based game

Page 8: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

8

“Hot Salad Death”Rob Miles

demo

Page 9: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

9

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

Page 10: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

10

Starting with our Cheese

To begin writing our game we can make some cheese bounce around the screenTo draw some cheese the game needs to remember two things

The picture to drawThe position on the screen to draw it

This is the basis of a sprite

Texture2D cheeseTexture;Rectangle cheeseRectangle;

Page 11: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

11

Games and Resources

Modern games contain thousands of content items:

Pictures Sounds3D modelsScripts

A game has to manage all these items so that the program can find and use them

cheeseTexture = Content.Load<Texture2D>("Cheese");

Page 12: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

12

Loading the Cheese Texture

LoadContent is called when the game startsIt loads the content and makes it available for use in the game

It loads the cheese texture from the Content ManagerIt creates a rectangle to bound and position the sprite

It should scale the rectangle to match the display dimensions

protected override void LoadContent(){ cheeseTexture = Content.Load<Texture2D>("Cheese"); cheeseRectangle = new Rectangle (0, 0, 100,100);}

Page 13: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

13

Drawing the Game World

Now we have our cheese we want to draw it for the player to seeThe game contains a Draw method that is called to draw the game display on the screenWe need to add some code to tell draw to put the cheese on the screenThe draw commands are batched up before being sent to the graphics hardware

Creating a Bank Class

Page 14: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

14

XNA Game Drawingprotected override void Draw(GameTime gameTime){ graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

spriteBatch.Draw(cheeseTexture, cheeseRectangle, Color.White );

spriteBatch.End();

base.Draw(gameTime);}

Page 15: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

15

“Drawing Cheese”Rob Miles

demo

Page 16: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

16

Making things move with the Update method

At the moment the cheese is always drawn at the same placeWe need to make it move about the screenGames do this by having an Update behaviour

In a racing game this would mean moving all the cars on the track, checking for collisions etcIn a shooting game this would mean moving all the players, checking to see if any bullets have hit anything etc

Page 17: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

17

Stupid XNA Game Update

We have two integer variables that hold the speed of our cheese

int cheeseXSpeed = 3;int cheeseYSpeed = 3;

protected override void Update(){ cheeseRectangle.X = cheeseRectangle.X + cheeseXSpeed; cheeseRectangle.Y = cheeseRectangle.Y + cheeseYSpeed;}

Page 18: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

18

“Moving Cheese”Rob Miles

demo

Page 19: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

19

Adding some Bounce to the Cheese

We want the cheese to bounce off the edge of the screenWe can do this by changing the sign of the speed value

protected override void Update(){ cheeseRectangle.X = cheeseRectangle.X + cheeseXSpeed;

if (cheeseRectangle.X < 0 || cheeseRectangle.Right > GraphicsDevice.Viewport.Width) { cheeseXSpeed *= -1; }

Page 20: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

20

“Bouncing Cheese”Rob Miles

demo

Page 21: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

21

Creating a bread bat

Now we need to create a bat texture and then allow the player to control itXNA provides support for keyboard and XBOX 360 gamepad

You can plug a wired gamepad into an PC and it will just workYou can also get a USB adapter so you can use wireless gamepads

The gamepad buttons can be tested during the Update method

Page 22: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

22

Using the Thumbstick DPad 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; }}

Page 23: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

23

Using the Thumbstick DPad 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);}

Page 24: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

24

Hitting the Cheese

This code reverses the vertical direction of the cheese when it hits the breadIt works by detecting when the cheese and bread rectangles intersectThis is a very simple kind of collision detection

if ( breadRectangle.Intersects(cheeseRectangle)){ cheeseYSpeed *= -1;}

Page 25: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

25

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

Page 26: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

26

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);

Page 27: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

27

XNA and Windows Phone

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

Page 28: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

28

“Windows Phone Starlight”Rob Miles

demo

Page 29: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

29

Windows Phone 7 Game Development

Games are controlled using the Accelerometer and the multi-touch inputThere is no physical controller as suchWindows Phone provides X, Y and Z values for acceleration and four points of multi-touchIn 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

Page 30: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

30

“Zune HD Album Juggler”Rob Miles

demo

Page 31: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

31

Getting Started with XNA

All the software is free:Visual Studio 2010 Express EditionXNA Game Studio 4.0 (when it is released) – use the Windows Phone SDK for now

Games can be run on the XBOX 360You need to join the "Creators Club" in order to do thisStudents can get free membership through DreamSpark

Visit robmiles.com for details on how to get started

Page 32: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

32

Selling your Games

You can put your XNA games onto Xbox LiveAll Xbox Live subscribers are able to download and play XNA games you have writtenYou can even charge money for themWindows Phone will have its own marketplace

Page 33: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

33

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 gamesIt is easy to doIt is fun!You might make some money!

Page 34: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

34

ResourcesXNA Creators Club:http://creators.xna.com/

DreamSparkhttps://downloads.channel8.msdn.com/

Microsoft XNA blogshttp://blogs.msdn.com/xna/

All the sample code and resource links:http://www.robmiles.com

Very Silly Games and book links:http://verysillygames.com

Page 35: Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.

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