From d7c0925b3d7ffb1a58402c05e664966e2f8a597f Mon Sep 17 00:00:00 2001 From: Reimar Date: Thu, 4 Dec 2014 12:02:53 +0100 Subject: added custom random generator, ball no has a roll animation, revisited difficulty impact --- JnR.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++--------------- Makefile | 2 +- blank.bmp | Bin 0 -> 890 bytes random.cpp | 18 ++++++++++ random.h | 14 ++++++++ 5 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 blank.bmp create mode 100644 random.cpp create mode 100644 random.h diff --git a/JnR.cpp b/JnR.cpp index 8b9bd43..f9b2500 100644 --- a/JnR.cpp +++ b/JnR.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -12,10 +13,35 @@ #include #include #include +#include "random.h" using namespace std; +//loads a bitmap from file and converts it to screen properties +SDL_Surface* loadBMP(std::string filename) +{ + SDL_Surface* loadedImage = NULL; + + SDL_Surface* convertedImage = NULL; + + loadedImage = IMG_Load(filename.c_str()); + if(loadedImage != NULL) + { + convertedImage = SDL_DisplayFormat(loadedImage); + if(convertedImage) + { + Uint32 colorkey = SDL_MapRGB(convertedImage->format, 10, 10, 10); + SDL_SetColorKey(convertedImage, SDL_SRCCOLORKEY, colorkey); + SDL_FreeSurface(loadedImage); + return convertedImage; + } + cout << "Error: Could not convert Image" << filename << '.' << endl; + } + cout << "Error: Could not load bitmap from file " << filename << endl; + return NULL; +} + string lltostr(const long long& l) { @@ -141,14 +167,29 @@ void drawPixel(SDL_Surface *screen,int x, int y, Uint8 R, Uint8 G, Uint8 B) SDL_FillRect(screen,&r,color); } -void drawCircle(SDL_Surface* screen, double x, double y, double radius, Uint8 R, Uint8 G, Uint8 B) +SDL_Surface* preBall; + +void drawCircle(SDL_Surface* screen, double x, double y, double angle, double radius, Uint8 R, Uint8 G, Uint8 B) { - for(int i = x-radius; i <= x+radius; ++i) - for(int j = y-radius; j <= y+radius; ++j) - if(((i-x)*(i-x)+(j-y)*(j-y) <= radius*radius) && inBounds(i,j)) + int tempseed = rand(); + srand(87); + Uint32 colorkey = SDL_MapRGB(preBall->format, 10, 10, 10); + SDL_FillRect(preBall,NULL,colorkey); + for(int i = 0; i <= 2*radius; ++i) + 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)) { - drawPixel(screen, i,j,R,G,B); + double alt = 0.90+(rand()%10000)/50000.0; + drawPixel(preBall, i,j,R*alt,G*alt,B*alt); } + SDL_Rect drawpos; + SDL_Surface* temp = preBall; + temp = rotozoomSurface(temp,angle/M_PI*180,1,0); + drawpos.x = x-temp->w/2; + drawpos.y = y - temp->h/2; + SDL_BlitSurface(temp,NULL,screen,&drawpos); + SDL_FreeSurface(temp); + srand(tempseed); } void blacken(SDL_Surface* screen) @@ -197,6 +238,7 @@ public: } }; + class Particle { public: @@ -242,12 +284,12 @@ public: } else if(mode == 2) { - x += (rand()%3-1)/3.0; - y += (rand()%3-1)/3.0; + x += (rand()%3-1)/2.0; + y += (rand()%3-1)/2.0; } else if(mode == 3) { - x += (rand()%3-1)/3.0; + x += (rand()%3-1)/8.0; y += vy*time; } @@ -288,6 +330,8 @@ vector makeDeathAnimation(double x, double y, double r, double vxb, do double x_pos = 0; double x_vel = 0; +double phi = 0; +double vphi = 0; double ground_level = 20; double y_vel = 0; double ball_rad = 8; @@ -322,6 +366,7 @@ void loadConstants() int main() { loadConstants(); + string name; cout << "This is a simple jump and run" << endl; cout << "Please enter your name:"; @@ -338,17 +383,19 @@ int main() } else cout << "Very sane decision..." << endl; - if(difficulty > 0) + if(difficulty >= 0) cout << "solvability not guarenteed! Have fun!" << endl; if(difficulty < 0) cout << "This should be a piece of cake and you should feel bad!" << endl; difficulty = max(-50,difficulty); if(difficulty > 100) difficulty = 100; + //SEED + srandn(2334508 + difficulty); srand(2334508 + difficulty); + cout << "difficulty = " << difficulty << endl; position_multiplier += difficulty/40.0; -// char l; if(SDL_Init(SDL_INIT_VIDEO) == -1) { cout << "Error: Could not initialize SDL" << endl; @@ -366,34 +413,38 @@ int main() cout << "could not initialize True Fonts" << endl; return 1; } - SDL_WM_SetCaption("Ballroller - v0.2", NULL); + SDL_WM_SetCaption("Ballroller - v0.3", NULL); + + preBall = loadBMP("blank.bmp"); bool Game_Quit = false; int down = 0; int right = 0; - int space = 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(-300 + rand()%10000,ground_level + rand()%((int) (yres- ground_level)),3,255,255,255); - p.vy = -2; + 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); } - int opos = 100; + //Longest Jump: 608 + //Highest Jump: 150 + int opos = 200; while(opos < 10000) { - opos += rand()%(900-6*difficulty); - int blub = rand()%180 + 3; - obstacles.push_back(Obstacle(opos,ground_level,blub,200/2-blub/2)); + 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) { - Obstacle tempo = Obstacle(i*100,ground_level, 10, 10); + Obstacle tempo = Obstacle(i*300,ground_level, 10, 10); tempo.vx = -60; obstacles.push_back(tempo); } @@ -406,6 +457,9 @@ int main() 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) { @@ -437,7 +491,7 @@ int main() nparticles.push_back(particles[i]); } } - if(!(steps%100) && hardcore) + if(!(steps%300) && hardcore) { Obstacle tempo = Obstacle(110*100,ground_level, 10, 10); tempo.vx = -60; @@ -460,17 +514,19 @@ int main() } } particles = nparticles; - for(int i = 0; i < 10; ++i) + for(int i = 0; i < 1; ++i) { - Particle p = Particle(-300 + rand()%10000,ground_level + rand()%((int) (yres- ground_level)),3,255,255,255); - p.vy = -6; + 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; - drawCircle(screen, xres/4, fabs(yres-y_pos), ball_rad, playerR, playerG, playerB); + 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)))); } @@ -479,25 +535,24 @@ int main() { right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; } - space = keyState[SDLK_SPACE]; - if(space) - { - cout << "x-Position = " << x_pos << endl; - } 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) @@ -535,6 +590,9 @@ int main() if(1000/60.0 - time > 0) SDL_Delay(1000/60.0 - time); } + cout << "Longest Jump: " << maxjumpx << endl; + cout << "Highest Jump: " << maxjumpy << endl; + /*The Game has ended*/ if(!quickquit) @@ -594,6 +652,7 @@ int main() cout << highlist[0].second << ": " << highlist[0].first << '\n'; cout << endl; } + SDL_FreeSurface(preBall); atexit(SDL_Quit); diff --git a/Makefile b/Makefile index 47e7d82..4bb9d9b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ all: JnR JnR: JnR.cpp - g++ JnR.cpp -O2 -Wall -lSDL -std=c++11 -lSDL_ttf -o JnR + g++ JnR.cpp -O2 -Wall -ggdb -lSDL -lSDL_gfx -lSDL_ttf -lSDL_image -o JnR clean: rm -f JnR diff --git a/blank.bmp b/blank.bmp new file mode 100644 index 0000000..9889159 Binary files /dev/null and b/blank.bmp 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 @@ + +long long rand_state = 24; +long long rand_para1 = 37; +long long rand_para2 = 37; + +int randn() +{ + rand_state = (rand_state*rand_state + rand_state*rand_para1 +rand_para2)%15555557; + return rand_state; +} + +//seed with number from 0 to 672 +void srandn(int seed) +{ + rand_state = seed + 2753; + + randn(); +} diff --git a/random.h b/random.h new file mode 100644 index 0000000..2050414 --- /dev/null +++ b/random.h @@ -0,0 +1,14 @@ +#ifndef RANDOM_H +#define RANDOM_H + +#include "random.cpp" + +//guarenteed no loop with length < 150 when less then 5000 times called +int randn(); + +//seed with number from 0 to 672 +void srandn(); + + + +#endif -- cgit v1.2.3