1 / 22

Tetris Help Session

Tetris Help Session. How to Play User Interaction Moving Pieces Maintaining the Board Creating Pieces. Tetris Overview. Tetris pieces move down the board One square at a time, at regular intervals. There’s only one piece moving at any given time. A piece is made up of four squares.

bliss
Download Presentation

Tetris Help Session

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TetrisHelp Session • How to Play • User Interaction • Moving Pieces • Maintaining the Board • Creating Pieces

  2. Tetris Overview • Tetris pieces move down the board • One square at a time, at regular intervals. • There’s only one piece moving at any given time. • A piece is made up of four squares. • The user can make the current piece move left, right, down, rotate, and drop, and can also pause the program using the keyboard. • Two squares cannot occupy the same square in the board. • A piece cannot move if any of its squares would move in to a space that is already occupied. Outta my square foo Tetris Help Session 2 of 22 November 13, 2013

  3. Overview continued • After a piece cannot fall any further: • Its squares become part of the board. • A new piece starts falling from the top. • When a row gets filled, it should disappear, and the rows above it should “fall” down one square. • If the top row of the board has a square in it, then the game is over! Oh Noes! Tetris Help Session 3 of 22 November 13, 2013

  4. Creating Random Pieces • You want to create bunches and bunches of pieces, over and over again. • Make a piece factory! • Use the Factory pattern! • It has the ability to create new objects. • Remember this from lecture? (The M&M Factory!) • It will have a method which returns a new random piece! • I know how to make a piece, but how do I make a random piece? • Get a random number between 0 and 6. • Use Math.random() • Multiply Math.random() by 7 to get a number between 0 and 6 • Create different pieces based on the number returned. • switch on the random number and create different pieces. • See the Making Decisions Lecture for more information on the Factory Pattern and the switch statement. Tetris Help Session 4 of 22 November 13, 2013

  5. User Interaction m Move Down Tetris Help Session 5 of 22 November 13, 2013

  6. Next Piece – One (Bad) Idea! • Every time the new piece starts moving down the board, have to update all references from old piece to the new piece. Tetris Help Session 6 of 22 November 13, 2013

  7. Problem • We may have a few classes which need to talk to a piece that keeps changing. • We need something to act as a place holder for the current piece. Something that all the interactors can reference. Then this “something” can change the current piece at will without the interactors having to worry about it! • This sounds familiar… Tetris Help Session 7 of 22 November 13, 2013

  8. Proxy to the Rescue! • One proxy object for all classes to communicate with! • It can forward all messages to the current piece. • It can change the current piece without the interactors needing to change their reference! Tetris Help Session 8 of 22 November 13, 2013

  9. Moving a Piece • A piece is made up of four squares. • How do I move a piece? • Move all of its squares. • Or more accurately, have the piece tell all of its squares to move themselves… • How do I move a square in a drawing panel? • Change its position • Can a piece always complete its move? • No! • How do I know if a piece can move? • If all four of its squares can move. • How do I know if a square can move? • If the place it wants to move to on the board is empty. • And if the place it wants to move to is actually on the board! Tetris Help Session 9 of 22 November 13, 2013

  10. Converting from Indices to Pixels • The squares’ unit of size is in pixels. • Pixels are real small. Wicked hella mad small. • Squares should be 20 pixels by 20 pixels or more. • I feel a constant coming on! • Make a SquareConstantsclass, with constants for SQUARE_SIZE, NUM_ROWS, etc. • So to set the location of a square: • x = col *SquareConstants.SQUARE_SIZE • y = row *SquareConstants.SQUARE_SIZE Tetris Help Session 10 of 22 November 13, 2013

  11. Rotate • How do I rotate a point 90 degrees around another point? java.awt.Point centerOfRotation; //set to the value of the center point //around which I am rotating //Note: java.awt.Point represents a pair //of integers (x, y)‏ java.awt.Point location; //set to the value of the point’s old //position int xLocation, yLocation; //new coords of the moved point xLocation = centerOfRotation.x – centerOfRotation.y + location.y; yLocation = centerOfRotation.x + centerOfRotation.y – location.x; • Notice we need to know about the center of rotation (the piece’s “center”)‏ • What does this formula do? • It rotates one square around another square. • Let’s rotate a red square around a black one. Tetris Help Session 11 of 22 November 13, 2013

  12. Graphical Rotation Example Tetris Help Session 12 of 22 November 13, 2013

  13. What if it can’t move? • Can’t move or rotate off the end of the board. • How does a square know if the position is off the end? • You could check each position you try against the boundaries of the array. • Or you could take a REALLY CAREFUL look at what the demo does! • Can’t move into a position already occupied by old pieces. The green piece cannot rotate into the blue outlined squares: • But wait! The piece is not a part of the array until it has fallen as far as it can • Who is going to keep track of all of the fallen squares? I've rotated and I can't get up ...because I am too fat Tetris Help Session 13 of 22 November 13, 2013

  14. The Tetris Board • It needs to keep track of where squares have landed. • The current piece needs to communicate with the board to check if its next desired move is legal. • Other responsibilities: • Checking for and removing horizontal lines. • Checking for end of game. • How do we do this? • Read on, grasshopper. Tetris Help Session 14 of 22 November 13, 2013

  15. Handling Fallen Squares • The Tetris board needs to store and organize squares. How? • 2-D Array! • We can use it to find/store a square at (x, y)‏ • Everything done to the array must be reflected on the screen! • Everything changed on the screen (except the currently moving piece) should be reflected in the array! • Remember that array index (col, row) is not the same as pixel (x, y). • Unless your squares are 1 pixel by 1 pixel • We would not suggest this. • We would highly, highly, not suggest this. Tetris Help Session 15 of 22 November 13, 2013

  16. What is the Board? • The Tetris Board displays an arrayof Squares! • But the Tetris Board is also a drawing panel in which to draw the squares! • The “is a” relationship signifies inheritance. • The Tetris Board is a drawing panel. • The Tetris Board contains an array of Squares. • The Tetris Board interacts directly with its component array Tetris Help Session 16 of 22 November 13, 2013

  17. Detecting and Deleting Lines • When a piece lands, Board adds the piece’s squares to its array. • Then, Board checks to see if any horizontal lines were filled • Filled lines should be removed and pieces above it should be moved downward Tetris Help Session 17 of 22 November 13, 2013

  18. End of Game • After Board handles horizontal lines, Board checks for End of Game. • for every location in the top row of the Board, if any location is full, then the game is over. • Or, if the new piece cannot fall because there is no room for it to do so • Either design is acceptable, but please indicate which one you have chosen in your program comments • Board tells game to shut down: • Stop any new Tetris pieces from falling. • Nicely tell user that game has ended. • If the game is not over: • Make a new random piece and continue. Tetris Help Session 18 of 22 November 13, 2013

  19. Tetris Help Session One Possible Design… • Here is one possible containment diagram for Tetris • Remember! There are several acceptable designs – if you want to discuss changes in your design, come see a TA 19 of 22 November 13, 2013

  20. Tetris Help Session One Note on Coding… “I have a bug” “TA Hours take forever” “Better just keep coding anyway” “I have 10 bugs” 20 of 22 November 13, 2013

  21. Tetris Help Session Code Modularly! • Get one part of your code working before you move on to the next step • This will save you time, effort and frustration • The TAs will love you like no one else • As you code: • “What have I already done?” • “What am I doing next?” • When you run into a bug: • “When was the last time it was working the way I wanted it to?” • What have I changed since then?” • For Tetris: • Get your board to show up before making your piece • Get line clearing to work before dealing with a game over • Do not implement extra credit until all requirements are satisfied 21 of 22 November 13, 2013

  22. Last thing… • You need to implement the following features for your Tetris: • Two-player • Networked • Audio • Playable over the Internet so your mother can see • Neon • Edible • 3-D • Look like a chicken • Cluck like a chicken • Walk like a…uh…pig • GOOD LUCK! (that doesn’t make sense)‏ Tetris Help Session 22 of 22 November 13, 2013

More Related