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 rXMin = -100.0;
13 const float rXMax = 100.0;
14 const float rYMin = -10.0;
15 const float rYMax = 140.0;
17 const float hauteurMin = 10;
18 const float hauteurMax = 130;
19 const float largeurMin = 40;
20 const float largeurMax = 150;
22 const float positionChateau1 = -85.0;
23 const float positionChateau2 = 85.0;
25 /* Retourne un nombre pseudo-aléatoire compris entre 0 et le paramètre
28 float frand(float min, float max)
30 static bool first = true;
35 return min + (max - min)* (rand() / (RAND_MAX + 1.0));
38 // conversion coordonnées réelles -> coordonnées fenêtre
39 int rtowX(const DrawingWindow& w, float rx)
41 return (int )roundf(w.width * (rx - rXMin) / (rXMax - rXMin + 1.0));
44 int rtowY(const DrawingWindow& w, float ry)
46 return (int )roundf(w.height * (rYMax - ry) / (rYMax - rYMin + 1.0));
49 // conversion coordonnées réelles -> coordonnées fenêtre
50 float wtorX(const DrawingWindow& w, int wx)
52 return (rXMax - rXMin + 1.0) * wx / w.width + rXMin;
55 float wtorY(const DrawingWindow& w, int wy)
57 return -(rYMax - rYMin + 1.0) * wy / w.height - rYMax;
60 void dessineTerrain(DrawingWindow& w, float largeur, float hauteur)
62 float l = largeur / 2.0;
64 int y0 = rtowY(w, 0) + 1;
65 int xmin = rtowX(w, -l) - 1;
66 int xmax = rtowX(w, l) + 1;
67 w.setColor("forestgreen");
68 for (int x = xmin; x <= xmax; x++) {
69 float rx = wtorX(w, x) / l;
70 float ry = h * (1.0 - rx * rx);
73 w.drawLine(x, y0, x, y);
76 w.fillRect(0, y0 + 1, w.width - 1, w.height - 1);
79 void dessineChateau(DrawingWindow& w, float position)
82 w.setColor("darkslategray");
86 for (int i = 0; i < 7; i++) {
87 int h = i % 2 ? h0 : h1;
88 int x1 = rtowX(w, position + i - 3.5);
89 int x2 = rtowX(w, position + i - 2.5) - 1;
90 w.fillRect(x1, y1, x2, h);
92 w.setColor("dimgray");
95 for (int i = 0; i < 5; i++) {
96 int h = i % 2 ? h0 : h1;
97 int x1 = rtowX(w, position + i - 8.5);
98 int x2 = rtowX(w, position + i - 7.5) - 1;
99 w.fillRect(x1, y1, x2, h);
100 x1 = rtowX(w, position + i + 3.5);
101 x2 = rtowX(w, position + i + 4.5) - 1;
102 w.fillRect(x1, y1, x2, h);
106 void dessineExplosion(DrawingWindow& w, float rx, float ry)
108 const int maxray = rtowX(w, 5) - rtowX(w, 0);
109 // 1/2 rouge -> rouge -> jaune
110 const int x = rtowX(w, rx);
111 const int y = rtowY(w, ry);
113 for (i = 0; i <= maxray / 3; i++) {
114 w.setColor(0.5 + 3.0 * i / (2.0 * maxray), 0.0, 0.0);
115 w.drawCircle(x, y, i);
118 for (/* i */; i < maxray; i++) {
119 w.setColor(1.0, 1.5 * i / maxray - 0.5, 0.0);
120 w.drawCircle(x, y, i);
124 for (i = 0; i < maxray; i++) {
125 w.drawCircle(x, y, i);
128 w.fillCircle(x, y, maxray - 1);
131 void jeu(DrawingWindow& w)
133 dessineTerrain(w, frand(largeurMin, largeurMax),
134 frand(hauteurMin, hauteurMax));
135 dessineChateau(w, positionChateau1);
136 dessineChateau(w, positionChateau2);
139 dessineExplosion(w, frand(rXMin, rXMax), frand(rYMin, rYMax));
143 int main(int argc, char *argv[])
145 QApplication application(argc, argv);
146 DrawingWindow window(jeu, 640, 480);
148 return application.exec();