Optimisation (lenteur)

J00k4 - 14 févr. 2017 à 15:44
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 - 16 févr. 2017 à 12:52
Bonjour,
je voudrais vous demandez conseil au près de vous pour mon programme.
Je suis un élevé de premier année et j'ai fais un programme de jeu sous windows form et je constate un ralentisement lors de mes verification en l'air et j'aimerais savoir le problemes et comment le résoudre pour que mon jeux devienne plus fluide merci de votre compréhension mes salutation les plus sincères J00k4 <3

Le code
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LAsSautSix
{
public partial class Fond : Form
{
// variable public
PictureBox[] PBEntite = new PictureBox[16];
PictureBox[] pNPiege = new PictureBox[7];
bool ToucheGauche = false;
bool ToucheDroite = false;
bool ToucheSaut = false;
int Vitesse = 0;
const int Vitt = 10;
bool bInPossibleG = false;
bool bInPossibleD = false;
// variable public
public Fond()
{
InitializeComponent();
InitializeMap1();
}

private void InitializeMap1()
{
// Placement des blocks
PBEntite[1] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =700 ,
Height =70 ,
Left =130 ,
Top =885 ,
};
Controls.Add(PBEntite[1]);
//---------------------------------------------
PBEntite[2] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =70 ,
Height =70 ,
Left =350 ,
Top =480 ,
= false
};
Controls.Add(PBEntite[2]);
//---------------------------------------------
PBEntite[3] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width = 70,
Height = 70,
Left = 500,
Top = 675,
= false
};
Controls.Add(PBEntite[3]);
//---------------------------------------------
PBEntite[4] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width = 70,
Height = 280,
Left = 580,
Top = -70
};
Controls.Add(PBEntite[4]);
//---------------------------------------------
PBEntite[5] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width = 70,
Height = 210,
Left = 580,
Top = 450,
= false
};
Controls.Add(PBEntite[5]);
//---------------------------------------------
PBEntite[6] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =70 ,
Height =210 ,
Left =850 ,
Top = 185,
};
Controls.Add(PBEntite[6]);
//---------------------------------------------
PBEntite[7] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =560 ,
Height =70 ,
Left =850 ,
Top =325 ,
};
Controls.Add(PBEntite[7]);
//---------------------------------------------
PBEntite[8] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =70 ,
Height =280 ,
Left =1340 ,
Top =325 ,
};
Controls.Add(PBEntite[8]);
//---------------------------------------------
PBEntite[9] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width = 70,
Height = 420,
Left = 1340,
Top = 535,
= false
};
Controls.Add(PBEntite[9]);
//---------------------------------------------
PBEntite[10] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =350 ,
Height =70 ,
Left =1060 ,
Top =885 ,
};
Controls.Add(PBEntite[10]);
//---------------------------------------------
PBEntite[11] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width = 280,
Height = 70,
Left = 1700,
Top = 885,
};
Controls.Add(PBEntite[11]);
//---------------------------------------------
PBEntite[12] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width = 70,
Height = 70,
Left = 1840,
Top = 745,
};
Controls.Add(PBEntite[12]);
//---------------------------------------------
PBEntite[13] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =70 ,
Height =70 ,
Left = 1630,
Top = 605,
};
Controls.Add(PBEntite[13]);
//---------------------------------------------
PBEntite[14] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =70 ,
Height =70 ,
Left =1490 ,
Top =465 ,
};
Controls.Add(PBEntite[14]);
//---------------------------------------------
PBEntite[0] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width = 280,
Height = 140,
Left = 1700,
Top = 255,
};
Controls.Add(PBEntite[0]);
//---------------------------------------------
PBEntite[15] = new PictureBox
{
ImageLocation = (@"Image\Block.png"),
Width =350 ,
Height =70 ,
Left =0 ,
Top =1085 ,
};
Controls.Add(PBEntite[15]);

