1 #include <QApplication>
2 #include <DrawingWindow.h>
8 /* Note : les coordonnées réelles vont de -100 à +100 en abscisse, et
9 * de -10 à +140 en ordonnée
12 const float PI = 4.0 * atan(1.0);
14 const float rXMin = -100.0;
15 const float rXMax = 100.0;
16 const float rYMin = -10.0;
17 const float rYMax = 140.0;
19 const float hauteurMin = 10;
20 const float hauteurMax = 130;
21 const float largeurMin = 40;
22 const float largeurMax = 150;
24 const float largeurChateau = 8.5;
25 const float hauteurChateau = 7;
27 const float positionChateau1 = -85.0;
28 const float positionChateau2 = 85.0;
31 const float k = 0.005;
32 const float dt = 0.05;
38 /* Retourne un nombre pseudo-aléatoire compris entre 0 et le paramètre
41 float frand(float min, float max)
43 static bool first = true;
48 return min + (max - min)* (rand() / (RAND_MAX + 1.0));
51 float deg2rad(float deg)
53 return deg * PI / 180.0;
56 // conversion coordonnées réelles -> coordonnées fenêtre
57 int rtowX(const DrawingWindow& w, float rx)
59 return (int )roundf(w.width * (rx - rXMin) / (rXMax - rXMin + 1.0));
62 int rtowY(const DrawingWindow& w, float ry)
64 return (int )roundf(w.height * (rYMax - ry) / (rYMax - rYMin + 1.0));
67 // conversion coordonnées réelles -> coordonnées fenêtre
68 float wtorX(const DrawingWindow& w, int wx)
70 return (rXMax - rXMin + 1.0) * wx / w.width + rXMin;
73 float wtorY(const DrawingWindow& w, int wy)
75 return -(rYMax - rYMin + 1.0) * wy / w.height - rYMax;
78 float hauteurMontagne(float largeur, float hauteur, float x)
80 float rx = 2.0 * x / largeur;
81 return hauteur * (1.0 - rx * rx);
84 void dessineTerrain(DrawingWindow& w, float largeur, float hauteur)
86 int y0 = rtowY(w, 0) + 1;
87 int xmin = rtowX(w, -largeur / 2.0) - 1;
88 int xmax = rtowX(w, largeur / 2.0) + 1;
89 w.setColor("forestgreen");
90 for (int x = xmin; x <= xmax; x++) {
91 float rx = wtorX(w, x);
92 float ry = hauteurMontagne(largeur, hauteur, rx);
95 w.drawLine(x, y0, x, y);
98 w.fillRect(0, y0 + 1, w.width - 1, w.height - 1);
101 void dessineChateau(DrawingWindow& w, float position)
104 w.setColor("darkslategray");
105 int y1 = rtowY(w, 0);
106 int h0 = rtowY(w, 3);
107 int h1 = rtowY(w, 4);
108 for (int i = 0; i < 7; i++) {
109 int h = i % 2 ? h0 : h1;
110 int x1 = rtowX(w, position + i - 3.5);
111 int x2 = rtowX(w, position + i - 2.5) - 1;
112 w.fillRect(x1, y1, x2, h);
114 w.setColor("dimgray");
117 for (int i = 0; i < 5; i++) {
118 int h = i % 2 ? h0 : h1;
119 int x1 = rtowX(w, position + i - 8.5);
120 int x2 = rtowX(w, position + i - 7.5) - 1;
121 w.fillRect(x1, y1, x2, h);
122 x1 = rtowX(w, position + i + 3.5);
123 x2 = rtowX(w, position + i + 4.5) - 1;
124 w.fillRect(x1, y1, x2, h);
128 void dessineVent(DrawingWindow &w, float vitesse)
130 int lg = rtowX(w, vitesse) - rtowX(w, 0);
131 int dir = lg > 0 ? 1 : -1;
135 w.drawCircle(w.width / 2, y, 4);
137 int x1 = (w.width - lg) / 2;
138 int x2 = (w.width + lg) / 2;
139 w.drawLine(x1 - dir, y - 1, x2 - dir, y - 1);
140 w.drawLine(x1, y, x2, y);
141 w.drawLine(x1 - dir, y + 1, x2 - dir, y + 1);
142 for (int i = 0; i < 3; i++) {
143 w.drawLine(x2 - i * dir, y, x2 - (6 + i) * dir, y - 4);
144 w.drawLine(x2 - i * dir, y, x2 - (6 + i) * dir, y + 4);
149 void dessineExplosion(DrawingWindow& w, float rx, float ry)
151 const int maxray = rtowX(w, 2.5) - rtowX(w, 0);
152 // 1/2 rouge -> rouge -> jaune
153 const int x = rtowX(w, rx);
154 const int y = rtowY(w, ry);
156 for (i = 0; i <= maxray / 3; i++) {
157 w.setColor(0.5 + 3.0 * i / (2.0 * maxray), 0.0, 0.0);
158 w.drawCircle(x, y, i);
161 for (/* i */; i < maxray; i++) {
162 w.setColor(1.0, 1.5 * i / maxray - 0.5, 0.0);
163 w.drawCircle(x, y, i);
167 for (i = 0; i < maxray; i++) {
168 w.drawCircle(x, y, i);
171 w.fillCircle(x, y, maxray - 1);
174 void dessineFlammes(DrawingWindow& w, float x0, float y0)
176 for (int i = 0; i < 70; i++) {
178 float vx = frand(-2.5, 2.5);
179 float vy = frand(5, 17);
182 float red = frand(0.5, 1);
183 float green = frand(0, red);
185 w.setColor(red, green, blue);
187 w.drawPoint(rtowX(w, x), rtowY(w, y));
196 void initialise(DrawingWindow& w)
198 largeurMont = frand(largeurMin, largeurMax);
199 hauteurMont = frand(hauteurMin, hauteurMax);
200 wnd = frand(-30, 30);
202 dessineTerrain(w, largeurMont, hauteurMont);
203 dessineChateau(w, positionChateau1);
204 dessineChateau(w, positionChateau2);
208 /* Retour : numéro du perdant, 0 sinon
209 x et y contiennent les coordonnées de la collision
211 int tir(DrawingWindow& w,
212 float x0, float y0, float v0, float alpha, float& x, float &y)
214 float vx = v0 * cos(alpha);
215 float vy = v0 * sin(alpha);
220 int wx = rtowX(w, x);
221 int wy = rtowY(w, y);
223 w.fillCircle(wx, wy, 2);
225 float vxr = vx - wnd;
226 float kvr = -k * sqrt(vxr * vxr + vy * vy);
227 float ax = kvr * vxr;
228 float ay = kvr * vy - g;
236 w.fillCircle(wx, wy, 2);
237 // w.setColor("black");
238 // w.drawPoint(wx, wy);
242 } else if (y < hauteurChateau) {
243 if (positionChateau1 - largeurChateau <= x
244 && positionChateau1 + largeurChateau >= x)
246 else if (positionChateau2 - largeurChateau <= x
247 && positionChateau2 + largeurChateau >= x)
251 float h = hauteurMontagne(largeurMont, hauteurMont, x);
255 } while (!collision);
256 return collision == 3 ? 0 : collision;
259 void jeu1(DrawingWindow& w)
268 float v0 = frand(10, 100);
269 float alpha = deg2rad(frand(10, 90));
273 x0 = positionChateau1 + 8;
275 x0 = positionChateau2 - 8;
278 perdant = tir(w, x0, y0, v0, alpha, x, y);
279 dessineExplosion(w, x, y);
282 dessineFlammes(w, x, y);
283 std::cout << "Joueur " << perdant;
284 if (perdant == joueur)
285 std::cout << " s'est suicidé";
287 std::cout << " a perdu";
291 void jeu(DrawingWindow& w)
299 int main(int argc, char *argv[])
301 QApplication application(argc, argv);
302 DrawingWindow window(jeu, 640, 480);
304 return application.exec();