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

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


  • 100 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


Tetris

AlexeyPajinov 1985


Tetris

  • ( Wikipedia):

    • 90


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


Screen

  • Title Screen

    • TitleScreen

  • Play Screen

    • PlayScreen

  • Game Over Screen

    • GameOverScreen


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

Play

Title

title

Game Over


Title GAME OVER Screen



    • / (title)

    • Enter (choices)


...

Game Over Screen



MenuScreen

    • Title

    • Choices


MenuScreen

  • string title;

  • string[] choices;

  • intcurrentChoice;


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


MenuScreen

  • public abstract voidChoiceSelected(intindex, GameTimegameTime);

    • Enter

    • index = choice

    • abstract


Title Screen

publicclassTitleScreen:MenuScreen

{

publicTitleScreen(GameLib.Gamegame)

:base(

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

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

{

}

:

:

}


Title Screen

publicoverridevoidChoiceSelected(intindex,GameTimegameTime)

{

switch(index)

{

case0:

Game.SwitchScreen(Play");

break;

case1:

Game.Exit();

break;

default:

break;

}

}


Game Over Screen

publicclassGameOverScreen:MenuScreen

{

publicGameOverScreen(GameLib.Gamegame)

:base(

game,"GameOver",

"Game Over","Vera64",

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

{

}

:

:

}


Game Over Screen

publicoverridevoidChoiceSelected(intindex,GameTimegameTime)

{

switch(index)

{

case0:

Game.SwitchScreen("Play");

break;

case1:

Game.SwitchScreen("Title");

break;

case2:

Game.Exit();

break;

default:

break;

}

}


  • 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


?

KeyboardState.IsKeyDown(Keys.Down)

:


:

:


  • : text editor

  • Notepad a ?

    • a

    • a

    • a

    • a


KeySensor

KeySensor

KeySensor


KeySensor

  • KeySensor Screen

    • keySensor

  • Update Screen keySensor.Update(gameTime)keySensor


KeySensor

  • : (MenuScreen)

    privateGameLib.KeySensorkeySensor;

    publicoverridevoidUpdate(GameTimegameTime){

    // Update key sensor's internal information.

    keySensor.Update(gameTime);

    ::

    }


KeySensor

  • KeySensor Watch

  • constructor Screen


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);

    :

    :

    }


KeySensor

  • Update Screen KeySensor

    • public boolIsKeyPressed(Keys key)

      • ()

    • public boolIsKeyReleased(Keys key)

      • ()

    • public boolIsKeyDown(Keys key)

      • ()

    • public boolIsKeyTyped(Keys key)

      • text editor


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);

}



  • KeySensor


TimeMarker

  • public void Mark(stringeventName,GameTimenow)

    • (now) (eventName)

  • publicTimeSpanGetTimeSinceLastMarked(stringeventName, GameTime now)

    • 0


TimeMarker

publicclassTimeMarker

{

privateDictionary<string,double>markedTimes;

publicTimeMarker()

{

markedTimes=newDictionary<string,double>();

}

:

:

}


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


  • Tetromino

  • Block

  • TetrisBoard


Block

  • Tetromino

    • x = X

    • y = Y

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


TetrominoShape

  • enum

    publicenumTetrominoShape

    {

    I,

    J,

    L,

    O,

    S,

    T,

    Z

    }


.

L


(0,-1)

(0,0)

(0,1)

(1,1)

(0,0).


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)}


[[(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)]]


prototypes static field Tetromino

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

static method InitializePrototypes constructor


Tetromino

    • shape= ( TetrominoShape)

    • rotation= prototypes

    • x = X

    • y = Y


Tetromino

  • public voidRotateClockwise()

  • public void RotateCounterClockwise()

    • rotation 1

  • publicIEnumerable<Block> GetBlocks()

    • xy


TetrisBoard

  • ()

    • blocks= ()

    • width=

    • height=


  • public boolCheckSideCollision(Tetrominotetromino)

    • =

  • public boolCheckBottomCollision(Tetrominotetromino)

    • =


  • public boolCheckBlockCollision(Tetrominotetromino)

    • =

  • public boolCheckCollision(Tetrominotetromino)


  • public void Freeze(Tetrominotetromino)

  • publicList<int> GetFullRows()

  • publicvoidRemoveFullRows()


PLAY screen


Play Screen


Play Screen

    • board = (instance TetrisBoard)

    • playerPiece =

    • nextPiece = ()

    • descendDelay = (1 )

    • fullRowsCreated =

    • score =


Play Screen

  • Play Screen 2

    • Play Mode =

    • Full Row Mode =

    • mode = PlayScreenMode.Play PlayScreenMode.FullRow

    • blinkDelay =

    • blinkCounter =


  • public void ResetGame()

  • private void PrepareNextPiece()

    • nextPiece playerPiece

    • nextPiece


Update

    • Play Mode

    • Full Row Mode

      • blinkDelay blinkCounter


Play Mode

  • update


  • Update

    if(keySensor.IsKeyTyped(Keys.Up))

    RotateIfOkay();

  • RotateIfOkay

    privatevoidRotateIfOkay()

    {

    playerPiece.RotateCounterClockwise();

    if(board.CheckCollision(playerPiece))

    playerPiece.RotateClockwise();

    }


// 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;

}


    • descendDelay

  • TimeMarker

  • TimeMarker

  • descendDelay


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

timeMarker.GetTimeSinceLastMarked(

LastDescentEventName,gameTime)>descendDelay)

{

timeMarker.Mark(LastDescentEventName,gameTime);

playerPiece.Y+=1;

:

:

}


    • ?

      • Freeze

        • Full Row Mode

      • Game Over

        • Game Over Screen


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

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