Jan112010

Jan112010 - 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 XNA
Game
Studio
 
 January
12,
2010 Arnav
Jhala
 Adapted
from
Jim
Whitehead’s
slides
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Announcements
 •  Session
schedules
being
finalized
 •  Homework
#1
(Hunt
the
Wumpus)
 –  Due
Thursday,
January
21
 –  Detailed
instrucOons
will
be
up
by
tomorrow
 •  Project
themes
and
Omeline
 •  Check
website
frequently
for
updates
 –  Come
for
help
with
Homework
#1,
C#,
XNA
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Imagine
Cup
 •  Worldwide
compeOOon
 –  MicrosoU
sponsored
 –  Many
categories,
including
game
development
 –  RegistraOon
Feb
1
 –  MulOple
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
 –  h[p://imaginecup.com/
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 •  A
game
where
you
move
through
a
dodecahedron
shaped
map
 –  Each
room
is
connected
to
three
other
rooms
 –  Shortest
non‐repeaOng
path
back
to
same
point
is
five
moves
 Hunt
the
Wumpus
 A
dodecahedron
 Source:
Wikipedia
 A
fla[ened
dodecahedron
 Source:
More
BASIC
Computer
Games
 www.atariarchives.org/morebasicgames/showpage.php?page=178
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
draU”
 –  Superbats
 •  Places
player
in
a

 random
new
room
 •  “Bats
nearby”
 Source:
Best
of
CreaOve
CompuOng,
Vol.
1
 www.atariarchives.org/bcc1/showpage.php?page=247
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
connecOons
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
 •  •  •  •  Main
game
loop
 Display
of
warnings
when
player
one
room
away
from
obstacles
 Behavior
of
Wumpus,
pits,
bats
when
they
interact
with
player
 Handling
shooOng
logic
 –  WriOng
game
logic
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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?
 What
are
some
of
the
acOons
on
the
nouns?
 What
can
the
player
do?
 What
can
a
Wumpus
do?
 What
can
bats
do?
 What
can
pits
do?
 –  Verbs
are
methods
on
the
classes
 –  Also
need
to
consider
what
informaOon
other
classes
 might
need
 •  These
will
be
properOes
 •  LocaOon
of
Wumpus,
bats,
pits

 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Console
input
in
C#
 •  There
are
three
methods
for
reading
console
 input
in
C#
 –  ReadLine
 –  ReadKey
 –  Read
 •  Read
a
line
of
input
into
a
string
 •  Read
the
next
key
pressed
into
ConsoleKeyInfo
instance
 •  Provides
access
to
whether
ctrl,
alt,
shiU
were
pressed
 •  Read
a
line
of
input,
then
gives
successive
characters
each
 Ome
you
call
Read
 •  Any
of
these
could
be
used
for
your
assignment
 –  ReadLine
is
easiest
to
use
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Console.ReadLine
 public class ConsoleDemo { static void Main(string args) { string my_input; •  ReadLine
 –  Program
execuOon
 blocks
unOl
user
enters
 a
line,
terminated
by
 Enter
 –  Line
typed
by
user
is
 returned
as
a
string
 } System.Console.WriteLine(“(M)ove or (S)hoot :”); my_input = System.Console.ReadLine(); } UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
sensiOve
 –  An
overloaded
method
 –  We
want:
 •  No
problem
–
use
string.Compare
 •  string.Compare
has
10
variaOons
 –  Returns
0
if
strings
are
the
same

 •  public
staOc
int
Compare(string
strA,
int
indexA,
string
strB,
int
indexB,
int
 length,
bool
ignoreCase);
 •  Compare
two
strings,
strA
and
strB
 •  …
starOng
at
character
number
indexA
in
strA,
and
indexB
in
strB…
 •  …
and
comparing
length
characters
in
each
…
 •  …
with
the
ability
to
ignoreCase.
 •  ‐1
if
A
lexically
smaller
than
B,
1
if
A
lexiacally
larger
than
B
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Parsing
Input
(cont’d)
 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 } •  We
want
to
compare
the
first
 character
of
user
input
 against
 –  “M”
for
move
 –  “S”
for
shoot
 –  “Q”
for
quit
 } Demonstra*on of Console input and string parsing in Visual C# 2008 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Unified
