Bonjour,
Je fais un petit jeu de tank en C++ mais juste apres avoir rajouté un destructeur à ma classe balle,le programme compile mais plante à l’exécution
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <SDL/SDL_rotozoom.h>
#include <vector>
#include <cmath>
#include "balle.h"
using namespace std;
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,*cannon = NULL;
    SDL_Rect pos_bloc,pos_perso;
    pos_perso.x=30;
    pos_perso.y=60;
    SDL_Event event;
    bool enf=0;
    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);
    cannon = IMG_Load("data/perso.png");
    tank = IMG_Load("data/perso.png");
    perso=tank;
    vector<Balle> balles;
    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,angle;
    int mat[50][25],symb,i,j,sourisX,sourisY,time=SDL_GetTicks();
    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);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
                break;
            case SDL_MOUSEBUTTONDOWN:
                enf=1;
                break;
            case SDL_MOUSEBUTTONUP:
                enf=0;
                break;
            case SDL_MOUSEMOTION:
                sourisX=event.button.x;
                sourisY=event.button.y;
                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_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;
        an=atan2(px+10-sourisX,py+10-sourisY)/3.14159265358979323846*180+90;
        if(enf && time+2<=SDL_GetTicks())
        {
            time=SDL_GetTicks();
            angle=atan2(px+10-sourisX,py+10-sourisY);
            balles.push_back(Balle(px+10,py+10,-6*sin(angle),-6*cos(angle)));
        }
        SDL_BlitSurface(perso, NULL, ecran, &pos_perso);
        for(i=0;i<balles.size();i++)
        {
            if(balles[i].update(ecran,mat)<0)
                balles.erase(balles.begin());
        }
        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
19
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#ifndef DEF_BALLE
#define DEF_BALLE
class Balle
{
    public:
    Balle();
    Balle(double x,double y,double vx,double vy);
    ~Balle();
    int update(SDL_Surface* ecran,int mat[][25]);
    private:
    double vy,y;
    double vx,x;
    int d;
    SDL_Surface* balle;
    SDL_Rect pos_balle;
};
#endif
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "balle.h"
using namespace std;
int bloc(int x,int y,int mat[][25]);
Balle::Balle():
    vx(1),
    vy(1),
    balle( IMG_Load("data/balle.png")),
    d(100)
{
    x=50;
    y=50;
    pos_balle.x = 50;
    pos_balle.y = 50;
}
Balle::Balle(double m_x,double m_y,double m_vx,double m_vy):
    vx(m_vx),
    vy(m_vy),
    balle( IMG_Load("data/balle.png")),
    d(100)
{
    x=m_x;
    y=m_y;
    pos_balle.x =x;
    pos_balle.y =y;
}
Balle::~Balle()
{
    SDL_FreeSurface(balle);
}
int Balle::update(SDL_Surface* ecran,int mat[][25])
{
    x+=vx;
    y+=vy;
    if((bloc(x,y-5,mat)=='1' && vy<0) || (bloc(x,y+5,mat)=='1' && vy>0))
        vy=-vy;
    if((bloc(x-5,y,mat)=='1' && vx<0) || (bloc(x+5,y,mat)=='1' && vx>0))
        vx=-vx;
    pos_balle.x=x-5;
    pos_balle.y=y-5;
    SDL_BlitSurface(balle, NULL,ecran,&pos_balle);
    d-=1;
    return d;
}
quelqu'un saurai trouver pourquoi?