My Project
Chargement...
Recherche...
Aucune correspondance
pong.c
Aller à la documentation de ce fichier.
1/*
2 * ManuX/Arcade - Pong, console ANSI 80x25.
3 * Concu avec l'aide de ChatGPT (OpenAI), 2026.
4 * SPDX-License-Identifier: 0BSD
5 *
6 * Fonctions externes sous ManuX : printf, lire, attendreMillisecondes.
7 * Commandes ANSI : CUP et SGR.
8 */
9#include <stdio.h>
10#include <manux/config.h>
11#include <manux/types.h>
12#include <unistd.h>
13
14#define PONG_GAUCHE 3
15#define PONG_DROITE 78
16#define PONG_HAUT 3
17#define PONG_BAS 22
18#define PONG_RAQUETTE 5
19#define PONG_PAS_MS 20
20#define PONG_BALLE_TICKS 4
21#define PONG_SCORE_MAX 7
22
23#define PONG_RESET "\033[0m"
24#define PONG_CYAN "\033[36m"
25#define PONG_JAUNE "\033[33m"
26#define PONG_VERT "\033[32m"
27#define PONG_BLANC "\033[97m"
28
29static int pongJoueurY, pongOrdinateurY;
30static int pongBalleX, pongBalleY, pongDX, pongDY;
31static int pongScoreJoueur, pongScoreOrdinateur;
32static int pongQuitter;
33
34static void pongPosition(int y, int x)
35{
36 printf("\033[%d;%dH", y, x);
37}
38
39static void pongPause(void)
40{
42}
43
44static void pongRafraichir(void)
45{
46#ifndef __MANUX__
47 fflush(stdout);
48#endif
49}
50
51static void pongEffacer(void)
52{
53 for (int y = 1; y <= 25; y++) {
54 pongPosition(y, 1);
55 for (int x = 1; x < 80; x++)
56 printf(" ");
57 }
58 pongRafraichir();
59}
60
61static void pongScore(void)
62{
63 pongPosition(1, 22);
64 printf(PONG_BLANC "PONG Vous : %d ManuX : %d" PONG_RESET,
65 pongScoreJoueur, pongScoreOrdinateur);
66}
67
68static void pongDecor(void)
69{
70 pongEffacer();
71 pongScore();
72 for (int x = PONG_GAUCHE; x <= PONG_DROITE; x++) {
73 pongPosition(PONG_HAUT, x);
75 pongPosition(PONG_BAS, x);
77 }
78 for (int y = PONG_HAUT + 1; y < PONG_BAS; y++) {
79 pongPosition(y, PONG_GAUCHE);
81 pongPosition(y, PONG_DROITE);
83 if (y % 2 == 0) {
84 pongPosition(y, (PONG_GAUCHE + PONG_DROITE) / 2);
85 printf(":");
86 }
87 }
88 pongPosition(24, 9);
89 printf("j : monter k : descendre q : quitter Premier a 7");
90 pongRafraichir();
91}
92
93static void pongRaquette(int x, int y, int visible, const char *couleur)
94{
95 for (int i = 0; i < PONG_RAQUETTE; i++) {
96 pongPosition(y + i, x);
97 if (visible)
98 printf("%s#%s", couleur, PONG_RESET);
99 else
100 printf(" ");
101 }
102}
103
104static void pongBalle(int visible)
105{
106 pongPosition(pongBalleY, pongBalleX);
107 if (visible)
109 else
110 printf(" ");
111}
112
113static void pongRecentrer(int direction)
114{
115 pongBalleX = (PONG_GAUCHE + PONG_DROITE) / 2;
116 pongBalleY = (PONG_HAUT + PONG_BAS) / 2;
117 pongDX = direction;
118 pongDY = 1;
119}
120
121static void pongInitialiser(void)
122{
123 pongJoueurY = 11;
124 pongOrdinateurY = 11;
125 pongScoreJoueur = 0;
126 pongScoreOrdinateur = 0;
127 pongQuitter = 0;
128 pongRecentrer(-1);
129}
130
131static void pongDeplacerJoueur(int delta)
132{
133 int y = pongJoueurY + delta;
134 if (y < PONG_HAUT + 1)
135 y = PONG_HAUT + 1;
136 if (y > PONG_BAS - PONG_RAQUETTE)
138 if (y != pongJoueurY) {
139 pongRaquette(PONG_GAUCHE + 2, pongJoueurY, 0, PONG_VERT);
140 pongJoueurY = y;
141 pongRaquette(PONG_GAUCHE + 2, pongJoueurY, 1, PONG_VERT);
142 }
143}
144
145static void pongDeplacerOrdinateur(void)
146{
147 int centre = pongOrdinateurY + PONG_RAQUETTE / 2;
148 int cible = pongBalleY;
149 int y = pongOrdinateurY;
150 if (cible < centre)
151 y--;
152 else if (cible > centre)
153 y++;
154 if (y < PONG_HAUT + 1)
155 y = PONG_HAUT + 1;
156 if (y > PONG_BAS - PONG_RAQUETTE)
158 if (y != pongOrdinateurY) {
159 pongRaquette(PONG_DROITE - 2, pongOrdinateurY, 0, PONG_JAUNE);
160 pongOrdinateurY = y;
161 pongRaquette(PONG_DROITE - 2, pongOrdinateurY, 1, PONG_JAUNE);
162 }
163}
164
165static void pongAvancerBalle(void)
166{
167 int nx = pongBalleX + pongDX;
168 int ny = pongBalleY + pongDY;
169
170 pongBalle(0);
171 /* La ligne centrale est redessinee si la balle l'a effacee. */
172 if (pongBalleX == (PONG_GAUCHE + PONG_DROITE) / 2 &&
173 pongBalleY % 2 == 0) {
174 pongPosition(pongBalleY, pongBalleX);
175 printf(":");
176 }
177
178 if (ny <= PONG_HAUT || ny >= PONG_BAS) {
179 pongDY = -pongDY;
180 ny = pongBalleY + pongDY;
181 }
182
183 if (pongDX < 0 && nx == PONG_GAUCHE + 2 &&
184 ny >= pongJoueurY && ny < pongJoueurY + PONG_RAQUETTE) {
185 pongDX = 1;
186 pongDY = (ny < pongJoueurY + 2) ? -1 : 1;
187 nx = pongBalleX + pongDX;
188 }
189 if (pongDX > 0 && nx == PONG_DROITE - 2 &&
190 ny >= pongOrdinateurY && ny < pongOrdinateurY + PONG_RAQUETTE) {
191 pongDX = -1;
192 pongDY = (ny < pongOrdinateurY + 2) ? -1 : 1;
193 nx = pongBalleX + pongDX;
194 }
195
196 if (nx <= PONG_GAUCHE) {
197 pongScoreOrdinateur++;
198 pongScore();
199 pongRecentrer(1);
200 } else if (nx >= PONG_DROITE) {
201 pongScoreJoueur++;
202 pongScore();
203 pongRecentrer(-1);
204 } else {
205 pongBalleX = nx;
206 pongBalleY = ny;
207 }
208 pongBalle(1);
209}
210
211int pongJouer(void)
212{
213 int ticks = 0;
214 int aiTicks = 0;
215 char touche;
216 pongInitialiser();
217 pongDecor();
218 pongRaquette(PONG_GAUCHE + 2, pongJoueurY, 1, PONG_VERT);
219 pongRaquette(PONG_DROITE - 2, pongOrdinateurY, 1, PONG_JAUNE);
220 pongBalle(1);
221 pongRafraichir();
222
223 while (!pongQuitter &&
224 pongScoreJoueur < PONG_SCORE_MAX &&
225 pongScoreOrdinateur < PONG_SCORE_MAX) {
226 /* On traite au plus 16 touches par iteration pour eviter la famine. */
227 for (int i = 0; i < 16; i++) {
228 long n = lire(0, &touche, 1);
229 if (n != 1)
230 break;
231 if (touche == 'j')
232 pongDeplacerJoueur(-1);
233 else if (touche == 'k')
234 pongDeplacerJoueur(1);
235 else if (touche == 'q') {
236 pongQuitter = 1;
237 break;
238 }
239 }
240 if (++aiTicks >= 8) {
241 aiTicks = 0;
242 pongDeplacerOrdinateur();
243 }
244 if (++ticks >= PONG_BALLE_TICKS) {
245 ticks = 0;
246 pongAvancerBalle();
247 }
248 pongRafraichir();
249 pongPause();
250 }
251
252 pongPosition(24, 9);
253 if (pongQuitter)
254 printf("Partie interrompue. ");
255 else if (pongScoreJoueur == PONG_SCORE_MAX)
256 printf("Victoire ! ");
257 else
258 printf("ManuX gagne ! ");
259 pongPosition(25, 1);
260 pongRafraichir();
261 return 0;
262}
#define PONG_BALLE_TICKS
Definition pong.c:20
#define PONG_SCORE_MAX
Definition pong.c:21
#define PONG_RAQUETTE
Definition pong.c:18
#define PONG_GAUCHE
Definition pong.c:14
#define PONG_JAUNE
Definition pong.c:25
#define PONG_HAUT
Definition pong.c:16
#define PONG_BLANC
Definition pong.c:27
#define PONG_VERT
Definition pong.c:26
#define PONG_RESET
Definition pong.c:23
#define PONG_BAS
Definition pong.c:17
int pongJouer(void)
Definition pong.c:211
#define PONG_CYAN
Definition pong.c:24
#define PONG_PAS_MS
Definition pong.c:19
#define PONG_DROITE
Definition pong.c:15
#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
: 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