Jan52010

Jan52010 - UC
Santa
Cruz
...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UC
Santa
Cruz
 Computer
Science
–
Game
Design
 CMPS 20: Game Design Experience Course
Overview
 Introduc;on
to
XNA
 Introduc;on
to
C#
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Administrivia
 •  CMPS
20
 •  Professor:
Arnav
Jhala
([email protected])
 –  Office
Hours:
Monday
3
to
4:30
and
by
 appointment
 •  Teaching
Assistant:
David
Seagal
 ([email protected])
 •  Readers/Tutors:
Robert
Kavert,
Adrien
Young,
 Slade
Villena
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Class
Informa;on
 •  Website
 www.soe.ucsc.edu/classes/cmps020/Winter10
 •  Schedule
(Lecture
slides,
notes,
due
dates)
 •  Homework
and
Project
Informa;on
 –  Descrip;on
and
Evalua;on
Criteria
 •  Resources
(Links
to
ar;cles,
tutorials,
examples,
etc.)
 •  Twiger:
CMPS20W10
 •  Keep
up
with
class
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Introduc;ons
 •  Professor
 •  Teaching
Assistant
 •  Students
 –  Name
 –  Major
 –  One
Favorite
Game
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Course
Intro:
Objec;ves
 •  Learn
basic
principles
of
game
programming
 –  –  –  –  Main
game
loop,
display
of
2D
sprites
and
3D
objects
 Content
pipeline,
Art
Integra;on
 Collision
detec;on,
scrolling
game
worlds,
shaders
 Audio
 •  Learn
basic
game
AI
techniques
 –  Simple
behaviors,
A*
pathfinding
 –  Subdividing
a
project
into
classes
 –  Unified
Modeling
Language
structure
diagrams
 –  Sokware
design
pagerns
 –  C#
language,
coding
focused
assignments
 •  Learn
basic
principles
of
object‐oriented
design
 •  Develop
increased
proficiency
in
programming
 •  Learn
techniques
for
working
as
a
team
 –  Quarter‐long
game
project
developed
in
4
person
team
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
selec;on:
1%

 Game
concept
document:
5%

 Work
breakdown
and
schedule:
3%

 Technical
design
document:
7%

 Par;ally
opera;onal
game
prototype:
3%

 Updated
schedule:
1%

 Final
game
project:
20%

 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–Game
Design
 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
 –  Ar;cles
that
are
uploaded
on
class
website
 –  Links
to
XNA
and
C#
development
forums,
 tutorials,
etc.
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Project
 •  Work
in
teams
of
4
to
create
a
fully
playable
computer
 game
 –  Developed
on
XNA
plaporm
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
 –  –  –  –  –  –  –  Team
Forma;on
–
Week
2
 Game
Concept
Document
–
Week
4
 Produc;on
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

 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 •  Phases
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 XNA
Game
Studio
Express
 •  XNA
GSE
is
a
series
of
libraries
for
crea;ng
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:
 –  hgp://catalog.xna.com/en‐US/gamescatalog.aspx
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 XNA
Game
Studio
Architecture
 •  You
write
your
game
in
C#
 –  Using
features
in
XNA
Framework

 •  Runs
on
top
of
common
language
run;me
(“Managed
Code”)
 Game
code
(C#)
&
content
 XNA
Framework
 Common
Language
Run;me
(CLR)
 Windows
APIs,
DirectX

 You
provide
 Provided
for
you
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 XNA
Features
 •  2D
&
3D
graphics
support
 •  Audio
support
 •  Pixel
and
vertex
shaders
 –  Access
to
HLSL
(High
level
shader
language)
 –  XACT
cross‐plaporm
audio
tool
 –  Xbox
360
controller
 •  Controller
and
keyboard
input
 •  •  •  •  •  Font
support
 Content
Pipeline
 Game
save
storage
 Networking
 …
and
much
more
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Installing
XNA
Game
Studio
Express
 •  Follow
instruc;ons
on
pages
linked
from:
 –  hgp://creators.xna.com/en‐US/downloads
 –  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
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 •  XNA
Creator’s
Club
Website
 XNA
Creator’s
Club
 –  hgp://creators.xna.com/
 –  Community
website
for
XNA
GSE
 –  Mul;ple
complete
games
with
source
code
 –  Many
tutorials,
and
code
examples
 –  Very
ac;ve
discussion
forums
 –  Can
put
game
on
Xbox
360
 –  Access
to
premium
content
 •  Creator’s
Club
Subscrip;ons
 •  Trial
membership
‐
free
 –  Available
through
Dream
Spark
or
MSDNAA
 •  Allows
you
to
put
game
on
Xbox
360
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 •  XNA
Team
Blog
 •  Ziggyware
 XNA
Community
Web
Sites
 –  blogs.msdn.com/xna/
 –  Announcements
from
the
XNA
dev.
Team
 –  www.ziggyware.com
 –  Developer‐oriented
XNA
news
 –  Recent
contest
for
XNA
tutorial
ar;cles
 •  Winner:
Skeel
Keng‐Siang
Lee’s
Introduc;on
To
Sok
Body
 Physics
 •  XNA
Development
 –  www.xnadevelopment.com
 –  XNA
tutorials.
See
also
the
Links
page
for
links
to
 other
quality
XNA
websites
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 •  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)
 Controllers
 •  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
