tetris

EN ESTA ocacion no hay Código explicaré (sin ganas Tengo) Solamente postearé el Código de las Naciones Unidas Juego de t

Views 74 Downloads 3 File size 290KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

EN ESTA ocacion no hay Código explicaré (sin ganas Tengo) Solamente postearé el Código de las Naciones Unidas Juego de tetris Hecho en Java Que encontre en UNA web (no la guarde Fuente). El Juego De CONSTA 3 Clases:

1. Clase Forma 2. Clase Junta 3. Clase Tetris

Clase Forma EN ESTA Clase es en Donde sí Crean las Diferentes Figuras del Juego, por bulerías Cierto Estás Figuras de Se Llaman tetrominoes . la vista de impresión ?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37.

/ ** * * @ Author www.javafrikitutorials.com * / importar java.util.Random; pública

de clase

públicos

enum

Shape { {tetrominoes

NoShape, ZShape, LineShape SShape, TShape, SquareShape, LShape, MirroredLShape }; privadas tetrominoes pieceShape; privadas int coordenadas [] []; privada int [] [] [] coordsTable; pública Shape () { coords = nueva int [ 4 ] [ 2 ]; setShape (Tetrominoes.NoShape); } pública

sin efecto

setShape (forma tetrominoes) {

coordsTable = nueva int [] [] [] { {{ 0 , 0 }, { 0 , 0 }, { 0 , 0 }, { 0 , 0 }}, {{ 0 , - 1 }, { 0 , 0 }, {- 1 , 0 }, {- 1 , 1 }}, {{ 0 , - 1 }, { 0 , 0 }, { 1 , 0 }, { 1 , 1 }}, {{ 0 , - 1 }, { 0 , 0 }, { 0 , 1 }, { 0 , 2 }}, {{- 1 , 0 }, { 0 , 0 }, { 1 , 0 }, { 0 , 1 }}, {{ 0 , 0 }, { 1 , 0 }, { 0 , 1 }, { 1 , 1 }}, {{- 1 , - 1 }, { 0 , - 1 }, { 0 , 0 }, { 0 , 1 }}, {{ 1 , - 1 }, { 0 , - 1 }, { 0 , 0 }, { 0 , 1 }} }; de

( int i = 0 ; i < 4 ; i + +) { de ( int j = 0 ; j < 2 ; + + j) {

38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98.

coordenadas [i] [j] = coordsTable [shape.ordinal ()] [i] [j]; } } pieceShape = forma; } privada sin efecto setX ( int coords [indice] [ 0 ] = x; }

índice,

int

x) {

privada sin efecto setY ( int coords [indice] [ 1 ] = y; }

índice,

int

y) {

pública int x ( int index) { volver coordenadas [indice] [ 0 ]; } pública int y ( int index) { volver coordenadas [indice] [ 1 ]; } pública tetrominoes getShape () { volver pieceShape; } pública sin efecto setRandomShape () { Random r = nueva Random (); int x = Math.abs (r.nextInt ())% 7 + 1 ; Tetrominoes [] valores = Tetrominoes.values (); setShape (valores [x]); } pública int minX () { int m = coordenadas [ 0 ] [ 0 ]; de ( int i = 0 ; i < 4 ; i + +) { m = Math.min (m, coordenadas [i] [ 0 ]); } volver m; } pública int miny () { int m = coordenadas [ 0 ] [ 1 ]; de ( int i = 0 ; i < 4 ; i + +) { m = Math.min (m, coordenadas [i] [ 1 ]); } volver m; } pública rotateLeft Shape () { si (== pieceShape Tetrominoes.SquareShape) { regresar este ; } Resultado = Forma nueva forma (); result.pieceShape = pieceShape; de

( int i = 0 ; i < 4 ; + + i) { result.setX (i, y (i)); result.setY (i,-x (i));

99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117.

} volver

resultado;

} pública rotateRight Shape () { si (== pieceShape Tetrominoes.SquareShape) { regresar este ; } Resultado = Forma nueva forma (); result.pieceShape = pieceShape; de

( int i = 0 ; i < 4 ; + + i) { result.setX (i,-y (i)); result.setY (I, X (i));

} volver

resultado;

} }

Clase Junta This Clase de las Naciones Unidas extiende JPanel y es aqui Donde sí grafica o repinta El Juego. la vista de impresión ?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31.

/ ** * * @ Author www.javafrikitutorials.com * / importar java.awt.Color; importar java.awt.Dimension; importación java.awt.Graphics; importar java.awt.event.ActionEvent; importar java.awt.event.ActionListener; importación java.awt.event.KeyAdapter; importar java.awt.event.KeyEvent; importar javax.swing.JLabel; importar javax.swing.JPanel; importación javax.swing.Timer; importación pública

tetris.Shape.Tetrominoes;

de clase

Junta

se extiende

JPanel

final, int BoardWidth = 10 ; final, int BoardHeight = 22 ; Timer temporizador; boolean isFallingFinished = false ; boolean isStarted = false ; boolean isPaused = false ; int numLinesRemoved = 0 ; int curX = 0 ; int mercurio = 0 ; JLabel barra de estado; Forma curPiece;

