Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add some words on installation.
[graphlib.git] / chateaux / chateaux.cpp
index 44e22d4..6ad81b7 100644 (file)
@@ -4,9 +4,10 @@
 #include <iostream>
 #include <ctime>
 #include <cstdlib>
+#include <sstream>
 
-/* Note : les coordonnées réelles vont de -100 à +100 en abscisse, et
- *  de -10 à +140 en ordonnée
+/* Note : les coordonnées réelles vont de -100 à +100 en abscisse, et
+ *  de -10 à +140 en ordonnée
  */
 
 const float PI = 4.0 * atan(1.0);
@@ -21,6 +22,8 @@ const float hauteurMax = 130;
 const float largeurMin = 40;
 const float largeurMax = 150;
 
+const float ventMax = 30;
+
 const float largeurChateau = 8.5;
 const float hauteurChateau = 7;
 
@@ -32,13 +35,15 @@ const float k = 0.005;
 const float dt = 0.05;
 
 int nbJoueurs = 2;
+int score1 = 0;
+int score2 = 0;
 
 float largeurMont;
 float hauteurMont;
 float wnd;
 
-/* Retourne un nombre pseudo-aléatoire compris entre 0 et le paramètre
- * 'max' (exclus)
+/* Retourne un nombre pseudo-aléatoire compris entre le paramètre
+ * 'min' (inclus) et le paramètre 'max' (exclus)
  */
 float frand(float min, float max)
 {
@@ -55,26 +60,26 @@ float deg2rad(float deg)
     return deg * PI / 180.0;
 }
 
-// conversion coordonnées réelles -> coordonnées fenêtre
+// conversion coordonnées réelles -> coordonnées fenêtre
 int rtowX(const DrawingWindow& w, float rx)
 {
-    return (int )roundf(w.width * (rx - rXMin) / (rXMax - rXMin + 1.0));
+    return (int )roundf((w.width - 1) * (rx - rXMin) / (rXMax - rXMin));
 }
 
 int rtowY(const DrawingWindow& w, float ry)
 {
-    return (int )roundf(w.height * (rYMax - ry) / (rYMax - rYMin + 1.0));
+    return (int )roundf((w.height - 1) * (rYMax - ry) / (rYMax - rYMin));
 }
 
-// conversion coordonnées réelles -> coordonnées fenêtre
+// conversion coordonnées fenêtre -> coordonnées réelles
 float wtorX(const DrawingWindow& w, int wx)
 {
-    return (rXMax - rXMin + 1.0) * wx / w.width + rXMin;
+    return (rXMax - rXMin) * wx / (w.width - 1) + rXMin;
 }
 
 float wtorY(const DrawingWindow& w, int wy)
 {
-    return -(rYMax - rYMin + 1.0) * wy / w.height - rYMax;
+    return -(rYMax - rYMin) * wy / (w.height - 1) + rYMax;
 }
 
 float hauteurMontagne(float largeur, float hauteur, float x)
@@ -165,12 +170,12 @@ void dessineExplosion(DrawingWindow& w, float rx, float ry)
         w.drawCircle(x, y, i);
         w.msleep(20);
     }
-    w.setColor("white");
+    w.setColor("skyblue");
     for (i = 0; i < maxray; i++) {
         w.drawCircle(x, y, i);
         w.msleep(10);
     }
-    w.fillCircle(x, y, maxray - 1);
+    //    w.fillCircle(x, y, maxray - 1);
 }
 
 void dessineFlammes(DrawingWindow& w, float x0, float y0)
