Skip to content
Extraits de code Groupes Projets
Valider 036c7878 rédigé par Quentin LECOMTE's avatar Quentin LECOMTE
Parcourir les fichiers

Delete boucle_de_jeu2508635812604719863.autosave

parent 19b678fe
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import processing.sound.*;
Context context;
SensorManager manager;
Sensor sensorA;
Sensor sensorG;
AccelerometerListener listenerA;
float ax, ay, az;
SoundFile droite1, droite2, droite3, gauche1, gauche2, gauche3, neutre1, neutre2, neutre3;
SoundFile epee1, epee2, epee3, woosh, clang1, clang2, clang3;
SoundFile cri1, cri2;
SoundFile bgMusic, mort, aie;
KalmanFilter kf = new KalmanFilter();
float timer;
float cooldown_listener;
float seuil;
int vie;
int score;
int mode;
int combo;
int max_combo;
float cooldown_ennemy;
float init_cooldown;
float reaction;
float init_reaction;
int ennemy_move; //0 : droite, 1 : gauche, 2 : neutre.
void setup() {
//capteurs
context = getActivity();
manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
sensorA = manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorG = manager.getDefaultSensor(Sensor.TYPE_GRAVITY);
listenerA = new AccelerometerListener();
manager.registerListener(listenerA, sensorA, SensorManager.SENSOR_DELAY_GAME);
seuil = 4;
cooldown_listener = 0.5;
//jeu
init_cooldown = 3;
cooldown_ennemy = init_cooldown;
init_reaction = 3;
reaction = init_reaction;
mode = 1;
vie = 3;
score = 0;
combo = 0;
ennemy_move = -1;
frameRate(60);
//sons
bgMusic = new SoundFile(this, "bgmusic.wav");
bgMusic.loop();
droite1 = new SoundFile(this, "droite1.wav");
droite2 = new SoundFile(this, "droite2.wav");
droite3 = new SoundFile(this, "droite3.wav");
gauche1 = new SoundFile(this, "gauche1.wav");
gauche2 = new SoundFile(this, "gauche2.wav");
gauche3 = new SoundFile(this, "gauche3.wav");
neutre1 = new SoundFile(this, "neutre1.wav");
neutre2 = new SoundFile(this, "neutre2.wav");
neutre3 = new SoundFile(this, "neutre3.wav");
epee1 = new SoundFile(this, "sching.wav");
epee2 = new SoundFile(this, "schyang.wav");
epee3 = new SoundFile(this, "sling.wav");
woosh = new SoundFile(this, "woosh.mp3");
clang1 = new SoundFile(this, "bangbang.mp3");
clang2 = new SoundFile(this, "pafpaf.mp3");
clang3 = new SoundFile(this, "bonk.mp3");
aie = new SoundFile(this, "aie.wav");
mort = new SoundFile(this, "mort.wav");
cri1 = new SoundFile(this, "cri.mp3");
cri2 = new SoundFile(this, "rale.wav");
textFont(createFont("SansSerif", 30 * displayDensity));
}
void draw() {
background(0);
text("X: " + ax + "\nY: " + ay + "\nZ: " + az, 0, 0, width, height);
if (mode == 0) {
background(255);
//menu
}else if (mode == 1) {
//dessiner les courbes de Kiwi
background(255, 255, 0);
text("X: " + ax + "\nY: " + ay + "\nZ: " + az, 0, 0, width, height);
//choix de la direction + son
if ((reaction == init_reaction || ennemy_move == -1) && cooldown_ennemy <= 0){
ennemy_move = int(random(3));
int sound = int(random(4));
reaction -= 0.017;
switch (ennemy_move){
case 0 :
switch (sound) {
case 0 : droite1.play(); break;
case 1 : droite2.play(); break;
case 2 : droite3.play(); break;
case 3 : epee1.pan(1.0); epee1.play(); break;
}
break;
case 1 :
switch (sound) {
case 0 : gauche1.play(); break;
case 1 : gauche2.play(); break;
case 2 : gauche3.play(); break;
case 3 : epee2.pan(-1.0); epee2.play(); break;
}
break;
case 2 :
switch (sound) {
case 0 : neutre1.play(); break;
case 1 : neutre2.play(); break;
case 2 : neutre3.play(); break;
case 3 : epee3.pan(0.0); epee3.play(); break;
}
break;
}
}else if (reaction >= 0 && ennemy_move != -1){
background(255, 0, 255);
text("\nennemy : " + ennemy_move + "\nreaction : " + reaction, 0, 0, width, height);
//gauche
if (az > seuil && ennemy_move == 1){
score += 1;
combo += 1;
reaction = init_reaction;
ennemy_move = -1;
cooldown_ennemy = init_cooldown;
int sound = int(random(4));
switch (sound) {
case 0 : woosh.play(); break;
case 1 : clang2.play(); break;
case 2 : clang3.play(); break;
case 3 : cri2.play(); break;
}
}
//droite
if (az < -seuil && ennemy_move == 0){
score += 1;
combo += 1;
reaction = init_reaction;
ennemy_move = -1;
cooldown_ennemy = init_cooldown;
int sound = int(random(4));
switch (sound) {
case 0 : woosh.play(); break;
case 1 : woosh.play(); break;
case 2 : clang3.play(); break;
case 3 : cri1.play(); break;
}
}
//neutre
if ((ax < -seuil || ax > seuil) && ennemy_move == 2){
score += 1;
combo += 1;
reaction = init_reaction;
ennemy_move = -1;
cooldown_ennemy = init_cooldown;
int sound = int(random(4));
switch (sound) {
case 0 : woosh.play(); break;
case 1 : clang1.play(); break;
case 2 : clang3.play(); break;
case 3 : woosh.play(); break;
}
}
reaction -= 0.017;
}else if (reaction < 0 && ennemy_move != -1) {
background(0);
vie -= 1;
combo = 0;
if (vie > 0) aie.play();
//animation sur les coeurs
if (vie <= 0){
mode = 2;
}
reaction = init_reaction;
cooldown_ennemy = init_cooldown;
ennemy_move = -1;
}else {
background(0, 255, 0);
text("Vies : " + vie + "\nCombo :" + combo + "\nMax Combo :" + max_combo, 0, 0, width, height);
cooldown_ennemy -= 0.017;
}
}else if (mode == 2){
//mort
mort.play();
mode = 0;
}else if (mode == 3){
//explications
mode = 0;
}
}
public void onResume() {
super.onResume();
if (manager != null) {
manager.registerListener(listenerA, sensorA, SensorManager.SENSOR_DELAY_GAME);
}
}
public void onPause() {
super.onPause();
if (manager != null) {
manager.unregisterListener(listenerA);
}
}
class AccelerometerListener implements SensorEventListener {
public void onSensorChanged(SensorEvent event) {
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
ax = kf.predict_and_correct(ax);
ay = kf.predict_and_correct(ay);
az = kf.predict_and_correct(az);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
class KalmanFilter {
float q = 1.0; // process variance
float r = 2.0; // estimate of measurement variance, change to see effect
float xhat = 0.0; // a posteriori estimate of x
float xhatminus; // a priori estimate of x
float p = 1.0; // a posteriori error estimate
float pminus; // a priori error estimate
float kG = 0.0; // kalman gain
KalmanFilter() {};
KalmanFilter(float q, float r) {
q(q);
r(r);
}
void q(float q) {
this.q = q;
}
void r(float r) {
this.r = r;
}
float xhat() {
return this.xhat;
}
void predict() {
xhatminus = xhat;
pminus = p + q;
}
float correct(float x) {
kG = pminus / (pminus + r);
xhat = xhatminus + kG * (x - xhatminus);
p = (1 - kG) * pminus;
return xhat;
}
float predict_and_correct(float x) {
predict();
return correct(x);
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter