Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience XNA Game Studio January 12, 2010 Arnav Jhala Adapted from Jim Whitehead’s slides.

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.
Advertisements

Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
Adobe Flash CS4 – Illustrated Unit E: Optimizing and Publishing a Movie.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Computer Science – Game DesignUC Santa Cruz Announcements Website – Readings and sample code updated – Last year’s mid-term exam with answer key – Mid-term.
Computer Science – Game DesignUC Santa Cruz Today Publish/Subscribe Design Pattern Delegates Sprite movement Particles.
Computer Science – Game DesignUC Santa Cruz Game Jam Two teams from CMPS 20 – Less Than Royal – Colon Trey.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Code Organization UML Patterns Code Smells Feb Arnav Jhala.
Horizontal Scrolling through Tileset Levels Game Design Experience Professor Jim Whitehead March 4, 2008 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
2D Graphics in XNA Game Studio Express (Modeling a Class in UML) Game Design Experience Professor Jim Whitehead February 5, 2008 Creative Commons Attribution.
Delegates & Events Observer and Strategy Patterns Game Design Experience Professor Jim Whitehead January 30, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
2D Graphics in XNA Game Studio Express (Plus, Random numbers in C#) Game Design Experience Professor Jim Whitehead January 16, 2009 Creative Commons Attribution.
Space Partitioning for Broad Sweep Collision Detection Part 1 - Grids Game Design Experience Professor Jim Whitehead February 11, 2009 Creative Commons.
Lecture 1 CS171: Game Design Studio 1I UC Santa Cruz School of Engineering 5 January 2010.
Introduction to UML (slides adapted from Michael Mateas)
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
Console I/O in C# 2D Graphics in XNA Game Studio Express (Modeling a Class in UML) Game Design Experience Professor Jim Whitehead January 14, 2009 Creative.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Distribution of Marks Internal Sessional Evaluation Assignments – 10 Quizzes – 10 Class Participation Attendence – 5 Mid – Term Test – 25 External Evaluation.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
Unified Modeling Language
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience January 14, 2010 Arnav Jhala.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
Backgrounds, Inheritance in GameMaker (BrickMania 1 of 2) Foundations of Interactive Game Design Professor Jim Whitehead January 28, 2008 Creative Commons.
Tutorial 2 Drawing Text, Adding Shapes, and Creating Symbols.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
© 2011 Delmar, Cengage Learning Chapter 9 Introduction to ActionScript 3.0.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Developing the Game User Interface (UI) Lesson 5.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
1 Useful Tools for Making Video Games Part V An overview of.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
XNA An Introduction. What XNA is… Microsoft® XNA™ is composed of industry- leading software, services, resources, and communities focused on enabling.
Object Oriented Design COSC 315 Fall 2014 Bridget M. Blodgett.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
XNA ● Proprietary Microsoft framework ● C#. Interface.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four.
Adobe Flash Professional CS5 – Illustrated Unit E: Optimizing and Publishing a Movie.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Computer Science – Game DesignUC Santa Cruz Tile Engine.
CMPS 20: Game Design Experience January 14, 2010 Arnav Jhala.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
Graphical Output Basic Images.
Where are we ? Setup Sprites Input Collision Drawing Sprites
Using and Creating Sprites
About the Presentations
Java Programming: Guided Learning with Early Objects
Analysis models and design models
Game Loop Update & Draw.
Presentation transcript:

Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience XNA Game Studio January 12, 2010 Arnav Jhala Adapted from Jim Whitehead’s slides

Computer Science – Game DesignUC Santa Cruz Announcements Session schedules being finalized Homework #1 (Hunt the Wumpus) – Due Thursday, January 21 – Detailed instructions will be up by tomorrow Project themes and timeline Check website frequently for updates – Come for help with Homework #1, C#, XNA

Computer Science – Game DesignUC Santa Cruz Imagine Cup Worldwide competition – Microsoft sponsored – Many categories, including game development – Registration Feb 1 – Multiple rounds – first round ends March 15 – Games must use XNA Game Studio 3.0 – Games must address contest theme – Would be possible to take your project for CS 20, and enter it into the contest –

Computer Science – Game DesignUC Santa Cruz Hunt the Wumpus A game where you move through a dodecahedron shaped map – Each room is connected to three other rooms – Shortest non-repeating path back to same point is five moves A dodecahedron Source: Wikipedia A flattened dodecahedron Source: More BASIC Computer Games

Computer Science – Game DesignUC Santa Cruz Hunt the Wumpus (cont’d) Turn based game – Each turn, player moves or shoots a crooked arrow (up to five rooms) Three main types of obstacles – Wumpus If awake, kills player if both in same room at end of turn “I smell a Wumpus” – Pits (2 instances) Player falls in and dies “I feel a draft” – Superbats Places player in a random new room “Bats nearby” Source: Best of Creative Computing, Vol. 1

Computer Science – Game DesignUC Santa Cruz Hunt the Wumpus (cont’d) Main challenges to the homework assignment – Reading and parsing input – How to represent game world How to represent each room How to represent the set of all the rooms – You have a fixed number of them (20) How to represent connections in the map – Each room has a number, and each room is connected to exactly three other rooms How to represent Wumpus, pits, bats – Each is located in the game map – Writing game logic Main game loop Display of warnings when player one room away from obstacles Behavior of Wumpus, pits, bats when they interact with player Handling shooting logic

Computer Science – Game DesignUC Santa Cruz Hunt the Wumpus (cont’d) As a rule of thumb, in object oriented design: – Nouns are represented as classes What are some of the nouns in Hunt the Wumpus? – Verbs are methods on the classes What are some of the actions on the nouns? What can the player do? What can a Wumpus do? What can bats do? What can pits do? – Also need to consider what information other classes might need These will be properties Location of Wumpus, bats, pits

Computer Science – Game DesignUC Santa Cruz Console input in C# There are three methods for reading console input in C# – ReadLine Read a line of input into a string – ReadKey Read the next key pressed into ConsoleKeyInfo instance Provides access to whether ctrl, alt, shift were pressed – Read Read a line of input, then gives successive characters each time you call Read Any of these could be used for your assignment – ReadLine is easiest to use

Computer Science – Game DesignUC Santa Cruz Console.ReadLine ReadLine – Program execution blocks until user enters a line, terminated by Enter – Line typed by user is returned as a string public class ConsoleDemo { static void Main(string[] args) { string my_input; System.Console.WriteLine(“(M)ove or (S)hoot :”); my_input = System.Console.ReadLine(); }

Computer Science – Game DesignUC Santa Cruz Parsing Input In Wumpus, need to check whether player entered – M for move, S for shoot, Q for quit – That is, check the first character of the user input – Would like to be case sensitive No problem – use string.Compare string.Compare has 10 variations – An overloaded method – We want: public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase); Compare two strings, strA and strB … starting at character number indexA in strA, and indexB in strB… … and comparing length characters in each … … with the ability to ignoreCase. – Returns 0 if strings are the same -1 if A lexically smaller than B, 1 if A lexiacally larger than B

Computer Science – Game DesignUC Santa Cruz Parsing Input (cont’d) We want to compare the first character of user input against – “M” for move – “S” for shoot – “Q” for quit public class ConsoleDemo { static void Main(string[] args) { string my_input; System.Console.WriteLine(“(M)ove or (S)hoot :”); my_input = System.Console.ReadLine(); if (string.Compare(my_input, 0, "M", 0, 1, true) = = 0) // Move if (string.Compare(my_input, 0, "S", 0, 1, true) = = 0) // Shoot if (string.Compare(my_input, 0, "Q", 0, 1, true) = = 0) // Quit } Demonstration of Console input and string parsing in Visual C# 2008

Computer Science – Game DesignUC Santa Cruz Unified Modeling Language (UML) A family of diagram types used to model object-oriented software projects – A standard way to graphically represent complex assemblages of objects, components, etc. Two useful diagram types – Class diagram Static view of software Object-oriented classes Relationships – Inheritance – Containment – Sequence diagram Dynamic view Methods calling methods, and in what order Jmgold, Flickr

Computer Science – Game DesignUC Santa Cruz Modeling a Class in UML An object oriented class contains: – Data: class variables Type & visibility of each variable – Methods Name, parameters, types of parameters UML classes can represent all of this myClass varname: type [= init value] … Amethod(type param) Bmethod(type param, …) Class name Class variables (attributes) Class methods and parameters

Computer Science – Game DesignUC Santa Cruz Modeling Visibility The visibility of a variable or method can be indicated using: + public # protected - private Book - title: string + Title > # num_pages: int = 0 + NumPages > … + lookupISBN()

Computer Science – Game DesignUC Santa Cruz Inheritance, Containment Two main relationships modeled in class diagrams – Inheritance (is-a, specialization) – Containment (has-a) shape circlesquare scene + elems: List square and circle are subclasses of (inherit from) shape Class scene contains a set of shapes (via the elems List) Open full triangle arrowhead significant for inheritance (a different arrowhead would not mean the same thing!)

Computer Science – Game DesignUC Santa Cruz XNA GSE Game Scaffolding Scaffolding for a simple XNA GSE game is created when you select a new game project in Visual C# Express – File … New Project … Windows Game (3.0) – Or File … New Project … Xbox 360 Game (3.0) Can fill-in this scaffolding to create your own game Creates a class (myGameClass) that includes – Constructor – Initialization Initialize(), LoadContent() – Update Update game state every clock tick – Draw Create display every clock tick Demonstration of XNA GSE scaffolding in Visual C# 2008 Express

Computer Science – Game DesignUC Santa Cruz XNA GSE Game Scaffolding Microsoft.Xna.Framework.Game myGame - graphics: GraphicsDeviceManager - content: ContentManager + myGame() # Initialize() # LoadContent(loadAllContent: bool) # UnloadContent(unloadAllContent: bool) # Update(gameTime: GameTime) # Draw(gameTime: GameTime) graphics = new GraphicsDeviceManager(this); Content.RootDirectory = “Content”; base.Initialize() # Initialize() # Run() # Tick() Update(gameTime); Draw(gameTime);

Computer Science – Game DesignUC Santa Cruz XNA GSE Game Initialization Create new myGame – Call to constructor, myGame() – myGame.run() 1.Initializes game, then, 2.Runs the main game loop & processes events Initialization phase of run(), – The following methods are called on myGame – Initialize() 1.call Initialize() on parent class 2.Initialize your game state 1.Create player object, create enemy objects, create object to hold main game state, etc. – LoadContent() Method used to load textures, create SpriteBatches

Computer Science – Game DesignUC Santa Cruz XNA GSE Main Game Loop Time elapsed between each clock tick: – Fixed: 1/60 th of a second ( milliseconds per tick) myGame.IsFixedTimeStep = true The default value – Variable: Adjusts based on the time required to perform previous tick myGame.IsFixedTimeStep = false Each clock tick – Run() calls Tick() – Tick() calls Update() then Draw() You supply Update() and Draw()

Computer Science – Game DesignUC Santa Cruz Update() and Draw() Update() – Update the state of all objects – Receive input, move player avatar – Compute opponent AI, move opponent objects – Collision detection & consequences – Detect end-of-game or end-of-level condition Draw() – Re-create the on-screen scene using the up-to-date positions of player, opponent Advice – Avoid stuffing your entire game into the definition of these two methods Methods become too big! – Have these methods call out to your player object, opponent objects, etc. foreach (Opponent o in opponentList) o.update();

Computer Science – Game DesignUC Santa Cruz Getting a 2D Image to Appear on Screen LoadContent() 1.Create a Texture – A bitmap image 2.Create a SpriteBatch – Collects all textures being drawn to screen Draw() 3.Begin the SpriteBatch 4.Draw texture – Draw() is defined on a SpriteBatch – Adds texture to the SpriteBatch 5.End the SpriteBatch – Causes textures in SpriteBatch to be drawn to screen

Computer Science – Game DesignUC Santa Cruz Creating a Texture Create an instance of ContentManager – XNA GSE scaffolding does this for you – Content = new ContentManager(Services) in constructor Call Load on ContentManager – For 2D sprites, type T is “Texture2D” – This loads an art asset that has been created by the Content Pipeline In our case, conversion of a 2D bitmap image in PNG or JPG into XNA internal bitmap format – Give the pathname of the bitmap image (e.g., in PNG or JPG) to load Path is relative to the “Content” directory of the Visual C# project Note: normally need to escape slash in a string “\\”  \ Can put at beginning of string to make string “verbatim” – No need to escape slashes in this case – “\\images\\” is the same

Computer Science – Game DesignUC Santa Cruz Example of creating a texture Create new bitmap image – In GIMP, Photoshop, etc. – Save to disk, then copy over to Visual C# project Copy to Visual Studio 2008\Projects\{your project}\{your project}\Content Go to Solution Explorer in Visual C# Express Right click on Bolded Project Name Add  Add Existing Item Pick filename of new bitmap image file Will now appear in the project file list Verify that Content Pipeline processed file by building solution (F6) – Build > Build Solution Create a Texture2D, then load the bitmap image via the content manager: Protected Texture2D m_bullet = null; m_bullet = Content.Load

Computer Science – Game DesignUC Santa Cruz SpriteBatch Once a texture has been made, how does this get displayed? – Create a SpriteBatch – Within a clock tick, begin() the batch Prepares the graphics device for drawing sprites – Draw() the texture as part of the batch – End() the batch Causes textures to be drawn to the screen Restores device to how it was before the batch – Typically this is performed in your game’s Draw() method

Computer Science – Game DesignUC Santa Cruz SpriteBatch Example Draw() inside SpriteBatch is heavily overloaded – 7 different choices protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { m_batch = new SpriteBatch(graphics.GraphicsDevice); // Initialize the sprite batch m_bullet = content.Load // Create Texture2D } protected override void Draw(GameTime gameTime) { Vector2 loc = new Vector2(120, 120); // Create Vector2 to give location of Texture2D m_batch.Begin(); // Start the batch m_batch.Draw(m_bullet, loc, Color.White); // Add Texture2D to batch. Not yet on screen. m_batch.End(); // Now Texture2D is drawn to screen. }

Computer Science – Game DesignUC Santa Cruz Tinting Sprites On previous slide, used Color.White in the Draw() method – This gives the tint of the sprite – White indicates use of the original colors – Can choose any other color to tint the sprite Visual C# Express gives list of predefined colors Can also defined a Vector3 to give RGB values protected override void Draw(GameTime gameTime) { Vector2 loc = new Vector2(120, 120); // Create Vector2 to give location of Texture2D m_batch.Begin(); // Start the batch m_batch.Draw(m_bullet, loc, Color.Red); // Add Texture2D to batch. Has red tint. m_batch.End(); // Now Texture2D is drawn to screen. }

Computer Science – Game DesignUC Santa Cruz Transparent Sprites It is possible to make a sprite partially opaque – Colors have RGB, and Alpha (opacity) – Use Vector4 to represent this – Create color by passing Vector4 into constructor protected override void Draw(GameTime gameTime) { Vector2 loc = new Vector2(120, 120); // Create Vector2 to give location of Texture2D Vector4 v4Color = new Vector4(1.0f, 1.0f, 1.0f, 0.5f); // Create Vector4 to create color w/opacity Color color = new Color(v4Color); // Create color from v4Color m_batch.Begin(); // Start the batch m_batch.Draw(m_bullet, loc, color); // Add Texture2D to batch. Is partially opaque m_batch.End(); // Now Texture2D is drawn to screen. }

Computer Science – Game DesignUC Santa Cruz Other Sprite features Depth ordering – Draw some sprites in front of (behind) others to give depth of field effect Rotation – Can rotate sprite image to a specific angle Scaling – Can make sprites larger or smaller Animated sprites – Need to write code that cycles the animation yourself – Variant of batch.Draw() where you specify a specific rectangle within a Texture2D to draw Warp effects – Deform the Texture2D before placing on screen