//
//---------------------------------------------
pNPiege[0] = new PictureBox
{
ImageLocation = (@"Image\PIC.png"),
BackColor = Color.Transparent,
Width = 172,
Height = 70,
Left = 500,
Top = 815,
};
Controls.Add(pNPiege[0]);
//---------------------------------------------
pNPiege[1] = new PictureBox
{
ImageLocation = (@"Image\PIC_W.png"),
BackColor = Color.Transparent,
Width = 70,
Height =207 ,
Left =510 ,
Top =0 ,
};
Controls.Add(pNPiege[1]);
//---------------------------------------------
pNPiege[2] = new PictureBox
{
ImageLocation = (@"Image\PIC_W.png"),
BackColor = Color.Transparent,
Width = 70,
Height = 207,
Left = 780,
Top = 186,
};
Controls.Add(pNPiege[2]);
//---------------------------------------------
pNPiege[3] = new PictureBox
{
ImageLocation = (@"Image\PIC.png"),
BackColor = Color.Transparent,
Width = 210,
Height = 70,
Left = 1130,
Top = 815,
};
Controls.Add(pNPiege[3]);
//---------------------------------------------
pNPiege[4] = new PictureBox
{
ImageLocation = (@"Image\PIC_E.png"),
BackColor = Color.Transparent,
Width = 70,
Height = 620,
Left = 1410,
Top = 326,
};
Controls.Add(pNPiege[4]);
//---------------------------------------------
pNPiege[5] = new PictureBox
{
ImageLocation = (@"Image\PIC.png"),
BackColor = Color.Transparent,
Width = 210,
Height = 70,
Left = 1770,
Top = 815,
};
Controls.Add(pNPiege[5]);
//---------------------------------------------
pNPiege[6] = new PictureBox
{
ImageLocation = (@"Image\PIC.png"),
BackColor = Color.Transparent,
Width =210 ,
Height =70 ,
Left =920 ,
Top =255 ,
};
Controls.Add(pNPiege[6]);
for (int i = 0; i < pNPiege.Length; i++)
{
pNPiege[i].Visible = false;
}

}
// Touche pressé
private void Fond_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
ToucheSaut = true;
break;
case Keys.Left:
ToucheGauche = true;
break;
case Keys.Right:
ToucheDroite = true;
break;
}
}
// Fin Touche pressé
// Touche relevé
private void Fond_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
ToucheSaut = false;
break;
case Keys.Left:
ToucheGauche = false;
break;
case Keys.Right:
ToucheDroite = false;
break;
}
}
// Fin touche relevé
private void VraiTimer_Tick(object sender, EventArgs e)
{
Gravite();
Deplacement();
}

// Action des déplacement gauche et droite
public void Deplacement()
{
bInPossibleD = false;
bInPossibleG = false;
for(int i = 0; i < PBEntite.Length; i++)
{
if (ColisionDroite(PBEntite[i]))
{
bInPossibleD = true;
}
if (ColisionGauche(PBEntite[i]))
{
bInPossibleG = true;
}
}
}
// Fin de l'action des déplacement gauche et droite
//xn--dplacement-b7a de l'axe verticale
public void Gravite()
{
bool sautPossible = false;
if (Vitesse > 0)
{
for(int i = 0; i < PBEntite.Length; i++)
{
if (Descendant(PBEntite[i]))
{
pbPlayer.Top = PBEntite[i].Top - pbPlayer.Height;
Vitesse = 0;
sautPossible = true;
}
}
}
if (Vitesse < 0)
{
for (int i = 0; i < PBEntite.Length; i++)
{
if (Montant(PBEntite[i]))
{
pbPlayer.Top = PBEntite[i].Top + PBEntite[i].Height;
Vitesse = 0;
}
}
}
// Vérification si saut effectif
if (ToucheSaut == true && Vitesse == 0 && sautPossible == true)
{
Vitesse = -pbPlayer.Height / 6;
pbPlayer.Top -= 5;
}
// Fin de la vérification si saut effectif
pbPlayer.Top += Vitesse;
if (Vitesse + 1 <= 25)
{
Vitesse++;
}
//====================================================

if (bInPossibleD != true && ToucheDroite == true && ToucheGauche == false)
{
pbPlayer.Left += Vitt;
}
if (bInPossibleG != true && ToucheDroite == false && ToucheGauche == true)
{
pbPlayer.Left -= Vitt;
}
}
// Fin Déplacement de l'axe verticale

