This commit is contained in:
QkoSad
2025-07-16 13:00:37 +03:00
commit 7894b48931
806 changed files with 162532 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
class Main {
function void main (){
var int s;
let s = -1;
do Output.moveCursor(0,20);
do Output.printString("Welcome to Tetris");
do Sys.wait(2000);
do Output.println();
do Output.println();
do Output.println();
while ((s < 0) | (s > 100)){ // gets a number used for seed for the random function
let s = Keyboard.readInt("Enter a number between 0 and 100:");
do Screen.clearScreen();
}
do Sys.wait(2000);
do Tetris.tetris(s);
return;
}
}
+473
View File
@@ -0,0 +1,473 @@
class Piece {
static int lBorder, rBorder, uBorder, dBorder, st;
field int x1, y1, x2, y2, x3, y3, x4, y4, piece, direction;
field bool move;
static Array st;
constructor Piece new(int Ax1, int Ay1, int Ax2, int Ay2, int Ax3, int Ay3, int Ax4, int Ay4, int p) {
let x1 = Ax1;
let y1 = Ay1;
let x2 = Ax2;
let y2 = Ay2;
let x3 = Ax3;
let y3 = Ay3;
let x4 = Ax4;
let y4 = Ay4;
let piece = p;
let direction = 0;
let move = true;
return this;
}
function void init(){
var int a;
let lBorder = 0;
let rBorder = 511;
let uBorder = 0;
let dBorder = 255;
let st = Array.new(16);
let st[0] = 1;
while (a < 15){
let a = a + 1;
let st[a] = st[a - 1] + st[a - 1];
}
return;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
/** draws 2 rectangles also checks if there is a piece at the spawn which will indicate end of the game. */
method bool draw() {
var bool finish;
let finish = true;
let finish = Piece.sample(x1,y1) & finish;
let finish = Piece.sample(x2,y2) & finish;
let finish = Piece.sample(x3,y3) & finish;
let finish = Piece.sample(x4,y4) & finish;
if (finish){
do Screen.setColor(true);
do Screen.drawRectangle(x1, y1, x2, y2);
do Screen.drawRectangle(x3, y3, x4, y4);
return true;
}
return false;
}
/** Erases the square from the screen. */
method void erase() {
do Screen.setColor(false);
do Screen.drawRectangle(x1, y2, x2, y2);
do Screen.drawRectangle(x3, y3, x4, y4);
return;
}
/** checks if those coodinates hold any value > 0. */
function bool sample(int x, int y){
var int value, address, bit, memBlock;
let memBlock = x / 16;
let address = (32 * y) + memBlock + 16384;
if (address > 24576){
return false;
}
let value = Memory.peek(address);
if (value = 0){
return true;
}
return false;
}
method bool getMove(){
return move;
}
/** Checks if the piece can move in the given direction(dir). */
method bool fullSample(int dir){
var int size1, size2, i, j;
var bool result, flagUpon;
if (dir = 1){ //down
let size1 = x2 - x1;
let size2 = x4 - x3;
let result = true;
while (i < size1){ // for each block of the first rectangle
let j = 0;
let flagUpon = false;
while (j < size2){ // for each block of the second rectangle
if ((x1 + i) = (x3 + j)){ // if this block of the first rectangle sits upon a block of the second rectangle
let flagUpon = true;
}
let result = result & Piece.sample(x3 + j, y4 + 1); // if there is a block under the second rectangle
let j = j + 16; // sets the coords for the next block of the second rectangle
}
if (~(flagUpon)){
let result = result & Piece.sample(x1 + i, y2 + 1); // if there is a block under the first rectangle
}
let i = i + 16; // sets the coords for the next block of the firs rectangle
}
return result;
}
else{
if (dir = 2){ //right
let size1 = y2 - y1;
let size2 = y4 - y3;
let result = true;
while (i < size1){
let j = 0;
let flagUpon = false;
while (j < size2){
if ((y1 + i) = (y3 + j)){
let flagUpon = true;
}
let result = result & Piece.sample(x4 + 1, (y3 + j));
let j = j + 16;
}
if (~(flagUpon)){
let result = result & Piece.sample(x2 + 1, (y1 + i));
}
let i = i +16;
}
return result;
}
else{ // left
let size1 = y2 - y1;
let size2 = y4 - y3;
let result = true;
while (i < size2){
let j = 0;
let flagUpon = false;
while (j < size1){
if ((y3 + i) = (y1 + j)){
let flagUpon = true;
}
let result = result & Piece.sample(x1 - 1, (y1 + j));
let j = j + 16;
}
if (~(flagUpon)){
let result = result & Piece.sample(x3 - 1, (y3 + i));
}
let i = i + 16;
}
return result;
}
}
}
/** Moves the game piece 16 pixels down. */
method void moveDown() {
var int i;
if ((y2 < dBorder) & (y4 < dBorder) & (move) & fullSample(1)) {
while (i < 16){
do Screen.setColor(false);
do Screen.drawRectangle(x1, y1, x2, y1);
do Screen.drawRectangle(x3, y3, x4, y3);
let y1 = y1 + 1;
let y2 = y2 + 1;
let y3 = y3 + 1;
let y4 = y4 + 1;
do Screen.setColor(true);
do Screen.drawRectangle(x1, y2, x2, y2);
do Screen.drawRectangle(x3, y4, x4, y4);
let i = i + 1;
do Sys.wait(10);
}
}
else{
let move = false;
}
return;
}
/** Moves the game piece 16 pixels to the left. */
method void moveLeft() {
var int i;
if ((x1 + 15 > lBorder) & (move) & (x3 + 15 > lBorder)& fullSample(0)){
while (i < 16){
do Screen.setColor(false);
do Screen.drawRectangle(x2, y1, x2, y2);
do Screen.drawRectangle(x4, y3, x4, y4);
let x1 = x1 - 1;
let x2 = x2 - 1;
let x3 = x3 - 1;
let x4 = x4 - 1;
do Screen.setColor(true);
do Screen.drawRectangle(x1, y1, x2, y2);
do Screen.drawRectangle(x3, y3, x4, y4);
let i = i + 1;
do Sys.wait(10);
}
}
return;
}
/** Moves the game piece 16 pixels to the right. */
method void moveRight() {
var int i;
if ((x4 < rBorder) & (move) & (x2 < rBorder) & fullSample(2)){
while (i < 16){
do Screen.setColor(false);
do Screen.drawRectangle(x1, y1, x1, y2);
do Screen.drawRectangle(x3, y3, x3, y4);
let x1 = x1 + 1;
let x2 = x2 + 1;
let x3 = x3 + 1;
let x4 = x4 + 1;
do Screen.setColor(true);
do Screen.drawRectangle(x1, y1, x2, y2);
do Screen.drawRectangle(x3, y3, x4, y4);
let i = i + 1;
do Sys.wait(10);
}
}
return;
}
/** Deletes the piece and draws it at the new coodinates. */
method void reDraw(int Ax1, int Ay1, int Ax2, int Ay2, int Ax3, int Ay3, int Ax4, int Ay4){
var int absTmp;
if((Ax1 < 175) | (Ax3 < 175) | (Ax2 > 336) | (Ax4 > 336)){
return;
}
if((Ay2 > 255) | (Ay4 > 255) | (Ay1 < 0) | (Ay3 < 0)){
return;
}
if((Ax1 > Ax2) | (Ax3 > Ax4) | (Ay1 > Ay2) | (Ay3 > Ay4)){
return;
}
do Screen.setColor(false);
do Screen.drawRectangle(x1, y1, x2, y2);
do Screen.drawRectangle(x3, y3, x4, y4);
do Screen.setColor(true);
let x1 = Ax1;
let y1 = Ay1;
let x2 = Ax2;
let y2 = Ay2;
let x3 = Ax3;
let y3 = Ay3;
let x4 = Ax4;
let y4 = Ay4;
do Screen.drawRectangle(x1, y1, x2, y2);
do Screen.drawRectangle(x3, y3, x4, y4);
return;
}
/** Rotates the piece clockwise. */
method void rotate(){
var bool samp;
let samp = true;
if (direction = 0){ // starting position
if (piece = 0){ // arrow
let samp = Piece.sample(x4, y1) & samp; // checks if the place at which the new piece will be drawn are free
let samp = Piece.sample(x4, y1 - 1) & samp;
if (samp){
do reDraw(x1, y1, x2, y2, x4 - 15, y4 - 47, x4, y4); // redraws it at the new location
let direction = 1;
}
}
if (piece = 1){
let samp = Piece.sample(x1 + 16, y1) & samp;
let samp = Piece.sample(x1 + 16, y1 - 16) & samp;
let samp = Piece.sample(x4, y1 - 16) & samp;
if(samp){
do reDraw(x1 + 16, y1 - 16, x4 - 16, y4, x4 - 15, y1 - 16, x4, y1 - 1);
let direction = 1;
}
}
if (piece = 2){
let samp = Piece.sample(x1 - 16, y1 - 16) & samp;
let samp = Piece.sample(x1 - 16, y1) & samp;
if (samp){
do reDraw(x1 - 16, y1 - 16, x2 - 16, y2 + 16, x3 + 32, y3, x4, y4);
let direction = 1;
}
}
if (piece = 3){
let samp = Piece.sample(x3 + 16, y3 - 32) & samp;
let samp = Piece.sample(x4, y4 - 16) & samp;
if (samp){
do reDraw(x1 + 16, y1, x2, y2 + 16, x3 + 16, y3 - 32, x4, y4 - 16);
let direction = 1;
}
}
if (piece = 4){
let samp = Piece.sample(x3 + 32, y3 - 32) & samp;
if (samp){
do reDraw(x1, y1, x2 - 16, y2 + 16, x3 + 32, y3 - 32, x4 + 16, y4 - 16);
let direction = 1;
}
}
if (piece = 5){
let samp = Piece.sample(x1 + 32, y1 - 32) & samp;
let samp = Piece.sample(x2 + 16, y2 - 16) & samp;
let samp = Piece.sample(x4 - 16, y4 + 16) & samp;
if (samp){
do reDraw(x1 + 32, y1 - 32, x2 + 16, y2 - 16, x3, y3, x4 - 16, y4 + 16);
let direction = 1;
}
}
}
else{
if (direction = 1){
if (piece = 0){
let samp = Piece.sample(x1, y3) & samp;
let samp = Piece.sample(x1 - 1, y3) & samp;
if (samp){
do reDraw(x1 - 16, y3, x4, y3 + 15, x1, y1, x2, y2);
let direction = 2;
}
}
if (piece = 1){
let samp = Piece.sample(x1 - 16, y1 + 16) & samp;
let samp = Piece.sample(x3, y3 + 16) & samp;
let samp = Piece.sample(x3, y3 + 32) & samp;
if (samp){
do reDraw(x1 - 16, y1 + 16, x4, y4 + 16, x3, y3 + 32, x4, y2);
let direction = 2;
}
}
if (piece = 2){
let samp = Piece.sample(x1 - 16, y1 + 16) & samp;
let samp = Piece.sample(x2 + 16, y2 - 16) & samp;
let samp = Piece.sample(x3 - 32, y3) & samp;
if (samp){
do reDraw(x1 - 16, y1 + 16, x2 + 16, y2 - 16, x3 - 32, y3, x4 - 32, y4);
let direction = 2;
}
}
if (piece = 3){
let samp = Piece.sample(x1 - 16, y1) & samp;
let samp = Piece.sample(x4, y4 + 16) & samp;
if (samp){
do reDraw(x1 - 16, y1, x2, y2 - 16, x3 - 16, y3 +32, x4, y4 + 16);
let direction = 2;
}
}
if (piece = 4){
let samp = Piece.sample(x3 - 32, y3 + 32) & samp;
if (samp){
do reDraw(x1, y1, x2 + 16, y2 - 16, x3 - 32, y3 + 32, x4 - 16, y4 + 16);
let direction = 2;
}
}
if (piece = 5){
let samp = Piece.sample(x1 - 16, y1 + 16) & samp;
let samp = Piece.sample(x3 + 16, y3 - 16) & samp;
let samp = Piece.sample(x4 + 32, y4 - 32) & samp;
if (samp){
do reDraw(x1 - 32, y1 + 16, x2 - 16, y2, x3, y3 - 16, x4 + 16, y4 - 32);
let direction = 2;
}
}
}
else{
if (direction = 2){
if (piece = 0){
let samp = Piece.sample(x1, y1+16) & samp;
let samp = Piece.sample(x1, y1+32) & samp;
if (samp){
do reDraw(x1, y1, x1 + 15, y1 + 47, x3, y3, x4, y4);
let direction = 3;
}
}
if (piece = 1){
let samp = Piece.sample(x1 - 16, y1 + 16) & samp;
let samp = Piece.sample(x3, y3 + 16) & samp;
let samp = Piece.sample(x3, y3 + 32) & samp;
if (samp){
do reDraw(x1, y1 + 16, x1 + 15, y1 + 31, x1 + 16, y1 - 16, x4 - 16, y4);
let direction = 3;
}
}
if (piece = 2){
let samp = Piece.sample(x1, y1 - 16) & samp;
let samp = Piece.sample(x2 + 32, y2 + 16) & samp;
let samp = Piece.sample(x4 + 16, y4) & samp;
if (samp){
do reDraw(x1, y1 - 16, x2 - 32, y2 - 16, x3 + 16, y3 - 32, x4 + 16, y4);
let direction = 3;
}
}
if (piece = 3){
let samp = Piece.sample(x2 - 16, y2 + 16) & samp;
let samp = Piece.sample(x3, y3 - 32) & samp;
if (samp){
do reDraw(x1, y1, x2 - 16, y2 + 16, x3, y3 - 32, x4 - 16, y4 - 16);
let direction = 3;
}
}
if (piece = 4){
let samp = Piece.sample(x1 - 16, y1 - 16) & samp;
let samp = Piece.sample(x2 - 32, y2) & samp;
if (samp){
do reDraw(x1 - 16, y1 - 16, x2 - 32, y2, x3 + 16, y3 - 16, x4, y4);
let direction = 3;
}
}
if (piece = 5){
let samp = Piece.sample(x1 + 16, y1 - 16) & samp;
let samp = Piece.sample(x3 - 16, y3 - 16) & samp;
let samp = Piece.sample(x4 - 32, y4 + 32) & samp;
if (samp){
do reDraw(x1 + 16, y1 - 16, x2, y2, x3 - 16, y3 + 16, x4 - 32, y4 + 32);
let direction = 3;
}
}
}
else{
if (piece = 0){
let samp = Piece.sample(x4 + 16, y4) & samp;
let samp = Piece.sample(x4 + 32, y4) & samp;
if (samp){
do reDraw(x3, y3, x4, y4, x1, y1 + 32, x1 + 47, y1 + 47);
let direction = 0;
}
}
if (piece = 1){
let samp = Piece.sample(x1, y1 - 16) & samp;
let samp = Piece.sample(x4 + 16, y4) & samp;
if (samp){
do reDraw(x1, y1 - 16, x2, y2 -16, x1, y1, x4 + 16, y4);
let direction = 0;
}
}
if (piece = 2){
let samp = Piece.sample(x1 + 32, y1 + 16) & samp;
let samp = Piece.sample(x3 - 16, y3 + 32) & samp;
let samp = Piece.sample(x4 + 16, y4) & samp;
if (samp){
do reDraw(x1 + 32, y1 + 16, x2 + 32, y2 + 16, x3 - 16, y3 + 32, x4 + 16, y4);
let direction = 0;
}
}
if (piece = 3){
let samp = Piece.sample(x3, y3 + 32) & samp;
let samp = Piece.sample(x4 + 16, y4 + 16) & samp;
if (samp){
do reDraw(x1, y1, x2 + 16, y2 - 16, x3, y3 + 32, x4 + 16, y4 + 16);
let direction = 0;
}
}
if (piece = 4){
let samp = Piece.sample(x3 - 16, y3 + 16) & samp;
let samp = Piece.sample(x2 + 32, y2) & samp;
if (samp){
do reDraw(x1 + 16, y1 + 16, x2 + 32, y2, x3 - 16, y3 + 16, x4, y4);
let direction = 0;
}
}
if (piece = 5){
let samp = Piece.sample(x1 - 16, y1 + 32) & samp;
let samp = Piece.sample(x3 + 16, y3) & samp;
let samp = Piece.sample(x4 + 32, y4 - 16) & samp;
if (samp){
do reDraw(x1 - 16, y1 + 32, x2, y2 + 16, x3 + 16, y3, x4 + 32, y4 - 16);
let direction = 0;
}
}
}
}
}
return;
}
}
+203
View File
@@ -0,0 +1,203 @@
class Tetris {
static int points, seed, next_seed;
function void tetris(int z){
var Piece p, nextPiece;
var int i, x1, y1, x2, y2, x3, y3, x4, y4, key, j, randTmp, piece_number, moves;
var Array left_g, right_g, box, stick, left_z, right_z,dick, all_piece, tmp;
do Piece.init();
do Tetris.border();
let points = 0;
let all_piece = Array.new(25);
let all_piece[0] = Tetris.newPiece(16,0,31,15, 0,16,47,31, 0); // arrow
let all_piece[1] = Tetris.newPiece(0,0,15,15, 0,16,47,31, 1); // right_L
let all_piece[2] = Tetris.newPiece(32,16,47,31, 0,32,47,47, 2); // left_L
let all_piece[3] = Tetris.newPiece(0,0,31,15, 16,16,47,31, 3); // left_z
let all_piece[4] = Tetris.newPiece(16,0,47,15, 0,16,31,31, 4); // right_z
let all_piece[5] = Tetris.newPiece(0,0,31,15, 32,0,63,15, 5); //stick
let all_piece[6] = Tetris.newPiece(0,0,15,31, 16,0,31,31, 6); //box
let seed = z * 3; // multiply by 3, because it gets in a loop on some even numbers
let next_seed = Tetris.randomNumber();
do Output.moveCursor(0,0);
do Output.printString("Point: ");
do Output.printInt(points);
do Output.println();
do Output.printString("Next Piece: ");
while (true){ // The main game loop
let j = next_seed;
let next_seed = Tetris.randomNumber();
let tmp = all_piece[j]; // initializes the coords of the game piece
let x1 = tmp[0];
let y1 = tmp[1];
let x2 = tmp[2];
let y2 = tmp[3];
let x3 = tmp[4];
let y3 = tmp[5];
let x4 = tmp[6];
let y4 = tmp[7];
let piece_number = tmp[8];
let p = Piece.new(x1 + 240, y1, x2 + 240, y2, x3 + 240, y3, x4 + 240, y4, piece_number);
if (~(p.draw())){ // if a piece tries to spawn above a limit the game ends
if (~(Tetris.finish())){
return;
}
}
let tmp = all_piece[next_seed];
let x1 = tmp[0]; // initializes the coords of the next game piece that is being displayed
let y1 = tmp[1];
let x2 = tmp[2];
let y2 = tmp[3];
let x3 = tmp[4];
let y3 = tmp[5];
let x4 = tmp[6];
let y4 = tmp[7];
let piece_number = tmp[8];
let nextPiece = Piece.new(x1 + 16, y1 + 32, x2+16, y2 + 32, x3+16, y3 + 32, x4 + 16, y4 + 32, piece_number);
do nextPiece.draw();
while (p.getMove()){
let moves = 0;
while (moves < 3){ // one can move its piece 3 times before it moves down automatically
let key = Keyboard.keyPressed();
if (key = 130){
do p.moveLeft();
do Sys.wait(80);
}
if (key = 132){
do p.moveRight();
do Sys.wait(80);
}
if (key = 131){
do p.rotate();
}
if (key = 133){
do p.moveDown();
do p.moveDown();
}
let moves = moves +1;
}
do p.moveDown();
let i = i + 1;
do Sys.wait(80);
}
let i = 0;
do p.dispose();
do Tetris.completeRow();
do Output.moveCursor(0,7);
do Output.printInt(points);
do Screen.setColor(false);
do Screen.drawRectangle(16,32,80,96);
do Screen.setColor(true);
do nextPiece.dispose();
}
return;
}
function void completeRow(){ // checks if a row has been completed
var int adddress, row, colloum;
var bool complete;
let adddress = 16384 + 11;
let row = 15;
while (row > 0){ // for each row
let complete = true;
let colloum = 0;
while ((colloum < 10) & (complete)){ // for each collum
let complete = Memory.peek(adddress + colloum + (row * 512)) & complete;
let colloum = colloum + 1;
}
if (complete){ // removes the row
do Screen.setColor(false);
do Screen.drawRectangle(176, row * 16, 335, ((row * 16)+ 15) );
let points = points + 1;
do Tetris.shift(row);
}
let row = row - 1;
}
return;
}
function void shift(int rowShift){ // shifts all rows above the completed one
var int j, val, adddress, tmp, k;
let adddress = 16384 + 11;
while (rowShift > 1){ // each row
let j = 0;
while (j < 10){ // collum in the game field
let k = 0;
while (k < 16){ // each line a in 16 x 16 box
let tmp = adddress + j + (((rowShift - 1) * 512) + (k * 32));
let val = Memory.peek(tmp);
do Memory.poke(adddress + j + ((rowShift * 512) + (k * 32)), val);
do Memory.poke(tmp,0);
let k = k+ 1;
}
let j = j + 1;
}
let rowShift = rowShift - 1;
}
return;
}
function void border(){ // draws a border
do Screen.drawLine(175,0,175,255);
do Screen.drawLine(336,0,336,255);
return;
}
function Array newPiece(int Ax1, int Ay1, int Ax2, int Ay2, int Ax3, int Ay3, int Ax4, int Ay4,int p){
var Array gamepice;
let gamepice = Array.new(9);
let gamepice[0] = Ax1;
let gamepice[1] = Ay1;
let gamepice[2] = Ax2;
let gamepice[3] = Ay2;
let gamepice[4] = Ax3;
let gamepice[5] = Ay3;
let gamepice[6] = Ax4;
let gamepice[7] = Ay4;
let gamepice[8] = p;
return gamepice;
}
function int randomNumber(){ // radom number generator
var int multiplier,additor,moduler,toBeModulated;
let multiplier = 7;
let additor = 13;
let moduler = 1133;
let toBeModulated = (multiplier * seed) + additor;
let seed = Tetris.mod(toBeModulated,moduler);
return Tetris.mod(seed,7);
}
function bool finish(){ // after finishing the game the player has 2 options to start onether or to end the game
var char c;
do Screen.clearScreen();
do Output.moveCursor(10,24);
do Output.printString("Your score: ");
do Output.printInt(points);
do Sys.wait(10000);
do Screen.clearScreen();
do Output.moveCursor(10,0);
do Output.printString("Would you like to play again, for yes click 'Y' and for no 'N':");
let c = Keyboard.readChar();
if (c = 89){
do Screen.clearScreen();
return false;
}
if (c = 78){
do Screen.clearScreen();
do Sys.halt();
}
return true;
}
function int mod(int tbm, int mo){ //simple moduler devision
var int val, tmp2;
if (tbm < mo){
return tbm;
}
let tmp2 = (tbm / mo) * mo;
let val = tbm - tmp2;
return val;
}
}