Title: pramook@gmail.com
1418383 ?????????????????????????????? 3
- ?????? ???????
- pramook_at_gmail.com
2XNA
- ??????????? C ??? Microsoft ??????????????
- Platform ???????? PC, Xbox, Zune
- ??????????
- ?????? 2 ??????? 3 ????
- ??????????????????
- ?????? content ??????
- ?????????????? input ??? keyboard, mouse, joystick
3XNA
- ????????????????????????????
- ?????????????????? hardware
- ??????????????????????????? ??????????????
????????????? - ???????????????????????????
- ??????????????????????????????????????????????????
??????????? - ????? collision detection
- ????? physics engine
- ???????????????????????????????? 2 ???????? 3
????????????
4??????????
- ????????? Visual C Express 2010
http//www.microsoft.com/express - ????????? Microsoft XNA Game Studio 4.0
- ?? Visual C ?????????????? XNA Game Studio
5????????????
- ???? Visual C 2010
- ????????? File ? New Project
- ????????????? ????? XNA Game Studio 4.0
- ???????????? ????? Windows Games (4.0)
- ????????????? Name
- ???????? Solution (???????? project
????????????????) ?? Solution Name
6????????????
7???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References
- Game.ico
- Game1.cs
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content)
- References
8???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame ? ?????????????????????????
- Properties
- References
- Content References
- Game.ico
- Game1.cs
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content)
- References
9???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties ? ?????????????????????????????????????
- References
- Content References
- Game.ico
- Game1.cs
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content)
- References
10???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References ? ?????????????????????? library
???????? - Content References
- Content
- Game.ico
- Game1.cs
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content)
- References
11???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References
- Content References
- Game.ico
- Game1.cs
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content) ? ???? content (???,
????) ?????? - References
12???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References
- Content References ? ????????? content project
??????? - Game.ico
- Game1.cs
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content)
- References
13???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References
- Content References
- Game.ico ??????
- Game1.cs
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content)
- References
14???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References
- Content References
- Game.ico
- Game1.cs ?????????
- GameThumbnail.png
- Program1.cs
- FirstGameContent (Content)
- References
15???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References
- Content References
- Game.ico
- Game1.cs
- GameThumbnail.png ??????????????????? Xbox
- Program1.cs
- FirstGameContent (Content)
- References
16???????????????????????????
- ?????? Solution Explorer ??????????
- FirstGame
- Properties
- References
- Content References
- Game.ico
- Game1.cs
- GameThumbnail.png
- Program1.cs ????????? main
- FirstGameContent (Content)
- References
17Program.cs
- using System
- namespace FirstGame
-
- static class Program
-
- /// ltsummarygt
- /// The main entry point for the
application. - /// lt/summarygt
- static void Main(string args)
-
- using (Game1 game new Game1())
-
- game.Run()
-
-
-
18Program.cs
- ?????????? main
- ?????????????????????????? ??????????? ??????
- ?? main ?????????? instance ??? Game1
- ?????????????? run ??????? Game1
- ????? run ?????????
- ???? Game1 ??????????????
19Game1.cs
- ????????? Game1 ???????????????????????????????
- Game1 ???? subclass ??? Microsoft.Xna.Framework.Ga
me(???????????????? XNA ???????)
20???????? Microsoft.XNA.Framework.Game
- void Initialize()
- ?????????????????????????? content ??????
- void LoadContent()
- ??????? content
- void UnloadContent()
- ?????????????????? content ???????????????????????
??? - void Update(GameTime gameTime)
- ???????????????????????????????????
- ???????????? logic ???????????
- void Draw(GameTime gameTime)
- ????????????????????????
21Flowchart ??????????????? Game
????? (????? Run)
Initialize()
LoadContent()
Update()
Draw()
??????????
??????
UnloadContent()
22(No Transcript)
23?????????? ?? Game1.cs
- Constructor
- public Game1()
-
- graphics new GraphicsDeviceManager(this)
- Content.RootDirectory "Content"
-
- GraphicsDeviceManager ? ????????????
- Content ? ?????? content ??????
24?????????? ?? Game1.cs
- Initialize()UnloadContent()
- ?????????????????????????? method ???????????
superclass - LoadContent()
- protected override void LoadContent()
-
- spriteBatch new SpriteBatch(GraphicsDevice)
-
- ????? spriteBatch ? object ????????????????????
25?????????? ?? Game1.cs
- Update()
- protected override void Update(GameTime
gameTime) -
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.
Back - ButtonState.Pressed)
- this.Exit()
- // TODO Add your update logic here
- base.Update(gameTime)
-
26?????????? ?? Game1.cs
- GamePad ? ?????? input ??? joystick ??? XBOX
- ????????????????????????????? back
??????????????? 1 ???????????????? - ??????????? this.Exit() ??????????????????
- ?????????????????????
- ??????????????? PC ?????? Keyboard ????????????
- ??????????????????? GamePad ??????
27?????????? ?? Game1.cs
- Draw()
- protected override void Draw(GameTime gameTime)
-
- GraphicsDevice.Clear(Color.CornflowerBlue)
- base.Draw(gameTime)
-
- ??????????????????? CornflowerBlue
28???????
29??????????
30??????????
31???????????? Content
- ????????????????????????????? ???????????????????
Content ???? - ?????????? HelloWorldContent ????????? Add ? New
Item - ????????????????????????????????????????????
32???????????? Content
- ???????????? Content ????????????????????
- ?????????????? Properties ????? Asset Name
?????? - ???????? Asset Name ??????????????????????
33??????????????????????
- ??????????????????? ??????????????????????????????
- ????????????? ????????????? Content ????
- ????? XNA ??????????? object ???? Texture2D
- ???????????????? 2 ??? ??????????, ????????
- ????? field ???? Texture2D ???????????? 2 field
- public class HelloWorld Microsoft.Xna.Framework.
Game -
- GraphicsDeviceManager graphics
- SpriteBatch spriteBatch
- Texture2D background
- Texture2D sprite
34??????????????????????
- ?????????????????????????? ??????? LoadContent()
- protected override void LoadContent()
-
- spriteBatch new SpriteBatch(GraphicsDevice)
- background Content.LoadltTexture2Dgt("kagami-wal
lpaper") - sprite Content.LoadltTexture2Dgt("kagami")
35Content.Load
- ????? type parameter ?????????????????????????????
???? - ??????????? Texture2D
- ?? argument ???????? Asset Name
????????????????? - ?????????????????????????????????????????? Asset
Name ???????? - ??????????? Content.Load ????
- ???
- ?????
- ??????????????????? (????????????????? 6)
36????????????????????
- ??????
- graphics.PreferredBackBufferWidth (?????????)
- graphics.PreferredBackBufferHeight (???????)
- ?? contructor
- public HelloWorld()
-
- graphics new GraphicsDeviceManager(this)
- // Setting the window's size.
- graphics.PreferredBackBufferWidth 800
- graphics.PreferredBackBufferHeight 600
- Content.RootDirectory "Content"
37???? coordinate
- ??????????????????????????????????
- ????????????????????????????
- (0,0) ?????????????????????????????
- (w, h) ????????????????????????????????????????
????? w ????????? ??? h ?????????????????
38???? coordinate
(0,0)
(w,h)
39????????????????????
- ?????????????????????????????????????????
- ??????????????????????????????????????????????????
?????? - ??????????????????????????????????????????????????
?????????????? - ??????????????????????????? object ???? Vector2
- ?? property ???? X ??? Y ????????????????????? X
??? Y - ????? field ???? spritePosition ????????????
- public class HelloWorld Microsoft.Xna.Framework.
Game -
- GraphicsDeviceManager graphics
- SpriteBatch spriteBatch
- Texture2D background
- Texture2D sprite
- Vector2 spritePosition
40??????????????????????????????
- spritePosition ???????????????????????
- ?????????????????????????? Update
- protected override void Update(GameTime gameTime)
-
- if ( ... )
- this.Exit()
- MouseState mouseState Mouse.GetState()
- spritePosition.X mouseState.X - sprite.Width
/ 2 - spritePosition.Y mouseState.Y - sprite.Height
/ 2 - base.Update(gameTime)
-
41Mouse
- ???? object ????????????????????????????????
- ????? GetState() ??? object ?????? MouseState
???????????????????????????????????????????? - ??????????????????? x ??? y ?????????????????????
??? property X ??? Y ??? MouseState
42??????
- ?????????? Draw
- protected override void Draw(GameTime gameTime)
-
- GraphicsDevice.Clear(Color.CornflowerBlue)
- spriteBatch.Begin()
- spriteBatch.Draw(background, new Vector2(0,
0), Color.White) - spriteBatch.Draw(sprite, spritePosition,
Color.White) - spriteBatch.End()
- base.Draw(gameTime)
43SpriteBatch
- ??????????????? 2 ??????? Texture2D
- ??????????????????????? Begin
- ???????????????????????????????? End
- ??????????????????????????????????????
- ??????????????? Draw
- ????? Draw ?? signature ??????? 7 ???
- ??????????????????????????????????
- ?????? draw ?????????????????????????? draw
???????????????????????
44SpriteBatch
- spriteBatch.Draw( sprite, ? Texture2D
spritePosition, Color.White)
45SpriteBatch
- spriteBatch.Draw( sprite, spritePosition, ?
???????????????? Color.White)
46SpriteBatch
- spriteBatch.Draw( sprite, spritePosition,
Color.White) ? ???????? ??? ??????
47SpriteBatch
- spriteBatch.Draw( sprite, spritePosition,
Color.Red) - ???????????????????????????????????????
48??
- ?? tuple ??????????????? 3 ??? (R, G, B)
- R ???????????????????????????
- G ?????????????????????????????
- B ???????????????????????????????
- ??????????????????????? 0 ??? 2???????????????????
??????????????-1 - ??????????????????????? 32 ??????? 1 ??????
- ?????????? 8 ???
- ??????????????????? 255
49Trichromatic Theory of Vision
- ????????????????????????????????????
- ??? ????? ???????
- ??????????????????????????????????????????????
- ??????? ??????? ????????????????????????????
- ???????
- ?????????????????????????
- ?????????????? ????? ??????? ????????? ????????
50???????
(255,0,0)
(0,255,0)
(255,255,0)
(255,255,255)
(255,0,255)
(0,255,255)
(0,0,0)
(0,0,255)
51Color
- ????????????????
- ??????????????????????????????? RGB ???
- Color color1 new Color(128, 0, 255)
- ????????????????????????? 0 ????????????????????
??? 1 ????????????????? - Color color2 new Color(0.5f, 0, 1)
- ?????????????? static field ??????????????????????
?????? ???? - Color.Green, Color.Blue, Color.Lavender, ???
52????????????????????????????????????
53??????????????????????????? keyboard
54Keyboard
- ???????????????????????????????
- ?????????? GetState()
- ??? object ?????? KeyboardState
55KeyboardState
- ????????????????????? ??? keyboard ??????
- ??????????
- IsKeyDown(Keys key)
- ???????????????????????????
- IsKeyUp(Keys key)
- ??????????????????????????????
56Keys
- enum ???????????????????????? ????
- Keys.A ? ???? A
- Keys.Space ? ???? space bar
- Keys.Right ? ????????????
57???????????????????????
- KeyboardState keyboardState Keyboard.GetState()
- if (keyboardState.IsKeyDown(Keys.Left))
-
- ... Do something ...
58????????????????????????????????????
- ??????? spritePosition ???????????????????????????
??????????????? - ???????
- xVelocity ? ???????????????????????????????????
- yVelocity ? ????????????????????????????????????
- ???????????????????? 5 ?????????????
- Vector2 xVelocity new Vector2(5, 0)
- Vector2 yVelocity new Vector2(0, 5)
59???????????????????
- ??????????????????????????????????????????????????
? - protected override void Update(GameTime gameTime)
-
- ??????????????????? back ???????????????????
????? - KeyboardState keyboardState
Keyboard.GetState() - if (keyboardState.IsKeyDown(Keys.Left))
- spritePosition - xVelocity
- if (keyboardState.IsKeyDown(Keys.Right))
- spritePosition xVelocity
- if (keyboardState.IsKeyDown(Keys.Down))
- spritePosition yVelocity
- if (keyboardState.IsKeyDown(Keys.Up))
- spritePosition - yVelocity
- ?????????????????????????????
60?????????????????????????????
- int screenWidth graphics.PreferredBackBufferWidt
h - int screenHeight graphics.PreferredBackBufferHei
ght - if (spritePosition.X lt 0)
- spritePosition.X 0
- if (spritePosition.X sprite.Width gt
screenWidth) - spritePosition.X screenWidth -
sprite.Width - if (spritePosition.Y lt 0)
- spritePosition.Y 0
- if (spritePosition.Y sprite.Height gt
screenHeight) - spritePosition.Y screenHeight -
sprite.Height
61?????????????????????????????
62?????????????????????????????
63Mouse
- ???????????????????????????
- ?????????? GetState()
- ??? object ?????? MouseState
64MouseState
- ?????????????????????
- Property ?????
- LeftButton ? ????????????????
- RightButton ? ???????????????
- MiddleButton ? ????????????????
- X ? ??????????????
- Y ? ?????????????
65ButtonState
- Enum ??????????????????????????
- ??????????????
- Pressed ? ?????????
- Released ? ????????????
66????????????
- ???????????????????????????????????????????
- Texture2D cursor
- Vector2 cursorPosition
- ?? Update() ??????????????????????
- protected override void Update(GameTime gameTime)
-
- ????????????????????
-
- MouseState mouseState Mouse.GetState()
- cursorPosition new Vector2(mouseState.X,
mouseState.Y) - ????????????????????
67????????????
- ?? Draw() ?????????????
- ???????????????????????????? ?????????????????????
???????????????????? - protected override void Draw(GameTime gameTime)
-
- GraphicsDevice.Clear(Color.CornflowerBlue)
- spriteBatch.Begin()
- // ???????????????? //
- spriteBatch.Draw(cursor, cursorPosition,
Color.White) - spriteBatch.End()
- base.Draw(gameTime)
-
68?????????????????
69?????????????????
70??????????????? XNA
- ????? Sprite Font ?? Content ??????
- Load Sprite Font ?? Load Content
- ?????????????? SpriteBatch.DrawString
71????? Sprite Font ?? Content
- ?????????????????? DisplayingTextContent ?Add ?
New Item
72????? Sprite Font ?? Content
73????? Sprite Font
- ???????? sprite font ?????????????????????
- ?????????
- ????
- ???
74Load Sprite Font ?? LoadContent()
- ?????????????? SpriteFont
- SpriteFont font01
- ??? Content.LoadltSpriteFontgt() ????????????
- protected override void LoadContent()
-
- // ????????????? //
- font01 Content.LoadltSpriteFontgt("font01")
75?????????????? SpriteBatch.DrawString
- protected override void Draw(GameTime gameTime)
-
- GraphicsDevice.Clear(Color.White)
-
- spriteBatch.Begin()
- // Draw "Hello" at the center of the screen.
- Vector2 helloSize font01.MeasureString("Hell
o") - float centerX (graphics.PreferredBackBufferW
idth - helloSize.X) / 2 - float centerY (graphics.PreferredBackBufferH
eight - helloSize.Y) / 2 - Vector2 centerPosition new Vector2(centerX,
centerY) -
- spriteBatch.DrawString(
- font01,
- "Hello",
- centerPosition,
- Color.Black)
- spriteBatch.End()
76???????????????
77(No Transcript)
78??????????????????????!
- ?????????????????????
- ????????????
- ???????????
- ???????????
- ??????????
- ??????????
- ??????????????
- ????????????????
- ???
79????
- ????????????????????????
- ????????????????????????????????????????
- ?????????????????????
- ?????????????????????????????
80?????????????????????????????
- ??????????????
- ??????????????????????????????????????????????????
?????? ??? - ??????????????????????????????
- ?????????????
- ??????????????????????????? ???
- ????????????????????????????????????
- ??????? ??????????? ????????? ?????????? if
??????????????????????? - ????????????????? if ???????????????????????
81?????????????????????????????
- ????????????????????????????
- ?????? if ???????????????????????????????????
?????? - ??????????????????????????????????????????????????
?????????? if ????? ?????? - ?????????????????????????? ??????????????????? if
??????????????? ???????????????
82Design Patterns
- ??? ?????????????????????????????????????????????
??????????????? (Wikipedia) - ??????????????????????? ??????????????????????????
????????????????????????????????????????????? - ??????????? pattern ???????????? Java ???? SA
????????
83The Gang of Four
- ??????????????????????? Design Patterns
- ???????????????????????? ???????????????????
84???????????????????????????????????
- ?????????? ?????????? ???????????????
- ??????????????????????????????? design pattern
???? - ???????? State design pattern
85State Design Pattern
- ?????????????????????????????????????????????????
???????????????????????? ?????????????????????????
???????????????????????? - ???????????? ????
- ???????????
- ???????? ???? ???
- ?????????? ???? ??????
- ???? with ????????????????????????
86The State Design Pattern
87State Design Pattern
- Context (?????)
- ???????????????????????????
- ????????? instance ??? ConcreteState
?????????????????? State ?????? ??????? instance
????????????????????????????????????????????? - State
- ?? interface ?????????????????????????????????????
??????????? - ConcreteState
- ??????????????????????????????????????????????????
?
88?????????????????
- Context ? Game
- ???????????????? Microsoft.Xna.Framework.Game
- State ? Screen
- ?????? Game ??????? currentScreen ??????????
screen ?????????????? - ????????????????? screen ?????????????????????
currentScreen
89???? Screen
- ?? method ???????? ??????????? Game
- Initialize
- Update(GameTime gameTime)
- LoadContent
- UnloadContent
- Draw(GameTime gameTime)
- Update(GameTime gameTime)
- Method ???????????????????? abstract method
???????????????????????????? Screen ?? subclass
???
90???? Screen
- ?? field ?????????????????????????
- name ??????????????
- game ?????????? instance ??? Game ??? screen
????????????? - ?? property ?????? field ??????????????
- Name
- Game
- ??????? property ?????????? read-only (????? get)
91???? Screen
- ?? method ???????????????????
- void SwitchedIntoFrom(string sceneName)
- ????????????? screen ??????????????? screen ???
- ?????????? screen ???????? argument
- void Draw(GameTime gameTime, SpriteBatch
spriteBatch) - ???????????????????? SpriteBatch ?????????
- ???? abstract method ????????????? override
- ??????????? Draw(GameTime gameTime) ?????????
SpriteBatch ??? Game ??? screen
??????????????????????????
92???? Game
- ???????????????? Microsoft.Xna.Framework.Game
- ??????????????????????????????????????????????????
?? - ???????? Microsoft.Xna.Frame.Game ?????????
- ?? field
- currentScreen ?????????? screen ????????
- screens ???? Dictionary ??? map ???? screen ?????
screen ????? ?????
93???? Game
- private Screen currentScreen
- public Screen CurrentScreen
-
- get return currentScreen
-
- Dictionaryltstring, Screengt screens
94???? Game
- ????????? screen ?????? ???????? methodvoid
AddScreen(Screen screen) - public void AddScreen(Screen screen)
-
- screensscreen.Name screen
95???? Game
- ??????????? screen ???????? methodvoid
SwitchScreen(string name) - public void SwitchScreen(string name)
-
- var newScreen GetScreenByName(name)
- var oldScreen CurrentScreen
- currentScreen newScreen
- if (oldScreen ! null)
- currentScreen.SwitchedIntoFrom(oldS
creen.Name) - else
- currentScreen.SwitchedIntoFrom("nul
l")
96???? Game
- Screen GetScreenByName(string name)
- ?????????????? screen ??????????????????????
- public Screen GetScreenByName(string name)
-
- if (screens.ContainsKey(name))
- return screensname
- else
- throw new InvalidOperationExceptio
n( - "Screen by name '" name
"' not found.")
97???? Game
- override method ??? Xna.Framework.Game???????????
??????????????? method ???????????? Screen - protected override void Initialize()
-
- foreach (Screen screen in screens.Values)
- screen.Initialize()
- base.Initialize()
-
98???? Game
- protected override void LoadContent()
-
- base.LoadContent()
- spriteBatch new SpriteBatch(Graphics
Device) - foreach (Screen screen in
screens.Values) - screen.LoadContent()
-
- protected override void UnloadContent()
-
- foreach (Screen screen in
screens.Values) - screen.UnloadContent()
- base.UnloadContent()
-
99???? Game
- protected override void Update(GameTime
gameTime) -
- base.Update(gameTime)
- currentScreen.Update(gameTime)
-
- protected override void Draw(GameTime
gameTime) -
- base.Draw(gameTime)
- currentScreen.Draw(gameTime)
-
100????????? Tetris
- ??? Tetris ?????????????? screen ???? 4 screen
- Title Screen ?????????????
- Play Screen ????????????????????????????????????
?? - Full Row Screen ?????????????????????????
?????????????????????? - Game Over Screen ??????????? Game Over
101????????? State Machine
- ????????? ??????
- ???????????? ?????????????????????????????????????
? (active) - ??????????????????????????????????????????
????????????????????????????? ????????????
(??????????????????????) - ?????????????????????? subclass ??? Screen ?????
subclass ???????????????????? - ?????????????????????????? ????????????
SwitchScreen - ??????????????????????????????????????????????????
?????? transition diagram
102Transition Diagram ?????? Tetris
Play
???????????????
????????
??? ????
Full Row
Title
????????
?????? ???
?????
???? title
Game Over
?????