X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/blobdiff_plain/9593d0d11261a3e71c3b8c5b1a53c47fa02d74d3..acf4714da7838afe963be32f33eca132ad9a1e00:/chateaux/chateaux.cpp diff --git a/chateaux/chateaux.cpp b/chateaux/chateaux.cpp index 4fbf996..b6c26e2 100644 --- a/chateaux/chateaux.cpp +++ b/chateaux/chateaux.cpp @@ -4,6 +4,7 @@ #include #include #include +#include /* Note : les coordonnées réelles vont de -100 à +100 en abscisse, et * de -10 à +140 en ordonnée @@ -11,24 +12,6 @@ const float PI = 4.0 * atan(1.0); -/* Retourne un nombre pseudo-aléatoire compris entre 0 et le paramètre - * 'max' (exclus) - */ -float frand(float min, float max) -{ - static bool first = true; - if (first) { - srand(time(NULL)); - first = false; - } - return min + (max - min)* (rand() / (RAND_MAX + 1.0)); -} - -float deg2rad(float deg) -{ - return deg * PI / 180.0; -} - const float rXMin = -100.0; const float rXMax = 100.0; const float rYMin = -10.0; @@ -39,9 +22,6 @@ const float hauteurMax = 130; const float largeurMin = 40; const float largeurMax = 150; -const float largeurMont = frand(largeurMin, largeurMax); -const float hauteurMont = frand(hauteurMin, hauteurMax); - const float largeurChateau = 8.5; const float hauteurChateau = 7; @@ -51,7 +31,30 @@ const float positionChateau2 = 85.0; const float g = 9.81; const float k = 0.005; const float dt = 0.05; -const float wnd = frand(-30, 30); + +int nbJoueurs = 2; + +float largeurMont; +float hauteurMont; +float wnd; + +/* Retourne un nombre pseudo-aléatoire compris entre 0 et le paramètre + * 'max' (exclus) + */ +float frand(float min, float max) +{ + static bool first = true; + if (first) { + srand(time(NULL)); + first = false; + } + return min + (max - min)* (rand() / (RAND_MAX + 1.0)); +} + +float deg2rad(float deg) +{ + return deg * PI / 180.0; +} // conversion coordonnées réelles -> coordonnées fenêtre int rtowX(const DrawingWindow& w, float rx) @@ -163,7 +166,7 @@ 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); @@ -193,6 +196,24 @@ void dessineFlammes(DrawingWindow& w, float x0, float y0) } } +void initialise(DrawingWindow& w) +{ + largeurMont = frand(largeurMin, largeurMax); + hauteurMont = frand(hauteurMin, hauteurMax); + wnd = frand(-30, 30); + w.setBgColor("skyblue"); + w.clearGraph(); + dessineTerrain(w, largeurMont, hauteurMont); + dessineChateau(w, positionChateau1); + dessineChateau(w, positionChateau2); + 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); + dessineVent(w, wnd); +} + /* Retour : numéro du perdant, 0 sinon x et y contiennent les coordonnées de la collision */ @@ -220,7 +241,7 @@ int tir(DrawingWindow& w, vy += ay * dt; w.msleep(10); - w.setColor("white"); + w.setColor("skyblue"); w.fillCircle(wx, wy, 2); // w.setColor("black"); // w.drawPoint(wx, wy); @@ -244,38 +265,91 @@ int tir(DrawingWindow& w, return collision == 3 ? 0 : collision; } -void jeu(DrawingWindow& w) +int jeu1(DrawingWindow& w) { - dessineTerrain(w, largeurMont, hauteurMont); - dessineChateau(w, positionChateau1); - dessineChateau(w, positionChateau2); - dessineVent(w, wnd); - + initialise(w); int joueur = 2; float x, y; int perdant; do { - float x0; - float y0 = 8; - float v0 = frand(10, 100); - float alpha = deg2rad(frand(10, 90)); - joueur = 3 - joueur; + + std::cout << "-=| Joueur " << joueur << " |=-"; + + float alpha; + float v0; + if (joueur <= nbJoueurs) { + std::cout << "\nangle ? "; + std::cin >> alpha; + std::cout << "vitesse initiale ? "; + std::cin >> v0; + } else { + alpha = frand(10, 90); + v0 = frand(10, 100); + std::cout << " [ " << alpha << " ; " << v0 << " ]\n"; + } + + alpha = deg2rad(alpha); + float x0; if (joueur == 1) { x0 = positionChateau1 + 8; } else { x0 = positionChateau2 - 8; alpha = PI - alpha; } + float y0 = 8; perdant = tir(w, x0, y0, v0, alpha, x, y); dessineExplosion(w, x, y); + dessineVent(w, wnd); } while (!perdant); dessineFlammes(w, x, y); + std::stringstream msg; + msg << "Joueur " << perdant; + if (perdant == joueur) + msg << " s'est suicidé !"; + else + msg << " a perdu !"; + w.setColor("darkred"); + w.setBgColor("white"); + w.drawTextBg(w.width / 2, w.height / 3, msg.str().c_str(), + Qt::AlignCenter); + std::cout << msg.str() << "\n"; + return perdant; +} + +void jeu(DrawingWindow& w) +{ + int score1 = 0; + int score2 = 0; + bool rejouer = true; + do { + int perdant = jeu1(w); + if (perdant == 1) + score2++; + else if (perdant == 2) + score1++; + std::cout << "### SCORE : " << score1 << " / " << score2 << " ###\n"; + if (nbJoueurs == 0) + w.sleep(2); + else { + char r; + do { + std::cout << "Recommencer (o/n) ? "; + std::cin >> r; + } while (std::cin.good() && r != 'o' && r != 'n'); + rejouer = r == 'o'; + } + } while (rejouer); + w.closeGraph(); } int main(int argc, char *argv[]) { QApplication application(argc, argv); + + if (argc > 1) + nbJoueurs = atoi(argv[1]); + DrawingWindow window(jeu, 640, 480); window.show(); return application.exec();