// Vérification des collisions avec Le déplacement droite
public bool ColisionDroite(PictureBox Brique)
{
if (pbPlayer.Top < Brique.Top + Brique.Height && pbPlayer.Top + pbPlayer.Height > Brique.Top)
{
if (pbPlayer.Left + Vitt < Brique.Left + Brique.Width && pbPlayer.Left + pbPlayer.Width + Vitt > Brique.Left)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// Fin des vérification des collisions avec Le déplacement droite
// Vérification des collisions avec Le déplacement gauche
public bool ColisionGauche(PictureBox Brique)
{
if (pbPlayer.Top < Brique.Top + Brique.Height && pbPlayer.Top + pbPlayer.Height > Brique.Top)
{
if (pbPlayer.Left - Vitt < Brique.Left + Brique.Width && pbPlayer.Left + pbPlayer.Width - Vitt > Brique.Left)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// Fin des vérification des collisions avec Le déplacement gauche

// Vérification des colision avec des block
public bool Descendant(PictureBox Block)
{
if (pbPlayer.Left < Block.Left + Block.Width && pbPlayer.Left + pbPlayer.Width > Block.Left)
{
if (pbPlayer.Top + pbPlayer.Height + Vitesse > Block.Top && pbPlayer.Top + Vitesse < Block.Top + Block.Height)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// Fin des Vérifications des collisions avec des blocks
public bool Montant(PictureBox Block)
{
if (pbPlayer.Left < Block.Left + Block.Width && pbPlayer.Left + pbPlayer.Width > Block.Left)
{
if (pbPlayer.Top + Vitesse < Block.Top + Block.Height && pbPlayer.Top + pbPlayer.Height + Vitesse > Block.Top)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}

5 réponses

Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
Modifié par Whismeril le 14/02/2017 à 20:49
Bonsoir

poster ton code, c'est bien, mais il faudrait le copier correctement.
Il y a plein de fois des trucs comme ça
//---------------------------------------------
            PBEntite[2] = new PictureBox
            {
                ImageLocation = (@"Image\Block.png"),
                Width =70 ,
                Height =70 ,
                Left =350 ,
                Top =480 ,
                 = false//sauf que là y'a rien à quoi affecter false.
            };         



Et tu utilises plein de fois pbPlayer qui n'est pas défini dans ce code.
Ça va être compliqué de t'aider avec un code ou il y a 48 erreurs.
Quand j'étais petit, la mer Morte n'était que malade.
George Burns
0
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
14 févr. 2017 à 21:45
Sans toujours pouvoir répondre à ta question.

Il y a un principe en programmation, on n'écrit pas 15 fois le même code, en effet, s'il y a une erreur, ça fera 15 corrections.
Donc on essaye de regrouper, de factoriser.

Par exemple initialiser ton tableau de block peut se faire avec une boucle
          int[] lesWidths = {280,700,70,70,70,70,70,560,70,70,350,280,70,70,70,350};
            for (int i = 0; i < PBEntite.Length; i++)
            {    
                PBEntite[i] = new PictureBox
                {
                    ImageLocation = (@"Image\Block.png"),
                    Width =lesWidths[i],
                    Height = 140,//xn--crer-cpa le tableau des Heigts
                    Left = 1700,//xn--crer-cpa le tableau des les lefts
                    Top = 255,// créer le tableau des Top
                    //xn--crer-cpa le tableau des trucs qui peuvent être false
                };
                Controls.Add(PBEntite[0]);
            }

0
Bonjour merci de vouloir m'aider mais enfaîte le bout de code fonctionne sous windows form et le problèmes c'est que je ne peux pas link le projet avec le design pour que vous puisez m'aidez c'est la le problèmes que je rencontre
le problèmes viens pas de l’initialisation des tableau avec les tailles mais je prends note pour améliorer mon codes merci bien si vous réussissez a m'aider dans ma quêtes

Je vous remercie d'avance J00k4 et passé une bonne journée
si vous voulez avoir le sln je vous mets le lien pour l'avoir
https://www.dropbox.com/s/i7fi7ls4y1zz2ux/LAsSautSix.zip?dl=0
0
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
Modifié par Whismeril le 15/02/2017 à 13:28
Sans avoir à fournir tout le projet, il suffit que le code que tu postes "fonctionne" donc que la variable soit définie qui à mettre un commentaire que c'est juste pour le forum et surtout
= false
Il faut quelquechose devant le égal....

EDIT: je jetterai un œil à ton projet ce soir si j'ai le temps, mais ce serait mieux d'avoir un code testable directement sur le forum
0
Bonjour je voulais vous dire après cette petite absence que mon copier/coller a bugé et des bouts de code ne sont pas apparue alors j'ai profité de corriger mon code et de le simplifié comme si dessus
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LAsSautSix
{
public partial class Fond : Form
{
// variable public
PictureBox[] PBEntite = new PictureBox[16];
PictureBox[] pNPiege = new PictureBox[7];
int[] LesWidth = new int[] { 700, 70, 70, 70, 70, 70, 560, 70, 70, 350, 280, 70, 70, 70, 280, 350 };
int[] LesHeight = new int[] { 70, 70, 70, 280, 210, 210, 70, 280, 420, 70, 70, 70, 70, 70, 140,70 };
int[] LesLeft = new int[] { 130, 350, 500, 580, 580, 850, 850, 1340, 1340, 1060, 1700, 1840, 1630, 1490, 1700, 0 };
int[] LesTop = new int[] { 885, 480, 675, -70, 450, 185, 325, 325, 535, 885, 885, 745, 605, 465, 255, 1085 };
int[] LesWidthPieges = new int[] { 172, 70, 70, 210, 70, 210, 210 };
int[] lesHeightPiges = new int[] { 70, 207, 207, 70, 620, 70, 70 };
int[] LesLeftPieges = new int[] { 500, 510, 780, 1130, 1410, 1770, 1920 };
int[] LesTopPieges = new int[] { 815, 0, 186, 815, 326, 815, 255};
string[] LesString = new string[] { "N", "W", "W", "N", "E", "N", "N" };
bool ToucheGauche = false;
bool ToucheDroite = false;
bool ToucheSaut = false;
int Vitesse = 0;
const int Vitt = 10;
bool bInPossibleG = false;
bool bInPossibleD = false;
// variable public
public Fond()
{
InitializeComponent();
InitializeMap1();
}

private void InitializeMap1()
{
for (int i = 0; i <PBEntite.Length;i++)
{
PBEntite[i] = new PictureBox
{
Width = LesWidth[i],
Height = LesHeight[i],
Left = LesLeft[i],
Top = LesTop[i],
ImageLocation = (@"Image\Block.png"),
};
Controls.Add(PBEntite[i]);

}

for(int i = 0; i < pNPiege.Length; i++)
{
pNPiege[i] = new PictureBox
{
Width = LesWidthPieges[i],
Height = lesHeightPiges[i],
BackColor = Color.Transparent,
Top = LesTopPieges[i],
Left = LesLeftPieges[i],
ImageLocation = string.Format(@"Image\PIC_{0}.png", LesString[i]),
// mets en invisible les pieges
// Visible = false,

};
Controls.Add(pNPiege[i]);
};



}
// Touche pressé
private void Fond_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
ToucheSaut = true;
break;
case Keys.Left:
ToucheGauche = true;
break;
case Keys.Right:
ToucheDroite = true;
break;
}
}
// Fin Touche pressé
// Touche relevé
private void Fond_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
ToucheSaut = false;
break;
case Keys.Left:
ToucheGauche = false;
break;
case Keys.Right:
ToucheDroite = false;
break;
}
}
// Fin touche relevé
private void VraiTimer_Tick(object sender, EventArgs e)
{
Gravite();
Deplacement();
}

// Action des déplacement gauche et droite
public void Deplacement()
{
bInPossibleD = false;
bInPossibleG = false;
for(int i = 0; i < PBEntite.Length; i++)
{
if (ColisionDroite(PBEntite[i]))
{
bInPossibleD = true;
}
if (ColisionGauche(PBEntite[i]))
{
bInPossibleG = true;
}
}
}
// Fin de l'action des déplacement gauche et droite
//xn--dplacement-b7a de l'axe verticale
public void Gravite()
{
bool sautPossible = false;
if (Vitesse > 0)
{
for(int i = 0; i < PBEntite.Length; i++)
{
if (Descendant(PBEntite[i]))
{
pbPlayer.Top = PBEntite[i].Top - pbPlayer.Height;
Vitesse = 0;
sautPossible = true;
}
}
}
if (Vitesse < 0)
{
for (int i = 0; i < PBEntite.Length; i++)
{
if (Montant(PBEntite[i]))
{
pbPlayer.Top = PBEntite[i].Top + PBEntite[i].Height;
Vitesse = 0;
}
}
}
// Vérification si saut effectif
if (ToucheSaut == true && Vitesse == 0 && sautPossible == true)
{
Vitesse = -pbPlayer.Height / 6;
pbPlayer.Top -= 5;
}
// Fin de la vérification si saut effectif
pbPlayer.Top += Vitesse;
if (Vitesse + 1 <= 25)
{
Vitesse++;
}
//====================================================

if (bInPossibleD != true && ToucheDroite == true && ToucheGauche == false)
{
pbPlayer.Left += Vitt;
}
if (bInPossibleG != true && ToucheDroite == false && ToucheGauche == true)
{
pbPlayer.Left -= Vitt;
}
}
// Fin Déplacement de l'axe verticale

// Vérification des collisions avec Le déplacement droite
public bool ColisionDroite(PictureBox Brique)
{
if (pbPlayer.Top < Brique.Top + Brique.Height && pbPlayer.Top + pbPlayer.Height > Brique.Top && pbPlayer.Left + Vitt < Brique.Left + Brique.Width && pbPlayer.Left + pbPlayer.Width + Vitt > Brique.Left)
{
return true;
}
else
{
return false;
}
}
// Fin des vérification des collisions avec Le déplacement droite
// Vérification des collisions avec Le déplacement gauche
public bool ColisionGauche(PictureBox Brique)
{
if (pbPlayer.Top < Brique.Top + Brique.Height && pbPlayer.Top + pbPlayer.Height > Brique.Top && pbPlayer.Left - Vitt < Brique.Left + Brique.Width && pbPlayer.Left + pbPlayer.Width - Vitt > Brique.Left)
{
return true;
}
else
{
return false;
}
}
// Fin des vérification des collisions avec Le déplacement gauche

// Vérification des colision avec des block
public bool Descendant(PictureBox Block)
{
if (pbPlayer.Left < Block.Left + Block.Width && pbPlayer.Left + pbPlayer.Width > Block.Left)
{
if (pbPlayer.Top + pbPlayer.Height + Vitesse > Block.Top && pbPlayer.Top + Vitesse < Block.Top + Block.Height)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// Fin des Vérifications des collisions avec des blocks
public bool Montant(PictureBox Block)
{
if (pbPlayer.Left < Block.Left + Block.Width && pbPlayer.Left + pbPlayer.Width > Block.Left)
{
if (pbPlayer.Top + Vitesse < Block.Top + Block.Height && pbPlayer.Top + pbPlayer.Height + Vitesse > Block.Top)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}

je m'excuse de l'attente mais je veux m'excusez de ne pas avoir compris votre question directe et aussi de pas avoir vérifie désolé sincèrement
0
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
15 févr. 2017 à 20:45
pbPlayer n'existe toujours pas.....
0
J00k4 > Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024
16 févr. 2017 à 08:09
pbplayer = picture box dans le windows form
0
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656 > J00k4
16 févr. 2017 à 12:52
Oui j'ai bien vu dans ton projet complet.
Mais quitte à reposter un code, avec la variable cela aurait été mieux...
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
15 févr. 2017 à 21:37
Bon
en regardant ton projet complet, le fait est que quand tu "marches" sur des briques, tu cherches les collisions à droite ou à gauche (en fonction de ton sens de déplacement) avec tous les obstacles existants.
Quand tu sautes, tu cherches les collisions à droite ou à gauche, puis tu cherches les collisions dessous ou dessus, puis tu vérifies si tu sautes et tu changes la vitesse (je suppose pour simuler la parabole du saut) et tu reviens au résultat des collisions droite / gauche pour modifier l'autre vitesse, et tout ça avec tous les obstacles existants.

Je pense que pour gagner du temps tu dois trouver un moyen de limiter ta recherche de collision aux obstacles immédiatement à proximité.

Par exemple, au lieu d'utiliser plusieurs tableau, tu en fais un seul à 2 dimension, qui va correspondre à ton "damier".
Tu écris une classe qui contient un picturebox configuré comme il faut, et qui définit si c'est un block, un pic N, un pic S etc...
Tu places les instances de cette classe dans le tableau en fonction de leur coordonnées sur l'écran, tu y mets aussi ton personnage.
Quand tu cherches des collisions, tu te limites au cellules du tableau qui sont autour de toi.

0
Rejoignez-nous