Bonjour,
Alors voilà, je suis dans la création d'un jeu et je dois faire la collision entre une flèche (image rectangulaire) et une bulle (rectangle qu'on rechange en cercle). Voici la classe bulle:
La classe cercle:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 #include "bubble.h" Bubble::Bubble(QWidget *parent, int type,int posX,bool droite,int posY): QLabel(parent) { this->parent = parent; initialiser(type,posX,droite,posY); versBas=true; ral=true; } void Bubble::initialiser(int type,int posX,bool droite,int posY) { /* string image="../bulle"+type+".gif"; m_imgBubble = new QPixmap(image); setPixmap(*m_imgBubble); */ switch(type) { case 11:m_imgBubble = new QPixmap("../bulle11.gif"); setPixmap(*m_imgBubble); setGeometry(posX, posY, 20, 20); hMax=300; break; case 12:m_imgBubble = new QPixmap("../bulle12.gif"); setPixmap(*m_imgBubble); setGeometry(posX, posY, 50, 50); hMax=250; break; case 13:m_imgBubble = new QPixmap("../bulle13.gif"); setPixmap(*m_imgBubble); setGeometry(posX, posY, 75, 75); hMax=150; break; case 14:m_imgBubble = new QPixmap("../bulle14.gif"); setPixmap(*m_imgBubble); setGeometry(posX, posY, 110, 110); hMax=100; break; case 15:m_imgBubble = new QPixmap("../bulle15.gif"); setPixmap(*m_imgBubble); setGeometry(posX, posY, 150, 150); hMax=50; break; case 16:m_imgBubble = new QPixmap("../bulle16.gif"); setPixmap(*m_imgBubble); setGeometry(posX, posY, 200, 200); hMax=30; break; default:break; } versDroite=droite; } void Bubble::moveBubble() { int newX=0; int newY=0; int px; //HORIZONTAL if(versDroite) { //S'il n'est pas au bord if((this->x() + this->width()) <= parent->width()) { newX=this->x()+1; } //S'il est au bord else { newX=this->x()-1; versDroite=false; } } else { if(this->x() >=0) { newX=this->x()-1; } else { newX=this->x()+1; versDroite=true; } } //VERTICAL if(this->y()<35) { px=4; } else if(this->y()>hMax+80) { px=3; } else if(this->y()>hMax+20) { px=2; } else { px=1; } if(versBas) { if((this->y() + this->height()) <= parent->height()-75) { newY=this->y()+px; } else { newY=this->y()-px; versBas=false; } } else { if(this->y() >=hMax) { newY=this->y()-px; } else { newY=this->y()+px; versBas=true; } } this->move(newX,newY); }
La classe flèche
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "cercle.h" Cercle::Cercle(int x,int y,int r) { centreX=x; centreY=y; rayon=r; } //Cercle::~Cerle(){} Cercle::Cercle(const Cercle& c) { centreX=c.centreX; centreY=c.centreY; rayon=c.rayon; } Cercle& Cercle::operator=(const Cercle& c) { if(this!=&c) { centreX=c.centreX; centreY=c.centreY; rayon=c.rayon; } return *this; } void Cercle::setCentreX(const int &x ) { centreX=x; } int Cercle::getCentreX() const { return centreX; } void Cercle::setCentreY(const int &y ) { centreY=y; } int Cercle::getCentreY() const { return centreY; } void Cercle::setRayon(const int &r ) { rayon=r; } int Cercle::getRayon() const { return rayon; }
et la classe Level:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "fleche.h" #include "level.h" #include<QLabel> Fleche::Fleche(QWidget *parent): QLabel(parent) { this->parent = parent; m_fleche = new QPixmap("../fleche.gif"); // m_fleche->setPixmap(QPixmap("../fleche.gif")); setPixmap(*m_fleche); setGeometry(431, 530, 28, 228); setBase(); timerUp= new QTimer(this); QObject::connect(timerUp,SIGNAL(timeout()),this,SLOT(slotUp())); timerRight = new QTimer(this); QObject::connect(timerRight, SIGNAL(timeout()),this, SLOT(slotRight())); timerLeft = new QTimer(this); QObject::connect(timerLeft, SIGNAL(timeout()),this, SLOT(slotLeft())); } void Fleche::setBase() { //repositionnement de la fleche setGeometry(431, 530, 28, 228); } void Fleche::startMoveUp(int speed) { timerUp->start(speed); } void Fleche::slotUp() { // m_fleche->move(m_fleche->x(),m_fleche->y()-1); this->move(this->x(),this->y()-1); this->stopMoveLeft(); this->stopMoveRight(); } void Fleche::slotRight() { if((this->x() + this->width()) <= parent->width()) { this->move(this->x()+1,this->y()); } } void Fleche::slotLeft() { if(this->x() >=0) { this->move(this->x()-1,this->y()); } } void Fleche::startMoveRight(int speed) { timerRight->start(speed); } void Fleche::stopMoveRight() { timerRight->stop(); } void Fleche::startMoveLeft(int speed) { timerLeft->start(speed); } void Fleche::stopMoveLeft() { timerLeft->stop(); } void Fleche::retourPositionPerso(int x,int y) { this->move(x+27,y); }
Voilà, si quelqu'un sait m'aider, ca serait GENIALLISIME!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 #include "level.h" Level::Level(QWidget *parent) :QWidget(parent) { setFixedSize(800, 600); m_imageSonOn=new QPixmap("../sonon.gif"); m_imageSonOff=new QPixmap("../sonoff.gif"); m_labelFond= new QLabel(this); m_labelFond->setGeometry(0,0,800,600); m_labelFond->setPixmap(QPixmap("../Fond.gif")); perso = new Personage(m_labelFond); fl= new Fleche(m_labelFond); timerBubble=new QTimer(this); QObject::connect(timerBubble, SIGNAL(timeout()),this, SLOT(slotTimeBubble())); bulle.push_back(new Bubble(m_labelFond,11,105)); bulle.push_back(new Bubble(m_labelFond,12,120,false)); bulle.push_back(new Bubble(m_labelFond,13,170)); bulle.push_back(new Bubble(m_labelFond,14,300,false)); bulle.push_back(new Bubble(m_labelFond,15,450)); timerBubble->start(5); //Petit mur devant le fond, pour la flèche-----// m_labelMur=new QLabel(m_labelFond); m_labelMur->setGeometry(0,267,800,600); m_labelMur->setPixmap(QPixmap("../mur.gif")); //-------------------------------------------------// //Move personnage timerRight = new QTimer(this); QObject::connect(timerRight, SIGNAL(timeout()),this, SLOT(slotRight())); timerLeft = new QTimer(this); QObject::connect(timerLeft, SIGNAL(timeout()),this, SLOT(slotLeft())); } void Level::slotTimeBubble() { for(int i=0;i<bulle.size();i++) { bulle.at(i)->moveBubble(); } } //méthode qui gère les évènements clavier lorsqu'une touche est pressée void Level::keyPressEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_Right: timerRight->start(3); fl->startMoveRight(3); break; case Qt::Key_Left: timerLeft->start(3); fl->startMoveLeft(3); break; case Qt::Key_Space: perso->setTir(); fl->startMoveUp(1); //fl=new Fleche(m_labelFond); fl->retourPositionPerso(perso->x(),perso->y()); break; } } //méthode qui gère les évènements clavier lorsqu'une touche est relevée void Level::keyReleaseEvent( QKeyEvent * event ) { switch (event->key()) { case Qt::Key_Right: timerRight->stop(); fl->stopMoveRight(); break; case Qt::Key_Left: timerLeft->stop(); fl->stopMoveLeft(); break; case Qt::Key_Space: perso->setBase(); //fl=new Fleche(m_labelFond); break; } } void Level::slotRight() { if((perso->x() + perso->width()) <= this->width()) { perso->move(perso->x()+2,perso->y()); } } void Level::slotLeft() { if(perso->x() >=0) { perso->move(perso->x()-2,perso->y()); } }
Partager