10#include <manux/config.h>
11#include <manux/types.h>
15#define TETRIS_LARGEUR 10
16#define TETRIS_HAUTEUR 16
19#define TETRIS_PAUSE_MS 100
20#define TETRIS_TICKS_DESCENTE 5
23static int tetrisType, tetrisRotation, tetrisPieceX, tetrisPieceY;
24static int tetrisScore, tetrisLignes, tetrisNumero;
25static unsigned int tetrisHasard = 12345;
28static const signed char tetrisFormes[7][4][2] = {
29 {{0,1},{1,1},{2,1},{3,1}},
30 {{1,0},{2,0},{1,1},{2,1}},
31 {{1,0},{0,1},{1,1},{2,1}},
32 {{1,0},{2,0},{0,1},{1,1}},
33 {{0,0},{1,0},{1,1},{2,1}},
34 {{0,0},{0,1},{1,1},{2,1}},
35 {{2,0},{0,1},{1,1},{2,1}}
38static void tetrisPosition(
int ligne,
int colonne)
40 printf(
"\033[%d;%dH", ligne, colonne);
43static void tetrisRafraichir(
void)
50static void tetrisAttendre(
void)
56static void tetrisEffacerEcran(
void)
58 for (
int y = 1; y <= 25; ++y) {
60 for (
int x = 1; x <= 79; ++x)
printf(
" ");
66static void tetrisCase(
int type,
int rotation,
int indice,
int *x,
int *y)
68 int a = tetrisFormes[type][indice][0];
69 int b = tetrisFormes[type][indice][1];
70 for (
int r = 0; r < rotation; ++r) {
79static int tetrisCollision(
int type,
int rotation,
int px,
int py)
81 for (
int i = 0; i < 4; ++i) {
83 tetrisCase(type, rotation, i, &x, &y);
88 if (y >= 0 && tetrisGrille[y][x])
return 1;
93static void tetrisDecor(
void)
108 tetrisPosition(4, 52);
printf(
"j : gauche");
109 tetrisPosition(5, 52);
printf(
"k : tourner");
110 tetrisPosition(6, 52);
printf(
"l : droite");
111 tetrisPosition(7, 52);
printf(
"s : descendre");
112 tetrisPosition(8, 52);
printf(
"espace : chute");
113 tetrisPosition(9, 52);
printf(
"q : quitter");
117static void tetrisDessinerPiece(
int type,
int rotation,
int px,
int py,
char symbole)
119 for (
int i = 0; i < 4; ++i) {
121 tetrisCase(type, rotation, i, &x, &y);
124 if (x >= 0 && x < TETRIS_LARGEUR && y >= 0 && y <
TETRIS_HAUTEUR) {
126 printf(
"%c%c", symbole, symbole);
131static void tetrisDessinerGrille(
void)
136 printf(tetrisGrille[y][x] ?
"##" :
" ");
138 tetrisDessinerPiece(tetrisType, tetrisRotation,
139 tetrisPieceX, tetrisPieceY,
'#');
140 tetrisPosition(12, 52);
printf(
"Score : %-8d", tetrisScore);
141 tetrisPosition(13, 52);
printf(
"Lignes : %-8d", tetrisLignes);
145static void tetrisSupprimerLignes(
void)
150 if (!tetrisGrille[y][x]) pleine = 0;
152 for (
int yy = y; yy > 0; --yy)
154 tetrisGrille[yy][x] = tetrisGrille[yy - 1][x];
156 tetrisGrille[0][x] = 0;
164static int tetrisNouvellePiece(
void)
167 tetrisHasard = tetrisHasard * 1664525U + 1013904223U;
168 tetrisType = (int)((tetrisHasard >> 16) % 7U);
173 return !tetrisCollision(tetrisType, tetrisRotation,
174 tetrisPieceX, tetrisPieceY);
178static int tetrisDescendre(
void)
180 if (!tetrisCollision(tetrisType, tetrisRotation,
181 tetrisPieceX, tetrisPieceY + 1)) {
185 for (
int i = 0; i < 4; ++i) {
187 tetrisCase(tetrisType, tetrisRotation, i, &x, &y);
191 tetrisGrille[y][x] = 1;
193 tetrisSupprimerLignes();
194 return tetrisNouvellePiece();
197static void tetrisTourner(
void)
199 int rotation = (tetrisRotation + 1) % 4;
200 if (!tetrisCollision(tetrisType, rotation, tetrisPieceX, tetrisPieceY))
201 tetrisRotation = rotation;
204static int tetrisCommande(
char touche)
206 if (touche ==
'q')
return 0;
208 !tetrisCollision(tetrisType, tetrisRotation,
209 tetrisPieceX - 1, tetrisPieceY)) --tetrisPieceX;
211 !tetrisCollision(tetrisType, tetrisRotation,
212 tetrisPieceX + 1, tetrisPieceY)) ++tetrisPieceX;
213 if (touche ==
'k') tetrisTourner();
214 if (touche ==
's' && !tetrisDescendre())
return 0;
216 while (!tetrisCollision(tetrisType, tetrisRotation,
217 tetrisPieceX, tetrisPieceY + 1))
219 if (!tetrisDescendre())
return 0;
232 actif = tetrisNouvellePiece();
235 long n =
lire(0, &touche, 1);
236 if (n == 1) actif = tetrisCommande(touche);
240 actif = tetrisDescendre();
242 tetrisDessinerGrille();
245 tetrisDessinerGrille();
246 tetrisPosition(21, 24);
247 printf(
"Fin de partie ! Score : %d", tetrisScore);
248 tetrisPosition(24, 1);
uint16_t a
Exemple de fonction de mise-à-jour d'un paramètre dans un registre.
int lire(int fd, void *buffer, int nb)
void printf(char *format,...)
Un premier printf.
#define TETRIS_TICKS_DESCENTE
: Définition des fonctions de base du mode utilisateur de ManuX.
int attendreMillisecondes(uint32_t nMs)
un appel système sans intérêt si ce n'est de faire des tests/demo