Generation

generate functionTue, 26 Nov 2024

Crea un tretris para android

Aquí te muestro un ejemplo básico de cómo crear un Tetris para Android en Java. Este es un proyecto básico que incluye las siguientes características: * El juego se ejecuta en una pantalla de 480x800 píxeles. * El jugador puede mover la pieza actual hacia la izquierda, hacia la derecha y hacia abajo. * El jugador puede rotar la pieza actual. * El jugador puede borrar líneas completas. * El juego termina cuando la pieza actual no puede ser colocada en la pantalla. **Tetris.java** ```java import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Random; public class Tetris extends Activity { private static final int WIDTH = 480; private static final int HEIGHT = 800; private static final int BLOCK_SIZE = 20; private int[][] grid; private int[][] nextPiece; private int[][] currentPiece; private int pieceType; private int pieceX; private int pieceY; private int score; private int level; private int linesCleared; private Button leftButton; private Button rightButton; private Button downButton; private Button rotateButton; private Button dropButton; private Button clearButton; private TextView scoreTextView; private TextView levelTextView; private TextView linesClearedTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tetris); grid = new int[HEIGHT / BLOCK_SIZE][WIDTH / BLOCK_SIZE]; nextPiece = new int[4][4]; currentPiece = new int[4][4]; pieceType = new Random().nextInt(7); pieceX = WIDTH / 2; pieceY = 0; score = 0; level = 1; linesCleared = 0; leftButton = findViewById(R.id.left_button); rightButton = findViewById(R.id.right_button); downButton = findViewById(R.id.down_button); rotateButton = findViewById(R.id.rotate_button); dropButton = findViewById(R.id.drop_button); clearButton = findViewById(R.id.clear_button); scoreTextView = findViewById(R.id.score_text_view); levelTextView = findViewById(R.id.level_text_view); linesClearedTextView = findViewById(R.id.lines_cleared_text_view); leftButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { pieceX -= BLOCK_SIZE; if (pieceX < 0) { pieceX = 0; } updateGrid(); } return true; } }); rightButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { pieceX += BLOCK_SIZE; if (pieceX > WIDTH - BLOCK_SIZE * 4) { pieceX = WIDTH - BLOCK_SIZE * 4; } updateGrid(); } return true; } }); downButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { pieceY += BLOCK_SIZE; updateGrid(); } return true; } }); rotateButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { rotatePiece(); updateGrid(); } return true; } }); dropButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { while (canMoveDown()) { pieceY += BLOCK_SIZE; updateGrid(); } } return true; } }); clearButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { clearLines(); updateGrid(); } return true; } }); updateGrid(); updateTextViews(); } private void updateGrid() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (currentPiece[i][j] == 1) { grid[(pieceY + i * BLOCK_SIZE) / BLOCK_SIZE][(pieceX + j * BLOCK_SIZE) / BLOCK_SIZE] = 1; } } } } private void updateTextViews() { scoreTextView.setText("Puntuación: " + score); levelTextView.setText("Nivel: " + level); linesClearedTextView.setText("Líneas eliminadas: " + linesCleared); } private void rotatePiece() { int[][] temp = new int[4][4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { temp[j][3 - i] = currentPiece[i][j]; } } currentPiece = temp; } private boolean canMoveDown() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (currentPiece[i][j] == 1 && (pieceY + i * BLOCK_SIZE + BLOCK_SIZE > HEIGHT || grid[(pieceY + i * BLOCK_SIZE + BLOCK_SIZE) / BLOCK_SIZE][(pieceX + j * BLOCK_SIZE) / BLOCK_SIZE] == 1)) { return false; } } } return true; } private void clearLines() { int lines = 0; for (int i = 0; i < HEIGHT / BLOCK_SIZE; i++) { boolean cleared = true; for (int j = 0; j < WIDTH / BLOCK_SIZE; j++) { if (grid[i][j] == 0) { cleared = false; break; } } if (cleared) { lines++; for (int k = i; k > 0; k--) { System.arraycopy(grid[k], 0, grid[k - 1], 0, WIDTH / BLOCK_SIZE); } for (int j = 0; j < WIDTH / BLOCK_SIZE; j++) { grid[0][j] = 0; } } } score += lines * level; linesCleared += lines; if (lines > 0) { level++; } } private void updatePiece() { pieceType = new Random().nextInt(7); currentPiece = getPiece(pieceType); pieceX = WIDTH / 2; pieceY = 0; } private int[][] getPiece(int type) { switch (type) { case 0: return new int[][]{{1, 1, 1, 1}}; case 1: return new int[][]{{1, 0, 0}, {1, 1, 1}}; case 2: return new int[][]{{0, 1, 0}, {1, 1, 1}}; case 3: return new int[][]{{1, 1}, {1, 1}}; case 4: return new int[][]{{1, 1, 1}, {0, 1, 0}}; case 5: return new int[][]{{0, 1, 0}, {0, 1, 0}, {1, 1, 1}}; case 6: return new int[][]{{1, 1, 0}, {0, 1, 1}, {0, 1, 0}}; default: return new int[][]{{0, 0, 0, 0}}; } } } ``` **activity_tetris.xml** ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/left_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Izquierda" /> <Button android:id="@+id/right_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Derecha" /> <Button android:id="@+id/down_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Abajo" /> <Button android:id="@+id/rotate_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rotar" /> <Button android:id="@+id/drop_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Caer" /> <Button android:id="@+id/clear_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Limpiar" /> <TextView android:id="@+id/score_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Puntuación: 0" /> <TextView android:id="@+id/level_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nivel: 1" /> <TextView android:id="@+id/lines_cleared_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Líneas eliminadas: 0" /> </LinearLayout> ``` Este es un ejemplo básico de cómo crear un Tetris para Android en Java. El juego se ejecuta en una pantalla de 480x800 píxeles y el jugador puede mover la pieza actual hacia la izquierda, hacia la derecha y hacia abajo. El jugador también puede rotar la pieza actual y caerla. El juego termina cuando la pieza actual no puede ser colocada en la pantalla.

Want to kickstart your project?Use the new AI Studio to create your code