diff options
| author | Reimar <Reimar@Leike.name> | 2014-12-04 12:02:53 +0100 | 
|---|---|---|
| committer | Reimar <Reimar@Leike.name> | 2014-12-04 12:02:53 +0100 | 
| commit | d7c0925b3d7ffb1a58402c05e664966e2f8a597f (patch) | |
| tree | 62cef6cc5b74481799c10dab8a18e756b74ea7e9 | |
| parent | 33e9f1db579685dd1ffab767ba7733a8e9503c78 (diff) | |
| download | SpaceCannon-d7c0925b3d7ffb1a58402c05e664966e2f8a597f.tar SpaceCannon-d7c0925b3d7ffb1a58402c05e664966e2f8a597f.tar.gz SpaceCannon-d7c0925b3d7ffb1a58402c05e664966e2f8a597f.tar.bz2 SpaceCannon-d7c0925b3d7ffb1a58402c05e664966e2f8a597f.tar.xz SpaceCannon-d7c0925b3d7ffb1a58402c05e664966e2f8a597f.zip | |
added custom random generator, ball no has a roll animation, revisited difficulty impact
| -rw-r--r-- | JnR.cpp | 117 | ||||
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | blank.bmp | bin | 0 -> 890 bytes | |||
| -rw-r--r-- | random.cpp | 18 | ||||
| -rw-r--r-- | random.h | 14 | 
5 files changed, 121 insertions, 30 deletions
| @@ -5,6 +5,7 @@ | |||
| 5 | #include <SDL/SDL.h> | 5 | #include <SDL/SDL.h> | 
| 6 | #include <SDL/SDL_ttf.h> | 6 | #include <SDL/SDL_ttf.h> | 
| 7 | #include <SDL/SDL_rotozoom.h> | 7 | #include <SDL/SDL_rotozoom.h> | 
| 8 | #include <SDL/SDL_image.h> | ||
| 8 | #include <algorithm> | 9 | #include <algorithm> | 
| 9 | #include <fstream> | 10 | #include <fstream> | 
| 10 | #include <sstream> | 11 | #include <sstream> | 
| @@ -12,10 +13,35 @@ | |||
| 12 | #include <cstdio> | 13 | #include <cstdio> | 
| 13 | #include <vector> | 14 | #include <vector> | 
| 14 | #include <list> | 15 | #include <list> | 
| 16 | #include "random.h" | ||
| 15 | 17 | ||
| 16 | 18 | ||
| 17 | using namespace std; | 19 | using namespace std; | 
| 18 | 20 | ||
| 21 | //loads a bitmap from file and converts it to screen properties | ||
| 22 | SDL_Surface* loadBMP(std::string filename) | ||
| 23 | { | ||
| 24 | SDL_Surface* loadedImage = NULL; | ||
| 25 | |||
| 26 | SDL_Surface* convertedImage = NULL; | ||
| 27 | |||
| 28 | loadedImage = IMG_Load(filename.c_str()); | ||
| 29 | if(loadedImage != NULL) | ||
| 30 | { | ||
| 31 | convertedImage = SDL_DisplayFormat(loadedImage); | ||
| 32 | if(convertedImage) | ||
| 33 | { | ||
| 34 | Uint32 colorkey = SDL_MapRGB(convertedImage->format, 10, 10, 10); | ||
| 35 | SDL_SetColorKey(convertedImage, SDL_SRCCOLORKEY, colorkey); | ||
| 36 | SDL_FreeSurface(loadedImage); | ||
| 37 | return convertedImage; | ||
| 38 | } | ||
| 39 | cout << "Error: Could not convert Image" << filename << '.' << endl; | ||
| 40 | } | ||
| 41 | cout << "Error: Could not load bitmap from file " << filename << endl; | ||
| 42 | return NULL; | ||
| 43 | } | ||
| 44 | |||
| 19 | 45 | ||
| 20 | string lltostr(const long long& l) | 46 | string lltostr(const long long& l) | 
| 21 | { | 47 | { | 
| @@ -141,14 +167,29 @@ void drawPixel(SDL_Surface *screen,int x, int y, Uint8 R, Uint8 G, Uint8 B) | |||
| 141 | SDL_FillRect(screen,&r,color); | 167 | SDL_FillRect(screen,&r,color); | 
| 142 | } | 168 | } | 
| 143 | 169 | ||
| 144 | void drawCircle(SDL_Surface* screen, double x, double y, double radius, Uint8 R, Uint8 G, Uint8 B) | 170 | SDL_Surface* preBall; | 
| 171 | |||
| 172 | void drawCircle(SDL_Surface* screen, double x, double y, double angle, double radius, Uint8 R, Uint8 G, Uint8 B) | ||
| 145 | { | 173 | { | 
| 146 | for(int i = x-radius; i <= x+radius; ++i) | 174 | int tempseed = rand(); | 
| 147 | for(int j = y-radius; j <= y+radius; ++j) | 175 | srand(87); | 
| 148 | if(((i-x)*(i-x)+(j-y)*(j-y) <= radius*radius) && inBounds(i,j)) | 176 | Uint32 colorkey = SDL_MapRGB(preBall->format, 10, 10, 10); | 
| 177 | SDL_FillRect(preBall,NULL,colorkey); | ||
| 178 | for(int i = 0; i <= 2*radius; ++i) | ||
| 179 | for(int j = 0; j <= 2*radius; ++j) | ||
| 180 | if(((i-radius+0.5)*(i-radius+0.5)+(j-radius+0.5)*(j-radius+0.5) <= radius*radius)) | ||
| 149 | { | 181 | { | 
| 150 | drawPixel(screen, i,j,R,G,B); | 182 | double alt = 0.90+(rand()%10000)/50000.0; | 
| 183 | drawPixel(preBall, i,j,R*alt,G*alt,B*alt); | ||
| 151 | } | 184 | } | 
| 185 | SDL_Rect drawpos; | ||
| 186 | SDL_Surface* temp = preBall; | ||
| 187 | temp = rotozoomSurface(temp,angle/M_PI*180,1,0); | ||
| 188 | drawpos.x = x-temp->w/2; | ||
| 189 | drawpos.y = y - temp->h/2; | ||
| 190 | SDL_BlitSurface(temp,NULL,screen,&drawpos); | ||
| 191 | SDL_FreeSurface(temp); | ||
| 192 | srand(tempseed); | ||
| 152 | } | 193 | } | 
| 153 | 194 | ||
| 154 | void blacken(SDL_Surface* screen) | 195 | void blacken(SDL_Surface* screen) | 
| @@ -197,6 +238,7 @@ public: | |||
| 197 | } | 238 | } | 
| 198 | }; | 239 | }; | 
| 199 | 240 | ||
| 241 | |||
| 200 | class Particle | 242 | class Particle | 
| 201 | { | 243 | { | 
| 202 | public: | 244 | public: | 
| @@ -242,12 +284,12 @@ public: | |||
| 242 | } | 284 | } | 
| 243 | else if(mode == 2) | 285 | else if(mode == 2) | 
| 244 | { | 286 | { | 
| 245 | x += (rand()%3-1)/3.0; | 287 | x += (rand()%3-1)/2.0; | 
| 246 | y += (rand()%3-1)/3.0; | 288 | y += (rand()%3-1)/2.0; | 
| 247 | } | 289 | } | 
| 248 | else if(mode == 3) | 290 | else if(mode == 3) | 
| 249 | { | 291 | { | 
| 250 | x += (rand()%3-1)/3.0; | 292 | x += (rand()%3-1)/8.0; | 
| 251 | y += vy*time; | 293 | y += vy*time; | 
| 252 | } | 294 | } | 
| 253 | 295 | ||
| @@ -288,6 +330,8 @@ vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, do | |||
| 288 | 330 | ||
| 289 | double x_pos = 0; | 331 | double x_pos = 0; | 
| 290 | double x_vel = 0; | 332 | double x_vel = 0; | 
| 333 | double phi = 0; | ||
| 334 | double vphi = 0; | ||
| 291 | double ground_level = 20; | 335 | double ground_level = 20; | 
| 292 | double y_vel = 0; | 336 | double y_vel = 0; | 
| 293 | double ball_rad = 8; | 337 | double ball_rad = 8; | 
| @@ -322,6 +366,7 @@ void loadConstants() | |||
| 322 | int main() | 366 | int main() | 
| 323 | { | 367 | { | 
| 324 | loadConstants(); | 368 | loadConstants(); | 
| 369 | |||
| 325 | string name; | 370 | string name; | 
| 326 | cout << "This is a simple jump and run" << endl; | 371 | cout << "This is a simple jump and run" << endl; | 
| 327 | cout << "Please enter your name:"; | 372 | cout << "Please enter your name:"; | 
| @@ -338,17 +383,19 @@ int main() | |||
| 338 | } | 383 | } | 
| 339 | else | 384 | else | 
| 340 | cout << "Very sane decision..." << endl; | 385 | cout << "Very sane decision..." << endl; | 
| 341 | if(difficulty > 0) | 386 | if(difficulty >= 0) | 
| 342 | cout << "solvability not guarenteed! Have fun!" << endl; | 387 | cout << "solvability not guarenteed! Have fun!" << endl; | 
| 343 | if(difficulty < 0) | 388 | if(difficulty < 0) | 
| 344 | cout << "This should be a piece of cake and you should feel bad!" << endl; | 389 | cout << "This should be a piece of cake and you should feel bad!" << endl; | 
| 345 | difficulty = max(-50,difficulty); | 390 | difficulty = max(-50,difficulty); | 
| 346 | if(difficulty > 100) | 391 | if(difficulty > 100) | 
| 347 | difficulty = 100; | 392 | difficulty = 100; | 
| 393 | //SEED | ||
| 394 | srandn(2334508 + difficulty); | ||
| 348 | srand(2334508 + difficulty); | 395 | srand(2334508 + difficulty); | 
| 396 | |||
| 349 | cout << "difficulty = " << difficulty << endl; | 397 | cout << "difficulty = " << difficulty << endl; | 
| 350 | position_multiplier += difficulty/40.0; | 398 | position_multiplier += difficulty/40.0; | 
| 351 | // char l; | ||
| 352 | if(SDL_Init(SDL_INIT_VIDEO) == -1) | 399 | if(SDL_Init(SDL_INIT_VIDEO) == -1) | 
| 353 | { | 400 | { | 
| 354 | cout << "Error: Could not initialize SDL" << endl; | 401 | cout << "Error: Could not initialize SDL" << endl; | 
| @@ -366,34 +413,38 @@ int main() | |||
| 366 | cout << "could not initialize True Fonts" << endl; | 413 | cout << "could not initialize True Fonts" << endl; | 
| 367 | return 1; | 414 | return 1; | 
| 368 | } | 415 | } | 
| 369 | SDL_WM_SetCaption("Ballroller - v0.2", NULL); | 416 | SDL_WM_SetCaption("Ballroller - v0.3", NULL); | 
| 417 | |||
| 418 | preBall = loadBMP("blank.bmp"); | ||
| 370 | 419 | ||
| 371 | 420 | ||
| 372 | bool Game_Quit = false; | 421 | bool Game_Quit = false; | 
| 373 | int down = 0; | 422 | int down = 0; | 
| 374 | int right = 0; | 423 | int right = 0; | 
| 375 | int space = 0; | ||
| 376 | vector<Obstacle> obstacles; | 424 | vector<Obstacle> obstacles; | 
| 377 | obstacles.push_back(Obstacle(-50,ground_level,10,200)); | 425 | obstacles.push_back(Obstacle(-50,ground_level,10,200)); | 
| 378 | vector<Particle> particles; | 426 | vector<Particle> particles; | 
| 379 | bool quickquit = false; | 427 | bool quickquit = false; | 
| 380 | for(int i = 0; i < 10*60*5; ++i) | 428 | for(int i = 0; i < 10*60*5; ++i) | 
| 381 | { | 429 | { | 
| 382 | Particle p = Particle(-300 + rand()%10000,ground_level + rand()%((int) (yres- ground_level)),3,255,255,255); | 430 | Particle p = Particle(rand()%11000 - 300,ground_level + rand()%((int)(yres-ground_level)),3,255,255,255); | 
| 383 | p.vy = -2; | 431 | p.life = 1000; | 
| 432 | p.vy = -5-rand()%30/9.0; | ||
| 384 | particles.push_back(p); | 433 | particles.push_back(p); | 
| 385 | } | 434 | } | 
| 386 | int opos = 100; | 435 | //Longest Jump: 608 | 
| 436 | //Highest Jump: 150 | ||
| 437 | int opos = 200; | ||
| 387 | while(opos < 10000) | 438 | while(opos < 10000) | 
| 388 | { | 439 | { | 
| 389 | opos += rand()%(900-6*difficulty); | 440 | int blub = 4+randn()%(192+difficulty/4-4); | 
| 390 | int blub = rand()%180 + 3; | 441 | obstacles.push_back(Obstacle(opos,ground_level,blub,(196 + difficulty/4)/2-blub/2)); | 
| 391 | obstacles.push_back(Obstacle(opos,ground_level,blub,200/2-blub/2)); | 442 | opos += randn()%(900-4*difficulty); | 
| 392 | } | 443 | } | 
| 393 | if(hardcore) | 444 | if(hardcore) | 
| 394 | for(int i = 1; i < 110; ++i) | 445 | for(int i = 1; i < 110; ++i) | 
| 395 | { | 446 | { | 
| 396 | Obstacle tempo = Obstacle(i*100,ground_level, 10, 10); | 447 | Obstacle tempo = Obstacle(i*300,ground_level, 10, 10); | 
| 397 | tempo.vx = -60; | 448 | tempo.vx = -60; | 
| 398 | obstacles.push_back(tempo); | 449 | obstacles.push_back(tempo); | 
| 399 | } | 450 | } | 
| @@ -406,6 +457,9 @@ int main() | |||
| 406 | lpos.w = 300; | 457 | lpos.w = 300; | 
| 407 | lpos.h = 20; | 458 | lpos.h = 20; | 
| 408 | Label scoreLabel = Label("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier))), lpos); | 459 | Label scoreLabel = Label("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier))), lpos); | 
| 460 | double maxjumpx = 0; | ||
| 461 | double maxjumpy = 0; | ||
| 462 | double lastjumppos = 0; | ||
| 409 | 463 | ||
| 410 | while(!Game_Quit) | 464 | while(!Game_Quit) | 
| 411 | { | 465 | { | 
| @@ -437,7 +491,7 @@ int main() | |||
| 437 | nparticles.push_back(particles[i]); | 491 | nparticles.push_back(particles[i]); | 
| 438 | } | 492 | } | 
| 439 | } | 493 | } | 
| 440 | if(!(steps%100) && hardcore) | 494 | if(!(steps%300) && hardcore) | 
| 441 | { | 495 | { | 
| 442 | Obstacle tempo = Obstacle(110*100,ground_level, 10, 10); | 496 | Obstacle tempo = Obstacle(110*100,ground_level, 10, 10); | 
| 443 | tempo.vx = -60; | 497 | tempo.vx = -60; | 
| @@ -460,17 +514,19 @@ int main() | |||
| 460 | } | 514 | } | 
| 461 | } | 515 | } | 
| 462 | particles = nparticles; | 516 | particles = nparticles; | 
| 463 | for(int i = 0; i < 10; ++i) | 517 | for(int i = 0; i < 1; ++i) | 
| 464 | { | 518 | { | 
| 465 | Particle p = Particle(-300 + rand()%10000,ground_level + rand()%((int) (yres- ground_level)),3,255,255,255); | 519 | Particle p = Particle(-300 + rand()%11001,yres,3,255,255,255); | 
| 466 | p.vy = -6; | 520 | p.vy = -5-rand()%30/9.0; | 
| 521 | p.life = 1000; | ||
| 467 | particles.push_back(p); | 522 | particles.push_back(p); | 
| 468 | } | 523 | } | 
| 469 | if(!lost) | 524 | if(!lost) | 
| 470 | { | 525 | { | 
| 471 | x_pos = x_pos + x_vel/60.0; | 526 | x_pos = x_pos + x_vel/60.0; | 
| 472 | y_pos = y_pos + y_vel/60.0; | 527 | y_pos = y_pos + y_vel/60.0; | 
| 473 | drawCircle(screen, xres/4, fabs(yres-y_pos), ball_rad, playerR, playerG, playerB); | 528 | phi = phi + vphi/60.0; | 
| 529 | drawCircle(screen, xres/4, fabs(yres-y_pos), phi, ball_rad, playerR, playerG, playerB); | ||
| 474 | scoreLabel.setCaption("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)))); | 530 | scoreLabel.setCaption("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)))); | 
| 475 | } | 531 | } | 
| 476 | 532 | ||
| @@ -479,25 +535,24 @@ int main() | |||
| 479 | { | 535 | { | 
| 480 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | 536 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | 
| 481 | } | 537 | } | 
| 482 | space = keyState[SDLK_SPACE]; | ||
| 483 | if(space) | ||
| 484 | { | ||
| 485 | cout << "x-Position = " << x_pos << endl; | ||
| 486 | } | ||
| 487 | 538 | ||
| 488 | if(y_pos > ground_level+ball_rad) | 539 | if(y_pos > ground_level+ball_rad) | 
| 489 | { | 540 | { | 
| 490 | y_vel = y_vel*(1.0-c_r) - g/60.0; | 541 | y_vel = y_vel*(1.0-c_r) - g/60.0; | 
| 491 | x_vel = x_vel*(1.0-c_r) + right*80/60.0; | 542 | x_vel = x_vel*(1.0-c_r) + right*80/60.0; | 
| 543 | maxjumpy = max(maxjumpy,y_pos); | ||
| 492 | } | 544 | } | 
| 493 | if(y_pos <= ground_level+ball_rad) | 545 | if(y_pos <= ground_level+ball_rad) | 
| 494 | { | 546 | { | 
| 547 | maxjumpx = max(maxjumpx,x_pos-lastjumppos); | ||
| 548 | lastjumppos = x_pos; | ||
| 495 | x_vel = x_vel*(1.0-c_r) + right*140./60.0; | 549 | x_vel = x_vel*(1.0-c_r) + right*140./60.0; | 
| 496 | y_pos = ground_level+ball_rad; | 550 | y_pos = ground_level+ball_rad; | 
| 497 | y_vel = fabs(y_vel); | 551 | y_vel = fabs(y_vel); | 
| 498 | if(down == -1) | 552 | if(down == -1) | 
| 499 | y_vel += 900; | 553 | y_vel += 900; | 
| 500 | y_vel *= 0.25; | 554 | y_vel *= 0.25; | 
| 555 | vphi = -x_vel/ball_rad; | ||
| 501 | } | 556 | } | 
| 502 | if(!lost) | 557 | if(!lost) | 
| 503 | if(highscore+x_pos*position_multiplier <= 0) | 558 | if(highscore+x_pos*position_multiplier <= 0) | 
| @@ -535,6 +590,9 @@ int main() | |||
| 535 | if(1000/60.0 - time > 0) | 590 | if(1000/60.0 - time > 0) | 
| 536 | SDL_Delay(1000/60.0 - time); | 591 | SDL_Delay(1000/60.0 - time); | 
| 537 | } | 592 | } | 
| 593 | cout << "Longest Jump: " << maxjumpx << endl; | ||
| 594 | cout << "Highest Jump: " << maxjumpy << endl; | ||
| 595 | |||
| 538 | 596 | ||
| 539 | /*The Game has ended*/ | 597 | /*The Game has ended*/ | 
| 540 | if(!quickquit) | 598 | if(!quickquit) | 
| @@ -594,6 +652,7 @@ int main() | |||
| 594 | cout << highlist[0].second << ": " << highlist[0].first << '\n'; | 652 | cout << highlist[0].second << ": " << highlist[0].first << '\n'; | 
| 595 | cout << endl; | 653 | cout << endl; | 
| 596 | } | 654 | } | 
| 655 | SDL_FreeSurface(preBall); | ||
| 597 | 656 | ||
| 598 | atexit(SDL_Quit); | 657 | atexit(SDL_Quit); | 
| 599 | 658 | ||
| @@ -3,7 +3,7 @@ | |||
| 3 | all: JnR | 3 | all: JnR | 
| 4 | 4 | ||
| 5 | JnR: JnR.cpp | 5 | JnR: JnR.cpp | 
| 6 | g++ JnR.cpp -O2 -Wall -lSDL -std=c++11 -lSDL_ttf -o JnR | 6 | g++ JnR.cpp -O2 -Wall -ggdb -lSDL -lSDL_gfx -lSDL_ttf -lSDL_image -o JnR | 
| 7 | 7 | ||
| 8 | clean: | 8 | clean: | 
| 9 | rm -f JnR | 9 | rm -f JnR | 
| diff --git a/blank.bmp b/blank.bmp new file mode 100644 index 0000000..9889159 --- /dev/null +++ b/blank.bmp | |||
| Binary files differ | |||
| diff --git a/random.cpp b/random.cpp new file mode 100644 index 0000000..92a3c4c --- /dev/null +++ b/random.cpp | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | |||
| 2 | long long rand_state = 24; | ||
| 3 | long long rand_para1 = 37; | ||
| 4 | long long rand_para2 = 37; | ||
| 5 | |||
| 6 | int randn() | ||
| 7 | { | ||
| 8 | rand_state = (rand_state*rand_state + rand_state*rand_para1 +rand_para2)%15555557; | ||
| 9 | return rand_state; | ||
| 10 | } | ||
| 11 | |||
| 12 | //seed with number from 0 to 672 | ||
| 13 | void srandn(int seed) | ||
| 14 | { | ||
| 15 | rand_state = seed + 2753; | ||
| 16 | |||
| 17 | randn(); | ||
| 18 | } | ||
| diff --git a/random.h b/random.h new file mode 100644 index 0000000..2050414 --- /dev/null +++ b/random.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | #ifndef RANDOM_H | ||
| 2 | #define RANDOM_H | ||
| 3 | |||
| 4 | #include "random.cpp" | ||
| 5 | |||
| 6 | //guarenteed no loop with length < 150 when less then 5000 times called | ||
| 7 | int randn(); | ||
| 8 | |||
| 9 | //seed with number from 0 to 672 | ||
| 10 | void srandn(); | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | #endif | ||