implementos

ActionListener {

32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92.

Tetrominoes [] tablero; pública Junta (padre de Tetris) { setFocusable ( verdad ); curPiece = nueva forma (); timer = nueva Timer ( 400 , este ); timer.start (); barra de estado = parent.getStatusBar (); mesa = nuevos tetrominoes [BoardWidth BoardHeight *]; addKeyListener ( nueva TAdapter ()); ClearBoard (); } pública void actionPerformed (ActionEvent e) { si (isFallingFinished) { isFallingFinished = false ; nuevaparte (); } más { oneLineDown (); } } int

squareWidth () { retorno ( int ) getSize () getWidth () / BoardWidth.;

} int

squareHeight () { retorno ( int ). getSize () getHeight () / BoardHeight;

} Tetrominoes shapeAt ( int x, int y) { volver de la junta [(y * BoardWidth) + x]; } pública void start () { si (isPaused) { regresar ; } isStarted = cierto ; isFallingFinished = false ; numLinesRemoved = 0 ; ClearBoard (); nuevaparte (); timer.start (); } privada sin efecto pausa () { si (! isStarted) { regresar ; } isPaused = isPaused!; si (isPaused) { timer.stop (); statusbar.setText ( "pausa" ); } más { timer.start (); statusbar.setText (String.valueOf (numLinesRemoved));

93. } 94. repaint (); 95. } 96. 97. @ Override 98. pública void paint (Graphics g) { 99. Super . de pintura (G); 100. 101. Tamaño de la dimensión = getSize (); 102. int boardTop = ( int ) size.getHeight () - * BoardHeight squareHeight (); 103. 104. 105. de ( int i = 0 ; i = BoardHeight) { return false; } if (shapeAt(x, y) != Tetrominoes.NoShape) { return false; } } curPiece = newPiece; curX = newX; curY = newY; repaint(); return true; } private void removeFullLines() { int numFullLines = 0; for (int i = BoardHeight - 1; i >= 0; --i) { boolean lineIsFull = true; for (int j = 0; j < BoardWidth; ++j) { if (shapeAt(j, i) == Tetrominoes.NoShape) { lineIsFull = false; break; } } if (lineIsFull) { ++numFullLines; for (int k = i; k < BoardHeight - 1; ++k) { for (int j = 0; j < BoardWidth; ++j) { board[(k * BoardWidth) + j] = shapeAt(j, k + 1);

213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. { 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271.

} } } } if (numFullLines > 0) { numLinesRemoved += numFullLines; statusbar.setText(String.valueOf(numLinesRemoved)); isFallingFinished = true; curPiece.setShape(Tetrominoes.NoShape); repaint(); } } private void drawSquare(Graphics g, int x, int y, Tetrominoes shape) { Color colors[] = {new Color(0, 0, new Color(102, 204, 102), new new Color(204, 204, 102), new new Color(102, 204, 204), new };

0), new Color(204, 102, 102), Color(102, 102, 204), Color(204, 102, 204), Color(218, 170, 0)

Color color = colors[shape.ordinal()]; g.setColor(color); g.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2); g.setColor(color.brighter()); g.drawLine(x, y + squareHeight() - 1, x, y); g.drawLine(x, y, x + squareWidth() - 1, y); g.setColor(color.darker()); g.drawLine(x + 1, y + squareHeight() - 1, x + squareWidth() - 1, y + squareHeight() - 1); g.drawLine(x + squareWidth() - 1, y + squareHeight() - 1, x + squareWidth() - 1, y + 1); } class TAdapter extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { if (!isStarted || curPiece.getShape() == Tetrominoes.NoShape) return; } int keycode = e.getKeyCode(); if (keycode == 'p' || keycode == 'P') { pause(); return; } if (isPaused) { return; }

272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298.

switch (keycode) { case KeyEvent.VK_LEFT: tryMove(curPiece, curX - 1, curY); break; case KeyEvent.VK_RIGHT: tryMove(curPiece, curX + 1, curY); break; case KeyEvent.VK_DOWN: tryMove(curPiece.rotateRight(), curX, curY); break; case KeyEvent.VK_UP: tryMove(curPiece.rotateLeft(), curX, curY); break; case KeyEvent.VK_SPACE: dropDown(); break; case 'd': oneLineDown(); break; case 'D': oneLineDown(); break; } } } }

Clase Tetris Esta clase extiende de un JFrame aqui es en donde se ejecuta el juego. view plainprint?

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.

/** * * @author www.javafrikitutorials.com */ import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; public class Tetris extends JFrame { JLabel statusbar; public Tetris() { statusbar = new JLabel(" 0"); add(statusbar, BorderLayout.SOUTH); Board board = new Board(this); add(board); board.start(); setSize(200, 400); setTitle("Tetris"); setDefaultCloseOperation(EXIT_ON_CLOSE); }

25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. }

public JLabel getStatusBar() { return statusbar; } public static void main(String[] args) { Tetris game = new Tetris(); game.setLocationRelativeTo(null); game.setVisible(true); }