From c20945f800a71a90d99b1822b80fe1c35976bdf0 Mon Sep 17 00:00:00 2001 From: Reimar Date: Thu, 4 Dec 2014 13:46:27 +0100 Subject: Added easy restart --- JnR.cpp | 457 +++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 247 insertions(+), 210 deletions(-) diff --git a/JnR.cpp b/JnR.cpp index f9b2500..a70909e 100644 --- a/JnR.cpp +++ b/JnR.cpp @@ -179,8 +179,11 @@ void drawCircle(SDL_Surface* screen, double x, double y, double angle, double ra for(int j = 0; j <= 2*radius; ++j) if(((i-radius+0.5)*(i-radius+0.5)+(j-radius+0.5)*(j-radius+0.5) <= radius*radius)) { - double alt = 0.90+(rand()%10000)/50000.0; - drawPixel(preBall, i,j,R*alt,G*alt,B*alt); + int texturkonstante = 60; + int alt1 = rand()%texturkonstante-texturkonstante/2; + int alt2 = rand()%texturkonstante-texturkonstante/2; + int alt3 = rand()%texturkonstante-texturkonstante/2; + drawPixel(preBall, i,j,R+alt1,G+alt2,B+alt3); } SDL_Rect drawpos; SDL_Surface* temp = preBall; @@ -390,12 +393,8 @@ int main() difficulty = max(-50,difficulty); if(difficulty > 100) difficulty = 100; - //SEED - srandn(2334508 + difficulty); - srand(2334508 + difficulty); - + bool wantEnd = false; cout << "difficulty = " << difficulty << endl; - position_multiplier += difficulty/40.0; if(SDL_Init(SDL_INIT_VIDEO) == -1) { cout << "Error: Could not initialize SDL" << endl; @@ -407,250 +406,288 @@ int main() cout << "could not initialize screen" << endl; return 1; } - + if(TTF_Init() == -1) { cout << "could not initialize True Fonts" << endl; return 1; } SDL_WM_SetCaption("Ballroller - v0.3", NULL); - - preBall = loadBMP("blank.bmp"); - - - bool Game_Quit = false; - int down = 0; - int right = 0; - vector obstacles; - obstacles.push_back(Obstacle(-50,ground_level,10,200)); - vector particles; - bool quickquit = false; - for(int i = 0; i < 10*60*5; ++i) - { - Particle p = Particle(rand()%11000 - 300,ground_level + rand()%((int)(yres-ground_level)),3,255,255,255); - p.life = 1000; - p.vy = -5-rand()%30/9.0; - particles.push_back(p); - } - //Longest Jump: 608 - //Highest Jump: 150 - int opos = 200; - while(opos < 10000) - { - int blub = 4+randn()%(192+difficulty/4-4); - obstacles.push_back(Obstacle(opos,ground_level,blub,(196 + difficulty/4)/2-blub/2)); - opos += randn()%(900-4*difficulty); - } - if(hardcore) - for(int i = 1; i < 110; ++i) + while(!wantEnd) { - Obstacle tempo = Obstacle(i*300,ground_level, 10, 10); - tempo.vx = -60; - obstacles.push_back(tempo); - } - vector deathanimation; - bool lost = false; - long long steps = 0; - SDL_Rect lpos; - lpos.x = 100; - lpos.y = 10; - lpos.w = 300; - lpos.h = 20; - Label scoreLabel = Label("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier))), lpos); - double maxjumpx = 0; - double maxjumpy = 0; - double lastjumppos = 0; + //SEED + srandn(2334508 + difficulty); + srand(2334508 + difficulty); - while(!Game_Quit) - { - steps++; - Uint32 start = SDL_GetTicks(); - if(!lost) - highscore--; - blacken(screen); - SDL_Event event; - while(SDL_PollEvent(&event)) + highscore = 1000; + position_multiplier = 3; + x_pos = 0; + y_pos = 0; + y_vel = 0; + x_vel = 0; + position_multiplier += difficulty/40.0; + preBall = loadBMP("blank.bmp"); + + bool Game_Quit = false; + int down = 0; + int right = 0; + vector obstacles; + obstacles.push_back(Obstacle(-50,ground_level,10,200)); + vector particles; + bool quickquit = false; + for(int i = 0; i < 10*60*5; ++i) { - if(event.type == SDL_QUIT) - { - Game_Quit = true; - quickquit = true; - break; - } + Particle p = Particle(rand()%11000 - 300,ground_level + rand()%((int)(yres-ground_level)),3,255,255,255); + p.life = 1000; + p.vy = -5-rand()%30/9.0; + particles.push_back(p); } - Uint8 *keyState = SDL_GetKeyState(NULL); - - down = -keyState[SDLK_UP]+keyState[SDLK_DOWN]; - - vector nparticles; - for(int i = 0; i < (int) particles.size(); ++i) + //Longest Jump: 608 + //Highest Jump: 150 + int opos = 200; + while(opos < 10000) { - if(particles[i].step(1/60.0) && particles[i].y > ground_level) - { - particles[i].draw(screen,x_pos-xres/4,0); - nparticles.push_back(particles[i]); - } + int blub = 4+randn()%(192+difficulty/4-4); + obstacles.push_back(Obstacle(opos,ground_level,blub,(196 + difficulty/4)/2-blub/2)); + opos += randn()%(900-4*difficulty); } - if(!(steps%300) && hardcore) + if(hardcore) + for(int i = 1; i < 110; ++i) { - Obstacle tempo = Obstacle(110*100,ground_level, 10, 10); + Obstacle tempo = Obstacle(i*300,ground_level, 10, 10); tempo.vx = -60; obstacles.push_back(tempo); } - for(int i = 0; i < (int) obstacles.size(); ++i) + vector deathanimation; + bool lost = false; + long long steps = 0; + SDL_Rect lpos; + lpos.x = 300; + lpos.y = 10; + lpos.w = 300; + lpos.h = 20; + Label scoreLabel = Label("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier))), lpos); + double maxjumpx = 0; + double maxjumpy = 0; + double lastjumppos = 0; + + while(!Game_Quit) { - obstacles[i].step(1/60.0); - obstacles[i].draw(screen,x_pos-xres/4,0); + steps++; + Uint32 start = SDL_GetTicks(); if(!lost) - if(obstacles[i].check_death(x_pos,y_pos,ball_rad)) + highscore--; + blacken(screen); + SDL_Event event; + while(SDL_PollEvent(&event)) { - cout << "Collision: You lost the game" << endl; - cout << "Better luck next time!" << endl; - highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)); - scoreLabel.setCaption("Score: " + lltostr(highscore)); - cout << "Your highscore: " << highscore << endl; - lost = true; - deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,1); + if(event.type == SDL_QUIT) + { + Game_Quit = true; + quickquit = true; + wantEnd = true; + break; + } + } + Uint8 *keyState = SDL_GetKeyState(NULL); + + down = -keyState[SDLK_UP]+keyState[SDLK_DOWN]; + + vector nparticles; + for(int i = 0; i < (int) particles.size(); ++i) + { + if(particles[i].step(1/60.0) && particles[i].y > ground_level) + { + particles[i].draw(screen,x_pos-xres/4,0); + nparticles.push_back(particles[i]); + } + } + if(!(steps%300) && hardcore) + { + Obstacle tempo = Obstacle(110*100,ground_level, 10, 10); + tempo.vx = -60; + obstacles.push_back(tempo); + } + for(int i = 0; i < (int) obstacles.size(); ++i) + { + obstacles[i].step(1/60.0); + obstacles[i].draw(screen,x_pos-xres/4,0); + if(!lost) + if(obstacles[i].check_death(x_pos,y_pos,ball_rad)) + { + cout << "Collision: You lost the game" << endl; + cout << "Better luck next time!" << endl; + highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)); + scoreLabel.setCaption("Score: " + lltostr(highscore)); + cout << "Your highscore: " << highscore << endl; + lost = true; + deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,1); + } + } + particles = nparticles; + for(int i = 0; i < 1; ++i) + { + Particle p = Particle(-300 + rand()%11001,yres,3,255,255,255); + p.vy = -5-rand()%30/9.0; + p.life = 1000; + particles.push_back(p); + } + if(!lost) + { + x_pos = x_pos + x_vel/60.0; + y_pos = y_pos + y_vel/60.0; + phi = phi + vphi/60.0; + drawCircle(screen, xres/4, fabs(yres-y_pos), phi, ball_rad, playerR, playerG, playerB); + scoreLabel.setCaption("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)))); } - } - particles = nparticles; - for(int i = 0; i < 1; ++i) - { - Particle p = Particle(-300 + rand()%11001,yres,3,255,255,255); - p.vy = -5-rand()%30/9.0; - p.life = 1000; - particles.push_back(p); - } - if(!lost) - { - x_pos = x_pos + x_vel/60.0; - y_pos = y_pos + y_vel/60.0; - phi = phi + vphi/60.0; - drawCircle(screen, xres/4, fabs(yres-y_pos), phi, ball_rad, playerR, playerG, playerB); - scoreLabel.setCaption("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)))); - } - right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; - if(!right) - { right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; - } + if(!right) + { + right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; + } - if(y_pos > ground_level+ball_rad) - { - y_vel = y_vel*(1.0-c_r) - g/60.0; - x_vel = x_vel*(1.0-c_r) + right*80/60.0; - maxjumpy = max(maxjumpy,y_pos); - } - if(y_pos <= ground_level+ball_rad) - { - maxjumpx = max(maxjumpx,x_pos-lastjumppos); - lastjumppos = x_pos; - x_vel = x_vel*(1.0-c_r) + right*140./60.0; - y_pos = ground_level+ball_rad; - y_vel = fabs(y_vel); - if(down == -1) - y_vel += 900; - y_vel *= 0.25; - vphi = -x_vel/ball_rad; - } - if(!lost) - if(highscore+x_pos*position_multiplier <= 0) - { - cout << "TIMEOUT: You lost the game." << endl; - cout << "Better luck next time!" << endl; - quickquit = true; - Game_Quit = true; - deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,0); - } - if(x_pos >= 10000) - { - cout << "You win the game!" << endl; - highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier + win_bonus)); - cout << "Your highscore: " << highscore << endl; - scoreLabel.setCaption("Score: " + lltostr(highscore)); - Game_Quit = true; - } - for(int i = 0; i < xres; ++i) - drawPixel(screen,i,yres-ground_level,255,255,255); - bool animation_term = true; - for(int i = 0; i < (int) deathanimation.size(); ++i) - { - if(deathanimation[i].step(1/60.0)) + if(y_pos > ground_level+ball_rad) { - animation_term = false; - deathanimation[i].draw(screen,x_pos-xres/4,0); + y_vel = y_vel*(1.0-c_r) - g/60.0; + x_vel = x_vel*(1.0-c_r) + right*80/60.0; + maxjumpy = max(maxjumpy,y_pos); } + if(y_pos <= ground_level+ball_rad) + { + maxjumpx = max(maxjumpx,x_pos-lastjumppos); + lastjumppos = x_pos; + x_vel = x_vel*(1.0-c_r) + right*140./60.0; + y_pos = ground_level+ball_rad; + y_vel = fabs(y_vel); + if(down == -1) + y_vel += 900; + y_vel *= 0.25; + vphi = -x_vel/ball_rad; + } + if(!lost) + if(highscore+x_pos*position_multiplier <= 0) + { + cout << "TIMEOUT: You lost the game." << endl; + cout << "Better luck next time!" << endl; + quickquit = true; + Game_Quit = true; + deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,0); + } + if(x_pos >= 10000) + { + cout << "You win the game!" << endl; + highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier + win_bonus)); + cout << "Your highscore: " << highscore << endl; + scoreLabel.setCaption("Score: " + lltostr(highscore)); + Game_Quit = true; + } + for(int i = 0; i < xres; ++i) + drawPixel(screen,i,yres-ground_level,255,255,255); + bool animation_term = true; + for(int i = 0; i < (int) deathanimation.size(); ++i) + { + if(deathanimation[i].step(1/60.0)) + { + animation_term = false; + deathanimation[i].draw(screen,x_pos-xres/4,0); + } + } + if(animation_term && lost) + Game_Quit = true; + scoreLabel.draw(screen); + SDL_Flip(screen); + Uint32 time = SDL_GetTicks()-start; + if(1000/60.0 - time > 0) + SDL_Delay(1000/60.0 - time); } - if(animation_term && lost) - Game_Quit = true; - scoreLabel.draw(screen); - SDL_Flip(screen); - Uint32 time = SDL_GetTicks()-start; - if(1000/60.0 - time > 0) - SDL_Delay(1000/60.0 - time); - } - cout << "Longest Jump: " << maxjumpx << endl; - cout << "Highest Jump: " << maxjumpy << endl; + cout << "Longest Jump: " << maxjumpx << endl; + cout << "Highest Jump: " << maxjumpy << endl; - /*The Game has ended*/ - if(!quickquit) - { - ifstream ins; - ins.open("highscore.dat"); - vector > highlist; - for(int i = 0; i < 10; ++i) + /*The Game has ended*/ + if(!quickquit) { - pair temp; - ins >> temp.first; - if(!ins.good()) - break; - ins >> temp.second; - if(ins.good()) + ifstream ins; + ins.open("highscore.dat"); + vector > highlist; + for(int i = 0; i < 10; ++i) { - highlist.push_back(temp); + pair temp; + ins >> temp.first; + if(!ins.good()) + break; + ins >> temp.second; + if(ins.good()) + { + highlist.push_back(temp); + } + else + break; } - else - break; - } - ins.close(); - sort(highlist.begin(), highlist.end()); - if(highlist.size() < 10 || highlist[0].first < highscore) - { - cout << "Congratulations! You made a new highscore." << endl; - pair youscore; - youscore.first = highscore; - youscore.second = name; - highlist.push_back(youscore); + ins.close(); sort(highlist.begin(), highlist.end()); - ofstream ofs; - ofs.open("highscore.dat"); - if(highlist.size() == 11) + if(highlist.size() < 10 || highlist[0].first < highscore) { - for(int i = 0; i < 10; ++i) + cout << "Congratulations! You made a new highscore." << endl; + pair youscore; + youscore.first = highscore; + youscore.second = name; + highlist.push_back(youscore); + sort(highlist.begin(), highlist.end()); + ofstream ofs; + ofs.open("highscore.dat"); + if(highlist.size() == 11) { - ofs << highlist[i+1].first << " " << highlist[i+1].second << endl; + for(int i = 0; i < 10; ++i) + { + ofs << highlist[i+1].first << " " << highlist[i+1].second << endl; + } } - } - else - { - for(int i = 0; i < (int)highlist.size(); ++i) + else { - ofs << highlist[i].first << " " << highlist[i].second << endl; + for(int i = 0; i < (int)highlist.size(); ++i) + { + ofs << highlist[i].first << " " << highlist[i].second << endl; + } } - } - ofs.close(); + ofs.close(); + } + cout << "\n**HIGHSCORE**\n"; + for(int i = min(10,(int) highlist.size()-1); i > 0; --i) + { + cout << highlist[i].second << ": " << highlist[i].first << '\n'; + } + if(highlist.size() < 11) + cout << highlist[0].second << ": " << highlist[0].first << '\n'; + cout << endl; } - cout << "\n**HIGHSCORE**\n"; - for(int i = min(10,(int) highlist.size()-1); i > 0; --i) + + SDL_Rect mpos; + mpos.x = 25; + mpos.y = 180; + mpos.w = 300; + mpos.h = 32; + Label message = Label("GAME OVER - Press space to restart", mpos); + message.draw(screen); + SDL_Flip(screen); + while(true) { - cout << highlist[i].second << ": " << highlist[i].first << '\n'; + SDL_Event event; + SDL_PollEvent(&event); + Uint8 *keyState = SDL_GetKeyState(NULL); + if(event.type == SDL_QUIT || keyState[SDLK_ESCAPE]) + { + Game_Quit = true; + quickquit = true; + wantEnd = true; + break; + } + if(keyState[SDLK_SPACE]) + break; + } - if(highlist.size() < 11) - cout << highlist[0].second << ": " << highlist[0].first << '\n'; - cout << endl; } SDL_FreeSurface(preBall); -- cgit v1.2.3