1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #include "perso.hpp
#define _DEPLACEMENT_LENT_ 3
#define _RIGHT_ true
#define _LEFT_ false
Personnage::Personnage(int xDeBase, int yDeBase, bool direction) : QWidget()
{
/* On initialise les positions de base du personnage */
m_x = xDeBase;
m_y = yDeBase;
/* on initialise la direction */
m_direction = direction;
/* On charge tous les elements dont on a besoin. */
Loading();
/* On affiche l'animation de début suivant la direction. */
if (m_direction == _RIGHT_) m_labelMovie->setMovie(m_animStanceNormal_R);
else if (m_direction == _LEFT_) m_labelMovie->setMovie(m_animStanceNormal_L);
/* On affiche l'animation aux coordonnées initiales. */
m_labelMovie->move(m_x, m_y);
}
void Personnage::Loading(void)
{
/* On crée le label et on inclu dedans les QMovie qui vont etre affichees dedans. */
m_labelMovie = new QLabel(this);
/* Chargement de toutes les animations (QMovie). */
m_animWalk_R = new QMovie(m_labelMovie);
m_animWalk_R->setFileName("perso1/runNormal-R.gif");
m_animWalk_R->start();
m_animWalk_L = new QMovie(m_labelMovie);
m_animWalk_L->setFileName("perso1/runNormal-L.gif");
m_animWalk_L->start();
m_animStance_R = new QMovie(m_labelMovie);
m_animStance_R->setFileName("perso1/stance-R.gif");
m_animStance_R->start();
m_animStance_L = new QMovie(m_labelMovie);
m_animStance_L->setFileName("perso1/stance-L.gif");
m_animStance_L->start();
}
void Personnage::keyPressEvent(QKeyEvent *event)
{
/* Le perso se dirige vers la droite. */
if(event->key() == Qt::Key_Right)
{
m_direction = _RIGHT_;
Walk();
}
/* Le perso se dirige vers la gauche.*/
else if(event->key() == Qt::Key_Left)
{
m_direction = _LEFT_;
Walk();
}
}
void Personnage::keyReleaseEvent(QKeyEvent *event)
{
Stance();
}
inline void Personnage::Walk(void)
{
if (m_direction == _RIGHT_)
{
m_labelMovie->resize(m_animWalk_R->frameRect().size());
m_labelMovie->setMovie(m_animWalk_R);
m_x += _DEPLACEMENT_LENT_;
m_labelMovie->move(m_x, m_y);
}
else if (m_direction == _LEFT_)
{
m_labelMovie->resize(m_animWalk_L->frameRect().size());
m_labelMovie->setMovie(m_animWalk_L);
m_x -= _DEPLACEMENT_LENT_;
m_labelMovie->move(m_x, m_y);
}
}
inline void Personnage::Stance(void)
{
if (m_direction == _RIGHT_)
{
m_labelMovie->resize(m_animStance_R->frameRect().size());
m_labelMovie->setMovie(m_animStance_R);
}
else if (m_direction == _LEFT_)
{
m_labelMovie->resize(m_animStance_L->frameRect().size());
m_labelMovie->setMovie(m_animStance_L);
}
} |
Partager