Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collisions with Static Objects

Similar presentations


Presentation on theme: "Collisions with Static Objects"— Presentation transcript:

1 Collisions with Static Objects
Lecture 6 Collisions with Static Objects MazeDemo Tutorial

2 1. Choose the size of the game space.
game space= 1200 x 900 pixels. 2. Choose a block size. blocksize = 30 x 30 pixels 3. Compute size of game region in blocks. game region = 40 x 30 blocks

3 4. Create textures for blocks.
FINISH 4. Create textures for blocks. 5. Design game space. wall floor START

4 6. Define a code for block types
floor block = 1 wall block = 2

5 7. Generate a 2D array of these values that
7. Generate a 2D array of these values that matches the maze you have created

6 8. Decide on format of maze array CSV file
40,30 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 2,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,2,1,1,2,1,1,1,1,1,1,1,1,2,2,1,1,2,2,2,1,1,1,2 2,1,2,1,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1,1,2,1,1,1,1,1,1,1,1,1,2,2,1,2,1,1,1,1,1,2 2,1,1,1,2,1,1,1,2,1,2,1,2,1,1,1,1,2,1,1,2,1,2,2,2,1,2,2,2,1,1,1,1,2,1,2,1,1,1,2 2,1,2,1,1,1,2,1,1,1,2,1,2,1,1,1,1,2,1,1,2,1,1,1,2,1,2,1,2,2,2,1,2,2,1,2,1,1,1,2 2,1,1,1,2,1,1,1,2,1,2,1,2,1,1,1,1,2,1,1,2,2,2,1,2,1,2,1,2,2,2,1,1,2,1,2,2,2,2,2 2,1,2,1,1,1,2,1,1,1,2,1,2,1,1,1,1,2,1,1,2,1,1,1,2,1,2,1,2,1,1,1,2,2,1,1,1,1,1,2 2,1,1,1,2,1,1,1,2,1,2,1,2,1,1,1,1,2,1,1,1,1,2,2,2,1,2,1,2,1,2,2,2,2,2,1,2,1,1,2 2,1,2,1,1,1,2,1,1,1,2,1,2,1,1,1,1,2,2,2,2,2,2,2,2,1,2,1,2,1,2,1,2,1,2,2,2,1,1,2 2,1,1,1,2,1,1,1,2,1,2,1,2,2,2,2,1,2,1,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,1,1,2,1,1,2 2,1,1,1,1,1,1,1,1,1,2,1,1,1,2,1,1,2,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2,1,2,1,2,1,1,2 2,2,2,2,2,2,2,2,2,2,2,1,1,1,2,2,2,2,1,1,1,1,2,1,2,1,2,1,2,2,2,2,2,1,2,1,2,1,1,2, 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,2,1,2,1,1,1,1,1,2,1,2,1,1,1,1,2 2,1,2,2,2,2,2,2,2,1,1,1,1,1,2,2,2,2,1,1,1,1,2,1,2,1,2,2,2,2,2,1,2,1,2,2,2,2,2,2 2,1,2,1,1,1,1,1,2,1,1,1,1,1,2,1,1,2,1,1,1,1,2,1,2,1,1,1,1,1,2,1,2,1,2,1,1,1,1,2 2,1,2,1,1,1,1,1,2,1,1,1,2,2,2,1,1,2,1,1,1,1,2,1,2,2,2,2,2,1,2,1,2,1,2,1,2,1,1,2 8. Decide on format of maze array CSV file header with num cols and num rows

