bonjour,
je cherche a créer une classe balle pour un petit jeu de tank mais c'est la première fois que je fais une classe C++ et évidemment je me retrouve avec pas mal d'erreurs à la compilation
voici le code:
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 -------------- Build: Debug in Tank (compiler: GNU GCC Compiler)--------------- mingw32-g++.exe -Wall -g -Iinclude -c "C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp" -o obj\Debug\sources\balle.o In file included from C:\Users\baud\Documents\c++ prog\Tank\sources\balle.cpp:1:0: C:\Users\___\Documents\c++ prog\Tank\sources\balle.h:8:17: error: 'SDL_Surface' has not been declared void update(SDL_Surface* ecran,int mat[][25]); ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.h:15:5: error: 'SDL_Surface' does not name a type SDL_Surface* balle; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.h:16:5: error: 'SDL_Rect' does not name a type SDL_Rect pos_balle; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp: In constructor 'Balle::Balle()': C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:9:5: error: 'SDL_Surface' was not declared in this scope SDL_Surface* balle=IMG_Load("data/balle.png"); ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:9:18: error: 'balle' was not declared in this scope SDL_Surface* balle=IMG_Load("data/balle.png"); ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:9:49: error: 'IMG_Load' was not declared in this scope SDL_Surface* balle=IMG_Load("data/balle.png"); ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:10:5: error: 'pos_balle' was not declared in this scope pos_balle.x=this.x; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:10:22: error: request for member 'x' in '(Balle*)this', which is of pointer type 'Balle*' (maybe you meant to use '->' ?) pos_balle.x=this.x; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:11:22: error: request for member 'y' in '(Balle*)this', which is of pointer type 'Balle*' (maybe you meant to use '->' ?) pos_balle.y=this.y; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:5:12: warning: unused variable 'x' [-Wunused-variable] double x=50; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:6:12: warning: unused variable 'y' [-Wunused-variable] double y=50; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:7:12: warning: unused variable 'vy' [-Wunused-variable] double vy=1; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:8:12: warning: unused variable 'vx' [-Wunused-variable] double vx=1; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:12:9: warning: unused variable 'd' [-Wunused-variable] int d=1000; ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp: At global scope: C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:14:1: error: prototype for 'Balle::Balle(double, double, double, double)' does not match any in class 'Balle' Balle::Balle(double m_x,double m_y,double m_vx,double m_vy) ^ In file included from C:\Users\baud\Documents\c++ prog\Tank\sources\balle.cpp:1:0: C:\Users\___\Documents\c++ prog\Tank\sources\balle.h:3:7: error: candidates are: Balle::Balle(const Balle&) class Balle ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.h:7:5: error: Balle::Balle(double, double) Balle(double x,double y); ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:3:1: error: Balle::Balle() Balle::Balle() ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:25:20: error: variable or field 'update' declared void void Balle::update(SDL_Surface* ecran,int mat[][25]) ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:25:20: error: 'SDL_Surface' was not declared in this scope C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:25:33: error: 'ecran' was not declared in this scope void Balle::update(SDL_Surface* ecran,int mat[][25]) ^ C:\Users\___\Documents\c++ prog\Tank\sources\balle.cpp:25:39: error: expected primary-expression before 'int' void Balle::update(SDL_Surface* ecran,int mat[][25]) ^ Process terminated with status 1 (0 minute(s), 41 second(s)) 17 error(s), 5 warning(s) (0 minute(s), 41 second(s))
Code main.cpp : 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 #include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <stdio.h> #include <SDL/SDL_rotozoom.h> #include "balle.h" int bloc(int x,int y,int mat[][25]) { return mat[x/20][y/20]; } int main(int argc, char *argv[]) { SDL_Surface *ecran = NULL,*block = NULL,*ice = NULL,*perso = NULL,*tank = NULL; SDL_Rect pos_bloc,pos_perso; pos_perso.x=30; pos_perso.y=60; SDL_Event event; int continuer = 1; int tempsPrecedent = 0, tempsActuel = 0; SDL_Init(SDL_INIT_VIDEO); ecran = SDL_SetVideoMode(1000,500, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); SDL_WM_SetCaption("Jeu en SDL", NULL); tank = IMG_Load("data/perso.png"); perso=tank; block = IMG_Load("data/bloc.png"); ice = IMG_Load("data/glace.png"); FILE* fichier = fopen("matrice.txt", "r"); double px=30,py=60,vx=0,vy=0,an=0; int mat[50][25],symb,i,j; for(j=0;j<25;j++) { for(i=0;i<50;i++) { symb=fgetc(fichier); if(symb=='\n') symb=fgetc(fichier); mat[i][j]=symb; } } SDL_EnableKeyRepeat(10, 10); while (continuer) { SDL_PollEvent(&event); /* On utilise PollEvent et non WaitEvent pour ne pas bloquer le programme */ switch(event.type) { case SDL_QUIT: continuer = 0; break; } SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 180,180, 250)); for(int i=0;i<50;i++) { for(int j=0;j<25;j++) { pos_bloc.x=20*i; pos_bloc.y=20*j; switch(mat[i][j]) { case '1': SDL_BlitSurface(block, NULL, ecran, &pos_bloc); break; case '2': SDL_BlitSurface(ice, NULL, ecran, &pos_bloc); break; } } } Uint8 *state=SDL_GetKeyState(NULL); if(state[SDLK_q]) an+=2; if(state[SDLK_w]) an-=2; if(state[SDLK_LEFT]) if(bloc(px-1,py,mat)!='0' || bloc(px-1,py+19,mat)!='0') px=20*int((px+10)/20); else px-=3; if(state[SDLK_RIGHT]) if(bloc(px+21,py,mat)!='0' || bloc(px+21,py+19,mat)!='0') px=20*int((px+10)/20); else px+=3; if(state[SDLK_UP]) if(bloc(px,py-1,mat)!='0' || bloc(px+19,py-1,mat)!='0') py=20*int((py+10)/20); else py-=3; if(state[SDLK_DOWN]) if(bloc(px,py+21,mat)!='0' || bloc(px+19,py+21,mat)!='0') py=20*int((py+10)/20); else py+=3; px+=vx; py+=vy; perso = rotozoomSurface(tank, an, 1.0, 1); pos_perso.y=py+10-perso->h/2; pos_perso.x=px+10-perso->w/2; SDL_BlitSurface(perso, NULL, ecran, &pos_perso); SDL_Flip(ecran); tempsActuel = SDL_GetTicks(); if (tempsActuel - tempsPrecedent > 20) { tempsPrecedent = tempsActuel; } else { SDL_Delay(20 - (tempsActuel - tempsPrecedent)); } } SDL_FreeSurface(block); SDL_FreeSurface(perso); SDL_FreeSurface(ice); SDL_Quit(); return EXIT_SUCCESS; }
Code balle.h : 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 #ifndef DEF_BALLE #define DEF_BALLE class Balle { public: Balle(); Balle(double x,double y); void update(SDL_Surface* ecran,int mat[][25]); private: double x; double y; double vy; double vx; int d; SDL_Surface* balle; SDL_Rect pos_balle; }; #endifmerci a ceux qui peuvent m'aider
Code balle.cpp : 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 #include "balle.h" using namespace std; Balle::Balle() { double x=50; double y=50; double vy=1; double vx=1; SDL_Surface* balle=IMG_Load("data/balle.png"); pos_balle.x=this.x; pos_balle.y=this.y; int d=1000; } Balle::Balle(double m_x,double m_y,double m_vx,double m_vy) { double x=m_x; double y=m_y; double vy=m_vy; double vx=m_vx; SDL_Surface* balle=IMG_Load("data/balle.png"); pos_balle.x=x; pos_balle.y=y; int d=1000; } void Balle::update(SDL_Surface* ecran,int mat[][25]) { }
Partager