418383 4
This presentation is the property of its rightful owner.
Sponsored Links
1 / 66

418383: การโปรแกรมเกม การบรรยายครั้งที่ 4 PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on
  • Presentation posted in: General

418383: การโปรแกรมเกม การบรรยายครั้งที่ 4. ประมุข ขันเงิน. Tetris. สร้างโดย Alexey Pajinov โปรแกรมเมอร์ชาวรัสเซีย ในปี 1985. Tetris. กฎ ( จาก Wikipedia): มีเทโทรมิโนตกลงมาสู่พื้นของบอร์ดทีละอัน

Download Presentation

418383: การโปรแกรมเกม การบรรยายครั้งที่ 4

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


418383 4

418383: 4


Tetris

Tetris

AlexeyPajinov 1985


Tetris1

Tetris

  • ( Wikipedia):

    • 90


418383 4

I, J, L, O, S, T, Z.


Screen

Screen

  • Title Screen

    • TitleScreen

  • Play Screen

    • PlayScreen

  • Game Over Screen

    • GameOverScreen


418383 4

publicclassTetris:GameLib.Game

{

publicTetris()

:base()

{

Content.RootDirectory="Content";

Graphics.PreferredBackBufferWidth=800;

Graphics.PreferredBackBufferHeight=600;

AddScreen(newScreens.TitleScreen(this));

AddScreen(newScreens.GameOverScreen(this));

AddScreen(newScreens.PlayScreen(this));

SwitchScreen(Title");

Tetromino.InitializePrototypes();

}

}


Transition diagram

Transition Diagram

Play

Title

title

Game Over


Title game over screen

Title GAME OVER Screen


418383 4


418383 4

    • / (title)

    • Enter (choices)


418383 4

...

Game Over Screen


418383 4


Menuscreen

MenuScreen

    • Title

    • Choices


Menuscreen1

MenuScreen

  • string title;

  • string[] choices;

  • intcurrentChoice;


Menuscreen2

MenuScreen

  • publicMenuScreen(GameLib.Gamegame,string name,string title,stringtitleFontName,string[]choices,stringchoiceFontName)

    • game = screen

    • name = screen

    • title =

    • titleFontName = asset name font title

    • choices =

    • choiceFontName = assert name font choice


Menuscreen3

MenuScreen

  • public abstract voidChoiceSelected(intindex, GameTimegameTime);

    • Enter

    • index = choice

    • abstract


Title screen

Title Screen

publicclassTitleScreen:MenuScreen

{

publicTitleScreen(GameLib.Gamegame)

:base(

game,"title_menu","Tetris","Vera64",

newstring[]{"Play","Quit"},"Vera32")

{

}

:

:

}


Title screen1

Title Screen

publicoverridevoidChoiceSelected(intindex,GameTimegameTime)

{

switch(index)

{

case0:

Game.SwitchScreen(Play");

break;

case1:

Game.Exit();

break;

default:

break;

}

}


Game over screen

Game Over Screen

publicclassGameOverScreen:MenuScreen

{

publicGameOverScreen(GameLib.Gamegame)

:base(

game,"GameOver",

"Game Over","Vera64",

newstring[]{"Play Again","Return to Title","Quit"},"Vera32")

{

}

:

:

}


Game over screen1

Game Over Screen

publicoverridevoidChoiceSelected(intindex,GameTimegameTime)

{

switch(index)

{

case0:

Game.SwitchScreen("Play");

break;

case1:

Game.SwitchScreen("Title");

break;

case2:

Game.Exit();

break;

default:

break;

}

}


418383 4

  • Bitstream Vera

  • http://www.gnome.org/fonts/

  • TetrisContent Sprite Font Bitstream Vera

    • Vera64

      • Bitstream Vera Sans Mono 64p

      • title

    • Vera32

      • Bitstream Vera Sans Mono 32p

      • choice


Keyboard

keyboard


418383 4

?

KeyboardState.IsKeyDown(Keys.Down)

:


418383 4

:

:


418383 4

  • : text editor

  • Notepad a ?

    • a

    • a

    • a

    • a


Keysensor

KeySensor

KeySensor

KeySensor


Keysensor1

KeySensor

  • KeySensor Screen

    • keySensor

  • Update Screen keySensor.Update(gameTime)keySensor


Keysensor2

KeySensor

  • : (MenuScreen)

    privateGameLib.KeySensorkeySensor;

    publicoverridevoidUpdate(GameTimegameTime){

    // Update key sensor's internal information.

    keySensor.Update(gameTime);

    ::

    }


Keysensor3

KeySensor

  • KeySensor Watch

  • constructor Screen


Keysensor4

KeySensor

  • :(MenuScreen)

    publicMenuScreen(...):base(game,name)

    {

    :

    :

    // Create the key sensor.

    this.keySensor=newKeySensor();

    // We will watch three keys:

    this.keySensor.Watch(Keys.Up);

    this.keySensor.Watch(Keys.Down);

    this.keySensor.Watch(Keys.Enter);

    :

    :

    }


Keysensor5

KeySensor

  • Update Screen KeySensor

    • public boolIsKeyPressed(Keys key)

      • ()

    • public boolIsKeyReleased(Keys key)

      • ()

    • public boolIsKeyDown(Keys key)

      • ()

    • public boolIsKeyTyped(Keys key)

      • text editor


Keysensor menuscene

KeySensor MenuScene

publicoverridevoidUpdate(GameTimegameTime)

{

// Update key sensor's internal information.

keySensor.Update(gameTime);

// If the user types the up arrow,

// move the choice upward.

if(keySensor.IsKeyTyped(Keys.Up))

{

currentChoice-=1;

if(currentChoice<0)

currentChoice=choices.Length-1;

}

// If the user types the down arrow,

// move the choice downward.

elseif(keySensor.IsKeyTyped(Keys.Down))

{

currentChoice+=1;

if(currentChoice>=choices.Length)

currentChoice=0;

}

// If the user pressed enter,

// the choice is selected.

elseif(keySensor.IsKeyTyped(Keys.Enter))

ChoiceSelected(currentChoice,gameTime);

}


418383 4


418383 4

  • KeySensor


Timemarker

TimeMarker

  • public void Mark(stringeventName,GameTimenow)

    • (now) (eventName)

  • publicTimeSpanGetTimeSinceLastMarked(stringeventName, GameTime now)

    • 0


Timemarker1

TimeMarker

publicclassTimeMarker

{

privateDictionary<string,double>markedTimes;

publicTimeMarker()

{

markedTimes=newDictionary<string,double>();

}

:

:

}


Timemarker2

TimeMarker

publicvoidMark(stringeventName,GameTimenow)

{

markedTimes[eventName]=now.TotalGameTime.TotalMilliseconds;

}

publicTimeSpanGetTimeSinceLastMarked(stringeventName,GameTimenow)

{

if(markedTimes.ContainsKey(eventName))

returnTimeSpan.FromMilliseconds(

now.TotalGameTime.TotalMilliseconds

markedTimes[eventName]);

else

returnnewTimeSpan(365,0,0,0);

}


Game logic

Game Logic


418383 4

  • Tetromino

  • Block

  • TetrisBoard


Block

Block

  • Tetromino

    • x = X

    • y = Y

    • shape = (I, J, L, O, S, T, Z)


Tetrominoshape

TetrominoShape

  • enum

    publicenumTetrominoShape

    {

    I,

    J,

    L,

    O,

    S,

    T,

    Z

    }


418383 4

.

L


418383 4

(0,-1)

(0,0)

(0,1)

(1,1)

(0,0).


418383 4

L 4

{(0,0),(-1,0),(1,0),(-1,1)}

{(0,0),(0,-1),(0,1),(1,1)}

{(0,0),(-1,0),(1,0),(1,-1)}

{(0,0),(0,-1),(0,1),(-1,-1)}


418383 4

[[(0,0),(-1,0),(1,0),(-1,1)], [(0,0),(0,-1),(0,1),(1,1)], [(0,0),(-1,0),(1,0),(1,-1)] , [(0,0),(0,-1),(0,1),(-1,-1)]]


418383 4

prototypes static field Tetromino

privatestaticDictionary<TetrominoShape,List<List<Block>>>prototypes=null;

static method InitializePrototypes constructor


Tetromino

Tetromino

    • shape= ( TetrominoShape)

    • rotation= prototypes

    • x = X

    • y = Y


Tetromino1

Tetromino

  • public voidRotateClockwise()

  • public void RotateCounterClockwise()

    • rotation 1

  • publicIEnumerable<Block> GetBlocks()

    • xy


Tetrisboard

TetrisBoard

  • ()

    • blocks= ()

    • width=

    • height=


418383 4

  • public boolCheckSideCollision(Tetrominotetromino)

    • =

  • public boolCheckBottomCollision(Tetrominotetromino)

    • =


418383 4

  • public boolCheckBlockCollision(Tetrominotetromino)

    • =

  • public boolCheckCollision(Tetrominotetromino)


418383 4

  • public void Freeze(Tetrominotetromino)

  • publicList<int> GetFullRows()

  • publicvoidRemoveFullRows()


Play screen

PLAY screen


Play screen1

Play Screen


Play screen2

Play Screen

    • board = (instance TetrisBoard)

    • playerPiece =

    • nextPiece = ()

    • descendDelay = (1 )

    • fullRowsCreated =

    • score =


Play screen3

Play Screen

  • Play Screen 2

    • Play Mode =

    • Full Row Mode =

    • mode = PlayScreenMode.Play PlayScreenMode.FullRow

    • blinkDelay =

    • blinkCounter =


418383 4

  • public void ResetGame()

  • private void PrepareNextPiece()

    • nextPiece playerPiece

    • nextPiece


Update

Update

    • Play Mode

    • Full Row Mode

      • blinkDelay blinkCounter


Play mode

Play Mode

  • update


418383 4

  • Update

    if(keySensor.IsKeyTyped(Keys.Up))

    RotateIfOkay();

  • RotateIfOkay

    privatevoidRotateIfOkay()

    {

    playerPiece.RotateCounterClockwise();

    if(board.CheckCollision(playerPiece))

    playerPiece.RotateClockwise();

    }


418383 4

// Move left if the user types the left arrows.

if(keySensor.IsKeyTyped(Keys.Left))

{

playerPiece.X-=1;

if(board.CheckCollision(playerPiece))

playerPiece.X+=1;

}

// Move right if the user types the right arrows.

elseif(keySensor.IsKeyTyped(Keys.Right))

{

playerPiece.X+=1;

if(board.CheckCollision(playerPiece))

playerPiece.X-=1;

}


418383 4

    • descendDelay

  • TimeMarker

  • TimeMarker

  • descendDelay


418383 4

if(keySensor.IsKeyTyped(Keys.Down)||

timeMarker.GetTimeSinceLastMarked(

LastDescentEventName,gameTime)>descendDelay)

{

timeMarker.Mark(LastDescentEventName,gameTime);

playerPiece.Y+=1;

:

:

}


418383 4

    • ?

      • Freeze

        • Full Row Mode

      • Game Over

        • Game Over Screen


418383 4

if(board.CheckCollision(playerPiece))

{

playerPiece.Y-=1;

board.Freeze(playerPiece);

PrepareNextPiece();

score+=PieceScore();

varfullRows=board.GetFullRows();

if(fullRows.Count>0)

{

mode=PlayScreenMode.FullRow;

ResetFullRowMode(gameTime);

}

if(board.CheckCollision(playerPiece))

Game.SwitchScreen("GameOver");

}


Full row mode

Full Row Mode

if(timeMarker.GetTimeSinceLastMarked(

LastBlinkEventName,gameTime)>blinkDelay)

{

timeMarker.Mark(LastBlinkEventName,gameTime);

blinkCounter++;

}

if(blinkCounter>=4)

{

intfullRowCount=board.GetFullRows().Count;

fullRowsCreated+=fullRowCount;

score+=FullRowScore(fullRowCount);

board.RemoveFullRows();

mode=PlayScreenMode.Play;

timeMarker.Mark(LastDescentEventName,gameTime);

}


  • Login