7 The Cell Class Define a Cell class for the maze.
class Cell { public int Block; public Vector2 Pos; public Cell(int block, int xpos, int ypos) Block = block; Pos.X = xpos; Pos.Y = ypos; } Define a Cell class for the maze. type of texture block for the current Cell position (upper left corner of cell in pixels

8 Class Level Variables 2D array of Cells defines the Maze
wall, floor, and player sprites number of rows and columns of the Maze width and height of the game space size of each cell in units of pixels KeyboardState object for monitoring keyboard events - used in update( ) speed of player in units of pixels Cell[,] Maze; Texture2D wall; Texture2D floor; Texture2D player; Vector2 playerPos; int nrow, ncol; int gWidth = 1200; int gHeight = 900; int cellSize = 30; KeyboardState kbrd; int speed = 2;

9 Sizing the Game Space public Game1() {
graphics = new GraphicsDeviceManager(this); graphics.PreferredBackBufferWidth = gWidth; graphics.PreferredBackBufferHeight = gHeight; Content.RootDirectory = "Content"; }

10 Initial Position of Player
protected override void Initialize() { base.Initialize(); load_maze(); playerPos.X = cellSize + 1; playerPos.Y = gHeight - 2*cellSize - 1; }

11 Loading the Maze Block Array
ncol public void load_maze() { string str; StreamReader tr = new StreamReader("maze_data.csv"); str = tr.ReadLine(); string[] stary = str.Split(','); ncol = Convert.ToInt32(stary[0]); nrow = Convert.ToInt32(stary[1]); Maze = new Cell[nrow, ncol]; for (int r = 0; r < nrow; r++) stary = str.Split(','); for (int c = 0; c < ncol; c++) Maze[r, c] = new Cell(Convert.ToInt32(stary[c]),c*cellSize,r*cellSize); } tr.Close(); . . . nrow . . . . . . maze_data.csv

12 Managing the Content Pipeline
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); wall = Content.Load<Texture2D>("awall"); floor = Content.Load<Texture2D>("afloor"); player = Content.Load<Texture2D>("aplayer"); }

13 controlling player motion
The Update( ) Method controlling player motion protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); kbrd = Keyboard.GetState(); if (kbrd.IsKeyDown(Keys.Up) && moveOK(0, playerPos)) playerPos.Y -= speed; if (kbrd.IsKeyDown(Keys.Down) && moveOK(2, playerPos)) playerPos.Y += speed; if (kbrd.IsKeyDown(Keys.Left) && moveOK(3, playerPos)) playerPos.X -= speed; if (kbrd.IsKeyDown(Keys.Right) && moveOK(1, playerPos)) playerPos.X += speed; base.Update(gameTime); } playerPos 1 2 3 direction of player motion

14 moveOK( ) r2,c2 r1,c1 r2,c2 r1,c1 r2,c2 r1,c1 r2,c2 r1,c1
public bool moveOK(int dir, Vector2 pos) { int r1, c1, r2, c2; bool rtnval = true; if (dir == 0) r1 = (int)(playerPos.Y+1-speed) / cellSize; c1 = (int)(playerPos.X+1) / cellSize; r2 = r1; c2 = (int)(playerPos.X + cellSize-2) / cellSize; if (Maze[r1, c1].Block == 2 || Maze[r2, c2].Block == 2) rtnval = false; } if (dir == 2) r1 = (int)(playerPos.Y-1 + cellSize-2 + speed) / cellSize; if (dir == 1) { r1 = (int)(playerPos.Y + 1) / cellSize ; c1 = (int)(playerPos.X + cellSize speed) / cellSize; r2 = (int)(playerPos.Y + cellSize - 2) / cellSize; c2 = c1; if (Maze[r1, c1].Block == 2 || Maze[r2, c2].Block == 2) rtnval = false; } if (dir == 3) r1 = (int)(playerPos.Y + 1) / cellSize; c1 = (int)(playerPos.X - speed) / cellSize; return rtnval;

15 the Draw( ) Method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); for (int r = 0; r < nrow; r++) for (int c = 0; c < ncol; c++) if (Maze[r, c].Block == 1) spriteBatch.Draw(floor, Maze[r, c].Pos, Color.White); else spriteBatch.Draw(wall, Maze[r, c].Pos, Color.White); } spriteBatch.Draw(player, playerPos, Color.White); spriteBatch.End(); base.Draw(gameTime); this section draws 1200 blocks for every frame of animation if the maze layout is constant we could simply load it as a background image this command draws the player


Download ppt "Collisions with Static Objects"

Similar presentations


Ads by Google