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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
class bunny
{
public:
~~~~
void update_rect(int x,int y) //this updates the objects rect for collisions
{
obscolbox.x=x;
obscolbox.y=y;
obscolbox.w=50;
obscolbox.h=50;
}
bunny()//the constructor initializes loads of random starting points
{
int sex = (1+rand()%2);
if (sex == 2)
{
female = true;
}
start_pos_x = (40+rand()%540);
start_pos_y = (40+rand()%300);
int dir = (1+rand()%4);
if(dir==4)
{
up=true;
}
if(dir==1)
{
down = true;
}
if(dir==2)
{
left = true;
}
if(dir==3)
{
right = true;
}
}
~~~
bool bunny_collision(SDL_Rect &A,SDL_Rect &B)//good old fasioned collision detecting// function, takes two rects
{
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
leftA =A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
if( bottomA <= topB )
{
return false;
}
if( topA >= bottomB )
{
return false;
}
if( rightA <= leftB )
{
return false;
}
if( leftA >= rightB )
{
return false;
}
collided=true;
return true;
}
void new_direction()//when this is called new direction logic is initialized
{
int dir = 0;
dir = (1+rand()%5);
if(dir==1)
{
up=true;
}
if(dir==2)
{
down = true;
}
if(dir==3)
{
left = true;
}
if(dir==4)
{
right = true;
}
if(dir==5)
{
still = true;
}
}
bool check_pos()//so the bunny doesnt roam off the edge
{
if ((start_pos_x < 5)||(start_pos_x > 640-51)||(start_pos_y<5)||(start_pos_y>475-47)||(collided==true))
return true;
else
return false;
}
bool pregnancy_test (bool humping)//if succesful mating occured :D
{
pregnant = true;
Mix_PlayChannel(-1,note,0);
return true;
}
void roam_about()//each loop position, animation frame and direction logic are updated
{
if (still == true)
{
apply_surface(start_pos_x,start_pos_y,bunnysheet,screen,&clips[stillclips]);
stillclips++;
roamtime++;
if (stillclips == 23)
{
stillclips =12;
}
if(roamtime == 12)
{
still=false;
roamtime = 0;
new_direction();
}
}
else if (up == true)
{
apply_surface(start_pos_x,start_pos_y = start_pos_y+5,bunnysheet,screen,&clips[upclips]);
upclips++;
roamtime++;
if(upclips == 3)
{
upclips = 0;
}
if(check_pos()==true)
{
up = false;
roamtime = 0;
down = true;
}
else if(roamtime == 9)
{
up = false;
roamtime = 0;
new_direction();
}
}
else if (down == true)
{
apply_surface(start_pos_x,start_pos_y = start_pos_y-5,bunnysheet,screen,&clips[downclips]);
downclips++;
if(downclips == 6)
{
downclips = 3;
}
if(check_pos() ==true)
{
down = false;
roamtime = 0;
up = true;
}
else if(roamtime == 9)
{
down = false;
roamtime = 0;
new_direction();
}
}
else if (left == true)
{
apply_surface(start_pos_x = start_pos_x-5,start_pos_y = start_pos_y,bunnysheet,screen,&clips[leftclips]);
leftclips++;
roamtime++;
if(leftclips == 12)
{
leftclips = 9;
}
if(check_pos() == true)
{
left = false;
roamtime = 0;
right = true;
}
else if(roamtime == 9)
{
left = false;
roamtime = 0;
new_direction();
}
}
else if (right == true)
{
apply_surface(start_pos_x = start_pos_x +5,start_pos_y = start_pos_y,bunnysheet,screen,&clips[rightclips]);
rightclips++;
roamtime++;
if(rightclips == 9)
{
rightclips = 6;
}
if(check_pos()==true)
{
right = false;
roamtime = 0;
left = true;
}
else if(roamtime == 9)
{
right = false;
roamtime = 0;
new_direction();
}
}
collided = false;//gotta set that to false again otherwise bunny isnt allowed to go anywhere
}
};
int main( int argc, char* args[] )
{
~~~~
vector<bunny>bunny_list;//a vector of objects to be manipulated in the nested loop
bunny_list.push_back(bunny());
bunny_list.push_back(bunny());
bunny_list.push_back(bunny());
bunny_list.push_back(bunny());//innitial bunnehs
bunny_list.push_back(bunny());
bunny_list.push_back(bunny());
for(int q = 0; q < 1000; q++)//main game loop
{
for (int n =0; n<bunny_list.size(); n++ )
//first of nested loop checks bunneh
{
//this calls the update collision rect to bunnys location
bunny_list[n].update_rect(bunny_list[n].start_pos_x,bunny_list[n].start_pos_y);
for (int z =0; z<bunny_list.size(); z++ )
{
if(n==z)//this is supposed to stop n and z being the same so a bunny does not check itself for a collision
{
z++;
}
bunny_list[n].bunny_collision(bunny_list[n].obscolbox,bunny_list[z].obscolbox);
//if they are collided then bool colided is set to true
if((bunny_list[n].collided==true)&&(bunny_list[z].collided==true))
{//if the two bunnies that collided are male and female
if((bunny_list[n].female==true)&&(bunny_list[z].female==false))
{
if(bunny_list[n].pregnancy_test(bunny_list[n].collided)==true)
{//if nocked up then BABIES! :D
bunny_list.push_back(bunny());
}
}
}
}
if(bunny_list[n].collided==true)
{
bunny_list[n].collided=false;//gotta set it back to false again
}
bunny_list[n].roam_about();
}
~~~
SDL_Delay(120);
~~~~~~
|