Here’s a minimal “no‐Texture2D” Tetris in MonoGame. Instead of sprites, we use BasicEffect + DrawUserPrimitives to draw each block as a colored quad of 4 vertices.
— Program.cs —
csharp
using System;
namespace MonoTetris
{
public static class Program
{
[STAThread]
static void Main()
{
using var game = new TetrisGame();
game.Run();
}
}
}
— TetrisGame.cs —
csharp
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace MonoTetris
{
public class TetrisGame : Game
{
GraphicsDeviceManager _graphics;
BasicEffect _effect;
const int COLS = 10;
const int ROWS = 20;
const int BLOCK_SIZE = 24;
const float DROP_INTERVAL = 0.5f; // seconds
// playfield: 0 = empty, >0 = color index
int[,] _field = new int[ROWS, COLS];
// Tetromino definitions (4x4 masks)
readonly int[,,] _tetrominos = new int[7,4,4]
{
// I
{
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}
},
// J
{
{2,0,0,0},
{2,2,2,0},
{0,0,0,0},
{0,0,0,0}
},
// L
{
{0,0,3,0},
{3,3,3,0},
{0,0,0,0},
{0,0,0,0}
},
// O
{
{4,4,0,0},
{4,4,0,0},
{0,0,0,0},
{0,0,0,0}
},
// S
{
{0,5,5,0},
{5,5,0,0},
{0,0,0,0},
{0,0,0,0}
},
// T
{
{0,6,0,0},
{6,6,6,0},
{0,0,0,0},
{0,0,0,0}
},
// Z
{
{7,7,0,0},
{0,7,7,0},
{0,0,0,0},
{0,0,0,0}
}
};
Color[] _colors = {
Color.Transparent,
Color.Cyan, Color.Blue, Color.Orange,
Color.Yellow, Color.Green, Color.Purple, Color.Red
};
// Active piece
int _type, _rotation, _posX, _posY;
float _dropTimer = 0f;
KeyboardState _prevKb;
public TetrisGame()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "";
IsMouseVisible = false;
_graphics.PreferredBackBufferSize = (COLS + 6) * BLOCK_SIZE, ROWS * BLOCK_SIZE;
}
protected override void Initialize()
{
_effect = new BasicEffect(GraphicsDevice)
{
VertexColorEnabled = true,
Projection = Matrix.CreateOrthographicOffCenter(
0, GraphicsDevice.Viewport.Width,
GraphicsDevice.Viewport.Height, 0,
0, 1)
};
SpawnPiece();
base.Initialize();
}
void SpawnPiece()
{
_type = RandomNumberGenerator.GetInt32(0, 7);
_rotation = 0;
_posX = COLS / 2 - 2;
_posY = 0;
if (Collides(_posX, _posY, _rotation))
Array.Clear(_field, 0, _field.Length); // game over → reset
}
bool Collides(int x, int y, int rot)
{
for (int py = 0; py < 4; py++)
for (int px = 0; px < 4; px++)
{
if (_tetrominos[_type, py, px] == 0) continue;
int fx = x + px, fy = y + py;
if (fx < 0 || fx >= COLS || fy >= ROWS) return true;
if (fy >= 0 && _field[fy, fx] != 0) return true;
}
return false;
}
void LockPiece()
{
for (int py = 0; py < 4; py++)
for (int px = 0; px < 4; px++)
{
if (_tetrominos[_type, py, px] == 0) continue;
int fx = _posX + px, fy = _posY + py;
if (fy >= 0 && fy < ROWS && fx >= 0 && fx < COLS)
_field[fy, fx] = _tetrominos[_type, py, px];
}
ClearLines();
SpawnPiece();
}
void ClearLines()
{
for (int y = ROWS - 1; y >= 0; y--)
{
bool full = true;
for (int x = 0; x < COLS; x++)
if (_field[y, x] == 0) { full = false; break; }
if (!full) continue;
// drop above
for (int ty = y; ty > 0; ty--)
for (int x = 0; x < COLS; x++)
_field[ty, x] = _field[ty - 1, x];
for (int x = 0; x < COLS; x++) _field[0, x] = 0;
y++; // recheck same row
}
}
protected override void Update(GameTime gt)
{
var kb = Keyboard.GetState();
if (kb.IsKeyDown(Keys.Escape)) Exit();
float dt = (float)gt.ElapsedGameTime.TotalSeconds;
_dropTimer += dt;
// Horizontal Move
if (IsNewKey(kb, Keys.Left) && !Collides(_posX - 1, _posY, _rotation))
_posX--;
if (IsNewKey(kb, Keys.Right) && !Collides(_posX + 1, _posY, _rotation))
_posX++;
// Rotate
if (IsNewKey(kb, Keys.Up))
{
int nr = (_rotation + 1) % 4;
if (!Collides(_posX, _posY, nr))
_rotation = nr;
}
// Soft drop
if (kb.IsKeyDown(Keys.Down) || _dropTimer >= DROP_INTERVAL)
{
_dropTimer = 0f;
if (!Collides(_posX, _posY + 1, _rotation))
_posY++;
else
LockPiece();
}
_prevKb = kb;
base.Update(gt);
}
bool IsNewKey(KeyboardState kb, Keys key)
=> kb.IsKeyDown(key) && !_prevKb.IsKeyDown(key);
protected override void Draw(GameTime gt)
{
GraphicsDevice.Clear(Color.Black);
_effect.CurrentTechnique.Passes[0].Apply();
// Draw field
for (int y = 0; y < ROWS; y++)
for (int x = 0; x < COLS; x++)
if (_field[y, x] != 0)
DrawBlock(x, y, _colors[_field[y, x]]);
// Draw active piece
for (int py = 0; py < 4; py++)
for (int px = 0; px < 4; px++)
{
int c = _tetrominos[_type, py, px];
if (c == 0) continue;
DrawBlock(_posX + px, _posY + py, _colors[c]);
}
base.Draw(gt);
}
void DrawBlock(int gridX, int gridY, Color color)
{
float x = gridX * BLOCK_SIZE;
float y = gridY * BLOCK_SIZE;
VertexPositionColor[] verts = new VertexPositionColor[6];
Vector3 topLeft = new(x, y, 0);
Vector3 topRight = new(x + BLOCK_SIZE, y, 0);
Vector3 bottomLeft = new(x, y + BLOCK_SIZE, 0);
Vector3 bottomRight = new(x + BLOCK_SIZE, y + BLOCK_SIZE, 0);
// two triangles
verts[0] = new VertexPositionColor(topLeft, color);
verts[1] = new VertexPositionColor(bottomLeft, color);
verts[2] = new VertexPositionColor(topRight, color);
verts[3] = new VertexPositionColor(topRight, color);
verts[4] = new VertexPositionColor(bottomLeft, color);
verts[5] = new VertexPositionColor(bottomRight, color);
GraphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, verts, 0, 2);
}
}
}
Key points:
We never create or use a Texture2D.
BasicEffect with VertexColorEnabled lets us color each vertex.
Each block is drawn as two triangles via DrawUserPrimitives.