mul;ple
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
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Demonstra;on
of
Visual
C#
Express
&
XNA
 •  Demonstra*on of loading, compiling, and running one of the sample games for XNA Game Studio Express Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
(Ge|ng
Started)
in
XNA 3.0 Read
in
Programming C# 3.0 –  Chapter
1
(C#
3.0
and.NET
3.5)
 –  Chapter
2
(Ge|ng
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
 Book
is
available
online,
via
O’Reilly
Safari
 –  Get
familiar
with
Visual
C#
2008
environment
 –  hgp://proquest.safaribooksonline.com/9780596527433
 –  Use
on‐campus
computer
to
access
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Intro
to
C#
 •  Slides
adapted
from
Jim
Whitehead’s
“Intro
to
C#”
at
UCSC
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Goals
of
the
C#
language
 •  A
simple,
modern,
general‐purpose
object‐oriented
langauge
 •  Sokware
robustness
and
programmer
produc;vity
 –  Strong
type
checking,
array
bounds
checking,
detec;on
of
use
of
 unini;alized
variables,
source
code
portability,
automa;c
garbage
 collec;on
 •  Useable
in
crea;ng
sokware
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
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Brief
history
of
C#
 •  Originated
by
Microsok
as
a

 response
to
Java
 •  Language
name
inspired
by
musical
note
C#
 •  Lead
designers:
Anders
Hejlsberg,
Scog
 Wiltamuth
 •  C#
standardized
via
ECMA
and
ISO
 –  Ini;al
public
release
in
2000
 –  A
“step
above”
C/C++
(and
Java)
 –  Linux
wags:
Db
(D‐flat,
same
note,
different
name)
 –  Hejlsberg
experience:
Turbo
Pascal,
Borland
Delphi,
J+ +
 –  However,
Microsok
retains
architectural
control
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Key
language
features
 •  Unified
object
system
 –  Everything
type
is
an
object,

 even
primi;ves
 •  Single
inheritance
 •  Interfaces
 –  Specify
methods
&
interfaces,

 but
no
implementa;on
 •  Structs
 –  A
restricted,
lightweight
(efficient)
type
 •  Delegates
 –  Expressive
typesafe
func;on
pointer
 –  Useful
for
strategy
and
observer
design
pagerns
 cking,
Flickr
 www.flickr.com/photos/spotsgot/1414345/
 •  Preprocessor
direc;ves
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
ac;on
is
called
WriteLine.
 The
WriteLine
method
belongs
to
the
System.Console
object.
 The
keyword
“sta;c”
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.
 Demonstra*on of crea*ng Hello World inside Visual C# Express Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 oskay,
Flickr
 www.flickr.com/photos/oskay/472097903/
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Syntax
 •  Case‐sensi;ve
 •  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
 –  ///
<comment_in_xml>
 •  Automa;c
XML
commen;ng
facility

 Peter
Hellberg,
Flickr
 www.flickr.com/photos/peterhellberg/1858249410
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Classes
and
Objects
 •  A
class
combines
together
 –  Data
 –  Behavior
 •  Class
variables
 •  Methods
 Jelly
bean
mold,
photo
by
daxiang
stef
 www.flickr.com/photos/daxiang/96508482/
 •  A
key
feature
of
object‐
 oriented
languages
 •  Class/instance
dis;nc;on
 –  Procedural
languages,
such
as
C,
did
not
require
clustering
of
data
and
 behavior
 –  Class
defines
variables
&
methods
 –  Need
to
create
instanced
of
the
class,
called
objects,
to
use
variables
&
 methods
 –  Excep;on:
sta;c
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)
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Defining
a
class
 [attributes] [access-modifiers] class identifier [:base-class [,interface(s)]] { class-body } Simple example: class A { int num = 0; } •  •  •  •  •  // a simple variable cking,
Flickr
 www.flickr.com/photos/spotsgot/1559060/
 A (int initial_num) { num = initial_num; } // set initial value of num Agributes:
used
to
add
metadata
to
a
class
 –  Can
safely
be
ignored
 Access
modifiers:
one
of
 Base‐class
 Interfaces
 –  public,
private,
protected,
internal,
protected
internal
 –  Indicates
(op;onal)
parent
for
inheritance
 –  Indicates
(op;onal)
interfaces
that
supply
method
signatures
that
need
to
be
implemented
in
the
class
 Class‐body
 –  Code
for
the
variables
and
methods
of
the
class
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Inheritance
 •  Opera;onally
 –  If
class
B
inherits
from
base
class
A,
it
gains
all
of
the
 variables
and
methods
of
A
 –  Class
B
can
op;onally
add
more
variables
and
methods
 –  Class
B
can
op;onally
change
the
methods
of
A
 –  Reuse
of
class
by
specializing
it
for
a
specific
context
 –  Extending
a
general
class
for
more
specific
uses
 –  Allow
