tetris game in matlab

To implement Tetris game in Matlab, we need to follow some basic steps:

  1. Create a figure window to display the game board
  2. Define the game board matrix with zeros representing empty spaces and ones representing occupied spaces with a block
  3. Randomly select a Tetrimino shape and place it on the board matrix
  4. Continuously update the board matrix based on the player's actions
  5. Check for completed horizontal lines and clear them from the board matrix
  6. Update the score and display it on the game board
  7. Add game-over condition when the blocks reach the top of the game board

Here is a sample implementation of Tetris game in Matlab with simple graphics:

function TetrisGame()
  % Create a figure window to display the game board
  gameFigure = figure('KeyPressFcn',@keyPressed, 'Name', 'MATLAB Tetris', 'NumberTitle', 'off',...
                      'MenuBar', 'none', 'ToolBar', 'none');
  boardAxes = axes(gameFigure, 'Units', 'normalized', 'Position', [0.06 0.1 0.5 0.8]); 
  
  % Define the game board matrix
  gameBoardWidth = 10;
  gameBoardHeight = 20;
  gameBoard = zeros(gameBoardHeight, gameBoardWidth);
  
  % Set up game variables
  tetriminos = {[0 1 0; 1 1 1], [1 0 0; 1 1 1], [0 0 1; 1 1 1], [1 1; 1 1], [0 1 1; 1 1 0], [1 1 0; 0 1 1], [1 0 1; 1 1 0]};
  currentTetrimino = [];
  currentTetriminoPosition = [0 0];
  score = 0;
  tickTime = 0.5;
  gameRunning = true;
  
  % Draw initial board
  gameBoardImage = imagesc(boardAxes, gameBoard);
  colormap(boardAxes, [1 1 1; 0 0.7 0.7]);
  
  % Start the game loop
  while gameRunning
      if isempty(currentTetrimino)
          % Randomly select next Tetrimino shape
          currentTetrimino = tetriminos{randi([1 7])};
          currentTetriminoPosition = [1, ceil(size(gameBoard, 2)/2)-ceil(size(currentTetrimino, 2)/2)];
          if collides(gameBoard, currentTetrimino, currentTetriminoPosition)
              % Game over
              gameRunning = false;
              fprintf('Game over!\n');
              continue;
          end
          tickTime = 0.5;
      end
      
      % Update game board
      tickTime = tickTime - 0.02;
      if tickTime <= 0
          tickTime = 0.5;
          if collides(gameBoard, currentTetrimino, currentTetriminoPosition + [1 0])
              gameBoard = updateBoard(gameBoard, currentTetrimino, currentTetriminoPosition);
              currentTetrimino = [];
          else
              currentTetriminoPosition(1) = currentTetriminoPosition(1) + 1;
          end
      end
      
      % Update game board image
      set(gameBoardImage, 'CData', gameBoard);
      
      % Update score
      score = score + clearLines(gameBoard);
      title(boardAxes, ['MATLAB Tetris - Score: ' num2str(score)]);
      drawnow;
  end
end

function collided = collides(gameBoard, tetrimino, position)
  % Check whether the Tetrimino collides with anything already on the board.
  collisions = gameBoard(position(1)+(0:size(tetrimino,1)-1), position(2)+(0:size(tetrimino,2)-1))+tetrimino;
  collided = any(collisions(:) > 1);
end

function newBoard = updateBoard(gameBoard, tetrimino, position)
  % Update the game board with the new Tetrimino position.
  newBoard = gameBoard;
  newBoard(position(1)+(0:size(tetrimino,1)-1), position(2)+(0:size(tetrimino,2)-1)) = ...
      newBoard(position(1)+(0:size(tetrimino,1)-1), position(2)+(0:size(tetrimino,2)-1)) + tetrimino;
end

function numLinesCleared = clearLines(gameBoard)
  % Check for completed horizontal lines and clear them from the board matrix
  numLinesCleared = 0;
  for i = size(gameBoard, 1):-1:1
      if all(gameBoard(i,:))
          gameBoard(i,:) = [];
          gameBoard = [zeros(1,size(gameBoard,2)); gameBoard];
          numLinesCleared = numLinesCleared + 1;
      end
  end
end

function keyPressed(~, event)
  % Handle player's key presses
  global currentTetriminoPosition gameBoard
  switch event.Key
      case 'leftarrow'
          if ~collides(gameBoard, currentTetrimino, currentTetriminoPosition + [0 -1])
              currentTetriminoPosition(2) = currentTetriminoPosition(2) - 1;
          end
      case 'rightarrow'
          if ~collides(gameBoard, currentTetrimino, currentTetriminoPosition + [0 1])
              currentTetriminoPosition(2) = currentTetriminoPosition(2) + 1;
          end
      case 'downarrow'
          if ~collides(gameBoard, currentTetrimino, currentTetriminoPosition + [1 0])
              currentTetriminoPosition(1) = currentTetriminoPosition(1) + 1;
          end
      case 'uparrow'
          newTetrimino = rot90(currentTetrimino);
          if size(tetrimino, 1) ~= size(tetrimino, 2)
              % Wall-kick
              newX = currentTetriminoPosition(2) + size(currentTetrimino, 2) - size(newTetrimino, 2);
              if collides(gameBoard, newTetrimino, [currentTetriminoPosition(1) newX])
                  if ~collides(gameBoard, newTetrimino, [currentTetriminoPosition(1) newX+1])
                      newX = newX + 1;
                  elseif ~collides(gameBoard, newTetrimino, [currentTetriminoPosition(1) newX-1])
                      newX = newX - 1;
                  end
              end
              currentTetriminoPosition(2) = newX;
          end
          if ~collides(gameBoard, newTetrimino, currentTetriminoPosition)
              currentTetrimino = newTetrimino;
          end
  end
end
4687 chars
121 lines

To run the game, just call the TetrisGame function in the Matlab command window. This implementation is just a basic example and can be further developed to improve the graphics, add sound effects, or different level difficulties.

gistlibby LogSnag