1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
// Here is the tetris.cpp file
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
// __________________________________
int txr[4] = {-1, 0, 1, 1};
int tyr[4] = {0, 0, 0 ,1};
// ______________________________________________
int tx = 5;
int ty = 19;
// Structure of previous tetris
// bool structure[20][10];
// _____________________________________________
void initializeScreen() {
initscr(); // Initialization.
cbreak(); // Don't wait for RETURN.
noecho(); // Don't echo key presses on screen.
curs_set(false); // Don't show the cursor.
nodelay(stdscr, true); // Don't wait for key pressed
keypad(stdscr, true); // Special codes for KEY_LEFT, KEY_UP, etc..
}
// ____________________________
void showTetris(bool showOrDelete) {
if (showOrDelete == true) printf("\x1b[7m");
if (ty == 1) printf("\x1b[31m");
printf("\x1b[%d;%dH ", 20 - ty - tyr[0], 5 + tx + txr[0]);
printf("\x1b[%d;%dH ", 20 - ty - tyr[1], 5 + tx + txr[1]);
printf("\x1b[%d;%dH ", 20 - ty - tyr[2], 5 + tx + txr[2]);
printf("\x1b[%d;%dH ", 20 - ty - tyr[3], 5 + tx + txr[3]);
printf("\x1b[0m");
}
// __________________________
void rotateTetris(int key) {
int txr[4] = {txr[0]*0 + (-1)*tyr[0], txr[1]*0 + (-1)*tyr[1], txr[2]*0 + (-1)*tyr[2],
txr[3]*0 + (-1)*tyr[3]};
int tyr[4] = {txr[0]*1 + 0*tyr[0], txr[1]*1 + 0*tyr[1], txr[2]*1 + 0*tyr[2],
txr[3]*1 + 0*tyr[3]};
}
// _____________________________
void moveTetris(int key, bool fall) {
if (fall && ty >1) ty--;
switch (key) {
case KEY_UP: rotateTetris(key);
case KEY_DOWN:
if (!fall && ty > 1) ty--;
break;
case KEY_LEFT: if (tx > 2) tx--;
break;
case KEY_RIGHT: if (tx < 9) tx++;
break;
}
// Here is the Main file
// Main Function.
int main(int argc, char** argv) {
initializeScreen();
showTetris(true);
refresh;
int count = 0;
int key;
bool fall;
while (true) {
key = getch();
fall = (++count % 50 == 0);
showTetris(false);
moveTetris(key, fall);
showTetris(true);
refresh();
usleep (10 * 1000);
if (ty == 1) {
ty = 19;
tx = 5;
}
if (key == 'q') {break; }
}
}
}
|