Modeling
Language
(UML)
 •  A
family
of
diagram
types
used
to
model
object‐oriented
 soUware
projects
 –  A
standard
way
to
graphically
represent
complex
assemblages
of
objects,
 components,
etc.
 •  Two
useful
diagram
types
 –  Class
diagram
 •  StaOc
view
of
soUware
 •  Object‐oriented
classes
 •  RelaOonships
 –  Inheritance
 –  Containment
 –  Sequence
diagram
 •  Dynamic
view
 •  Methods
calling
methods,

 and
in
what
order
 Jmgold,
Flickr
 www.flickr.com/photos/jmgold/2210820262/
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
methods
and
parameters
 Class
name
 Class
variables
(a[ributes)
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Modeling
Visibility
 •  The
visibility
of
a
variable
or
method
can
be
 indicated
using:
 +


public
 #


protected
 ‐



private
 Book
 ‐ 
Otle:
string
 +
Title
<<C#property>>
 #
num_pages:
int
=
0
 +
NumPages
<<C#property>>
 …
 +
lookupISBN()
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Inheritance,
Containment
 •  Two
main
relaOonships
modeled
in
class
diagrams
 –  Inheritance
(is‐a,
specializaOon)
 –  Containment
(has‐a)
 shape
 scene
 +
elems:
List<shape>
 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!)
 square
 circle
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 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
 –  IniOalizaOon
 –  Update
 –  Draw
 •  IniOalize(),
LoadContent()
 •  Update
game
state
every
clock
Ock
 •  Create
display
every
clock
Ock
 •  Demonstra*on of XNA GSE scaffolding in Visual C# 2008 Express UC
Santa
Cruz
 Computer
Science
–
Game
Design
 XNA
GSE
Game
Scaffolding
 MicrosoU.Xna.Framework.Game
 #
IniOalize()
 #
Run()
 #
Tick()
 Update(gameTime); Draw(gameTime); myGame
 ‐
graphics:
GraphicsDeviceManager
 ‐
content:
ContentManager
 +
myGame()
 #
IniOalize()
 #
LoadContent(loadAllContent:
bool)
 #
UnloadContent(unloadAllContent:
bool)
 #
Update(gameTime:
GameTime)
 #
Draw(gameTime:
GameTime)
 graphics = new GraphicsDeviceManager(this); Content.RootDirectory = “Content”; base.Initialize() UC
Santa
Cruz
 Computer
Science
–
Game
Design
 •  Create
new
myGame
 XNA
GSE
Game
IniOalizaOon
 –  Call
to
constructor,
myGame()
 –  myGame.run()

 •  IniOalizaOon
phase
of
run(),

 1.  call
IniOalize()
on
parent
class
 2.  IniOalize
your
game
state
 1.  IniOalizes
game,
then,
 2.  Runs
the
main
game
loop
&
processes
events
 –  The
following
methods
are
called
on
myGame
 –  IniOalize()
 1.  Create
player
object,
create
enemy
objects,
create
object
to
hold
 main
game
state,
etc.
 –  LoadContent()
 •  Method
used
to
load
textures,
create
SpriteBatches
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 XNA
GSE
Main
Game
Loop
 •  Time
elapsed
between
each
clock
Ock:
 –  Fixed:
 •  1/60th
of
a
second
(16.6667
milliseconds
per
Ock)
 •  myGame.IsFixedTimeStep
=
true
 •  The
default
value
 •  Adjusts
based
onthe
Ome
required
to
perform
previous
Ock
 •  myGame.IsFixedTimeStep
=
false
 –  Variable:
 •  Each
clock
Ock
 –  Run()