@@ -199,16 +204,26 @@ void initialise(DrawingWindow& w)
 {
     largeurMont = frand(largeurMin, largeurMax);
     hauteurMont = frand(hauteurMin, hauteurMax);
-    wnd = frand(-30, 30);
+    wnd = frand(-ventMax, ventMax);
+    w.setBgColor("skyblue");
     w.clearGraph();
     dessineTerrain(w, largeurMont, hauteurMont);
+    dessineVent(w, wnd);
     dessineChateau(w, positionChateau1);
     dessineChateau(w, positionChateau2);
-    dessineVent(w, wnd);
+    w.setColor("wheat");
+    w.drawText(rtowX(w, positionChateau1), rtowY(w, 0) + 8, "Joueur 1",
+               Qt::AlignHCenter);
+    w.drawText(rtowX(w, positionChateau2), rtowY(w, 0) + 8, "Joueur 2",
+               Qt::AlignHCenter);
+    std::stringstream s;
+    s << score1 << " / " << score2;
+    w.drawText(rtowX(w, 0), rtowY(w, 0) + 8, s.str(),
+               Qt::AlignHCenter);
 }
 
-/* Retour : numéro du perdant, 0 sinon
-   x et y contiennent les coordonnées de la collision
+/* Retour : numéro du perdant, 0 sinon
+   x et y contiennent les coordonnées de la collision
 */
 int tir(DrawingWindow& w,
         float x0, float y0, float v0, float alpha, float& x, float &y)
@@ -234,7 +249,8 @@ int tir(DrawingWindow& w,
         vy += ay * dt;
 
         w.msleep(10);
-        w.setColor("white");
+//         w.sync();
+        w.setColor("skyblue");
         w.fillCircle(wx, wy, 2);
 //         w.setColor("black");
 //         w.drawPoint(wx, wy);
@@ -267,6 +283,7 @@ int jeu1(DrawingWindow& w)
     do {
         joueur = 3 - joueur;
 
+        w.sync();
         std::cout << "-=| Joueur " << joueur << " |=-";
 
         float alpha;
@@ -279,36 +296,41 @@ int jeu1(DrawingWindow& w)
         } else {
             alpha = frand(10, 90);
             v0 = frand(10, 100);
-            std::cout << " [ " << alpha << " ; " << v0 << " ]\n";
+            std::cout << " [ " << (int )alpha << "° ; " << (int )v0 << " ]"
+                      << std::endl;
         }
 
         alpha = deg2rad(alpha);
         float x0;
         if (joueur == 1) {
-            x0 = positionChateau1 + 8;
+            x0 = positionChateau1 + 0.8 * largeurChateau;
         } else {
-            x0 = positionChateau2 - 8;
+            x0 = positionChateau2 - 0.8 * largeurChateau;
             alpha = PI - alpha;
         }
-        float y0 = 8;
+        float y0 = hauteurChateau + 1;
         perdant = tir(w, x0, y0, v0, alpha, x, y);
         dessineExplosion(w, x, y);
         dessineVent(w, wnd);
     } while (!perdant);
     dessineFlammes(w, x, y);
-    std::cout << "Joueur " << perdant;
+    std::stringstream msg;
+    msg << " Joueur " << perdant;
     if (perdant == joueur)
-        std::cout << " s'est suicidé";
+        msg << " s'est suicidé ! ";
     else
-        std::cout << " a perdu";
-    std::cout << " !\n";
+        msg << " a perdu ! ";
+    w.setColor("darkred");
+    w.setBgColor("white");
+    w.drawTextBg(w.width / 2, w.height / 3, msg.str(),
+                 Qt::AlignCenter);
+    w.sync();
+    std::cout << msg.str() << std::endl;
     return perdant;
 }
 
 void jeu(DrawingWindow& w)
 {
-    int score1 = 0;
-    int score2 = 0;
     bool rejouer = true;
     do {
         int perdant = jeu1(w);
@@ -316,7 +338,8 @@ void jeu(DrawingWindow& w)
             score2++;
         else if (perdant == 2)
             score1++;
-        std::cout << "### SCORE : " << score1 << " / " << score2 << " ###\n";
+        std::cout << "### SCORE : " << score1 << " / " << score2 << " ###"
+                  << std::endl;
         if (nbJoueurs == 0)
             w.sleep(2);
         else {