main.cpp
Personnage.cpp
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 #include <cstdlib> #include <iostream> #include <SDL/SDL.h> #include "Personnage.h" #include "clavier.h" using namespace std; int main ( int argc, char** argv ) { if ( SDL_Init(SDL_INIT_VIDEO)<0) { cout<<"Erreur initialisation!"<<endl; return -1; } SDL_Surface *ecran; ecran=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE); SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,152,152,255)); SDL_Flip(ecran); bool continuer=true; SDL_Event evenement; Personnage *joueur1= new Personnage; joueur1->nom("ouss59"); while (continuer) { test_clavier(continuer,joueur1); joueur1->personnageBlit(); if (joueur1->position().y>600) { delete joueur1; } } return 0; }
Personnage.h
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 #include <iostream> #include <string> #include <SDL/SDL.h> #include "Personnage.h" using namespace std; Personnage::Personnage() { m_vie=100; m_armure=0; m_perso=SDL_LoadBMP("image/Joueur/joueur_Droite_1.bmp"); m_positionPersonnage.x=0; m_positionPersonnage.y=0; } Personnage::~Personnage() { SDL_FreeSurface(m_perso); } void Personnage::nom(std::string nomPersonnage ) { m_nomPersonnage=nomPersonnage; } void Personnage::frapper(string frappe) { if (frappe=="haut") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Haut_frappe.bmp"); personnageBlit(); SDL_Delay(100); m_perso=SDL_LoadBMP("image/Joueur/joueur_Haut_1.bmp"); } else if (frappe=="bas") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Bas_frappe.bmp"); personnageBlit(); SDL_Delay(100); m_perso=SDL_LoadBMP("image/Joueur/joueur_Bas_1.bmp"); } else if (frappe=="gauche") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Gauche_frappe.bmp"); personnageBlit(); SDL_Delay(100); m_perso=SDL_LoadBMP("image/Joueur/joueur_Gauche_1.bmp"); } else if (frappe=="droite") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Droite_frappe.bmp"); personnageBlit(); SDL_Delay(100); m_perso=SDL_LoadBMP("image/Joueur/joueur_Droite_1.bmp"); } } void Personnage::deplacer(string direction) { if(direction=="droite") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Droite_1.bmp"); m_positionPersonnage.x+=15; m_direction=direction; } else if(direction=="gauche") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Gauche_1.bmp"); m_positionPersonnage.x-=15; m_direction=direction; } else if(direction=="bas") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Bas_1.bmp"); m_positionPersonnage.y+=15; m_direction=direction; } else if(direction=="haut") { m_perso=SDL_LoadBMP("image/Joueur/joueur_Haut_1.bmp"); m_positionPersonnage.y-=15; m_direction=direction; } } void Personnage::personnageBlit() { SDL_FillRect(SDL_GetVideoSurface(),NULL,SDL_MapRGB(SDL_GetVideoSurface()->format,152,152,255)); SDL_SetColorKey(m_perso,SDL_SRCCOLORKEY,SDL_MapRGB(m_perso->format,0,0,0)); SDL_BlitSurface(m_perso,NULL,SDL_GetVideoSurface(),&m_positionPersonnage); SDL_Flip(SDL_GetVideoSurface()); } SDL_Rect Personnage::position() { return m_positionPersonnage; } string Personnage::direction_actuelle() { return m_direction; }
clavier.h
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 #ifndef PERSONNAGE_H_INCLUDED #define PERSONNAGE_H_INCLUDED #include <string> #include <SDL/SDL.h> class Personnage { public: Personnage(); ~Personnage(); void nom(std::string nomPersonnage ); void frapper(std::string frappe); void deplacer(std::string direction); void personnageBlit(); SDL_Rect position(); std::string direction_actuelle(); private: int m_vie,m_armure,m_degat; std::string m_nomPersonnage, m_direction; SDL_Surface *m_perso; SDL_Rect m_positionPersonnage; }; #endif // PERSONNAGE_H_INCLUDED
clavier.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 #ifndef CLAVIER_H_INCLUDED #define CLAVIER_H_INCLUDED #include <string> #include <SDL/SDL.h> #include "Personnage.h" void test_clavier (bool &continuer,Personnage *perso); #endif // CLAVIER_H_INCLUDED
probleme
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 <SDL/SDL.h> #include "clavier.h" #include "Personnage.h" using namespace std; void test_clavier (bool &continuer,Personnage *perso) { SDL_Event evenement; SDL_PollEvent(&evenement); if(evenement.type==SDL_QUIT) { continuer=false; } if (evenement.type==SDL_KEYDOWN) { if (evenement.key.keysym.sym==SDLK_LEFT) { perso->deplacer("gauche"); } else if (evenement.key.keysym.sym==SDLK_RIGHT) { perso->deplacer("droite"); } else if (evenement.key.keysym.sym==SDLK_DOWN) { perso->deplacer("bas"); } else if (evenement.key.keysym.sym==SDLK_UP) { perso->deplacer("haut"); } else if (evenement.key.keysym.sym==SDLK_f) { if (perso->direction_actuelle()=="gauche") { perso->frapper("gauche"); } else if (perso->direction_actuelle()=="droite") { perso->frapper("droite"); } else if (perso->direction_actuelle()=="haut") { perso->frapper("haut"); } else if (perso->direction_actuelle()=="bas") { perso->frapper("bas"); } } }
bonjour/bonsoir
J'ai un problème avec les déplacements du personnage , quand je le deplace vert une direction , il faut appuyer plusieurs fois sur la touche pour qu'il avance de 15px a chaque fois ,
alors que dans clavier.cpp j'ai fait SDL_PollEvent() et non pas SDL_WaitEvent().
aidez moi , merci d'avance
Partager