Pong with nucleo en shield
Dependencies: C12832 LM75B MMA7660 Speaker mbed
Fork of app-shield-LCD by
main.cpp@6:402e96a482a9, 2017-01-24 (annotated)
- Committer:
- kv09
- Date:
- Tue Jan 24 16:04:35 2017 +0000
- Revision:
- 6:402e96a482a9
- Parent:
- 4:39c7c31b8fb0
Nucleo chiaverini
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:f6a57b843f79 | 1 | #include "mbed.h" |
chris | 3:2db94ee076ee | 2 | #include "C12832.h" |
kv09 | 6:402e96a482a9 | 3 | #include "MMA7660.h" |
kv09 | 6:402e96a482a9 | 4 | #include "LM75B.h" |
kv09 | 6:402e96a482a9 | 5 | #include "Speaker.h" |
kv09 | 6:402e96a482a9 | 6 | #include <stdlib.h> |
kv09 | 6:402e96a482a9 | 7 | #include <time.h> |
kv09 | 6:402e96a482a9 | 8 | #include <string> |
dreschpe | 0:f6a57b843f79 | 9 | |
kv09 | 6:402e96a482a9 | 10 | // Using Arduino pin notation |
kv09 | 6:402e96a482a9 | 11 | /************* LCD ****************/ |
kv09 | 6:402e96a482a9 | 12 | C12832 lcd(D11, D13, D12, D7, D10); |
kv09 | 6:402e96a482a9 | 13 | |
kv09 | 6:402e96a482a9 | 14 | /********* Potentiometre *********/ |
kv09 | 6:402e96a482a9 | 15 | AnalogIn pot1(A0); |
kv09 | 6:402e96a482a9 | 16 | AnalogIn pot2(A1); |
kv09 | 6:402e96a482a9 | 17 | |
kv09 | 6:402e96a482a9 | 18 | /************** LED ***************/ |
kv09 | 6:402e96a482a9 | 19 | DigitalOut r(D5); |
kv09 | 6:402e96a482a9 | 20 | DigitalOut b(D8); |
kv09 | 6:402e96a482a9 | 21 | DigitalOut g(D9); |
kv09 | 6:402e96a482a9 | 22 | |
kv09 | 6:402e96a482a9 | 23 | /*********** Analogue *************/ |
kv09 | 6:402e96a482a9 | 24 | DigitalIn up(A2); //AnalogIn |
kv09 | 6:402e96a482a9 | 25 | DigitalIn down(A3); |
kv09 | 6:402e96a482a9 | 26 | DigitalIn gau(A4); |
kv09 | 6:402e96a482a9 | 27 | DigitalIn droi(A5); |
kv09 | 6:402e96a482a9 | 28 | DigitalIn fire(D4); |
kv09 | 6:402e96a482a9 | 29 | |
kv09 | 6:402e96a482a9 | 30 | /*********** Speaker **************/ |
kv09 | 6:402e96a482a9 | 31 | Speaker spkr(D6); |
kv09 | 6:402e96a482a9 | 32 | |
kv09 | 6:402e96a482a9 | 33 | /********* Accelerometre **********/ |
kv09 | 6:402e96a482a9 | 34 | MMA7660 MMA(D14, D15); |
kv09 | 6:402e96a482a9 | 35 | |
kv09 | 6:402e96a482a9 | 36 | /***** Capteur de température *****/ |
kv09 | 6:402e96a482a9 | 37 | LM75B sensor(D14, D15); |
kv09 | 6:402e96a482a9 | 38 | |
kv09 | 6:402e96a482a9 | 39 | //Variable globale |
kv09 | 6:402e96a482a9 | 40 | // Integer |
kv09 | 6:402e96a482a9 | 41 | int horizontalBall = 35; |
kv09 | 6:402e96a482a9 | 42 | int scorej1 = 0; |
kv09 | 6:402e96a482a9 | 43 | int scorej2 = 0; |
kv09 | 6:402e96a482a9 | 44 | int verticalJ1 = 0; |
kv09 | 6:402e96a482a9 | 45 | int verticalJ2 = 0; |
kv09 | 6:402e96a482a9 | 46 | int victoirej1 = 0; |
kv09 | 6:402e96a482a9 | 47 | int victoirej2 = 0; |
kv09 | 6:402e96a482a9 | 48 | int nbcontact = 0; |
kv09 | 6:402e96a482a9 | 49 | int rapide = 3; |
kv09 | 6:402e96a482a9 | 50 | int choix; |
kv09 | 6:402e96a482a9 | 51 | int ajoutvit = 1; |
kv09 | 6:402e96a482a9 | 52 | // Float |
kv09 | 6:402e96a482a9 | 53 | float angle = 1.5; |
kv09 | 6:402e96a482a9 | 54 | float verticalBall = 5.0; |
kv09 | 6:402e96a482a9 | 55 | float vitesse = 0; |
kv09 | 6:402e96a482a9 | 56 | float vitessebase = 0; |
kv09 | 6:402e96a482a9 | 57 | // Boolean |
kv09 | 6:402e96a482a9 | 58 | bool avance = true; |
kv09 | 6:402e96a482a9 | 59 | bool recule = false; |
kv09 | 6:402e96a482a9 | 60 | bool but = false; |
kv09 | 6:402e96a482a9 | 61 | bool joue = true; |
kv09 | 6:402e96a482a9 | 62 | bool diag1 = true; |
kv09 | 6:402e96a482a9 | 63 | bool diag2 = false; |
kv09 | 6:402e96a482a9 | 64 | bool partie = true; |
kv09 | 6:402e96a482a9 | 65 | bool lance = true; |
kv09 | 6:402e96a482a9 | 66 | bool stop = false; |
kv09 | 6:402e96a482a9 | 67 | //String |
kv09 | 6:402e96a482a9 | 68 | string tab[3] = { |
kv09 | 6:402e96a482a9 | 69 | "lent", |
kv09 | 6:402e96a482a9 | 70 | "normal", |
kv09 | 6:402e96a482a9 | 71 | "rapide" |
kv09 | 6:402e96a482a9 | 72 | };; |
kv09 | 6:402e96a482a9 | 73 | |
kv09 | 6:402e96a482a9 | 74 | // Fonction du contact entre la balle et le joueur |
kv09 | 6:402e96a482a9 | 75 | bool contact(int a, float b) { |
kv09 | 6:402e96a482a9 | 76 | nbcontact++; |
kv09 | 6:402e96a482a9 | 77 | // Fait accelerer la balle pour un certain nombre de contact |
kv09 | 6:402e96a482a9 | 78 | if (nbcontact % rapide == 0) { |
kv09 | 6:402e96a482a9 | 79 | if (vitesse > 0.03) { |
kv09 | 6:402e96a482a9 | 80 | vitesse = vitesse - 0.005; |
kv09 | 6:402e96a482a9 | 81 | } |
kv09 | 6:402e96a482a9 | 82 | if (ajoutvit < 4) { |
kv09 | 6:402e96a482a9 | 83 | ajoutvit++; |
kv09 | 6:402e96a482a9 | 84 | } |
kv09 | 6:402e96a482a9 | 85 | } |
kv09 | 6:402e96a482a9 | 86 | // Variation de l'angle en fonction d'ou est tapé la balle |
kv09 | 6:402e96a482a9 | 87 | if (a >= b + 2 && a <= b + 5) { |
kv09 | 6:402e96a482a9 | 88 | angle = 0.5; |
kv09 | 6:402e96a482a9 | 89 | return true; |
kv09 | 6:402e96a482a9 | 90 | } else if (a >= b - 1 && a <= b + 2) { |
kv09 | 6:402e96a482a9 | 91 | angle = 1.0; |
kv09 | 6:402e96a482a9 | 92 | return true; |
kv09 | 6:402e96a482a9 | 93 | } else if (a >= b - 4 && a <= b - 1) { |
kv09 | 6:402e96a482a9 | 94 | angle = 1.5; |
kv09 | 6:402e96a482a9 | 95 | return true; |
kv09 | 6:402e96a482a9 | 96 | } else { |
kv09 | 6:402e96a482a9 | 97 | return false; |
kv09 | 6:402e96a482a9 | 98 | } |
kv09 | 6:402e96a482a9 | 99 | } |
kv09 | 6:402e96a482a9 | 100 | |
kv09 | 6:402e96a482a9 | 101 | // Fonction d'initialisationialisation |
kv09 | 6:402e96a482a9 | 102 | void initialisation() { |
kv09 | 6:402e96a482a9 | 103 | horizontalBall = 50; |
kv09 | 6:402e96a482a9 | 104 | verticalBall = (rand() % 20) + 1; |
kv09 | 6:402e96a482a9 | 105 | avance = true; |
kv09 | 6:402e96a482a9 | 106 | recule = false; |
kv09 | 6:402e96a482a9 | 107 | but = false; |
kv09 | 6:402e96a482a9 | 108 | joue = true; |
kv09 | 6:402e96a482a9 | 109 | vitesse = vitessebase; |
kv09 | 6:402e96a482a9 | 110 | ajoutvit = 1; |
kv09 | 6:402e96a482a9 | 111 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 112 | // Décompte avant commencement de la partie |
kv09 | 6:402e96a482a9 | 113 | for (int i = 2; i > 0; i--) { |
kv09 | 6:402e96a482a9 | 114 | lcd.locate(35, 10); |
kv09 | 6:402e96a482a9 | 115 | lcd.printf("Commence dans %d", i); |
kv09 | 6:402e96a482a9 | 116 | wait(1); |
kv09 | 6:402e96a482a9 | 117 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 118 | } |
kv09 | 6:402e96a482a9 | 119 | } |
kv09 | 6:402e96a482a9 | 120 | |
kv09 | 6:402e96a482a9 | 121 | // Fonction d'affichage |
kv09 | 6:402e96a482a9 | 122 | void affichage() { |
kv09 | 6:402e96a482a9 | 123 | // Cadre du terrain |
kv09 | 6:402e96a482a9 | 124 | lcd.rect(32, 0, 127, 31, 1); |
kv09 | 6:402e96a482a9 | 125 | lcd.copy_to_lcd(); |
kv09 | 6:402e96a482a9 | 126 | |
kv09 | 6:402e96a482a9 | 127 | //Score |
kv09 | 6:402e96a482a9 | 128 | lcd.locate(0, 0); |
kv09 | 6:402e96a482a9 | 129 | lcd.printf("SCORE"); |
kv09 | 6:402e96a482a9 | 130 | lcd.locate(2, 12); |
kv09 | 6:402e96a482a9 | 131 | lcd.printf("J1"); |
kv09 | 6:402e96a482a9 | 132 | lcd.locate(3, 20); |
kv09 | 6:402e96a482a9 | 133 | lcd.printf("%d", scorej1); |
kv09 | 6:402e96a482a9 | 134 | lcd.locate(15, 12); |
kv09 | 6:402e96a482a9 | 135 | lcd.printf("J2"); |
kv09 | 6:402e96a482a9 | 136 | lcd.locate(16, 20); |
kv09 | 6:402e96a482a9 | 137 | lcd.printf("%d", scorej2); |
kv09 | 6:402e96a482a9 | 138 | |
kv09 | 6:402e96a482a9 | 139 | //bar 1 |
kv09 | 6:402e96a482a9 | 140 | verticalJ1 = (float) pot1 * 22; |
kv09 | 6:402e96a482a9 | 141 | lcd.locate(35, verticalJ1); |
kv09 | 6:402e96a482a9 | 142 | lcd.printf("l"); |
kv09 | 6:402e96a482a9 | 143 | |
kv09 | 6:402e96a482a9 | 144 | // bar 2 |
kv09 | 6:402e96a482a9 | 145 | verticalJ2 = (float) pot2 * 22; |
kv09 | 6:402e96a482a9 | 146 | lcd.locate(120, verticalJ2); |
kv09 | 6:402e96a482a9 | 147 | lcd.printf("l"); |
dreschpe | 1:1c6a9eaf55b5 | 148 | |
kv09 | 6:402e96a482a9 | 149 | //balle |
kv09 | 6:402e96a482a9 | 150 | lcd.locate(horizontalBall, verticalBall); |
kv09 | 6:402e96a482a9 | 151 | lcd.printf("o"); |
kv09 | 6:402e96a482a9 | 152 | } |
kv09 | 6:402e96a482a9 | 153 | |
kv09 | 6:402e96a482a9 | 154 | // Fonction de son pour le démarrage |
kv09 | 6:402e96a482a9 | 155 | void son(float f) { |
kv09 | 6:402e96a482a9 | 156 | spkr.PlayNote(969.0 * f, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 157 | spkr.PlayNote(800.0 * f, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 158 | spkr.PlayNote(768.0 * f, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 159 | spkr.PlayNote(879.0 * f, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 160 | } |
kv09 | 6:402e96a482a9 | 161 | |
kv09 | 6:402e96a482a9 | 162 | // Fonction de son pour un but |
kv09 | 6:402e96a482a9 | 163 | void son_marque() { |
kv09 | 6:402e96a482a9 | 164 | spkr.PlayNote(1200.0, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 165 | spkr.PlayNote(900.0, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 166 | spkr.PlayNote(1000.0, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 167 | spkr.PlayNote(700.0, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 168 | } |
kv09 | 6:402e96a482a9 | 169 | |
kv09 | 6:402e96a482a9 | 170 | // Fonction de son pour une victoire |
kv09 | 6:402e96a482a9 | 171 | void son_victoire() { |
kv09 | 6:402e96a482a9 | 172 | spkr.PlayNote(1800.0, 0.2, 0.8); |
kv09 | 6:402e96a482a9 | 173 | wait(0.03); |
kv09 | 6:402e96a482a9 | 174 | spkr.PlayNote(1800.0, 0.2, 0.8); |
kv09 | 6:402e96a482a9 | 175 | wait(0.03); |
kv09 | 6:402e96a482a9 | 176 | spkr.PlayNote(1800.0, 0.5, 0.8); |
kv09 | 6:402e96a482a9 | 177 | wait(0.03); |
kv09 | 6:402e96a482a9 | 178 | spkr.PlayNote(600.0, 0.7, 1.1); |
kv09 | 6:402e96a482a9 | 179 | wait(0.03); |
kv09 | 6:402e96a482a9 | 180 | spkr.PlayNote(1800.0, 0.2, 0.8); |
kv09 | 6:402e96a482a9 | 181 | wait(0.03); |
kv09 | 6:402e96a482a9 | 182 | spkr.PlayNote(1600.0, 0.2, 0.8); |
kv09 | 6:402e96a482a9 | 183 | wait(0.03); |
kv09 | 6:402e96a482a9 | 184 | spkr.PlayNote(1800.0, 0.2, 0.8); |
kv09 | 6:402e96a482a9 | 185 | wait(0.03); |
kv09 | 6:402e96a482a9 | 186 | spkr.PlayNote(1800.0, 0.5, 0.8); |
kv09 | 6:402e96a482a9 | 187 | wait(0.03); |
kv09 | 6:402e96a482a9 | 188 | spkr.PlayNote(600.0, 0.7, 1.1); |
kv09 | 6:402e96a482a9 | 189 | } |
kv09 | 6:402e96a482a9 | 190 | |
kv09 | 6:402e96a482a9 | 191 | // Fonction de son lors d'un contact avec |
kv09 | 6:402e96a482a9 | 192 | void son_contact() { |
kv09 | 6:402e96a482a9 | 193 | spkr.PlayNote(900.0, 0.1, 1.1); |
kv09 | 6:402e96a482a9 | 194 | } |
kv09 | 6:402e96a482a9 | 195 | |
kv09 | 6:402e96a482a9 | 196 | // Fonction pour le mouvement de la balle |
kv09 | 6:402e96a482a9 | 197 | void mouvement_balle(bool av, bool rec) { |
kv09 | 6:402e96a482a9 | 198 | // Fonction pour faire avancer ou reculer la balle |
kv09 | 6:402e96a482a9 | 199 | if (av) { |
kv09 | 6:402e96a482a9 | 200 | horizontalBall = horizontalBall + ajoutvit; |
kv09 | 6:402e96a482a9 | 201 | } |
kv09 | 6:402e96a482a9 | 202 | if (rec) { |
kv09 | 6:402e96a482a9 | 203 | horizontalBall = horizontalBall - ajoutvit; |
kv09 | 6:402e96a482a9 | 204 | } |
kv09 | 6:402e96a482a9 | 205 | // Fonction pour le rebond de la balle sur le haut et le bas de l'écran |
kv09 | 6:402e96a482a9 | 206 | if (diag1) { |
kv09 | 6:402e96a482a9 | 207 | verticalBall = verticalBall + angle; |
kv09 | 6:402e96a482a9 | 208 | } |
kv09 | 6:402e96a482a9 | 209 | if (diag2) { |
kv09 | 6:402e96a482a9 | 210 | verticalBall = verticalBall - angle; |
kv09 | 6:402e96a482a9 | 211 | } |
kv09 | 6:402e96a482a9 | 212 | if (verticalBall < 0) { |
kv09 | 6:402e96a482a9 | 213 | diag1 = true; |
kv09 | 6:402e96a482a9 | 214 | diag2 = false; |
kv09 | 6:402e96a482a9 | 215 | } |
kv09 | 6:402e96a482a9 | 216 | if (verticalBall > 22) { |
kv09 | 6:402e96a482a9 | 217 | diag1 = false; |
kv09 | 6:402e96a482a9 | 218 | diag2 = true; |
kv09 | 6:402e96a482a9 | 219 | } |
kv09 | 6:402e96a482a9 | 220 | } |
kv09 | 6:402e96a482a9 | 221 | |
kv09 | 6:402e96a482a9 | 222 | // Fonction pour le but |
kv09 | 6:402e96a482a9 | 223 | void marque() { |
kv09 | 6:402e96a482a9 | 224 | // Si c'est le joueur qui marque |
kv09 | 6:402e96a482a9 | 225 | if (horizontalBall >= 115) { |
kv09 | 6:402e96a482a9 | 226 | // Si il y a contact la balle repart |
kv09 | 6:402e96a482a9 | 227 | if (contact(verticalJ2, verticalBall)) { |
kv09 | 6:402e96a482a9 | 228 | son_contact(); |
kv09 | 6:402e96a482a9 | 229 | avance = false; |
kv09 | 6:402e96a482a9 | 230 | recule = true; |
kv09 | 6:402e96a482a9 | 231 | } |
kv09 | 6:402e96a482a9 | 232 | // Sinon il y a but |
kv09 | 6:402e96a482a9 | 233 | else { |
kv09 | 6:402e96a482a9 | 234 | but = true; |
kv09 | 6:402e96a482a9 | 235 | scorej1++; |
kv09 | 6:402e96a482a9 | 236 | son_marque(); |
kv09 | 6:402e96a482a9 | 237 | joue = false; |
kv09 | 6:402e96a482a9 | 238 | r = 0; |
kv09 | 6:402e96a482a9 | 239 | b = 1; |
kv09 | 6:402e96a482a9 | 240 | g = 1; |
kv09 | 6:402e96a482a9 | 241 | wait(0.2); |
kv09 | 6:402e96a482a9 | 242 | r = 1; |
kv09 | 6:402e96a482a9 | 243 | b = 1; |
kv09 | 6:402e96a482a9 | 244 | g = 1; |
kv09 | 6:402e96a482a9 | 245 | } |
kv09 | 6:402e96a482a9 | 246 | } |
kv09 | 6:402e96a482a9 | 247 | // Si c'est le joueur 2 qui marque |
kv09 | 6:402e96a482a9 | 248 | if (horizontalBall <= 35) { |
kv09 | 6:402e96a482a9 | 249 | // Si il y a contact la balle repart |
kv09 | 6:402e96a482a9 | 250 | if (contact(verticalJ1, verticalBall)) { |
kv09 | 6:402e96a482a9 | 251 | son_contact(); |
kv09 | 6:402e96a482a9 | 252 | avance = true; |
kv09 | 6:402e96a482a9 | 253 | recule = false; |
kv09 | 6:402e96a482a9 | 254 | } |
kv09 | 6:402e96a482a9 | 255 | // Sinon il y a but |
kv09 | 6:402e96a482a9 | 256 | else { |
kv09 | 6:402e96a482a9 | 257 | but = true; |
kv09 | 6:402e96a482a9 | 258 | scorej2++; |
kv09 | 6:402e96a482a9 | 259 | son_marque(); |
kv09 | 6:402e96a482a9 | 260 | joue = false; |
kv09 | 6:402e96a482a9 | 261 | r = 1; |
kv09 | 6:402e96a482a9 | 262 | b = 0; |
kv09 | 6:402e96a482a9 | 263 | g = 1; |
kv09 | 6:402e96a482a9 | 264 | wait(0.2); |
kv09 | 6:402e96a482a9 | 265 | r = 1; |
kv09 | 6:402e96a482a9 | 266 | b = 1; |
kv09 | 6:402e96a482a9 | 267 | g = 1; |
kv09 | 6:402e96a482a9 | 268 | } |
kv09 | 6:402e96a482a9 | 269 | } |
kv09 | 6:402e96a482a9 | 270 | } |
kv09 | 6:402e96a482a9 | 271 | |
kv09 | 6:402e96a482a9 | 272 | // Fonction de lancement du jeux |
kv09 | 6:402e96a482a9 | 273 | void commencement() { |
kv09 | 6:402e96a482a9 | 274 | lcd.rect(0, 0, 127, 31, 1); |
kv09 | 6:402e96a482a9 | 275 | lcd.copy_to_lcd(); |
kv09 | 6:402e96a482a9 | 276 | scorej1 = 0; |
kv09 | 6:402e96a482a9 | 277 | scorej2 = 0; |
kv09 | 6:402e96a482a9 | 278 | // Menu pour le choixx de la vitesse |
kv09 | 6:402e96a482a9 | 279 | while (!fire) { |
kv09 | 6:402e96a482a9 | 280 | lcd.locate(25, 10); |
kv09 | 6:402e96a482a9 | 281 | lcd.printf("choixsissez la vitesse"); |
kv09 | 6:402e96a482a9 | 282 | lcd.locate(50, 18); |
kv09 | 6:402e96a482a9 | 283 | lcd.printf("- %s", tab[choix]); |
kv09 | 6:402e96a482a9 | 284 | // Monte dans le menu |
kv09 | 6:402e96a482a9 | 285 | if (!up) { |
kv09 | 6:402e96a482a9 | 286 | choix++; |
kv09 | 6:402e96a482a9 | 287 | if (choix > 2) { |
kv09 | 6:402e96a482a9 | 288 | choix = 0; |
kv09 | 6:402e96a482a9 | 289 | } |
kv09 | 6:402e96a482a9 | 290 | } |
kv09 | 6:402e96a482a9 | 291 | // Descend dans le menu |
kv09 | 6:402e96a482a9 | 292 | if (!down) { |
kv09 | 6:402e96a482a9 | 293 | choix--; |
kv09 | 6:402e96a482a9 | 294 | if (choix < 0) { |
kv09 | 6:402e96a482a9 | 295 | choix = 2; |
kv09 | 6:402e96a482a9 | 296 | } |
kv09 | 6:402e96a482a9 | 297 | } |
kv09 | 6:402e96a482a9 | 298 | wait(0.2); |
chris | 2:a87e255a8f3a | 299 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 300 | } |
kv09 | 6:402e96a482a9 | 301 | // Liste des choixx de vitesse |
kv09 | 6:402e96a482a9 | 302 | switch (choix) { |
kv09 | 6:402e96a482a9 | 303 | case 0: |
kv09 | 6:402e96a482a9 | 304 | vitessebase = 0.09; |
kv09 | 6:402e96a482a9 | 305 | rapide = 5; |
kv09 | 6:402e96a482a9 | 306 | break; |
kv09 | 6:402e96a482a9 | 307 | case 1: |
kv09 | 6:402e96a482a9 | 308 | vitessebase = 0.07; |
kv09 | 6:402e96a482a9 | 309 | rapide = 3; |
kv09 | 6:402e96a482a9 | 310 | break; |
kv09 | 6:402e96a482a9 | 311 | case 2: |
kv09 | 6:402e96a482a9 | 312 | vitessebase = 0.05; |
kv09 | 6:402e96a482a9 | 313 | rapide = 2; |
kv09 | 6:402e96a482a9 | 314 | ajoutvit = 2; |
kv09 | 6:402e96a482a9 | 315 | break; |
kv09 | 6:402e96a482a9 | 316 | default: |
kv09 | 6:402e96a482a9 | 317 | vitessebase = 0.09; |
kv09 | 6:402e96a482a9 | 318 | rapide = 5; |
kv09 | 6:402e96a482a9 | 319 | } |
kv09 | 6:402e96a482a9 | 320 | partie = true; |
kv09 | 6:402e96a482a9 | 321 | lance = false; |
kv09 | 6:402e96a482a9 | 322 | } |
kv09 | 6:402e96a482a9 | 323 | |
kv09 | 6:402e96a482a9 | 324 | // Fonction pour savoir qui gagne |
kv09 | 6:402e96a482a9 | 325 | void victoire() { |
kv09 | 6:402e96a482a9 | 326 | /* Si le score arrive à 5 |
kv09 | 6:402e96a482a9 | 327 | * On ajoute une victoire pour le joueur qui gagne |
kv09 | 6:402e96a482a9 | 328 | * Appel de la fonction de musique de victoire |
kv09 | 6:402e96a482a9 | 329 | */ |
kv09 | 6:402e96a482a9 | 330 | if (scorej1 == 5) { |
kv09 | 6:402e96a482a9 | 331 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 332 | lcd.locate(25, 10); |
kv09 | 6:402e96a482a9 | 333 | lcd.printf("Victoire du joueur 1"); |
kv09 | 6:402e96a482a9 | 334 | son_victoire(); |
kv09 | 6:402e96a482a9 | 335 | victoirej1++; |
kv09 | 6:402e96a482a9 | 336 | wait(2); |
kv09 | 6:402e96a482a9 | 337 | partie = false; |
kv09 | 6:402e96a482a9 | 338 | } |
kv09 | 6:402e96a482a9 | 339 | if (scorej2 == 5) { |
kv09 | 6:402e96a482a9 | 340 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 341 | lcd.locate(25, 10); |
kv09 | 6:402e96a482a9 | 342 | lcd.printf("Victoire du joueur 2"); |
kv09 | 6:402e96a482a9 | 343 | son_victoire(); |
kv09 | 6:402e96a482a9 | 344 | victoirej2++; |
kv09 | 6:402e96a482a9 | 345 | wait(2); |
kv09 | 6:402e96a482a9 | 346 | partie = false; |
kv09 | 6:402e96a482a9 | 347 | } |
kv09 | 6:402e96a482a9 | 348 | } |
kv09 | 6:402e96a482a9 | 349 | |
kv09 | 6:402e96a482a9 | 350 | // Fonction qui permet de faire rejouer lorsque l'on secoue le nucleo |
kv09 | 6:402e96a482a9 | 351 | void rejouer() { |
kv09 | 6:402e96a482a9 | 352 | float AX = 0, AY = 0, AZ = 0; |
kv09 | 6:402e96a482a9 | 353 | float valeur = 1.3; |
kv09 | 6:402e96a482a9 | 354 | int compteur = 0; |
kv09 | 6:402e96a482a9 | 355 | int sortie = 0; |
kv09 | 6:402e96a482a9 | 356 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 357 | lcd.locate(20, 8); |
kv09 | 6:402e96a482a9 | 358 | lcd.printf("Secouer pour rejouer"); |
kv09 | 6:402e96a482a9 | 359 | // Si le joueur secoue la nucleo on rejoue |
kv09 | 6:402e96a482a9 | 360 | while (compteur < 3) { |
kv09 | 6:402e96a482a9 | 361 | // Test si les capteurs dépasse la valeur donné si oui on ajoute 1 au compteur |
kv09 | 6:402e96a482a9 | 362 | if (MMA.x() > AX + valeur || MMA.y() > AY + valeur || MMA.z() > AZ + valeur) { |
kv09 | 6:402e96a482a9 | 363 | compteur++; |
kv09 | 6:402e96a482a9 | 364 | } |
kv09 | 6:402e96a482a9 | 365 | wait(0.2); |
kv09 | 6:402e96a482a9 | 366 | sortie++; |
kv09 | 6:402e96a482a9 | 367 | // Sinon on attend 5 seconde pour sortir |
kv09 | 6:402e96a482a9 | 368 | if (sortie == 25) { |
kv09 | 6:402e96a482a9 | 369 | compteur = 25; |
kv09 | 6:402e96a482a9 | 370 | } |
kv09 | 6:402e96a482a9 | 371 | } |
kv09 | 6:402e96a482a9 | 372 | if (compteur == 25) { |
kv09 | 6:402e96a482a9 | 373 | lance = false; |
kv09 | 6:402e96a482a9 | 374 | } else { |
kv09 | 6:402e96a482a9 | 375 | lance = true; |
kv09 | 6:402e96a482a9 | 376 | } |
kv09 | 6:402e96a482a9 | 377 | } |
kv09 | 6:402e96a482a9 | 378 | |
kv09 | 6:402e96a482a9 | 379 | // Fonction pour afficher le récapitulatif des victoires |
kv09 | 6:402e96a482a9 | 380 | void recapitulatif() { |
kv09 | 6:402e96a482a9 | 381 | // Affiche le nombre de victoire de chaque joueur |
kv09 | 6:402e96a482a9 | 382 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 383 | lcd.locate(25, 10); |
kv09 | 6:402e96a482a9 | 384 | lcd.printf("Joueur 1 : %d victoires", victoirej1); |
kv09 | 6:402e96a482a9 | 385 | lcd.locate(25, 20); |
kv09 | 6:402e96a482a9 | 386 | lcd.printf("Joueur 2 : %d victoires", victoirej2); |
kv09 | 6:402e96a482a9 | 387 | wait(4); |
kv09 | 6:402e96a482a9 | 388 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 389 | lcd.locate(30, 10); |
kv09 | 6:402e96a482a9 | 390 | lcd.printf("Merci d avoir joue"); |
kv09 | 6:402e96a482a9 | 391 | wait(2); |
kv09 | 6:402e96a482a9 | 392 | } |
kv09 | 6:402e96a482a9 | 393 | |
kv09 | 6:402e96a482a9 | 394 | // Fonction pour faire pause lors du jeux |
kv09 | 6:402e96a482a9 | 395 | void pause() { |
kv09 | 6:402e96a482a9 | 396 | // Si il appuie sur le bouton du joystick on lance la pause |
kv09 | 6:402e96a482a9 | 397 | if (fire) { |
kv09 | 6:402e96a482a9 | 398 | stop = true; |
kv09 | 6:402e96a482a9 | 399 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 400 | lcd.locate(50, 10); |
kv09 | 6:402e96a482a9 | 401 | lcd.printf("Pause"); |
kv09 | 6:402e96a482a9 | 402 | lcd.locate(25, 2); |
kv09 | 6:402e96a482a9 | 403 | // Affiche la température récupéré par le capteur |
kv09 | 6:402e96a482a9 | 404 | lcd.printf("Temperature = %.1f\n", sensor.read()); |
kv09 | 6:402e96a482a9 | 405 | lcd.locate(3, 18); |
kv09 | 6:402e96a482a9 | 406 | // Affiche le score |
kv09 | 6:402e96a482a9 | 407 | lcd.printf("joueur 1 : %d - joueur 2 : %d", scorej1, scorej2); |
kv09 | 6:402e96a482a9 | 408 | int lightp = 6; |
kv09 | 6:402e96a482a9 | 409 | while (stop) { |
kv09 | 6:402e96a482a9 | 410 | // On fait clignoter la LED en vert |
kv09 | 6:402e96a482a9 | 411 | if (lightp % 6 == 0) { |
kv09 | 6:402e96a482a9 | 412 | r = 1; |
kv09 | 6:402e96a482a9 | 413 | b = 1; |
kv09 | 6:402e96a482a9 | 414 | g = 0; |
kv09 | 6:402e96a482a9 | 415 | } |
kv09 | 6:402e96a482a9 | 416 | wait(0.1); |
kv09 | 6:402e96a482a9 | 417 | // Si il réappuie sur le bouton du joystick on sort de la pause |
kv09 | 6:402e96a482a9 | 418 | if (fire) { |
kv09 | 6:402e96a482a9 | 419 | stop = false; |
kv09 | 6:402e96a482a9 | 420 | } |
kv09 | 6:402e96a482a9 | 421 | // On éteint la lED |
kv09 | 6:402e96a482a9 | 422 | r = 1; |
kv09 | 6:402e96a482a9 | 423 | g = 1; |
kv09 | 6:402e96a482a9 | 424 | b = 1; |
kv09 | 6:402e96a482a9 | 425 | lightp++; |
kv09 | 6:402e96a482a9 | 426 | } |
kv09 | 6:402e96a482a9 | 427 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 428 | affichage(); |
kv09 | 6:402e96a482a9 | 429 | wait(0.8); |
kv09 | 6:402e96a482a9 | 430 | } |
kv09 | 6:402e96a482a9 | 431 | } |
kv09 | 6:402e96a482a9 | 432 | |
kv09 | 6:402e96a482a9 | 433 | // Fonction de démarrage du jeu |
kv09 | 6:402e96a482a9 | 434 | void lancement() { |
kv09 | 6:402e96a482a9 | 435 | r = 1; |
kv09 | 6:402e96a482a9 | 436 | g = 1; |
kv09 | 6:402e96a482a9 | 437 | b = 1; |
kv09 | 6:402e96a482a9 | 438 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 439 | lcd.locate(50, 10); |
kv09 | 6:402e96a482a9 | 440 | lcd.printf("Bienvenue"); |
kv09 | 6:402e96a482a9 | 441 | wait(1); |
dreschpe | 0:f6a57b843f79 | 442 | |
kv09 | 6:402e96a482a9 | 443 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 444 | int po = 45, ng = 17; |
kv09 | 6:402e96a482a9 | 445 | bool ok = true; |
kv09 | 6:402e96a482a9 | 446 | int light = 3; |
kv09 | 6:402e96a482a9 | 447 | float zz = 1.0; |
kv09 | 6:402e96a482a9 | 448 | while (ok) { |
kv09 | 6:402e96a482a9 | 449 | son(zz); |
kv09 | 6:402e96a482a9 | 450 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 451 | lcd.locate(15, 17); |
kv09 | 6:402e96a482a9 | 452 | lcd.printf("l"); |
kv09 | 6:402e96a482a9 | 453 | lcd.locate(110, 6); |
kv09 | 6:402e96a482a9 | 454 | lcd.printf("l"); |
kv09 | 6:402e96a482a9 | 455 | // Affiche le nom PoNG en le déplacent |
kv09 | 6:402e96a482a9 | 456 | lcd.locate(po, ng); |
kv09 | 6:402e96a482a9 | 457 | lcd.printf("PoNG"); |
kv09 | 6:402e96a482a9 | 458 | po = po + 4; |
kv09 | 6:402e96a482a9 | 459 | ng--; |
kv09 | 6:402e96a482a9 | 460 | if (po == 85 || ng == 0) { |
kv09 | 6:402e96a482a9 | 461 | ok = false; |
kv09 | 6:402e96a482a9 | 462 | } |
kv09 | 6:402e96a482a9 | 463 | // Fait clignoter la LED en Rouge, Bleu et Vert |
kv09 | 6:402e96a482a9 | 464 | if (light % 3 == 0) { |
kv09 | 6:402e96a482a9 | 465 | r = 1; |
kv09 | 6:402e96a482a9 | 466 | g = 1; |
kv09 | 6:402e96a482a9 | 467 | b = 0; |
kv09 | 6:402e96a482a9 | 468 | } |
kv09 | 6:402e96a482a9 | 469 | if (light % 3 == 1) { |
kv09 | 6:402e96a482a9 | 470 | r = 1; |
kv09 | 6:402e96a482a9 | 471 | g = 0; |
kv09 | 6:402e96a482a9 | 472 | b = 1; |
kv09 | 6:402e96a482a9 | 473 | } |
kv09 | 6:402e96a482a9 | 474 | if (light % 3 == 2) { |
kv09 | 6:402e96a482a9 | 475 | r = 0; |
kv09 | 6:402e96a482a9 | 476 | g = 1; |
kv09 | 6:402e96a482a9 | 477 | b = 1; |
dreschpe | 0:f6a57b843f79 | 478 | } |
kv09 | 6:402e96a482a9 | 479 | wait(0.08); |
kv09 | 6:402e96a482a9 | 480 | r = 1; |
kv09 | 6:402e96a482a9 | 481 | g = 1; |
kv09 | 6:402e96a482a9 | 482 | b = 1; |
kv09 | 6:402e96a482a9 | 483 | light++; |
kv09 | 6:402e96a482a9 | 484 | // Diminue l'intensité de la musique |
kv09 | 6:402e96a482a9 | 485 | zz = zz - 0.05; |
kv09 | 6:402e96a482a9 | 486 | } |
dreschpe | 0:f6a57b843f79 | 487 | } |
kv09 | 6:402e96a482a9 | 488 | |
kv09 | 6:402e96a482a9 | 489 | // Fonction de fin |
kv09 | 6:402e96a482a9 | 490 | void fin() { |
kv09 | 6:402e96a482a9 | 491 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 492 | int des = 9; |
kv09 | 6:402e96a482a9 | 493 | r = 1; |
kv09 | 6:402e96a482a9 | 494 | b = 1; |
kv09 | 6:402e96a482a9 | 495 | g = 1; |
kv09 | 6:402e96a482a9 | 496 | while (des - 8 < 19) { |
kv09 | 6:402e96a482a9 | 497 | // Affiche le générique |
kv09 | 6:402e96a482a9 | 498 | lcd.locate(25, des - 4); |
kv09 | 6:402e96a482a9 | 499 | lcd.printf("Realise par Mael"); |
kv09 | 6:402e96a482a9 | 500 | // Musique de fin |
kv09 | 6:402e96a482a9 | 501 | switch (des % 9) { |
kv09 | 6:402e96a482a9 | 502 | case 0: |
kv09 | 6:402e96a482a9 | 503 | spkr.PlayNote(220.00, 0.5, 0.8); |
kv09 | 6:402e96a482a9 | 504 | r = 0; |
kv09 | 6:402e96a482a9 | 505 | b = 1; |
kv09 | 6:402e96a482a9 | 506 | g = 1; |
kv09 | 6:402e96a482a9 | 507 | wait(0.250); |
kv09 | 6:402e96a482a9 | 508 | break; |
kv09 | 6:402e96a482a9 | 509 | case 1: |
kv09 | 6:402e96a482a9 | 510 | spkr.PlayNote(220.00, 0.5, 0.8); |
kv09 | 6:402e96a482a9 | 511 | r = 1; |
kv09 | 6:402e96a482a9 | 512 | b = 1; |
kv09 | 6:402e96a482a9 | 513 | g = 1; |
kv09 | 6:402e96a482a9 | 514 | wait(0.250); |
kv09 | 6:402e96a482a9 | 515 | break; |
kv09 | 6:402e96a482a9 | 516 | case 2: |
kv09 | 6:402e96a482a9 | 517 | spkr.PlayNote(220.00, 0.5, 0.8); |
kv09 | 6:402e96a482a9 | 518 | r = 0; |
kv09 | 6:402e96a482a9 | 519 | b = 1; |
kv09 | 6:402e96a482a9 | 520 | g = 1; |
kv09 | 6:402e96a482a9 | 521 | wait(0.250); |
kv09 | 6:402e96a482a9 | 522 | break; |
kv09 | 6:402e96a482a9 | 523 | case 3: |
kv09 | 6:402e96a482a9 | 524 | spkr.PlayNote(174.61, 0.375, 0.8); |
kv09 | 6:402e96a482a9 | 525 | r = 1; |
kv09 | 6:402e96a482a9 | 526 | b = 1; |
kv09 | 6:402e96a482a9 | 527 | g = 1; |
kv09 | 6:402e96a482a9 | 528 | wait(0.187); |
kv09 | 6:402e96a482a9 | 529 | break; |
kv09 | 6:402e96a482a9 | 530 | case 4: |
kv09 | 6:402e96a482a9 | 531 | spkr.PlayNote(261.63, 0.125, 0.8); |
kv09 | 6:402e96a482a9 | 532 | r = 0; |
kv09 | 6:402e96a482a9 | 533 | b = 1; |
kv09 | 6:402e96a482a9 | 534 | g = 1; |
kv09 | 6:402e96a482a9 | 535 | wait(0.062); |
kv09 | 6:402e96a482a9 | 536 | break; |
kv09 | 6:402e96a482a9 | 537 | case 5: |
kv09 | 6:402e96a482a9 | 538 | spkr.PlayNote(220.00, 0.5, 0.8); |
kv09 | 6:402e96a482a9 | 539 | r = 1; |
kv09 | 6:402e96a482a9 | 540 | b = 1; |
kv09 | 6:402e96a482a9 | 541 | g = 1; |
kv09 | 6:402e96a482a9 | 542 | wait(0.250); |
kv09 | 6:402e96a482a9 | 543 | break; |
kv09 | 6:402e96a482a9 | 544 | case 6: |
kv09 | 6:402e96a482a9 | 545 | spkr.PlayNote(174.61, 0.375, 0.8); |
kv09 | 6:402e96a482a9 | 546 | r = 0; |
kv09 | 6:402e96a482a9 | 547 | b = 1; |
kv09 | 6:402e96a482a9 | 548 | g = 1; |
kv09 | 6:402e96a482a9 | 549 | wait(0.187); |
kv09 | 6:402e96a482a9 | 550 | break; |
kv09 | 6:402e96a482a9 | 551 | case 7: |
kv09 | 6:402e96a482a9 | 552 | spkr.PlayNote(261.63, 0.125, 0.8); |
kv09 | 6:402e96a482a9 | 553 | r = 1; |
kv09 | 6:402e96a482a9 | 554 | b = 1; |
kv09 | 6:402e96a482a9 | 555 | g = 1; |
kv09 | 6:402e96a482a9 | 556 | wait(0.062); |
kv09 | 6:402e96a482a9 | 557 | break; |
kv09 | 6:402e96a482a9 | 558 | case 8: |
kv09 | 6:402e96a482a9 | 559 | spkr.PlayNote(220.00, 0.7, 0.8); |
kv09 | 6:402e96a482a9 | 560 | r = 0; |
kv09 | 6:402e96a482a9 | 561 | b = 1; |
kv09 | 6:402e96a482a9 | 562 | g = 1; |
kv09 | 6:402e96a482a9 | 563 | wait(0.7); |
kv09 | 6:402e96a482a9 | 564 | break; |
kv09 | 6:402e96a482a9 | 565 | default: |
kv09 | 6:402e96a482a9 | 566 | r = 1; |
kv09 | 6:402e96a482a9 | 567 | b = 1; |
kv09 | 6:402e96a482a9 | 568 | g = 1; |
kv09 | 6:402e96a482a9 | 569 | } |
kv09 | 6:402e96a482a9 | 570 | des++; |
kv09 | 6:402e96a482a9 | 571 | } |
kv09 | 6:402e96a482a9 | 572 | lcd.cls(); |
kv09 | 6:402e96a482a9 | 573 | r = 1; |
kv09 | 6:402e96a482a9 | 574 | b = 1; |
kv09 | 6:402e96a482a9 | 575 | g = 1; |
kv09 | 6:402e96a482a9 | 576 | } |
kv09 | 6:402e96a482a9 | 577 | |
kv09 | 6:402e96a482a9 | 578 | // Fonction pour effacer une zone sur l'écran |
kv09 | 6:402e96a482a9 | 579 | void efface() { |
kv09 | 6:402e96a482a9 | 580 | //bar 1 |
kv09 | 6:402e96a482a9 | 581 | lcd.locate(35, verticalJ1); |
kv09 | 6:402e96a482a9 | 582 | lcd.printf(" "); |
kv09 | 6:402e96a482a9 | 583 | |
kv09 | 6:402e96a482a9 | 584 | // bar 2 |
kv09 | 6:402e96a482a9 | 585 | lcd.locate(120, verticalJ2); |
kv09 | 6:402e96a482a9 | 586 | lcd.printf(" "); |
kv09 | 6:402e96a482a9 | 587 | |
kv09 | 6:402e96a482a9 | 588 | //balle |
kv09 | 6:402e96a482a9 | 589 | lcd.locate(horizontalBall - 3, verticalBall); |
kv09 | 6:402e96a482a9 | 590 | lcd.printf(" "); |
kv09 | 6:402e96a482a9 | 591 | lcd.locate(horizontalBall - 3, verticalBall - 1); |
kv09 | 6:402e96a482a9 | 592 | lcd.printf(" "); |
kv09 | 6:402e96a482a9 | 593 | lcd.locate(horizontalBall - 3, verticalBall + 1); |
kv09 | 6:402e96a482a9 | 594 | lcd.printf(" "); |
kv09 | 6:402e96a482a9 | 595 | } |
kv09 | 6:402e96a482a9 | 596 | |
kv09 | 6:402e96a482a9 | 597 | // Fonction de main |
kv09 | 6:402e96a482a9 | 598 | int main() { |
kv09 | 6:402e96a482a9 | 599 | lancement(); |
kv09 | 6:402e96a482a9 | 600 | // Boucle pour jouer un match |
kv09 | 6:402e96a482a9 | 601 | while (lance) { |
kv09 | 6:402e96a482a9 | 602 | commencement(); |
kv09 | 6:402e96a482a9 | 603 | // Boucle pour jouer une partie |
kv09 | 6:402e96a482a9 | 604 | while (partie) { |
kv09 | 6:402e96a482a9 | 605 | initialisation(); |
kv09 | 6:402e96a482a9 | 606 | // Boucle pour jouer une manche |
kv09 | 6:402e96a482a9 | 607 | while (joue) { |
kv09 | 6:402e96a482a9 | 608 | affichage(); |
kv09 | 6:402e96a482a9 | 609 | |
kv09 | 6:402e96a482a9 | 610 | marque(); |
kv09 | 6:402e96a482a9 | 611 | |
kv09 | 6:402e96a482a9 | 612 | pause(); |
kv09 | 6:402e96a482a9 | 613 | |
kv09 | 6:402e96a482a9 | 614 | mouvement_balle(avance, recule); |
kv09 | 6:402e96a482a9 | 615 | |
kv09 | 6:402e96a482a9 | 616 | victoire(); |
kv09 | 6:402e96a482a9 | 617 | |
kv09 | 6:402e96a482a9 | 618 | wait(vitesse); |
kv09 | 6:402e96a482a9 | 619 | |
kv09 | 6:402e96a482a9 | 620 | efface(); |
kv09 | 6:402e96a482a9 | 621 | } |
kv09 | 6:402e96a482a9 | 622 | wait(1); |
kv09 | 6:402e96a482a9 | 623 | } |
kv09 | 6:402e96a482a9 | 624 | rejouer(); |
kv09 | 6:402e96a482a9 | 625 | } |
kv09 | 6:402e96a482a9 | 626 | recapitulatif(); |
kv09 | 6:402e96a482a9 | 627 | fin(); |
kv09 | 6:402e96a482a9 | 628 | } |