calls
Tick()
 –  Tick()
calls
Update()
then
Draw()
 •  You
supply
Update()
and
Draw()
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Update()
and
Draw()
 •  Update()
 –  –  –  –  –  Update
the
state
of
all
objects
 Receive
input,
move
player
avatar
 Compute
opponent
AI,
move
opponent
objects
 Collision
detecOon
&
consequences
 Detect
end‐of‐game
or
end‐of‐level
condiOon
 •  Draw()
 –  Re‐create
the
on‐screen
scene
using
the
up‐to‐date
posiOons
of
player,
 opponent
 •  Advice
 –  Avoid
stuffing
your
enOre
game
into
the
definiOon
of
these
two
methods
 –  Have
these
methods
call
out
to
your
player
object,
opponent
objects,
etc.
 •  foreach
(Opponent
o
in
opponentList)
o.update();
 •  Methods
become
too
big!
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Ge}ng
a
2D
Image
to
Appear
on
Screen
 LoadContent()
 1.  Create
a
Texture
 –  –  A
bitmap
image
 Collects
all
textures
being
drawn
to
screen
 2.  Create
a
SpriteBatch
 Draw()
 3.  Begin
the
SpriteBatch
 4.  Draw
texture
 –  –  –  Draw()
is
defined
on
a
SpriteBatch
 Adds
texture
to
the
SpriteBatch
 Causes
textures
in
SpriteBatch
to
be
drawn
to
screen
 5.  End
the
SpriteBatch
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 CreaOng
a
Texture
 •  Create
an
instance
of
ContentManager
 –  XNA
GSE
scaffolding
does
this
for
you
 –  Content
=
new
ContentManager(Services)
in
constructor
 •  Call
Load<T>
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
relaOve
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
“verbaOm”
 –  No
need
to
escape
slashes
in
this
case
 –  “\\images\\”
is
the
same
as
@“\images\”
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 •  Create
new
bitmap
image
 Example
of
creaOng
a
texture
 –  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
SoluOon
Explorer
in
Visual
C#
Express
 •  Right
click
on
Bolded
Project
Name
 •  Add

Add
ExisOng
Item
 •  Pick
filename
of
new
bitmap
image
file
 •  Will
now
appear
in
the
project
file
list
 •  Verify
that
Content
Pipeline
processed
file
by
building
soluOon
(F6)
 –  Build
>
Build
SoluOon
 •  Create
a
Texture2D,
then
load
the
bitmap
image
via
the
content
 manager:
 Protected Texture2D m_bullet = null; m_bullet = Content.Load<Texture2D>(@“mushi‐bullet”);
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 SpriteBatch
 •  Once
a
texture
has
been
made,
how
does
this
get
 displayed?
 –  Create
a
SpriteBatch
 –  Within
a
clock
Ock,
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
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 SpriteBatch
Example
 protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { m_batch = new SpriteBatch(graphics.GraphicsDevice); // Initialize the sprite batch m_bullet = content.Load<Texture2D>(@"mushi-bullet"); // 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. } •  Draw()
inside
SpriteBatch
is
heavily
overloaded
 –  7
different
choices

 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 TinOng
Sprites
 •  On
previous
slide,
used
Color.White
in
the
Draw()
method
 –  This
gives
the
Ont
of
the
sprite
 –  White
indicates
use
of
the
original
colors
 –  Can
choose
any
other
color
to
Ont
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. } UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Transparent
Sprites
 •  It
is
possible
to
make
a
sprite
parOally
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. } UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Other
Sprite
features
 •  Depth
ordering
 –  Draw
some
sprites
in
front
of
(behind)
others
to
give
depth
of
field
effect
 •  RotaOon
 –  Can
rotate
sprite
image
to
a
specific
angle
 •  Scaling
 –  Can
make
sprites
larger
or
smaller
 •  Animated
sprites
 –  Need
to
write
code
that
cycles
the
animaOon
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
 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online