My Project
Chargement...
Recherche...
Aucune correspondance
tetris.c
Aller à la documentation de ce fichier.
1/*
2 * Mini Tetris ASCII pour ManuX (console ANSI 80x25).
3 * Concu avec l'aide de ChatGPT (OpenAI), 2026.
4 * SPDX-License-Identifier: 0BSD
5 *
6 * ManuX : printf, lire (non bloquante), attendreMillisecondes.
7 * Linux : lancer avec : stty -icanon -echo min 0 time 0
8 * puis restaurer avec : stty sane
9 */
10#include <manux/config.h>
11#include <manux/types.h>
12#include <stdio.h>
13#include <unistd.h>
14
15#define TETRIS_LARGEUR 10
16#define TETRIS_HAUTEUR 16
17#define TETRIS_X 26
18#define TETRIS_Y 3
19#define TETRIS_PAUSE_MS 100
20#define TETRIS_TICKS_DESCENTE 5
21
22static unsigned char tetrisGrille[TETRIS_HAUTEUR][TETRIS_LARGEUR];
23static int tetrisType, tetrisRotation, tetrisPieceX, tetrisPieceY;
24static int tetrisScore, tetrisLignes, tetrisNumero;
25static unsigned int tetrisHasard = 12345;
26
27/* Chaque piece est definie par quatre cases dans un carre 4x4. */
28static const signed char tetrisFormes[7][4][2] = {
29 {{0,1},{1,1},{2,1},{3,1}}, /* I */
30 {{1,0},{2,0},{1,1},{2,1}}, /* O */
31 {{1,0},{0,1},{1,1},{2,1}}, /* T */
32 {{1,0},{2,0},{0,1},{1,1}}, /* S */
33 {{0,0},{1,0},{1,1},{2,1}}, /* Z */
34 {{0,0},{0,1},{1,1},{2,1}}, /* J */
35 {{2,0},{0,1},{1,1},{2,1}} /* L */
36};
37
38static void tetrisPosition(int ligne, int colonne)
39{
40 printf("\033[%d;%dH", ligne, colonne);
41}
42
43static void tetrisRafraichir(void)
44{
45#ifndef __MANUX__
46 fflush(stdout);
47#endif
48}
49
50static void tetrisAttendre(void)
51{
53
54}
55
56static void tetrisEffacerEcran(void)
57{
58 for (int y = 1; y <= 25; ++y) {
59 tetrisPosition(y, 1);
60 for (int x = 1; x <= 79; ++x) printf(" ");
61 }
62 tetrisRafraichir();
63}
64
65/* Calcule la position d'une case apres rotation dans le carre 4x4. */
66static void tetrisCase(int type, int rotation, int indice, int *x, int *y)
67{
68 int a = tetrisFormes[type][indice][0];
69 int b = tetrisFormes[type][indice][1];
70 for (int r = 0; r < rotation; ++r) {
71 int tmp = a;
72 a = 3 - b;
73 b = tmp;
74 }
75 *x = a;
76 *y = b;
77}
78
79static int tetrisCollision(int type, int rotation, int px, int py)
80{
81 for (int i = 0; i < 4; ++i) {
82 int x, y;
83 tetrisCase(type, rotation, i, &x, &y);
84 x += px;
85 y += py;
86 if (x < 0 || x >= TETRIS_LARGEUR || y >= TETRIS_HAUTEUR)
87 return 1;
88 if (y >= 0 && tetrisGrille[y][x]) return 1;
89 }
90 return 0;
91}
92
93static void tetrisDecor(void)
94{
95 tetrisEffacerEcran();
96 tetrisPosition(1, TETRIS_X - 1);
97 printf("T E T R I S");
98 for (int y = 0; y < TETRIS_HAUTEUR; ++y) {
99 tetrisPosition(TETRIS_Y + y, TETRIS_X - 2);
100 printf("|");
101 tetrisPosition(TETRIS_Y + y, TETRIS_X + 2 * TETRIS_LARGEUR);
102 printf("|");
103 }
104 tetrisPosition(TETRIS_Y + TETRIS_HAUTEUR, TETRIS_X - 2);
105 printf("+");
106 for (int x = 0; x < 2 * TETRIS_LARGEUR + 1; ++x) printf("-");
107 printf("+");
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");
114 tetrisRafraichir();
115}
116
117static void tetrisDessinerPiece(int type, int rotation, int px, int py, char symbole)
118{
119 for (int i = 0; i < 4; ++i) {
120 int x, y;
121 tetrisCase(type, rotation, i, &x, &y);
122 x += px;
123 y += py;
124 if (x >= 0 && x < TETRIS_LARGEUR && y >= 0 && y < TETRIS_HAUTEUR) {
125 tetrisPosition(TETRIS_Y + y, TETRIS_X + 2 * x);
126 printf("%c%c", symbole, symbole);
127 }
128 }
129}
130
131static void tetrisDessinerGrille(void)
132{
133 for (int y = 0; y < TETRIS_HAUTEUR; ++y) {
134 tetrisPosition(TETRIS_Y + y, TETRIS_X);
135 for (int x = 0; x < TETRIS_LARGEUR; ++x)
136 printf(tetrisGrille[y][x] ? "##" : " ");
137 }
138 tetrisDessinerPiece(tetrisType, tetrisRotation,
139 tetrisPieceX, tetrisPieceY, '#');
140 tetrisPosition(12, 52); printf("Score : %-8d", tetrisScore);
141 tetrisPosition(13, 52); printf("Lignes : %-8d", tetrisLignes);
142 tetrisRafraichir();
143}
144
145static void tetrisSupprimerLignes(void)
146{
147 for (int y = TETRIS_HAUTEUR - 1; y >= 0; --y) {
148 int pleine = 1;
149 for (int x = 0; x < TETRIS_LARGEUR; ++x)
150 if (!tetrisGrille[y][x]) pleine = 0;
151 if (pleine) {
152 for (int yy = y; yy > 0; --yy)
153 for (int x = 0; x < TETRIS_LARGEUR; ++x)
154 tetrisGrille[yy][x] = tetrisGrille[yy - 1][x];
155 for (int x = 0; x < TETRIS_LARGEUR; ++x)
156 tetrisGrille[0][x] = 0;
157 ++tetrisLignes;
158 tetrisScore += 100;
159 ++y; /* Reexaminer la meme ligne apres decalage. */
160 }
161 }
162}
163
164static int tetrisNouvellePiece(void)
165{
166 /* Suite deterministe pseudo-aleatoire, sans rand(). */
167 tetrisHasard = tetrisHasard * 1664525U + 1013904223U;
168 tetrisType = (int)((tetrisHasard >> 16) % 7U);
169 tetrisRotation = 0;
170 tetrisPieceX = TETRIS_LARGEUR / 2 - 2;
171 tetrisPieceY = -1;
172 ++tetrisNumero;
173 return !tetrisCollision(tetrisType, tetrisRotation,
174 tetrisPieceX, tetrisPieceY);
175}
176
177/* Retourne 0 si la partie est terminee. */
178static int tetrisDescendre(void)
179{
180 if (!tetrisCollision(tetrisType, tetrisRotation,
181 tetrisPieceX, tetrisPieceY + 1)) {
182 ++tetrisPieceY;
183 return 1;
184 }
185 for (int i = 0; i < 4; ++i) {
186 int x, y;
187 tetrisCase(tetrisType, tetrisRotation, i, &x, &y);
188 x += tetrisPieceX;
189 y += tetrisPieceY;
190 if (y < 0) return 0;
191 tetrisGrille[y][x] = 1;
192 }
193 tetrisSupprimerLignes();
194 return tetrisNouvellePiece();
195}
196
197static void tetrisTourner(void)
198{
199 int rotation = (tetrisRotation + 1) % 4;
200 if (!tetrisCollision(tetrisType, rotation, tetrisPieceX, tetrisPieceY))
201 tetrisRotation = rotation;
202}
203
204static int tetrisCommande(char touche)
205{
206 if (touche == 'q') return 0;
207 if (touche == 'j' &&
208 !tetrisCollision(tetrisType, tetrisRotation,
209 tetrisPieceX - 1, tetrisPieceY)) --tetrisPieceX;
210 if (touche == 'l' &&
211 !tetrisCollision(tetrisType, tetrisRotation,
212 tetrisPieceX + 1, tetrisPieceY)) ++tetrisPieceX;
213 if (touche == 'k') tetrisTourner();
214 if (touche == 's' && !tetrisDescendre()) return 0;
215 if (touche == ' ') {
216 while (!tetrisCollision(tetrisType, tetrisRotation,
217 tetrisPieceX, tetrisPieceY + 1))
218 ++tetrisPieceY;
219 if (!tetrisDescendre()) return 0;
220 }
221 return 1;
222}
223
224void tetrisMain(void)
225{
226 int actif = 1;
227 int ticks = 0;
228 tetrisScore = 0;
229 tetrisLignes = 0;
230 tetrisNumero = 0;
231 tetrisDecor();
232 actif = tetrisNouvellePiece();
233 while (actif) {
234 char touche = 0;
235 long n = lire(0, &touche, 1);
236 if (n == 1) actif = tetrisCommande(touche);
237 if (!actif) break;
238 if (++ticks >= TETRIS_TICKS_DESCENTE) {
239 ticks = 0;
240 actif = tetrisDescendre();
241 }
242 tetrisDessinerGrille();
243 tetrisAttendre();
244 }
245 tetrisDessinerGrille();
246 tetrisPosition(21, 24);
247 printf("Fin de partie ! Score : %d", tetrisScore);
248 tetrisPosition(24, 1);
249 tetrisRafraichir();
250
251}
252
uint16_t b
uint16_t a
Exemple de fonction de mise-à-jour d'un paramètre dans un registre.
#define stdout
Definition stdio.h:19
int lire(int fd, void *buffer, int nb)
void printf(char *format,...)
Un premier printf.
Definition stdio.c:142
#define TETRIS_Y
Definition tetris.c:18
void tetrisMain(void)
Definition tetris.c:224
#define TETRIS_TICKS_DESCENTE
Definition tetris.c:20
#define TETRIS_X
Definition tetris.c:17
#define TETRIS_LARGEUR
Definition tetris.c:15
#define TETRIS_HAUTEUR
Definition tetris.c:16
#define TETRIS_PAUSE_MS
Definition tetris.c:19
: 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