From ed0d450090effb38deba0938c239aeef14a0dff9 Mon Sep 17 00:00:00 2001 From: ApfelTeeSaft <91074565+ApfelTeeSaft@users.noreply.github.com> Date: Thu, 31 Oct 2024 21:42:38 +0100 Subject: [PATCH] Made in 30 Mins --- HolyTetris.HC | 299 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 33 ++++++ 2 files changed, 332 insertions(+) create mode 100644 HolyTetris.HC create mode 100644 README.md diff --git a/HolyTetris.HC b/HolyTetris.HC new file mode 100644 index 0000000..5c7aed9 --- /dev/null +++ b/HolyTetris.HC @@ -0,0 +1,299 @@ +// Tetris Clone in TempleOS (HolyC) + +// HolyTetris +#include "std/Kernel.h" + +const int WIDTH = 10, HEIGHT = 20; +int board[WIDTH][HEIGHT]; +int score = 0; + +int tetrominoes[7][4][4][4] = { + // I Piece + { + {{1, 1, 1, 1}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 0, 0, 0}, + {1, 0, 0, 0}, + {1, 0, 0, 0}, + {1, 0, 0, 0}}, + + {{1, 1, 1, 1}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 0, 0, 0}, + {1, 0, 0, 0}, + {1, 0, 0, 0}, + {1, 0, 0, 0}} + }, + // J Piece + { + {{1, 1, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{0, 1, 0, 0}, + {0, 1, 0, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 0, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 1, 0, 0}, + {1, 0, 0, 0}, + {1, 0, 0, 0}, + {0, 0, 0, 0}} + }, + // L Piece + { + {{1, 1, 1, 0}, + {1, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 0, 0}}, + + {{0, 0, 1, 0}, + {1, 1, 1, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 0, 0, 0}, + {1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}} + }, + // O Piece + { + {{1, 1, 0, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 1, 0, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 1, 0, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 1, 0, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}} + }, + // S Piece + { + {{0, 1, 1, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 0, 0}}, + + {{0, 1, 1, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 0, 0}} + }, + // T Piece + { + {{1, 1, 1, 0}, + {0, 1, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{0, 1, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 0, 0}}, + + {{0, 1, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 0, 0, 0}, + {1, 1, 0, 0}, + {1, 0, 0, 0}, + {0, 0, 0, 0}} + }, + // Z Piece + { + {{1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{0, 1, 0, 0}, + {1, 1, 0, 0}, + {1, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}, + + {{0, 1, 0, 0}, + {1, 1, 0, 0}, + {1, 0, 0, 0}, + {0, 0, 0, 0}} + } +}; + +int pieceType, pieceRotation, pieceX, pieceY; + +void InitBoard() { + for (int x = 0; x < WIDTH; ++x) + for (int y = 0; y < HEIGHT; ++y) + board[x][y] = 0; +} + +void DrawBoard() { + ClrScr(); + for (int x = 0; x < WIDTH; ++x) { + for (int y = 0; y < HEIGHT; ++y) { + if (board[x][y]) + PlotChar(x + 10, y + 2, '#', WHITE); + else + PlotChar(x + 10, y + 2, '.', GRAY); + } + } + + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + if (tetrominoes[pieceType][pieceRotation][i][j]) + PlotChar(pieceX + i + 10, pieceY + j + 2, '#', LIGHTBLUE); + } + } + PrintAt(0, 0, "Score: %d", score); +} + +// is piece out of bounds? +bool IsValidPosition(int newX, int newY, int newRotation) { + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + if (tetrominoes[pieceType][newRotation][i][j]) { + int x = newX + i; + int y = newY + j; + if (x < 0 || x >= WIDTH || y >= HEIGHT || (y >= 0 && board[x][y])) + return FALSE; + } + } + } + return TRUE; +} + +void PlacePiece() { + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + if (tetrominoes[pieceType][pieceRotation][i][j]) { + board[pieceX + i][pieceY + j] = 1; + } + } + } +} + +// clear when line is full +void ClearLines() { + for (int y = 0; y < HEIGHT; ++y) { + bool fullLine = TRUE; + for (int x = 0; x < WIDTH; ++x) { + if (!board[x][y]) { + fullLine = FALSE; + break; + } + } + if (fullLine) { + // simple gravity impl + for (int newY = y; newY > 0; --newY) + for (int x = 0; x < WIDTH; ++x) + board[x][newY] = board[x][newY - 1]; + score += 10; + } + } +} + +void NewPiece() { + pieceType = Rand() % 7; + pieceRotation = 0; + pieceX = WIDTH / 2 - 2; + pieceY = 0; + if (!IsValidPosition(pieceX, pieceY, pieceRotation)) { + PrintAt(0, 2, "Game Over!"); + Wait(100); + exit(); + } +} + +void Tetris() { + InitBoard(); + NewPiece(); + + int speed = 20; + int dropCounter = 0; + + while (TRUE) { + ++dropCounter; + + switch (KeyGet()) { + case KEY_LEFT: + if (IsValidPosition(pieceX - 1, pieceY, pieceRotation)) + pieceX--; + break; + case KEY_RIGHT: + if (IsValidPosition(pieceX + 1, pieceY, pieceRotation)) + pieceX++; + break; + case KEY_UP: + int newRotation = (pieceRotation + 1) % 4; + if (IsValidPosition(pieceX, pieceY, newRotation)) + pieceRotation = newRotation; + break; + case KEY_SPACE: + speed = 10; + break; + } + + if (KeyPeek() != KEY_SPACE) { + speed = 20; + } + + if (dropCounter >= speed) { + dropCounter = 0; + if (IsValidPosition(pieceX, pieceY + 1, pieceRotation)) { + pieceY++; + } else { + PlacePiece(); + ClearLines(); + NewPiece(); + } + } + + DrawBoard(); + Wait(1); + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..42ddc0e --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# HolyTetris + +This is a simple Tetris clone written in HolyC for TempleOS. Use the arrow keys and spacebar to control the falling pieces, clear lines, and score points! + +## How to Run the Game + +1. **Save the Code**: + - Copy the Tetris code into a new file in TempleOS named `Tetris.HC`. + +2. **Load and Run the Game**: + - Open TempleOS, and at the command prompt, type: + ```c + #include "Tetris.HC" + Tetris(); + ``` + - The game will start, and you can begin playing immediately. + +## Game Controls + +| Key | Action | +|-----------|----------------------------------------| +| `←` | Move the piece left | +| `→` | Move the piece right | +| `↑` | Rotate the piece | +| `SPACE` | Speed up the piece's descent | + +- **Scoring**: You score points by completing rows, which are cleared automatically when filled. +- **Game Over**: The game ends when pieces stack up to the top of the play area. + +## Additional Notes + +- **Normal Drop Speed**: Pieces descend at a regular pace, controlled by the game’s internal timer. +- **Increased Speed with Spacebar**: Holding down the spacebar doubles the drop speed, making it easier to position pieces quickly. \ No newline at end of file