reuse
of
method
defini;ons
of

 interface
 –  Subclass
must
implement
method

 defini;ons

 cking,
Flickr
 •  Uses
 •  Interfaces
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 www.flickr.com/photos/spotsgot/1500855/
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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(); B b = new B(); // Create instance of A // 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 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Visibility
 •  A
class
is
a
container
for
data

 and
behavior
 •  Oken
want
to
control
over

 which
code:
 •  Access
modifiers:
 –  Public
 –  Can
read
&
write
data
 –  Can
call
methods
 •  No
restric;ons.
Members
visible

 to
any
method
of
any
class
 Clearly
Ambiguous,
Flickr
 www.flickr.com/photos/clearlyambiguous/47022668/
 –  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)
 •  Members
in
class
A
marked
protected
accessible
to
methods
of
class
A
and
 subclasses
of
A.
 –  Protected
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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_sigers:
is
private,
but
is
 defined
in
B
 •  Class
C
can
see:
 –  num_slugs:
is
public
in
A
 –  Can’t
see:
 Raindog,
Flickr
 •  num_trees:
protected
in
A
 •  num_tree_sigers:
private
in
B
 www.flickr.com/photos/raindog/
 436176848/
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
 –  Typically
classes
have
explicitly

 provided
constructor
 •  Creates
object
but
takes
no
other
ac;on
 bucklava,
Flickr
 www.fl[email protected]/1985775921/
 •  Constructor
 –  Has
same
name
as
the
class
 –  Can
take
arguments
 –  Usually
public,
though
not
always
 •  Singleton
design
pagern
makes
constructor
private
to
ensure
only
one
 object
instance
is
created
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 •  Value
types
 Type
System
 –  Directly
contain
data
 –  Cannot
be
null –  Allocated
on
the
stack
 •  Reference
types
 –  Contain
references
to
objects
 –  May
be
null
 –  Allocated
on
the
heap
 Numeral
type,
by
threedots
 www.flickr.com/photos/threedots/115805043/
 Slide
adapted
from
“Introduc;on
to
 C#”,
Anders
Hejlsberg
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 www.ecma‐interna;onal.org/ac;vi;es/Languages/
 Introduc;on%20to%20Csharp.ppt
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Predefined
Types
 •  C#
predefined
types
 –  Reference 

object,
string
 –  Signed 
 

sbyte,
short,
int,
long
 –  Unsigned
 

byte,
ushort,
uint,
ulong
 –  Character 

char
(2
byte,
Unicode)
 –  Floa;ng‐point 

float,
double,
decimal
 –  Logical 
 

bool
 •  Predefined
types
are
simply
aliases
for
system‐ provided
types
 –  For
example,
int
==
System.Int32
 Slide
from
“Introduc;on
to
C#”,
 Anders
Hejlsberg
 www.ecma‐interna;onal.org/ac;vi;es/Languages/
 Introduc;on%20to%20Csharp.ppt
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
calcula;ons
 –  300.5m
 •  Suffix
“m”
or
“M”
indicates
decimal
 tackyspoons,
Flickr
 www.flickr.com/photos/tackyspoons/812710409/
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Unified
type
system
 •  All
types
ul;mately
inherit
from
object
 –  Classes,
enums,
arrays,
delegates,
structs,
…
 •  An
implicit
conversion
exists
from
any
type
to
 type
object
 Slide
from
“Introduc;on
to
C#”,
 Anders
Hejlsberg
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 www.ecma‐interna;onal.org/ac;vi;es/Languages/
 Introduc;on%20to%20Csharp.ppt
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Unified
Type
System
(Boxing)
 •  Boxing
 –  Process
of
conver;ng
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
alloca;ng
a
“box”,
then
copying
the
value
into
it
 •  Unboxing
 –  Extracts
the
value
type
from
the
object
 –  Checks
type
of
box,
copies
value
out
 i o j Slide
adapted
from
“Introduc;on
to
 C#”,
Anders
Hejlsberg
 Introduc;on%20to%20Csharp.ppt
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 www.ecma‐interna;onal.org/ac;vi;es/Languages/
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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; Variables
 •  Variables
must
be
ini;alized
or
assigned
to
 before
first
use
 •  Class
members
take
a
visibility
operator
 beforehand
(private
by
default)
 •  Constants
cannot
be
changed
 Adaptedfrom
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 enum identifier [: base-type] { enumerator-list} Example: enum Grades { gradeA = 94, gradeAminus = 90, gradeBplus = 87, gradeB = 84 } Enumera;ons
 •  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
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 if (expression) statement1 [else statement2] Example: Condi;onals
 if (i < 5) { System.Console.Writeln(“i is smaller than 5”); } else { System.Console.Writeln(“i is greater than or equal to 5”); } •  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
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Switch
statement
 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; •  Alterna;ve
to
if
 } •  Typically
use
break
 •  Can
use
goto
to
con;nue
to
another
case
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC 
 ...
View Full Document

This note was uploaded on 11/10/2010 for the course CMPS 20 taught by Professor Jhala,a during the Winter '10 term at UCSC.

Ask a homework question - tutors are online