Claro! Abaixo está um plugin para o RPG Maker MV que adiciona minigames de Tetris e Snake, ativados através dos comandos de plugin TetrisMinigameOn, TetrisMinigameOff, SnakeMinigameOn e SnakeMinigameOff.
Plugin: Minigames.js
/*:
* @plugindesc Adiciona minigames de Tetris e Snake ao jogo.
* @author
*
* @help
* Comandos de Plugin:
*
* TetrisMinigameOn - Inicia o minigame de Tetris.
* TetrisMinigameOff - Encerra o minigame de Tetris.
* SnakeMinigameOn - Inicia o minigame de Snake.
* SnakeMinigameOff - Encerra o minigame de Snake.
*
* Para usar, insira os comandos acima em um evento usando o comando "Comando de Plugin".
*/
(function() {
// Registro dos comandos de plugin
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === 'TetrisMinigameOn') {
SceneManager.push(Scene_Tetris);
}
if (command === 'TetrisMinigameOff') {
SceneManager.pop();
}
if (command === 'SnakeMinigameOn') {
SceneManager.push(Scene_Snake);
}
if (command === 'SnakeMinigameOff') {
SceneManager.pop();
}
};
// =========================
// Cena do Minigame de Tetris
// =========================
function Scene_Tetris() {
this.initialize.apply(this, arguments);
}
Scene_Tetris.prototype = Object.create(Scene_Base.prototype);
Scene_Tetris.prototype.constructor = Scene_Tetris;
Scene_Tetris.prototype.initialize = function() {
Scene_Base.prototype.initialize.call(this);
this._tetris = new Tetris();
};
Scene_Tetris.prototype.create = function() {
Scene_Base.prototype.create.call(this);
this.createBackground();
this._tetris.setup();
};
Scene_Tetris.prototype.createBackground = function() {
this._backgroundSprite = new Sprite();
this._backgroundSprite.bitmap = SceneManager.backgroundBitmap();
this.addChild(this._backgroundSprite);
};
Scene_Tetris.prototype.update = function() {
Scene_Base.prototype.update.call(this);
this._tetris.update();
};
// Classe Tetris
function Tetris() {
this.initialize.apply(this, arguments);
}
Tetris.prototype.initialize = function() {
this._stage = new Sprite();
this._stage.bitmap = new Bitmap(Graphics.width, Graphics.height);
SceneManager._scene.addChild(this._stage);
this._gameOver = false;
this._grid = this.createGrid(10, 20);
this._currentPiece = null;
this._nextPiece = this.randomPiece();
this._dropCounter = 0;
this._dropInterval = 1000; // Intervalo de queda em milissegundos
this._lastTime = performance.now();
this._score = 0;
this._level = 0;
this._lines = 0;
this._keyState = {};
this.setupInput();
};
Tetris.prototype.setup = function() {
this.spawnPiece();
this.updateScore();
};
Tetris.prototype.createGrid = function(width, height) {
const grid = [];
while (height--) {
grid.push(new Array(width).fill(0));
}
return grid;
};
Tetris.prototype.spawnPiece = function() {
this._currentPiece = this._nextPiece;
this._nextPiece = this.randomPiece();
this._currentPiece.pos = { x: (this._grid[0].length / 2 | 0) - (this._currentPiece.matrix[0].length / 2 | 0), y: 0 };
if (this.collide()) {
this.gameOver();
}
};
Tetris.prototype.randomPiece = function() {
const pieces = 'ILJOTSZ';
const type = pieces[pieces.length * Math.random() | 0];
return this.createPiece(type);
};
Tetris.prototype.createPiece = function(type) {
if (type === 'T') {
return {
matrix: [
[0, 0, 0],
[1, 1, 1],
[0, 1, 0]
]
};
} else if (type === 'O') {
return {
matrix: [
[2, 2],
[2, 2]
]
};
} else if (type === 'L') {
return {
matrix: [
[0, 0, 3],
[3, 3, 3],
[0, 0, 0]
]
};
} else if (type === 'J') {
return {
matrix: [
[4, 0, 0],
[4, 4, 4],
[0, 0, 0]
]
};
} else if (type === 'I') {
return {
matrix: [
[0, 5, 0, 0],
[0, 5, 0, 0],
[0, 5, 0, 0],
[0, 5, 0, 0]
]
};
} else if (type === 'S') {
return {
matrix: [
[0, 6, 6],
[6, 6, 0],
[0, 0, 0]
]
};
} else if (type === 'Z') {
return {
matrix: [
[7, 7, 0],
[0, 7, 7],
[0, 0, 0]
]
};
}
};
Tetris.prototype.update = function() {
const time = performance.now();
const deltaTime = time - this._lastTime;
this._lastTime = time;
this._dropCounter += deltaTime;
if (this._dropCounter > this._dropInterval) {
this.playerDrop();
}
this.draw();
};
Tetris.prototype.playerDrop = function() {
this._currentPiece.pos.y++;
if (this.collide()) {
this._currentPiece.pos.y--;
this.merge();
this.clearLines();
this.spawnPiece();
}
this._dropCounter = 0;
};
Tetris.prototype.playerMove = function(dir) {
this._currentPiece.pos.x += dir;
if (this.collide()) {
this._currentPiece.pos.x -= dir;
}
};
Tetris.prototype.playerRotate = function(dir) {
const pos = this._currentPiece.pos.x;
let offset = 1;
this.rotateMatrix(this._currentPiece.matrix, dir);
while (this.collide()) {
this._currentPiece.pos.x += offset;
offset = -(offset + (offset > 0 ? 1 : -1));
if (offset > this._currentPiece.matrix[0].length) {
this.rotateMatrix(this._currentPiece.matrix, -dir);
this._currentPiece.pos.x = pos;
return;
}
}
};
Tetris.prototype.rotateMatrix = function(matrix, dir) {
for (let y = 0; y < matrix.length; ++y) {
for (let x = 0; x < y; ++x) {
[
matrix[x][y],
matrix[y][x],
] = [
matrix[y][x],
matrix[x][y],
];
}
}
if (dir > 0) {
matrix.forEach(row => row.reverse());
} else {
matrix.reverse();
}
};
Tetris.prototype.collide = function() {
const m = this._currentPiece.matrix;
const o = this._currentPiece.pos;
for (let y = 0; y < m.length; ++y) {
for (let x = 0; x < m[y].length; ++x) {
if (m[y][x] !== 0 &&
(this._grid[y + o.y] &&
this._grid[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
};
Tetris.prototype.merge = function() {
m = this._currentPiece.matrix;
o = this._currentPiece.pos;
for (let y = 0; y < m.length; ++y) {
for (let x = 0; x < m[y].length; ++x) {
if (m[y][x] !== 0) {
this._grid[y + o.y][x + o.x] = m[y][x];
}
}
}
};
Tetris.prototype.clearLines = function() {
let rowCount = 1;
outer: for (let y = this._grid.length - 1; y >= 0; --y) {
for (let x = 0; x < this._grid[y].length; ++x) {
if (this._grid[y][x] === 0) {
continue outer;
}
}
const row = this._grid.splice(y, 1)[0].fill(0);
this._grid.unshift(row);
++y;
this._score += rowCount * 10;
rowCount *= 2;
this._lines++;
if (this._lines % 10 === 0) {
this._level++;
this._dropInterval *= 0.9; // Aumenta a velocidade
}
this.updateScore();
}
};
Tetris.prototype.draw = function() {
this._stage.bitmap.clear();
this.drawMatrix(this._grid, { x: 0, y: 0 });
this.drawMatrix(this._currentPiece.matrix, this._currentPiece.pos);
};
Tetris.prototype.drawMatrix = function(matrix, offset) {
for (let y = 0; y < matrix.length; ++y) {
for (let x = 0; x < matrix[y].length; ++x) {
if (matrix[y][x] !== 0) {
this._stage.bitmap.fillRect(
(x + offset.x) * 20,
(y + offset.y) * 20,
20, 20,
this.getColor(matrix[y][x])
);
}
}
}
};
Tetris.prototype.getColor = function(value) {
const colors = [
null,
'#FF0D72', // 1
'#0DC2FF', // 2
'#0DFF72', // 3
'#F538FF', // 4
'#FF8E0D', // 5
'#FFE138', // 6
'#3877FF', // 7
];
return colors[value];
};
Tetris.prototype.updateScore = function() {
// Atualize a exibição de pontuação, nível, etc.
// Você pode adicionar texto na tela usando o Sprite e Bitmap
};
Tetris.prototype.gameOver = function() {
this._gameOver = true;
// Exiba uma mensagem de "Game Over" e retorne ao jogo principal
SceneManager.pop();
};
Tetris.prototype.setupInput = function() {
const self = this;
document.addEventListener('keydown', function(event) {
if (event.keyCode === 37) { // Esquerda
self.playerMove(-1);
} else if (event.keyCode === 39) { // Direita
self.playerMove(1);
} else if (event.keyCode === 40) { // Baixo
self.playerDrop();
} else if (event.keyCode === 81) { // Q
self.playerRotate(-1);
} else if (event.keyCode === 87) { // W
self.playerRotate(1);
}
});
};
// ==========================
// Cena do Minigame de Snake
// ==========================
function Scene_Snake() {
this.initialize.apply(this, arguments);
}
Scene_Snake.prototype = Object.create(Scene_Base.prototype);
Scene_Snake.prototype.constructor = Scene_Snake;
Scene_Snake.prototype.initialize = function() {
Scene_Base.prototype.initialize.call(this);
this._snake = new Snake();
};
Scene_Snake.prototype.create = function() {
Scene_Base.prototype.create.call(this);
this.createBackground();
this._snake.setup();
};
Scene_Snake.prototype.createBackground = function() {
this._backgroundSprite = new Sprite();
this._backgroundSprite.bitmap = SceneManager.backgroundBitmap();
this.addChild(this._backgroundSprite);
};
Scene_Snake.prototype.update = function() {
Scene_Base.prototype.update.call(this);
this._snake.update();
};
// Classe Snake
function Snake() {
this.initialize.apply(this, arguments);
}
Snake.prototype.initialize = function() {
this._stage = new Sprite();
this._stage.bitmap = new Bitmap(Graphics.width, Graphics.height);
SceneManager._scene.addChild(this._stage);
this._gridSize = 20;
this._tileCountX = Math.floor(Graphics.width / this._gridSize);
this._tileCountY = Math.floor(Graphics.height / this._gridSize);
this._snakePos = [{ x: 10, y: 10 }];
this._velocity = { x: 0, y: 0 };
this._foodPos = this.randomPosition();
this._score = 0;
this._gameOver = false;
this._lastTime = performance.now();
this._interval = 100; // Velocidade da cobra
this.setupInput();
};
Snake.prototype.setup = function() {
// Configurações iniciais
this.updateScore();
};
Snake.prototype.update = function() {
const time = performance.now();
if (time - this._lastTime > this._interval) {
this._lastTime = time;
this.move();
this.draw();
}
};
Snake.prototype.move = function() {
const head = { x: this._snakePos[0].x + this._velocity.x, y: this._snakePos[0].y + this._velocity.y };
// Verifica colisões com paredes
if (head.x < 0 || head.x >= this._tileCountX || head.y < 0 || head.y >= this._tileCountY) {
this.gameOver();
return;
}
// Verifica colisões com si mesma
for (let i = 0; i < this._snakePos.length; i++) {
if (this._snakePos[i].x === head.x && this._snakePos[i].y === head.y) {
this.gameOver();
return;
}
}
this._snakePos.unshift(head);
// Verifica se comeu a comida
if (head.x === this._foodPos.x && head.y === this._foodPos.y) {
this._score++;
this._foodPos = this.randomPosition();
this.updateScore();
} else {
this._snakePos.pop();
}
};
Snake.prototype.draw = function() {
this._stage.bitmap.clear();
// Desenha a comida
this._stage.bitmap.fillRect(
this._foodPos.x * this._gridSize,
this._foodPos.y * this._gridSize,
this._gridSize, this._gridSize,
'#FF0000'
);
// Desenha a cobra
this._snakePos.forEach(segment => {
this._stage.bitmap.fillRect(
segment.x * this._gridSize,
segment.y * this._gridSize,
this._gridSize - 1, this._gridSize - 1,
'#00FF00'
);
});
};
Snake.prototype.randomPosition = function() {
return {
x: Math.floor(Math.random() * this._tileCountX),
y: Math.floor(Math.random() * this._tileCountY)
};
};
Snake.prototype.setupInput = function() {
const self = this;
document.addEventListener('keydown', function(event) {
if (event.keyCode === 37 && self._velocity.x !== 1) { // Esquerda
self._velocity = { x: -1, y: 0 };
} else if (event.keyCode === 38 && self._velocity.y !== 1) { // Cima
self._velocity = { x: 0, y: -1 };
} else if (event.keyCode === 39 && self._velocity.x !== -1) { // Direita
self._velocity = { x: 1, y: 0 };
} else if (event.keyCode === 40 && self._velocity.y !== -1) { // Baixo
self._velocity = { x: 0, y: 1 };
}
});
};
Snake.prototype.updateScore = function() {
// Atualize a exibição de pontuação.
// Você pode adicionar texto na tela usando o Sprite e Bitmap
};
Snake.prototype.gameOver = function() {
this._gameOver = true;
// Exiba uma mensagem de "Game Over" e retorne ao jogo principal
SceneManager.pop();
};
})();
Explicação Detalhada em Português do Brasil:
Este plugin adiciona minigames de Tetris e Snake ao seu projeto RPG Maker MV. Você pode iniciar os minigames usando comandos de plugin em eventos.
Como usar:
-
Adicionar o plugin ao seu jogo:
- Copie o código acima e cole em um arquivo de texto.
- Salve o arquivo com o nome
Minigames.js na pasta js/plugins do seu projeto RPG Maker MV.
- No editor do RPG Maker MV, vá em Gerenciador de Plugins e adicione
Minigames.js à lista.
-
Usando os comandos de plugin:
- Crie um evento no jogo onde você deseja iniciar o minigame.
- Dentro do evento, adicione o comando "Comando de Plugin".
- Digite
TetrisMinigameOn para iniciar o Tetris ou SnakeMinigameOn para iniciar o Snake.
- Para encerrar manualmente o minigame (embora o código já retorne ao jogo quando o jogador perde), você pode usar
TetrisMinigameOff ou SnakeMinigameOff.
Funcionamento do código:
Observações Importantes:
-
Desempenho:
- Como o RPG Maker MV não é originalmente projetado para jogos de ação em tempo real, o desempenho pode variar.
- Os métodos
update dos minigames são chamados no loop principal, mas podem não ser tão responsivos quanto em um jogo dedicado.
-
Personalizações:
- Você pode personalizar as cores, tamanhos e velocidade dos minigames ajustando os valores no código.
- Adicionar sons, melhorar a interface gráfica e implementar pontuação visual são melhorias recomendadas.
-
Testes:
- Certifique-se de testar exaustivamente os minigames em diferentes cenários para garantir que não haja bugs ou problemas de desempenho.
Espero que este plugin seja útil para adicionar diversão extra ao seu jogo!