SDL issue, why does this program freeze when there is 2 bunnies?

You may not need to know SDL to see whats going on here, the codes quite simple,
in the class the idea is to print an animated frame of the bunny [up/down/left/rightclips] each time apply surface is called, once it has ran in a direction for 5 frames a new direction activated and applysurface prints the coresponding clips, logicaly it makes sense to me, even when i tell my rubber ducky all about it, theres clearly a flaw in my logic, can you see it?

for a coding problem it should be as clear as day whats going wrong.

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
 void roam_about()
{
    if (up == true)
    {
        down = false,left = false, right = false;
    apply_surface(start_pos_x,start_pos_y = start_pos_y-1,bunnysheet,screen,&clips[upclips]);
    upclips++;
    roamtime++;
    if(upclips >= 3){upclips = 0;}
    if(roamtime == 5){up = false; roamtime = 0; new_direction();}
    }

    if (down == true)
    {
        up = false, left = false, right = false;
    apply_surface(start_pos_x,start_pos_y = start_pos_y+1,bunnysheet,screen,&clips[downclips]);
    downclips++;
    roamtime++;
    if(downclips >= 6){downclips = 3;}
    if(roamtime == 5){down = false; roamtime = 0; new_direction();}
    }

    if (left == true)
    {
        up = false, down = false, right = false;
    apply_surface(start_pos_x = start_pos_x-1,start_pos_y = start_pos_y,bunnysheet,screen,&clips[leftclips]);
    leftclips++;
    roamtime++;
    if(leftclips >= 9){leftclips = 6;}
    if(roamtime == 5){left = false; roamtime = 0; new_direction();}
    }

    if (right == true)
    {
        up = false, down = false, left = false;
    apply_surface(start_pos_x = start_pos_x -1,start_pos_y = start_pos_y,bunnysheet,screen,&clips[rightclips]);
    rightclips++;
    roamtime++;
    if(rightclips >= 12){rightclips = 9;}
    if(roamtime == 5){right = false; roamtime = 0; new_direction();}
    }
    else
        new_direction();
}

};


and in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bunny buneh;
buneh.get_start_pos();


while(1)
{

buneh.roam_about();

SDL_Flip(screen);

SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,254,254,254));


SDL_Delay(160);

}


I used some bad practices just so you can see wha'gwahn'

EDIT: Oh yeah a part of the class that may be of interest too

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
//
class bunny{

public:

int start_pos_x,start_pos_y,health,age,upclips=0,downclips=3,leftclips=9,rightclips=6,roamtime=0;
bool up,down,left,right,yellow,red,blue,purple,ill;

void get_start_pos()
{
start_pos_x = (rand()%640);
start_pos_y = (rand()%480);
int dir = (rand()%3);
if(dir==0){up=true;}
if(dir==1){down = true;}
if(dir==2){left = true;}
if(dir==3){right = true;}
}

void new_direction()
{
int dir = (rand()%3);
if(dir==0){up=true;}
if(dir==1){down = true;}
if(dir==2){left = true;}
if(dir==3){right = true;}
}
// 
Last edited on
wait i got it running :/ but now it freezes with two bunnys :( what is that
new_direction() don't set any of the directions to false, so if you make the change from up to left you don't know which one is the new direction and roam_about() will treat this as direction up and set left to false.

Instead of having 4 variables that you have to keep consistent, why not have one variable storing the direction. You can use an enum.
1
2
3
4
5
6
7
8
9
10
11
12
13
enum class Direction
{
	up,
	down,
	left,
	right
};

Direction dir = Direction::up;

if (dir == Direction::left)
{
	...


Also note that rand()%3 will give you a value in the range 0-2, leaving out 3.
I wondered why I had a zoolander bunny.

thanks is see the error of my ways.
Topic archived. No new replies allowed.