My Project
Chargement...
Recherche...
Aucune correspondance
snake.c
Aller à la documentation de ce fichier.
1/*
2 * ManuX/Arcade : Snake ANSI 80x25
3 * Concu avec l'aide de ChatGPT (OpenAI), 2026.
4 * SPDX-License-Identifier: 0BSD
5 */
6#include <manux/config.h>
7#include <manux/types.h>
8#include <stdio.h>
9#include <unistd.h>
10
11#define LARGEUR 50
12#define HAUTEUR 18
13#define CAPACITE (LARGEUR * HAUTEUR)
14#define X0 15
15#define Y0 3
16#define PAS_MS 10
17#define TICKS_MOUVEMENT 7
18#define RESET "\033[0m"
19#define VERT "\033[32m"
20#define JAUNE "\033[33m"
21#define ROUGE "\033[31m"
22#define CYAN "\033[36m"
23
24typedef struct { int x, y; } snakePoint;
25static snakePoint snakeCorps[CAPACITE], snakeFruit;
26static int snakeDebut, snakeLongueur, snakeDX, snakeDY;
27static int snakeProchainDX, snakeProchainDY, snakeScore;
28static unsigned int snakeGraine = 123456789u;
29
30static void snakePosition(int y, int x)
31{
32 printf("\033[%d;%dH", y, x);
33}
34
35static void snakePause(void)
36{
38}
39
40static void snakeRafraichir(void)
41{
42#ifndef __MANUX__
43 fflush(stdout);
44#endif
45}
46
47static void snakeAfficher(int x, int y, char c, const char *couleur)
48{
49 snakePosition(Y0 + y, X0 + x);
50 printf("%s%c" RESET, couleur, c);
51}
52
53static snakePoint snakeSegment(int i)
54{
55 return snakeCorps[(snakeDebut + i) % CAPACITE];
56}
57
58static int snakeOccupe(int x, int y, int ignorerQueue)
59{
60 int n = snakeLongueur - ignorerQueue;
61 for (int i = 0; i < n; ++i) {
62 snakePoint p = snakeSegment(i);
63 if (p.x == x && p.y == y) return 1;
64 }
65 return 0;
66}
67
68static void snakeScoreAfficher(void)
69{
70 snakePosition(1, 24);
71 printf(JAUNE "SNAKE Score : %5d" RESET, snakeScore);
72}
73
74static void snakeDecor(void)
75{
76 for (int y = 1; y <= 25; ++y) {
77 snakePosition(y, 1);
78 for (int x = 1; x < 80; ++x) printf(" ");
79 }
80 for (int x = -1; x <= LARGEUR; ++x) {
81 snakeAfficher(x, -1, '-', CYAN);
82 snakeAfficher(x, HAUTEUR, '-', CYAN);
83 }
84 for (int y = 0; y < HAUTEUR; ++y) {
85 snakeAfficher(-1, y, '|', CYAN);
86 snakeAfficher(LARGEUR, y, '|', CYAN);
87 }
88 snakeScoreAfficher();
89 snakePosition(23, 12);
90 printf("i : haut k : bas j : gauche l : droite q : quitter");
91}
92
93static void snakePlacerFruit(void)
94{
95 snakeGraine = snakeGraine * 1664525u + 1013904223u;
96 int debut = (int)(snakeGraine % CAPACITE);
97 for (int i = 0; i < CAPACITE; ++i) {
98 int p = (debut + i) % CAPACITE;
99 int x = p % LARGEUR, y = p / LARGEUR;
100 if (!snakeOccupe(x, y, 0)) {
101 snakeFruit.x = x;
102 snakeFruit.y = y;
103 snakeAfficher(x, y, '@', ROUGE);
104 return;
105 }
106 }
107}
108
109static void snakeInitialiser(void)
110{
111 snakeDebut = 0;
112 snakeLongueur = 4;
113 snakeDX = snakeProchainDX = 1;
114 snakeDY = snakeProchainDY = 0;
115 snakeScore = 0;
116 snakeGraine += 977u;
117 for (int i = 0; i < snakeLongueur; ++i) {
118 snakeCorps[i].x = LARGEUR / 2 - i;
119 snakeCorps[i].y = HAUTEUR / 2;
120 }
121}
122
123static int snakeLireClavier(void)
124{
125 char c;
126 for (int i = 0; i < 16; ++i) {
127 if (lire(0, &c, 1) != 1) break;
128 int dx = snakeProchainDX, dy = snakeProchainDY;
129 if (c == 'q') return 1;
130 if (c == 'i') { dx = 0; dy = -1; }
131 else if (c == 'k') { dx = 0; dy = 1; }
132 else if (c == 'j') { dx = -1; dy = 0; }
133 else if (c == 'l') { dx = 1; dy = 0; }
134 else continue;
135 if (dx != -snakeDX || dy != -snakeDY) {
136 snakeProchainDX = dx;
137 snakeProchainDY = dy;
138 }
139 }
140 return 0;
141}
142
143/* Retour : 0 = continue, 1 = collision, 2 = terrain rempli. */
144static int snakeAvancer(void)
145{
146 snakeDX = snakeProchainDX;
147 snakeDY = snakeProchainDY;
148 snakePoint tete = snakeSegment(0);
149 int x = tete.x + snakeDX, y = tete.y + snakeDY;
150 int mange = x == snakeFruit.x && y == snakeFruit.y;
151
152 if (x < 0 || x >= LARGEUR || y < 0 || y >= HAUTEUR ||
153 snakeOccupe(x, y, !mange)) return 1;
154
155 snakePoint queue = snakeSegment(snakeLongueur - 1);
156 if (!mange) snakeAfficher(queue.x, queue.y, ' ', RESET);
157
158 snakeDebut = (snakeDebut + CAPACITE - 1) % CAPACITE;
159 snakeCorps[snakeDebut].x = x;
160 snakeCorps[snakeDebut].y = y;
161
162 if (mange) {
163 ++snakeLongueur;
164 snakeScore += 10;
165 snakeScoreAfficher();
166 }
167 snakeAfficher(tete.x, tete.y, 'o', VERT);
168 snakeAfficher(x, y, 'O', JAUNE);
169
170 if (mange) {
171 if (snakeLongueur == CAPACITE) return 2;
172 snakePlacerFruit();
173 }
174 return 0;
175}
176
177void snakeJouer(void)
178{
179 int ticks = 0, resultat = 0, quitter = 0;
180 snakeInitialiser();
181 snakeDecor();
182 for (int i = 0; i < snakeLongueur; ++i) {
183 snakePoint p = snakeSegment(i);
184 snakeAfficher(p.x, p.y, i ? 'o' : 'O', i ? VERT : JAUNE);
185 }
186 snakePlacerFruit();
187 snakeRafraichir();
188
189 while (!resultat && !quitter) {
190 quitter = snakeLireClavier();
191 if (quitter) break;
192 if (++ticks >= TICKS_MOUVEMENT) {
193 ticks = 0;
194 resultat = snakeAvancer();
195 }
196 snakeRafraichir();
197 snakePause();
198 }
199 snakePosition(24, 20);
200 if (quitter) printf("Partie interrompue.");
201 else if (resultat == 2) printf("Victoire : terrain rempli !");
202 else printf("Perdu ! Score : %d", snakeScore);
203 snakePosition(25, 1);
204 snakeRafraichir();
205}
uint16_t c
#define Y0
Definition snake.c:15
#define VERT
Definition snake.c:19
#define JAUNE
Definition snake.c:20
#define ROUGE
Definition snake.c:21
#define LARGEUR
Definition snake.c:11
#define PAS_MS
Definition snake.c:16
#define CAPACITE
Definition snake.c:13
#define TICKS_MOUVEMENT
Definition snake.c:17
#define X0
Definition snake.c:14
#define RESET
Definition snake.c:18
#define CYAN
Definition snake.c:22
#define HAUTEUR
Definition snake.c:12
void snakeJouer(void)
Definition snake.c:177
#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
int y
Definition snake.c:24
int x
Definition snake.c:24
: 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