Title: DESIGN PATTERNS
1TetrisHelp Session
- How to Play
- User Interaction
- Moving Pieces
- Maintaining the Board
- Creating Pieces
2Tetris Overview
- Tetris pieces move down the board
- One square at a time, at regular intervals.
- Theres 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, drop, and 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
November 2, 2007
Tetris Help Session
2 of 24
3Overview 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!
November 2, 2007
Tetris Help Session
3 of 24
4Creating Random Pieces
- We can use the Factory pattern!
- It has the ability to create new objects at will.
- Remember this from lecture? MMs
- You want to create bunches and bunches of pieces,
over and over again. - Make a piece 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() or java.util.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.
November 2, 2007
Tetris Help Session
4 of 24
5User Interaction
m
Move Down
November 2, 2007
Tetris Help Session
5 of 24
6InputMaps and ActionMaps
- Alone, useless. BUT WHEN THEIR POWERS COMBINE
- Maps live in JComponents (e.g. JPanels)?
- First, call getInputMap() and getActionMap() on
the DrawingPanel to access the Maps. - Okay, actually call getInputMap(
- WHEN_IN_FOCUSED_WINDOW)?
- (see the Tetris handout if you want to know why)?
- For each key you want to use, call put() on your
InputMap reference. - put() requires a javax.swing.KeyStroke and an
Object as parameters. - We HIGHLY RECOMMEND you use a String for the
Object. Youll see why in a moment! - KeyStrokes are obtained via the static method
javax.swing.KeyStroke.getKeyStroke(char a)? - Because the method is static you do not have to
(and should not) instantiate a KeyStroke.
---
The physical manifestation of Nature and yetIm
wearing a belly shirt
November 2, 2007
Tetris Help Session
6 of 24
7- The Action Cat Map
- Call put() on the ActionMap for each key!
- ActionMaps put() requires an Object and a
javax.swing.Action. The Object should be the same
as the Object you sent into the corresponding
InputMap put() call. See why using a String is
useful? Okay, so, whats an Action?
8javax.swing.AbstractAction
- Youre going to subclass this like its your job!
Oh, oh wait. It is. - Each subclass will override that king among
methods, actionPerformed(ActionEvent e)? - Make it do something useful (move piece, pause
game, etc.)?
Im up for some Abstract Action!
---
This is getting surreal
---
MY EAR! OH GOD, MY EAR!
---
November 2, 2007
Tetris Help Session
8 of 24
9Next 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.
November 2, 2007
Tetris Help Session
9 of 24
10Problem
- 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
November 2, 2007
Tetris Help Session
10 of 24
11Proxy 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!
November 2, 2007
Tetris Help Session
11 of 24
12The Other Type of Movement
- The current piece needs to move down every once
in a while. - Sounds like we could use a timer.
- javax.swing.Timer
- Yay, more ActionListener fun!
- Some cool methods getDelay(), setDelay(),
start(), stop()? - And by cool we mean useful. ?
November 2, 2007
Tetris Help Session
12 of 24
13Class Description
Class javax.swing.Timer A Timer does not start
running automatically, you must call start()?
public void addActionListener() Dont forget
this! public void start() Starts the
timer. public void stop() Stops the
timer. Because other animals need love too
November 2, 2007
Tetris Help Session
13 of 24
14Moving 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!
November 2, 2007
Tetris Help Session
14 of 24
15Positions using our DrawingPanels
- Swing, like most computer graphics packages, does
not use a Cartesian Plane, they use Graphical
Planes. - The only difference is the positive-Y axis points
DOWN, toward the bottom of your monitor. - To move a distance of one pixel
- Left col col 1
- Right col col 1
- Down row row 1
- To rotate
- Just kidding. Well get to that later.
November 2, 2007
Tetris Help Session
15 of 24
16Shapes and DrawingPanels
- Their unit of size is in pixels.
- Pixels are real small. Wicked small.
- Squares should be 20 pixels by 20 pixels or more.
- I feel a constant coming on!
- public interface SquareConstants
- int SIZE 20
-
- So to set the location of a square
- x col SIZE
November 2, 2007
Tetris Help Session
16 of 24
17Rotate
- 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 points 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 pieces center)? - What does this formula do?
- It rotates one square around another square.
November 2, 2007
Tetris Help Session
17 of 24
18Graphical Rotation Example
November 2, 2007
Tetris Help Session
18 of 24
19What if it cant move?
- Cant 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! - Cant move into a position already occupied by
old pieces. The green piece cannot rotate into
the blue outlined squares - Someone must keep track of all those squares from
fallen pieces
I've rotated and I can't get up
...because I am too fat
November 2, 2007
Tetris Help Session
19 of 24
20The 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.
November 2, 2007
Tetris Help Session
20 of 24
21Handling 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 should be
reflected in the array! - Remember that array index (x, y) 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.
November 2, 2007
Tetris Help Session
21 of 24
22What is the Board?
- The Tetris Board represents an array of 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.
November 2, 2007
Tetris Help Session
22 of 24
23Detecting and Deleting Lines
- When a piece lands, Board adds the pieces
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
November 2, 2007
Tetris Help Session
23 of 24
24End 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.
November 2, 2007
Tetris Help Session
24 of 24
25Last 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 auhpig
- 10,000 pages long
- GOOD LUCK! (that doesnt make sense)?
November 2, 2007
Tetris Help Session
25 of 25