Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Course Overview Introduction to XNA Introduction to C#

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Chapter 1: Computer Systems
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 10.
Introduction to C# Properties, Arrays, Loops, Lists Game Design Experience Professor Jim Whitehead January 28, 2008 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Introduction to C# Game Design Experience Professor Jim Whitehead January 9, 2009 Creative Commons Attribution 3.0 (Except imported slides, as noted) creativecommons.org/licenses/by/3.0.
Course Overview Introduction to XNA Game Studio Express Game Design Experience Prof. Jim Whitehead January 7, 2008.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to C# Game Design Experience Professor Jim Whitehead January 22, 2008 Creative Commons Attribution 3.0 (Except imported slides, as noted)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Course Overview Introduction to XNA Game Studio Express Game Design Experience Prof. Jim Whitehead January 8, 2008.
Introduction to C# Properties, Arrays, Loops, Lists Game Design Experience Professor Jim Whitehead January 12, 2008 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Computer Science II Exam I Review Monday, February 6, 2006.
Lecture 9 Concepts of Programming Languages
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Abstract Data Types and Encapsulation Concepts
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience January 14, 2010 Arnav Jhala.
CIS 199 Test 01 Review. Computer Hardware  Central Processing Unit (CPU)  Brains  Operations performed here  Main Memory (RAM)  Scratchpad  Work.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 2: Using Data.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Understanding Data Types and Collections Lesson 2.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Object-Oriented Programming Chapter Chapter
Introduction to C# Modified by Ian Horswill,based on slides from: Game Design Experience Professor Jim Whitehead January 22, 2008 Creative Commons Attribution.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Object Oriented Software Development 4. C# data types, objects and references.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Written by: Dr. JJ Shepherd
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Computational Cinematics Studio // Center for Games and Playable Media // CMPS 20 – Game Design.
Basic Introduction to C#
Abstract Data Types and Encapsulation Concepts
Java Primer 1: Types, Classes and Operators
Chapter 3: Understanding C# Language Fundamentals
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Abstract Data Types and Encapsulation Concepts
Chapter 1: Computer Systems
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Course Overview Introduction to XNA Introduction to C#

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Administrivia If you did not sign for CMPS 20, here’s your chance to leave Permission codes: Class is already overflowing so chances are slim for any more students to join in 80K – Game Design Fundamentals : Covers history and design of games; could become a major requirement; very useful and highly recommended

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Administrivia CMPS 20 Professor: Arnav Jhala – Office Hours: Monday 3 to 4:30 and by appointment Teaching Assistant: David Seagal Readers/Tutors: Robert Kavert, Adrien Young, Slade Villena

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Class Information Website Schedule (Lecture slides, notes, due dates) Homework and Project Information – Description and Evaluation Criteria Resources (Links to articles, tutorials, examples, etc.) Twitter: CMPS20W10 Keep up with class

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Introductions Professor Teaching Assistant Students – Name – Major – One Favorite Game

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Course Intro: Objectives Learn basic principles of game programming – Main game loop, display of 2D sprites and 3D objects – Content pipeline, Art Integration – Collision detection, scrolling game worlds, shaders – Audio Learn basic game AI techniques – Simple behaviors, A* pathfinding Learn basic principles of object-oriented design – Subdividing a project into classes – Unified Modeling Language structure diagrams – Software design patterns Develop increased proficiency in programming – C# language, coding focused assignments Learn techniques for working as a team – Quarter-long game project developed in 4 person team

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Grades Homework: 30% (3 assignments, each worth 10%) Midterm exam: 15% Final exam: 15% Term project: 40%, broken down as follows – (Percentages are of final course grade, and sum to 40%) – Team selection: 1% – Game concept document: 5% – Work breakdown and schedule: 3% – Technical design document: 7% – Partially operational game prototype: 3% – Updated schedule: 1% – Final game project: 20%

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Reading Material Textbooks – Learning XNA 3.0 by Aaron Reed, O’Reilly publishers, 2008 – Programming C# 3.0 by Jesse Liberty and Donald Xie, O’Reilly publishers, 2007 – Available at campus bookstore and online Reference Materials – Articles that are uploaded on class website – Links to XNA and C# development forums, tutorials, etc.

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Project Work in teams of 4 to create a fully playable computer game – Developed on XNA platform in C# (covered in class) – XNA provides libraries and art content (meshes, textures, etc.) is freely available online – Created games can run on Xbox 360, PC, and Zune – Examples Phases – Team Formation – Week 2 – Game Concept Document – Week 4 – Production Schedule Document – Week 5 – Technical Design Document (including prototypes) – Week 7 – Playable Game Milestone 1 – Week 8 – Playable Game Milestone 2 – Week 9 – Final Game – Week 10

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC XNA Game Examples Some of these were made in 48 hours over a weekend by groups of 3 to 4 programmers Student games Research projects

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC XNA Game Studio Express XNA GSE is a series of libraries for creating 2D and 3D computer games – Uses C# as the primary programming language – Integrated with Visual Studio C# Express Also now the full version of Visual Studio – Games can run under Windows or on Xbox 360 – It is possible to create professional games using this toolkit Example games: –

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC XNA Game Studio Architecture You write your game in C# – Using features in XNA Framework Runs on top of common language runtime (“Managed Code”) Windows APIs, DirectX Common Language Runtime (CLR) XNA Framework Game code (C#) & content You provide Provided for you

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC XNA Features 2D & 3D graphics support – Access to HLSL (High level shader language) Pixel and vertex shaders Audio support – XACT cross-platform audio tool Controller and keyboard input – Xbox 360 controller Font support Content Pipeline Game save storage Networking … and much more

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Installing XNA Game Studio Express Follow instructions on pages linked from: – – Also found on Tools page of course website Install Visual Studio – Visual Studio is an integrated development environment (editor/debugger/compiler) – Unless you currently use Visual Studio, you want “Visual C# 2008 Express” XNA GSE will work with Visual Studio 2008 Professional if you have that installed instead Install XNA Game Studio 3.1 – You want version 3.1, the latest version – The textbook covers 3.0 – Version 3.1 is broadly similar to version 3.0

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC XNA Creator’s Club XNA Creator’s Club Website – – Community website for XNA GSE – Multiple complete games with source code – Many tutorials, and code examples – Very active discussion forums Creator’s Club Subscriptions – Can put game on Xbox 360 – Access to premium content Trial membership - free – Available through Dream Spark or MSDNAA Allows you to put game on Xbox 360

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC XNA Community Web Sites XNA Team Blog – blogs.msdn.com/xna/ – Announcements from the XNA dev. Team Ziggyware – – Developer-oriented XNA news – Recent contest for XNA tutorial articles Winner: Skeel Keng-Siang Lee’s Introduction To Soft Body Physics XNA Development – – XNA tutorials. See also the Links page for links to other quality XNA websites

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Controllers XNA Game Studio Express allows you to use Xbox 360 controllers – Normal Xbox 360 controller is Bluetooth wireless, and is not recognized by the Windows Bluetooth manager – Hence, when developing game under Windows, won’t be able to test control scheme (bad) To create a game using Xbox 360 controller, need to: – Buy a corded Windows Xbox 360 controller (~$35 + shipping) Google for “xbox 360 controller windows” for multiple online vendors – OR, buy an Xbox 360 wireless gaming receiver (~$20 + shipping) allows wireless controller to work with Windows – Should buy now, so you have it ready for when you start programming Can also create a game that uses keyboard input – Would need to change control scheme to port to Xbox 360

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Demonstration of Visual C# Express & XNA Demonstration of loading, compiling, and running one of the sample games for XNA Game Studio Express

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Homework Visit Creators Club website Download and install – Visual Studio C# 2008 Express – XNA Game Studio Express Compile and run a sample game Play around with starter kits Read Chapter 1 (Getting Started) in XNA 3.0 Read in Programming C# 3.0 – Chapter 1 (C# 3.0 and.NET 3.5) – Chapter 2 (Getting Started: "Hello World") – Chapter 3 (C# Language Fundamentals) Try one of the example code samples from the book for yourself in Visual C# 2008 Express – Get familiar with Visual C# 2008 environment Book is available online, via O’Reilly Safari – – Use on-campus computer to access

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Intro to C# Slides adapted from Jim Whitehead’s “Intro to C#” at UCSC

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Goals of the C# language A simple, modern, general-purpose object-oriented langauge Software robustness and programmer productivity – Strong type checking, array bounds checking, detection of use of uninitialized variables, source code portability, automatic garbage collection Useable in creating software components Ease of learning by programmers familiar with C++ and Java Usable for embedded and large system programming Strong performance, but not intended to compete with C or assembly language Type II safety cans for flammables

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Brief history of C# Originated by Microsoft as a response to Java – Initial public release in 2000 Language name inspired by musical note C# – A “step above” C/C++ (and Java) – Linux wags: D b (D-flat, same note, different name) Lead designers: Anders Hejlsberg, Scott Wiltamuth – Hejlsberg experience: Turbo Pascal, Borland Delphi, J++ C# standardized via ECMA and ISO – However, Microsoft retains architectural control

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Key language features Unified object system – Everything type is an object, even primitives Single inheritance Interfaces – Specify methods & interfaces, but no implementation Structs – A restricted, lightweight (efficient) type Delegates – Expressive typesafe function pointer – Useful for strategy and observer design patterns Preprocessor directives cking, Flickr

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Hello World example class Hello { static void Main() { // Use the system console object System.Console.WriteLine(“Hello, World!”); } Creates a new object type (class) called Hello. It contains a single method, called Main. Main contains one line, which writes “Hello, World!” on the display. The method that performs this action is called WriteLine. The WriteLine method belongs to the System.Console object. The keyword “static” means that the method Main can be called even if there is no current instance of the class. It’s a class method, not an instance method. The line beginning with // is a comment, and does not execute. Demonstration of creating Hello World inside Visual C# Express oskay, Flickr

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Syntax Case-sensitive Whitespace has no meaning – Sequences of space, tab, linefeed, carriage return Semicolons are used to terminate statements (;) Curly braces {} enclose code blocks Comments: – /* comment */ – // comment – /// Automatic XML commenting facility Peter Hellberg, Flickr

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Classes and Objects A class combines together – Data Class variables – Behavior Methods A key feature of object- oriented languages – Procedural languages, such as C, did not require clustering of data and behavior Class/instance distinction – Class defines variables & methods – Need to create instanced of the class, called objects, to use variables & methods – Exception: static methods and variables – Analogy: a jelly bean mold (class) can be used to create a large number of jelly beans (objects, instances of the class) Jelly bean mold, photo by daxiang stef

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Defining a class Attributes: used to add metadata to a class – Can safely be ignored Access modifiers: one of – public, private, protected, internal, protected internal Base-class – Indicates (optional) parent for inheritance Interfaces – Indicates (optional) interfaces that supply method signatures that need to be implemented in the class Class-body – Code for the variables and methods of the class [attributes] [access-modifiers] class identifier [:base-class [,interface(s)]] { class-body } Simple example: class A { int num = 0; // a simple variable A (int initial_num) { num = initial_num; } // set initial value of num } cking, Flickr

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Inheritance Operationally – If class B inherits from base class A, it gains all of the variables and methods of A – Class B can optionally add more variables and methods – Class B can optionally change the methods of A Uses – Reuse of class by specializing it for a specific context – Extending a general class for more specific uses Interfaces – Allow reuse of method definitions of interface – Subclass must implement method definitions cking, Flickr

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Inheritance Example class A { public void display_one() { System.Console.WriteLine("I come from A"); } class B : A { public void display_two() { System.Console.WriteLine("I come from B, child of A"); } class App { static void Main() { A a = new A(); // Create instance of A B b = new B(); // Create instance of B a.display_one(); // I come from A b.display_one(); // I come from A b.display_two(); // I come from B, child of A } In-class demo of this code in Visual C# Express

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Visibility A class is a container for data and behavior Often want to control over which code: – Can read & write data – Can call methods Access modifiers: – Public No restrictions. Members visible to any method of any class – Private Members in class A marked private only accessible to methods of class A Default visibility of class variables (but is good to state this explicitly) – Protected Members in class A marked protected accessible to methods of class A and subclasses of A. Clearly Ambiguous, Flickr

Computer Science – Game DesignUC Santa Cruz Visibility Example class A { public int num_slugs; protected int num_trees; … } class B : A { private int num_tree_sitters; … } class C { … } Class A can see: – num_slugs: is public – num_trees: is protected, but is defined in A Class B can see: – num_slugs: is public in A – num_trees: is protected in parent A – num_tree_sitters: is private, but is defined in B Class C can see: – num_slugs: is public in A – Can’t see: num_trees: protected in A num_tree_sitters: private in B Raindog, Flickr /

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Constructors Use “new” to create a new object instance – This causes the “constructor” to be called A constructor is a method called when an object is created – C# provides a default constructor for every class Creates object but takes no other action – Typically classes have explicitly provided constructor Constructor – Has same name as the class – Can take arguments – Usually public, though not always Singleton design pattern makes constructor private to ensure only one object instance is created bucklava, Flickr

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Type System Value types – Directly contain data – Cannot be null – Allocated on the stack Reference types – Contain references to objects – May be null – Allocated on the heap int i = 123; string s = "Hello world"; 123 i s "Hello world" Slide adapted from “Introduction to C#”, Anders Hejlsberg Introduction%20to%20Csharp.ppt Numeral type, by threedots

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Predefined Types C# predefined types – Reference object, string – Signed sbyte, short, int, long – Unsigned byte, ushort, uint, ulong – Character char (2 byte, Unicode) – Floating-point float, double, decimal – Logical bool Predefined types are simply aliases for system- provided types – For example, int == System.Int32 Slide from “Introduction to C#”, Anders Hejlsberg Introduction%20to%20Csharp.ppt

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Unusual types in C# Bool – Holds a boolean value, “true” or “false” – Integer values do not equal to boolean values 0 does not equal false There is no built-in conversion from integer to boolean Decimal – A fixed precision number up to 28 digits plus decimal point – Useful for money calculations – 300.5m Suffix “m” or “M” indicates decimal tackyspoons, Flickr

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Unified type system All types ultimately inherit from object – Classes, enums, arrays, delegates, structs, … An implicit conversion exists from any type to type object Stream MemoryStreamFileStream Hashtabledoubleint object Slide from “Introduction to C#”, Anders Hejlsberg Introduction%20to%20Csharp.ppt

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Unified Type System (Boxing) Boxing – Process of converting a value type to the type object – Wraps value inside a System.Object and stores it on the managed heap Can think of this as allocating a “box”, then copying the value into it Unboxing – Extracts the value type from the object – Checks type of box, copies value out int i = 123; object o = (object)i; int j = (int)o; 123 i o 123 System.Int j Slide adapted from “Introduction to C#”, Anders Hejlsberg Introduction%20to%20Csharp.ppt

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Variables Variables must be initialized or assigned to before first use Class members take a visibility operator beforehand (private by default) Constants cannot be changed type variable-name [= initialization-expression]; Examples: int number_of_slugs = 0; string name; float myfloat = 0.5f; bool hotOrNot = true; Also constants: const int freezingPoint = 32;

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Enumerations Base type can be any integral type (ushort, long) except for char Defaults to int Must cast to int to display in Writeln – Example: (int)g.gradeA enum identifier [: base-type] { enumerator-list} Example: enum Grades { gradeA = 94, gradeAminus = 90, gradeBplus = 87, gradeB = 84 }

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Conditionals C# supports C/C++/Java syntax for “if” statement Expression must evaluate to a bool value – No integer expressions here == means “equal to” for boolean comparison – if (i == 5) // if i equals 5 – if (i = 5) // error, since i = 5 is not a boolean expression if (expression) statement1 [else statement2] Example: if (i < 5) { System.Console.Writeln(“i is smaller than 5”); } else { System.Console.Writeln(“i is greater than or equal to 5”); }

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Switch statement Alternative to if Typically use break Can use goto to continue to another case switch (expression) { case constant-expression: statement(s); jump-statement [default: statement(s);] Example: const int raining = 1; const int snowing = 0; int weather = snowing; switch (weather) { case snowing: System.Console.Writeln(“It is snowing!”); goto case raining; case raining; System.Console.Writeln(“I am wet!”); break; default: System.Console.Writeln(“Weath er OK”); break; }

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Information Hiding in OO Languages Classes support information hiding – Data members of a class can be declared as private Hides implementation details of the class – Create accessor methods to access data Typically get__() and set__(), which are public “Getters and setters” – Other classes access data via get() and set() method So long as the interface to get and set stay the same, the class can change how it represents its data Information hiding permits implementations to change without affecting using classes – But, is tedious to always access data via accessors x = foo.getX() is more tedious than x = foo.x; Wouldn’t it be great to preserve benefits of accessors, while also retaining the syntax of direct access?

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC C# Properties Provide procedural access to data – Like accessors But have syntax similar to direct variable access – foo.X instead of foo.getX(); Minor feature, but provides substantial improvement in readability and fluidity of programming Travis thumbs his nose at private property

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC C# Property Syntax Get accessor returns value of same type as “return type” Set accessors have implicit parameter named “value” – Use to set internal data representation Properties can be public, private, protected – Public: any class can access, private: only that class, protected: that class and children By convention, property names have initial capital (“X” to access “x”) [access-modifiers] return-type property-name { get { … sequence of statements ending in a return (or throw) } set { … sequence of statements } get accessor set accessor

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC C# Property Example public class GameInfo { private string name; public string Name { get { return name; } set { name = value; } // Test code GameInfo g = new GameInfo(); // Call set accessor g.Name = “Radiant Silvergun”; // Call get accessor System.Console.Write(g.Nam e);

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Automatic Properties Very often, properties are straightforward getters and setters – Get accessor just reads value of one variable – Set accessor just writes the value of one variable Creating properties in this situation is very mechanical With automatic properties, the compiler creates these straightforward get and set accessors – New to C# 3.0

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC C# Automatic Property Example public class GameInfo { public string Name {get; set;} } // Test code GameInfo g = new GameInfo(); // Call set accessor g.Name = “Radiant Silvergun”; // Call get accessor System.Console.Write(g.Nam e); This property behaves the same as the first property example, two slides ago. A private class variable, private String name, is automatically created.

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Arrays Array is an indexed collection of objects – Index means you use array[i] syntax to access members – Recall that types like int, string, float, etc. are objects – Can even have arrays of arrays Unlike C++, in C# array is an object Arrays have many useful properties and methods – Properties: Length: length of array Rank: number of dimensions of array – Methods: Sort(): sorts values in one dimensional array BinarySearch(): searches one dimensional array Clear(): sets range of elements to 0 or null reference Reverse(): reverses elements in one dimensional array … and many others

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Declaring an Array Array numbering follows C conventions – First element is numbers[0] – Upper bound is 2, so 3 rd element is numbers[2] type[] array-name; Example: int[] numbers; numbers = new int[3]; Technically, just creates a variable (numbers) that will hold a reference to an array-of- integers object. Creates an array-of-integers object, of length 3, which are initialized to 0. numbers 0 0 0

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Arrays of Reference Types Creating a “new” array of a reference type – Just creates a series of null references – Need to assign object instances to array elements public class GameInfo { public string gameName; } GameInfo[] myGArray = new GameInfo[2]; myGArraynull

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Arrays of Reference Types (2) public class GameInfo { public string gameName; } GameInfo[] myGArray = new GameInfo[2]; GameInfo A = new GameInfo(); GameInfo B = new GameInfo(); myGArray[0] = A; myGArray[1] = B; myGArray[0] [1] A B GameInfo’1 gameName: null GameInfo’2 gameName: null There are only two instances of class GameInfo. There are four reference variables that point to GameInfo. A, B myGArray[0] myGArray[1]

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Initializing Arrays Both syntaxes have identical behavior Can also initialize reference types: int[] anIntArray = new int[3] { 2, 4, 6 } OR int[] anIntArray = { 2, 4, 6 } string[] aStringArray = { “The”, “Quick”, “Brown”, “Fox” } AClass[] AClassArray = { new AClass(), new AClass(), new AClass() }

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Two Dimensional Arrays Can have two (and more) dimensional arrays Also possible to initialize – Implicitly sets bounds of the array type [,] array-name int [,] myTwoDimArray = new int[2, 3]; // Create a 4 x 3 array int [,] myTwoDimArray = { { 0, 1, 2 }, {3, 4, 5}, {6, 7, 8}, {9, 10, 11} }

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Sorting Arrays Call Array.Sort(), passing your array to sort a one dimensional array Call Array.Reverse() to reverse elements string[] aStringArray = { “Cherry”, “Apple”, “Banana”, “Peach” }; // Sort elements Array.Sort( aStringArray ); // Elements now: Apple, Banana, Cherry, Peach Array.Reverse( aStringArray ); // Elements now: Peach, Cherry, Banana, Apple

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Looping in C# C# has four looping constructs – for for (j = 0; j < 5; j++) { … } Classic loop syntax in C-like languages Possibility of off-by-one errors in array indexing – foreach foreach (int j in intArray) Eliminates array indexing errors, no need to create index variable before statement – while while (j < 5) { …; j++; } Loop until an event occurs – do … while do {…; j++;} while (j < 5) Uncommon, perform action, then do condition check

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Foreach Statement Iterates through all elements in an array, or collection type Creates identifier just for the scope of the statements inside the foreach – Holds the current element within the array/collection Very convenient when it can be used string[] aStringArray = { “Cherry”, “Apple”, “Banana”, “Peach” }; // Sort elements Array.Sort( aStringArray ); foreach (string s in aStringArray) System.Console.Write (“{0} : ”, s); // Output: “Apple : Banana : Cherry : Peach : ” foreach ( type identifier in array-or-collection ) { … }

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC List Arrays have problem that you must know how many elements you want in advance – This is not always known List class is collection with variable size – Dynamically increases in size if needed – When an array reaches its capacity, need to create new array, and copy all elements from old array to new array Ugh!

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Creating a List Add elements with Add() method Clear() removes all elements from list Remove() removes first element from list Sort() sorts the list Count property: number of elements in list List listname Example: List stringList = new List () ; // Create list of string. Don’t forget () stringList.Add ( “Quick” ); stringList.Add ( “Brown” ); stringList.Add ( “Fox” ); foreach (string s in myStringList) // Lists work with foreach System.Console.Write("{0} ", s);

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Queue, Stack, Dictionary C# provides queue, stack, and dictionary Queue: first-in, first-out Enqueue(), Dequeue(), Peek() Stack: last-in, first-out Push(), Pop(), Peek() Dictionary – Holds set of key, value pairs – Permits lookup of a value given the key – Example use: extensible character attribute system Keys: strings, names of attribute Value: int, value of specific attribute

Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides from past CMPS 20/80K courses at UCSC Reading Chapter 4 (Classes and Objects) Chapter 9 (Arrays, Indexers, and Collections) from pp in Programming C# 3.0 If you are behind in the reading, you need to catch up fast. – You are already 5-10 hours behind. – It is hard to catch up after this point