diff options
-rw-r--r-- | JnR.cpp | 530 |
1 files changed, 329 insertions, 201 deletions
@@ -167,9 +167,24 @@ void drawPixel(SDL_Surface *screen,int x, int y, Uint8 R, Uint8 G, Uint8 B) | |||
167 | SDL_FillRect(screen,&r,color); | 167 | SDL_FillRect(screen,&r,color); |
168 | } | 168 | } |
169 | 169 | ||
170 | SDL_Surface* preBall; | 170 | void drawLine(SDL_Surface* screen,double x0,double y0,double x1, double y1, Uint8 R, Uint8 G, Uint8 B) |
171 | { | ||
172 | double x = x0; | ||
173 | double y = y0; | ||
174 | double dir_x = x1 - x0; | ||
175 | double dir_y = y1 - y0; | ||
176 | double norm = dir_x*dir_x + dir_y*dir_y; | ||
177 | dir_x /= sqrt(2*norm); | ||
178 | dir_y /= sqrt(2*norm); | ||
179 | while((x1-x)*dir_x + (y1-y)*dir_y > 0) | ||
180 | { | ||
181 | drawPixel(screen, x, y, R,G,B); | ||
182 | x += dir_x; | ||
183 | y += dir_y; | ||
184 | } | ||
185 | } | ||
171 | 186 | ||
172 | void drawCircle(SDL_Surface* screen, double x, double y, double angle, double radius, Uint8 R, Uint8 G, Uint8 B) | 187 | void drawCircle(SDL_Surface* screen, SDL_Surface* preBall, double x, double y, double angle, double radius, Uint8 R, Uint8 G, Uint8 B) |
173 | { | 188 | { |
174 | int tempseed = rand(); | 189 | int tempseed = rand(); |
175 | srand(87); | 190 | srand(87); |
@@ -301,7 +316,7 @@ public: | |||
301 | }; | 316 | }; |
302 | 317 | ||
303 | 318 | ||
304 | vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, double vyb, int mode) | 319 | vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, double vyb) |
305 | { | 320 | { |
306 | vector<Particle> result; | 321 | vector<Particle> result; |
307 | for(int i = 0; i < 2000; ++i) | 322 | for(int i = 0; i < 2000; ++i) |
@@ -315,37 +330,25 @@ vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, do | |||
315 | Particle temp = Particle(tx, ty, 1, playerR, playerG, playerB); | 330 | Particle temp = Particle(tx, ty, 1, playerR, playerG, playerB); |
316 | double vb = (rand()%5000)/30.0; | 331 | double vb = (rand()%5000)/30.0; |
317 | double vp = ((rand()%10000)/10000.0)*2*M_PI; | 332 | double vp = ((rand()%10000)/10000.0)*2*M_PI; |
318 | if(!mode) | 333 | |
319 | { | 334 | temp.vx = cos(vp)*vb+vxb*(rand()%1000)/1000.0; |
320 | temp.vx = cos(vp)*vb+vxb; | 335 | temp.vy = sin(vp)*vb+vyb*(rand()%1000)/1000.0; |
321 | temp.vy = sin(vp)*vb+vyb; | 336 | |
322 | } | ||
323 | else | ||
324 | { | ||
325 | temp.vx = cos(vp)*vb+vxb*(rand()%1000)/1000.0; | ||
326 | temp.vy = sin(vp)*vb+vyb*(rand()%1000)/1000.0; | ||
327 | } | ||
328 | temp.life = (rand()%100)/50.0; | 337 | temp.life = (rand()%100)/50.0; |
329 | result.push_back(temp); | 338 | result.push_back(temp); |
330 | } | 339 | } |
331 | return result; | 340 | return result; |
332 | } | 341 | } |
333 | 342 | ||
334 | double x_pos = 0; | ||
335 | double x_vel = 0; | ||
336 | double phi = 0; | ||
337 | double vphi = 0; | ||
338 | double ground_level = 20; | 343 | double ground_level = 20; |
339 | double y_vel = 0; | ||
340 | double ball_rad = 8; | 344 | double ball_rad = 8; |
341 | double y_pos = ground_level + ball_rad; | 345 | double y_pos = ground_level + ball_rad; |
342 | double g = 300.0; | 346 | double g = 300.0; |
343 | double c_r = 0.004; | 347 | double c_r = 0.004; |
344 | double highscore = 1000; | ||
345 | double position_multiplier = 3; | ||
346 | double win_bonus = 5000; | 348 | double win_bonus = 5000; |
347 | bool hardcore = false; | 349 | string name; |
348 | 350 | ||
351 | //loads the "settings" (color sceme) | ||
349 | void loadConstants() | 352 | void loadConstants() |
350 | { | 353 | { |
351 | ifstream ins; | 354 | ifstream ins; |
@@ -366,23 +369,20 @@ void loadConstants() | |||
366 | ins.close(); | 369 | ins.close(); |
367 | } | 370 | } |
368 | 371 | ||
369 | int main() | 372 | |
373 | //a console io menu with limited possibility to set difficulty and hardcore mode | ||
374 | void pregame_menu(string& name, int& difficulty, double& position_multiplier, bool& hardcore) | ||
370 | { | 375 | { |
371 | loadConstants(); | ||
372 | |||
373 | string name; | ||
374 | cout << "This is a simple jump and run" << endl; | 376 | cout << "This is a simple jump and run" << endl; |
375 | cout << "Please enter your name:"; | 377 | cout << "Please enter your name:"; |
376 | cin >> name; | 378 | cin >> name; |
377 | cout << "Please enter difficulty (0-100):"; | 379 | cout << "Please enter difficulty (0-100):"; |
378 | int difficulty; | ||
379 | cin >> difficulty; | 380 | cin >> difficulty; |
380 | cout << "Do you want to play in hardcore mode(0/1)?"; | 381 | cout << "Do you want to play in hardcore mode(0/1)?"; |
381 | cin >> hardcore; | 382 | cin >> hardcore; |
382 | if(hardcore) | 383 | if(hardcore) |
383 | { | 384 | { |
384 | highscore *= 1.5; | 385 | cout << "You are insane. +100% score!" << endl; |
385 | cout << "You are insane. +50% score!" << endl; | ||
386 | } | 386 | } |
387 | else | 387 | else |
388 | cout << "Very sane decision..." << endl; | 388 | cout << "Very sane decision..." << endl; |
@@ -393,14 +393,18 @@ int main() | |||
393 | difficulty = max(-50,difficulty); | 393 | difficulty = max(-50,difficulty); |
394 | if(difficulty > 100) | 394 | if(difficulty > 100) |
395 | difficulty = 100; | 395 | difficulty = 100; |
396 | bool wantEnd = false; | ||
397 | cout << "difficulty = " << difficulty << endl; | 396 | cout << "difficulty = " << difficulty << endl; |
397 | } | ||
398 | |||
399 | //initializes the imported libs and opens a window | ||
400 | int init_libs(SDL_Surface **screen) | ||
401 | { | ||
398 | if(SDL_Init(SDL_INIT_VIDEO) == -1) | 402 | if(SDL_Init(SDL_INIT_VIDEO) == -1) |
399 | { | 403 | { |
400 | cout << "Error: Could not initialize SDL" << endl; | 404 | cout << "Error: Could not initialize SDL" << endl; |
401 | return 1; | 405 | return 1; |
402 | } | 406 | } |
403 | screen = SDL_SetVideoMode(xres,yres,32,SDL_SWSURFACE); | 407 | *screen = SDL_SetVideoMode(xres,yres,32,SDL_SWSURFACE); |
404 | if(!screen) | 408 | if(!screen) |
405 | { | 409 | { |
406 | cout << "could not initialize screen" << endl; | 410 | cout << "could not initialize screen" << endl; |
@@ -413,201 +417,313 @@ int main() | |||
413 | return 1; | 417 | return 1; |
414 | } | 418 | } |
415 | SDL_WM_SetCaption("Ballroller - v0.3", NULL); | 419 | SDL_WM_SetCaption("Ballroller - v0.3", NULL); |
416 | while(!wantEnd) | 420 | return 0; |
421 | } | ||
422 | |||
423 | |||
424 | class GameHandle | ||
425 | { | ||
426 | //will always contain the CURRENT highscore | ||
427 | double highscore; | ||
428 | //level number | ||
429 | int difficulty; | ||
430 | bool hardcore; | ||
431 | double x_pos; | ||
432 | double y_pos; | ||
433 | double x_vel; | ||
434 | double y_vel; | ||
435 | double vphi; | ||
436 | double phi; | ||
437 | double position_multiplier; | ||
438 | vector<Obstacle> obstacles; | ||
439 | vector<Particle> particles; | ||
440 | SDL_Surface* preBall; | ||
441 | SDL_Surface* screen; | ||
442 | bool game_Quit; | ||
443 | bool quickquit; | ||
444 | Label* scoreLabel; | ||
445 | Label* message; | ||
446 | //-1,0,1 respectively depending on the status of ther arrow keys | ||
447 | int down; | ||
448 | int right; | ||
449 | double g_x; | ||
450 | double g_y; | ||
451 | |||
452 | void game_reset() | ||
417 | { | 453 | { |
418 | //SEED | 454 | //SEED |
419 | srandn(2334508 + difficulty); | 455 | srandn(2334508 + difficulty); |
420 | srand(2334508 + difficulty); | 456 | srand(2334508 + difficulty); |
421 | 457 | ||
422 | highscore = 1000; | 458 | highscore = 1000; |
423 | position_multiplier = 3; | ||
424 | x_pos = 0; | 459 | x_pos = 0; |
425 | y_pos = 0; | 460 | y_pos = 0; |
426 | y_vel = 0; | 461 | y_vel = 0; |
427 | x_vel = 0; | 462 | x_vel = 0; |
463 | vphi = 0; | ||
464 | phi = 0; | ||
465 | position_multiplier = 3; | ||
466 | //higher levels give more score | ||
428 | position_multiplier += difficulty/40.0; | 467 | position_multiplier += difficulty/40.0; |
429 | preBall = loadBMP("blank.bmp"); | 468 | //hardcore gives 100% score bonus |
430 | 469 | if(hardcore) | |
431 | bool Game_Quit = false; | 470 | highscore *= 2; |
432 | int down = 0; | 471 | game_Quit = false; |
433 | int right = 0; | 472 | quickquit = false; |
434 | vector<Obstacle> obstacles; | 473 | |
474 | //creating the level | ||
475 | obstacles = vector<Obstacle>(); | ||
435 | obstacles.push_back(Obstacle(-50,ground_level,10,200)); | 476 | obstacles.push_back(Obstacle(-50,ground_level,10,200)); |
436 | vector<Particle> particles; | 477 | //Longest Jump: 608 Highest Jump: 150 (TODO: filter obstacles based on that) |
437 | bool quickquit = false; | ||
438 | for(int i = 0; i < 10*60*5; ++i) | ||
439 | { | ||
440 | Particle p = Particle(rand()%11000 - 300,ground_level + rand()%((int)(yres-ground_level)),3,255,255,255); | ||
441 | p.life = 1000; | ||
442 | p.vy = -5-rand()%30/9.0; | ||
443 | particles.push_back(p); | ||
444 | } | ||
445 | //Longest Jump: 608 | ||
446 | //Highest Jump: 150 | ||
447 | int opos = 200; | 478 | int opos = 200; |
448 | while(opos < 10000) | 479 | while(opos < 10000) |
449 | { | 480 | { |
450 | int blub = 4+randn()%(192+difficulty/4-4); | 481 | int blub = 4+randn()%(162+difficulty/2-4); |
451 | obstacles.push_back(Obstacle(opos,ground_level,blub,(196 + difficulty/4)/2-blub/2)); | 482 | obstacles.push_back(Obstacle(opos,ground_level,blub,(176 + difficulty/2)/2-blub/2)); |
452 | opos += randn()%(900-4*difficulty); | 483 | opos += randn()%(890-9*difficulty/2); |
453 | } | 484 | } |
485 | //in hardcore mode, there are boxes going from right to left | ||
454 | if(hardcore) | 486 | if(hardcore) |
455 | for(int i = 1; i < 110; ++i) | 487 | for(int i = 1; i < 110; ++i) |
456 | { | 488 | { |
457 | Obstacle tempo = Obstacle(i*300,ground_level, 10, 10); | 489 | Obstacle tempo = Obstacle(i*300,ground_level, 10, 10); |
458 | tempo.vx = -60; | 490 | tempo.vx = -60; |
459 | obstacles.push_back(tempo); | 491 | obstacles.push_back(tempo); |
460 | } | 492 | } |
461 | vector<Particle> deathanimation; | 493 | } |
462 | bool lost = false; | 494 | public: |
463 | long long steps = 0; | 495 | ~GameHandle() |
496 | { | ||
497 | SDL_FreeSurface(preBall); | ||
498 | atexit(SDL_Quit); | ||
499 | delete scoreLabel; | ||
500 | } | ||
501 | |||
502 | GameHandle(int Ndifficulty, bool Nhardcore, SDL_Surface* Nscreen) | ||
503 | { | ||
504 | difficulty = Ndifficulty; | ||
505 | hardcore = Nhardcore; | ||
506 | highscore = 1000; | ||
507 | screen = Nscreen; | ||
508 | preBall = loadBMP("blank.bmp"); | ||
509 | for(int i = 0; i < 10*60*5; ++i) | ||
510 | { | ||
511 | Particle p = Particle(rand()%11000 - 300,ground_level + rand()%((int)(yres-ground_level)),3,255,255,255); | ||
512 | p.life = 1000; | ||
513 | p.vy = -5-rand()%30/9.0; | ||
514 | particles.push_back(p); | ||
515 | } | ||
516 | |||
464 | SDL_Rect lpos; | 517 | SDL_Rect lpos; |
465 | lpos.x = 300; | 518 | lpos.x = 300; |
466 | lpos.y = 10; | 519 | lpos.y = 10; |
467 | lpos.w = 300; | 520 | lpos.w = 300; |
468 | lpos.h = 20; | 521 | lpos.h = 20; |
469 | Label scoreLabel = Label("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier))), lpos); | 522 | scoreLabel = new Label("Score: " + lltostr(highscore), lpos); |
470 | double maxjumpx = 0; | 523 | } |
471 | double maxjumpy = 0; | ||
472 | double lastjumppos = 0; | ||
473 | 524 | ||
474 | while(!Game_Quit) | 525 | void update_scoreLabel() |
475 | { | 526 | { |
476 | steps++; | 527 | scoreLabel->setCaption("Score: " + lltostr(highscore)); |
477 | Uint32 start = SDL_GetTicks(); | 528 | } |
478 | if(!lost) | ||
479 | highscore--; | ||
480 | blacken(screen); | ||
481 | SDL_Event event; | ||
482 | while(SDL_PollEvent(&event)) | ||
483 | { | ||
484 | if(event.type == SDL_QUIT) | ||
485 | { | ||
486 | Game_Quit = true; | ||
487 | quickquit = true; | ||
488 | wantEnd = true; | ||
489 | break; | ||
490 | } | ||
491 | } | ||
492 | Uint8 *keyState = SDL_GetKeyState(NULL); | ||
493 | 529 | ||
494 | down = -keyState[SDLK_UP]+keyState[SDLK_DOWN]; | 530 | void paint(bool win, bool showmessage) |
531 | { | ||
532 | blacken(screen); | ||
533 | for(int i = 0; i < (int) particles.size(); ++i) | ||
534 | particles[i].draw(screen,x_pos-xres/4,0); | ||
535 | for(int i = 0; i < (int) obstacles.size(); ++i) | ||
536 | obstacles[i].draw(screen,x_pos-xres/4,0); | ||
537 | if(win) | ||
538 | drawCircle(screen, preBall, xres/4, fabs(yres-y_pos), phi, ball_rad, playerR, playerG, playerB); | ||
539 | for(int i = 0; i < xres; ++i) | ||
540 | drawPixel(screen,i,yres-ground_level,255,255,255); | ||
541 | scoreLabel->draw(screen); | ||
542 | if(showmessage) | ||
543 | message->draw(screen); | ||
495 | 544 | ||
496 | vector<Particle> nparticles; | 545 | if(hardcore) |
497 | for(int i = 0; i < (int) particles.size(); ++i) | 546 | drawLine(screen,500,20,500 + g_x/3,20 - g_y/3,255,255,255); |
498 | { | 547 | SDL_Flip(screen); |
499 | if(particles[i].step(1/60.0) && particles[i].y > ground_level) | 548 | } |
500 | { | 549 | |
501 | particles[i].draw(screen,x_pos-xres/4,0); | 550 | void step(int steps, bool inGame, double t) |
502 | nparticles.push_back(particles[i]); | 551 | { |
503 | } | 552 | if(inGame) |
504 | } | 553 | { |
554 | //spawning a new obstacle for hardcoremode | ||
505 | if(!(steps%300) && hardcore) | 555 | if(!(steps%300) && hardcore) |
506 | { | 556 | { |
507 | Obstacle tempo = Obstacle(110*100,ground_level, 10, 10); | 557 | Obstacle tempo = Obstacle(110*300,ground_level, 10, 10); |
508 | tempo.vx = -60; | 558 | tempo.vx = -60; |
509 | obstacles.push_back(tempo); | 559 | obstacles.push_back(tempo); |
510 | } | 560 | } |
511 | for(int i = 0; i < (int) obstacles.size(); ++i) | 561 | for(int i = 0; i < (int) obstacles.size(); ++i) |
512 | { | ||
513 | obstacles[i].step(1/60.0); | 562 | obstacles[i].step(1/60.0); |
514 | obstacles[i].draw(screen,x_pos-xres/4,0); | 563 | |
515 | if(!lost) | 564 | x_pos = x_pos + x_vel/60.0; |
516 | if(obstacles[i].check_death(x_pos,y_pos,ball_rad)) | 565 | y_pos = y_pos + y_vel/60.0; |
566 | //updating highscore | ||
567 | highscore -= 1.0; | ||
568 | highscore += x_vel/60.0*position_multiplier*(1+hardcore); | ||
569 | phi = phi + vphi/60.0; | ||
570 | if(hardcore) | ||
571 | { | ||
572 | double vart = (sin(t))/7; | ||
573 | g_x = g*sin(vart); | ||
574 | g_y = - g*cos(vart); | ||
575 | cout << g_x << endl; | ||
576 | if(y_pos > ground_level+ball_rad) | ||
517 | { | 577 | { |
518 | cout << "Collision: You lost the game" << endl; | 578 | y_vel = y_vel*(1.0-c_r) + g_y/60; |
519 | cout << "Better luck next time!" << endl; | 579 | x_vel = x_vel*(1.0-c_r) + right*80/60.0 + g_x/60; |
520 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)); | 580 | } |
521 | scoreLabel.setCaption("Score: " + lltostr(highscore)); | 581 | if(y_pos <= ground_level+ball_rad) |
522 | cout << "Your highscore: " << highscore << endl; | 582 | { |
523 | lost = true; | 583 | x_vel = x_vel*(1.0-c_r) + right*140./60.0 + g_x/60; |
524 | deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,1); | 584 | y_pos = ground_level+ball_rad; |
585 | y_vel = fabs(y_vel); | ||
586 | if(down == -1) | ||
587 | y_vel += 900; | ||
588 | y_vel *= 0.25; | ||
589 | /*double Dvphi = (x_vel/ball_rad+vphi)/2; | ||
590 | x_vel -= Dvphi*2;*/ | ||
591 | vphi += x_vel/ball_rad; | ||
525 | } | 592 | } |
526 | } | 593 | } |
527 | particles = nparticles; | 594 | else |
528 | for(int i = 0; i < 1; ++i) | ||
529 | { | 595 | { |
530 | Particle p = Particle(-300 + rand()%11001,yres,3,255,255,255); | 596 | if(y_pos > ground_level+ball_rad) |
531 | p.vy = -5-rand()%30/9.0; | 597 | { |
532 | p.life = 1000; | 598 | y_vel = y_vel*(1.0-c_r) - g/60.0; |
533 | particles.push_back(p); | 599 | x_vel = x_vel*(1.0-c_r) + right*80/60.0; |
600 | } | ||
601 | if(y_pos <= ground_level+ball_rad) | ||
602 | { | ||
603 | x_vel = x_vel*(1.0-c_r) + right*140./60.0; | ||
604 | y_pos = ground_level+ball_rad; | ||
605 | y_vel = fabs(y_vel); | ||
606 | if(down == -1) | ||
607 | y_vel += 900; | ||
608 | y_vel *= 0.25; | ||
609 | vphi = -x_vel/ball_rad; | ||
610 | } | ||
534 | } | 611 | } |
535 | if(!lost) | 612 | } |
613 | |||
614 | //deleting old particles | ||
615 | vector<Particle> nparticles; | ||
616 | for(int i = 0; i < (int) particles.size(); ++i) | ||
617 | { | ||
618 | if(particles[i].step(1/60.0) && particles[i].y > ground_level) | ||
536 | { | 619 | { |
537 | x_pos = x_pos + x_vel/60.0; | 620 | nparticles.push_back(particles[i]); |
538 | y_pos = y_pos + y_vel/60.0; | ||
539 | phi = phi + vphi/60.0; | ||
540 | drawCircle(screen, xres/4, fabs(yres-y_pos), phi, ball_rad, playerR, playerG, playerB); | ||
541 | scoreLabel.setCaption("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)))); | ||
542 | } | 621 | } |
622 | } | ||
543 | 623 | ||
544 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | 624 | particles = nparticles; |
545 | if(!right) | 625 | for(int i = 0; i < 1; ++i) |
626 | { | ||
627 | Particle p = Particle(-300 + rand()%11001,yres,3,255,255,255); | ||
628 | p.vy = -5-rand()%30/9.0; | ||
629 | p.life = 1000; | ||
630 | particles.push_back(p); | ||
631 | } | ||
632 | } | ||
633 | |||
634 | void handle_Events(bool &game_Quit, int &down, int& right) | ||
635 | { | ||
636 | SDL_Event event; | ||
637 | while(SDL_PollEvent(&event)) | ||
638 | { | ||
639 | if(event.type == SDL_QUIT) | ||
546 | { | 640 | { |
547 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | 641 | game_Quit = true; |
642 | break; | ||
548 | } | 643 | } |
644 | } | ||
645 | Uint8 *keyState = SDL_GetKeyState(NULL); | ||
646 | |||
647 | down = -keyState[SDLK_UP]+keyState[SDLK_DOWN]; | ||
648 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | ||
649 | } | ||
650 | |||
651 | void animate_death() | ||
652 | { | ||
653 | vector<Particle> deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel); | ||
654 | for(int i = 0; i < (int) deathanimation.size(); ++i) | ||
655 | particles.push_back(deathanimation[i]); | ||
656 | } | ||
657 | |||
658 | void play() | ||
659 | { | ||
660 | //closes gamewindow | ||
661 | bool quit_Game = false; | ||
662 | while(!quit_Game) | ||
663 | { | ||
664 | //won or lost the game | ||
665 | bool game_Over = false; | ||
666 | //resets the game variables to startpoint | ||
667 | game_reset(); | ||
549 | 668 | ||
550 | if(y_pos > ground_level+ball_rad) | 669 | bool win = false; |
551 | { | 670 | double time = 0; |
552 | y_vel = y_vel*(1.0-c_r) - g/60.0; | 671 | int steps = 0; |
553 | x_vel = x_vel*(1.0-c_r) + right*80/60.0; | 672 | |
554 | maxjumpy = max(maxjumpy,y_pos); | 673 | while(!game_Over) |
555 | } | ||
556 | if(y_pos <= ground_level+ball_rad) | ||
557 | { | ||
558 | maxjumpx = max(maxjumpx,x_pos-lastjumppos); | ||
559 | lastjumppos = x_pos; | ||
560 | x_vel = x_vel*(1.0-c_r) + right*140./60.0; | ||
561 | y_pos = ground_level+ball_rad; | ||
562 | y_vel = fabs(y_vel); | ||
563 | if(down == -1) | ||
564 | y_vel += 900; | ||
565 | y_vel *= 0.25; | ||
566 | vphi = -x_vel/ball_rad; | ||
567 | } | ||
568 | if(!lost) | ||
569 | if(highscore+x_pos*position_multiplier <= 0) | ||
570 | { | ||
571 | cout << "TIMEOUT: You lost the game." << endl; | ||
572 | cout << "Better luck next time!" << endl; | ||
573 | quickquit = true; | ||
574 | Game_Quit = true; | ||
575 | deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,0); | ||
576 | } | ||
577 | if(x_pos >= 10000) | ||
578 | { | ||
579 | cout << "You win the game!" << endl; | ||
580 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier + win_bonus)); | ||
581 | cout << "Your highscore: " << highscore << endl; | ||
582 | scoreLabel.setCaption("Score: " + lltostr(highscore)); | ||
583 | Game_Quit = true; | ||
584 | } | ||
585 | for(int i = 0; i < xres; ++i) | ||
586 | drawPixel(screen,i,yres-ground_level,255,255,255); | ||
587 | bool animation_term = true; | ||
588 | for(int i = 0; i < (int) deathanimation.size(); ++i) | ||
589 | { | 674 | { |
590 | if(deathanimation[i].step(1/60.0)) | 675 | Uint32 start = SDL_GetTicks(); |
676 | handle_Events(game_Quit,down, right); | ||
677 | if(game_Quit) | ||
591 | { | 678 | { |
592 | animation_term = false; | 679 | game_Over = true; |
593 | deathanimation[i].draw(screen,x_pos-xres/4,0); | 680 | break; |
594 | } | 681 | } |
682 | |||
683 | step(steps, true,time); | ||
684 | |||
685 | for(int i = 0; i < (int) obstacles.size(); ++i) | ||
686 | { | ||
687 | if(obstacles[i].check_death(x_pos,y_pos,ball_rad)) | ||
688 | { | ||
689 | cout << "Collision: You lost the game" << endl; | ||
690 | cout << "Better luck next time!" << endl; | ||
691 | update_scoreLabel(); | ||
692 | cout << "Your highscore: " << highscore << endl; | ||
693 | game_Over = true; | ||
694 | animate_death(); | ||
695 | } | ||
696 | } | ||
697 | if(game_Over) | ||
698 | break; | ||
699 | update_scoreLabel(); | ||
700 | steps++; | ||
701 | time += 1/60.0; | ||
702 | |||
703 | if(highscore <= 0) | ||
704 | { | ||
705 | cout << "TIMEOUT: You lost the game." << endl; | ||
706 | cout << "Better luck next time!" << endl; | ||
707 | game_Over = true; | ||
708 | animate_death(); | ||
709 | break; | ||
710 | } | ||
711 | if(x_pos >= 10000) | ||
712 | { | ||
713 | cout << "You win the game!" << endl; | ||
714 | cout << "Your highscore: " << highscore << endl; | ||
715 | update_scoreLabel(); | ||
716 | game_Over = true; | ||
717 | win = true; | ||
718 | break; | ||
719 | } | ||
720 | paint(true,false); | ||
721 | Uint32 time = SDL_GetTicks()-start; | ||
722 | if(1000/60.0 - time > 0) | ||
723 | SDL_Delay(1000/60.0 - time); | ||
595 | } | 724 | } |
596 | if(animation_term && lost) | ||
597 | Game_Quit = true; | ||
598 | scoreLabel.draw(screen); | ||
599 | SDL_Flip(screen); | ||
600 | Uint32 time = SDL_GetTicks()-start; | ||
601 | if(1000/60.0 - time > 0) | ||
602 | SDL_Delay(1000/60.0 - time); | ||
603 | } | ||
604 | cout << "Longest Jump: " << maxjumpx << endl; | ||
605 | cout << "Highest Jump: " << maxjumpy << endl; | ||
606 | 725 | ||
607 | 726 | /*finished a game, end-Game screen*/ | |
608 | /*The Game has ended*/ | ||
609 | if(!quickquit) | ||
610 | { | ||
611 | ifstream ins; | 727 | ifstream ins; |
612 | ins.open("highscore.dat"); | 728 | ins.open("highscore.dat"); |
613 | vector<pair<int, string> > highlist; | 729 | vector<pair<int, string> > highlist; |
@@ -641,8 +757,7 @@ int main() | |||
641 | { | 757 | { |
642 | for(int i = 0; i < 10; ++i) | 758 | for(int i = 0; i < 10; ++i) |
643 | { | 759 | { |
644 | ofs << highlist[i+1].first << " " << highlist[i+1].second << endl; | 760 | ofs << highlist[i+1].first << " " << highlist[i+1].second << endl; } |
645 | } | ||
646 | } | 761 | } |
647 | else | 762 | else |
648 | { | 763 | { |
@@ -651,8 +766,7 @@ int main() | |||
651 | ofs << highlist[i].first << " " << highlist[i].second << endl; | 766 | ofs << highlist[i].first << " " << highlist[i].second << endl; |
652 | } | 767 | } |
653 | } | 768 | } |
654 | 769 | ofs.close(); | |
655 | ofs.close(); | ||
656 | } | 770 | } |
657 | cout << "\n**HIGHSCORE**\n"; | 771 | cout << "\n**HIGHSCORE**\n"; |
658 | for(int i = min(10,(int) highlist.size()-1); i > 0; --i) | 772 | for(int i = min(10,(int) highlist.size()-1); i > 0; --i) |
@@ -662,36 +776,50 @@ int main() | |||
662 | if(highlist.size() < 11) | 776 | if(highlist.size() < 11) |
663 | cout << highlist[0].second << ": " << highlist[0].first << '\n'; | 777 | cout << highlist[0].second << ": " << highlist[0].first << '\n'; |
664 | cout << endl; | 778 | cout << endl; |
665 | } | ||
666 | 779 | ||
667 | SDL_Rect mpos; | 780 | SDL_Rect mpos; |
668 | mpos.x = 25; | 781 | mpos.x = 25; |
669 | mpos.y = 180; | 782 | mpos.y = 180; |
670 | mpos.w = 300; | 783 | mpos.w = 300; |
671 | mpos.h = 32; | 784 | mpos.h = 32; |
672 | Label message = Label("GAME OVER - Press space to restart", mpos); | 785 | if(win) |
673 | message.draw(screen); | 786 | message = new Label("YOU WIN! - Press space to restart",mpos); |
674 | SDL_Flip(screen); | 787 | else |
675 | while(true) | 788 | message = new Label("GAME OVER - Press space to restart", mpos); |
676 | { | 789 | while(quit_Game == false) |
677 | SDL_Event event; | ||
678 | SDL_PollEvent(&event); | ||
679 | Uint8 *keyState = SDL_GetKeyState(NULL); | ||
680 | if(event.type == SDL_QUIT || keyState[SDLK_ESCAPE]) | ||
681 | { | 790 | { |
682 | Game_Quit = true; | 791 | SDL_Event event; |
683 | quickquit = true; | 792 | SDL_PollEvent(&event); |
684 | wantEnd = true; | 793 | Uint8 *keyState = SDL_GetKeyState(NULL); |
685 | break; | 794 | if(event.type == SDL_QUIT || keyState[SDLK_ESCAPE]) |
795 | { | ||
796 | quit_Game = true; | ||
797 | break; | ||
798 | } | ||
799 | if(keyState[SDLK_SPACE] || keyState[SDLK_RETURN]) | ||
800 | break; | ||
801 | step(steps,false,time); | ||
802 | paint(win,true); | ||
803 | SDL_Delay(1000/60.0); | ||
686 | } | 804 | } |
687 | if(keyState[SDLK_SPACE]) | 805 | delete message; |
688 | break; | ||
689 | |||
690 | } | 806 | } |
691 | } | 807 | } |
692 | SDL_FreeSurface(preBall); | 808 | }; |
809 | |||
810 | int main() | ||
811 | { | ||
812 | loadConstants(); | ||
813 | bool hardcore = false; | ||
814 | int difficulty = 0; | ||
815 | double position_multiplier = 1; | ||
816 | pregame_menu(name,difficulty, position_multiplier, hardcore); | ||
817 | if(init_libs(&screen)) | ||
818 | return 1; | ||
819 | GameHandle game = GameHandle(difficulty, hardcore,screen); | ||
820 | game.play(); | ||
693 | 821 | ||
694 | atexit(SDL_Quit); | ||
695 | 822 | ||
696 | return 0; | 823 | return 0; |
697 | } | 824 | } |
825 | |||