From 6f84f40ed345d5e0e224990f03613695851c8b1a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 2 Dec 2014 17:08:03 +0000 Subject: Initial commit --- JnR.cpp | 559 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 559 insertions(+) create mode 100644 JnR.cpp diff --git a/JnR.cpp b/JnR.cpp new file mode 100644 index 0000000..b0d5d53 --- /dev/null +++ b/JnR.cpp @@ -0,0 +1,559 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +//#include "/usr/include/SDL/SDL_image.h" +#include +#include +#include + + +using namespace std; + +/* +class Label +{ +protected: + string caption; + SDL_Surface* image; +// int textSize; + SDL_Color textColor; + int render(); +public: + SDL_Rect pos; + Label(); + Label(string ncaption, SDL_Rect& npos); + ~Label(); + void draw(SDL_Surface* screen); + int setTextSize(int ntextSize); + void setTextColor(SDL_Color textColor); + int setCaption(string ncaption); +}; +Label::Label() +{ + pos.x = 0; + pos.y = 0; + pos.w = 0; + pos.h = 14; + caption = ""; + textColor.r = 255; + textColor.g = 255; + textColor.b = 255; + render(); +} + +Label::Label(string ncaption, SDL_Rect& npos) +{ + pos = npos; + textColor.r = 255; + textColor.g = 255; + textColor.b = 255; + caption = ncaption; + npos.w = render(); +} + +int Label::setTextSize(int ntextSize) +{ + pos.h = ntextSize; + SDL_FreeSurface(image); + return render(); +} + +void Label::setTextColor(SDL_Color ntextColor) +{ + textColor = ntextColor; + SDL_FreeSurface(image); + render(); +} + +int Label::setCaption(string ncaption) +{ + if(caption == ncaption) + return pos.w; + caption = ncaption; + SDL_FreeSurface(image); + return render(); +} + +int Label::render() +{ + TTF_Font *font = TTF_OpenFont("data/fonts/OpenSans-Semibold.ttf",pos.h); + image = NULL; + image = TTF_RenderText_Solid(font, caption.c_str(),textColor); + if(caption.size() == 0) + cout << "empty caption!!!" << endl; + if(image == NULL) + { + cout << "Error rendering Label with caption " << caption << endl; + return 0; + } + TTF_CloseFont(font); + pos.w = image->w; + return image->w; +} + + +void Label::draw(SDL_Surface* screen) +{ + SDL_Rect temp = pos; + SDL_BlitSurface(image, NULL, screen, &temp); +} + +Label::~Label() +{ + SDL_FreeSurface(image); +} +*/ +vector dirx = {1,0,-1,0}; +vector diry = {0,1,0,-1}; + + +int xres = 600; +int yres = 450; +SDL_Surface *screen; + +bool inBounds(int x, int y) +{ + if(x >= 0 && x < xres && y >= 0 && y < yres) + return true; + return false; +} + +void drawPixel(SDL_Surface *screen,int x, int y, Uint8 R, Uint8 G, Uint8 B) +{ + Uint32 color = SDL_MapRGB(screen->format, R, G, B); + + SDL_Rect r; + r.x = x; + r.y = y; + r.w = 1; + r.h = 1; + SDL_FillRect(screen,&r,color); +} + +void drawCircle(SDL_Surface* screen, double x, double y, 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)) + { + drawPixel(screen, i,j,R,G,B); + } +} + +void blacken(SDL_Surface* screen) +{ + SDL_FillRect(screen,NULL,0); +} + + +class Obstacle +{ +public: + SDL_Rect r; + int vx; + Obstacle(double xn, double yn, double width, double height) + { + r.x = xn; + r.y = yn; + r.h = height; + r.w = width; + vx = 0; + } + bool check_death(double x_pos, double y_pos, double radius) + { + if(x_pos + radius < r.x || x_pos - radius > r.x + r.w || y_pos + radius < r.y || y_pos - radius > r.y + r.h) + return false; + return true; + } + void draw(SDL_Surface* screen,double levelx, double levely) + { + SDL_Rect temp = r; + temp.x -= levelx; + temp.y -= levely; + temp.y = yres - temp.y- temp.h; + SDL_FillRect(screen, &temp, SDL_MapRGB(screen->format, 255, 255, 255)); + } + void step(double time) + { + r.x += time*vx; + } +}; + +class Particle +{ +public: + double x; + double y; + double vx; + double vy; + double life; + // 0 = static, 1 = strife, 2 = randomwalk, 3 = xrandomwalk + int mode; + Uint8 R,G,B; + Particle() + { + x = rand()%xres; + y = rand()%yres; + life = rand()%20; + R = rand()%256; + G = rand()%256; + B = rand()%256; + } + Particle(double xn, double yn, int nmode, Uint8 Rn, Uint8 Gn, Uint8 Bn) + { + mode = nmode; + x = xn; + y = yn; + R = Rn; + G = Gn; + B = Bn; + life = rand()%20; + } + void draw(SDL_Surface* screen,double levelx,double levely) + { + if(inBounds(x-levelx,y-levely)) + drawPixel(screen, x-levelx, yres- (y-levely), R, G, B); + } + bool step(double time) + { + life -= time; + // 0 = static, 1 = strife, 2 = randomwalk, 3 = xrandomwalk + if(mode == 1){ + x += vx*time; + y += vy*time; + } + else if(mode == 2) + { + x += (rand()%3-1)/3.0; + y += (rand()%3-1)/3.0; + } + else if(mode == 3) + { + x += (rand()%3-1)/3.0; + y += vy*time; + } + + return (life >= 0); + } +}; + +vector makeDeathAnimation(double x, double y, double r, double vxb, double vyb, int mode) +{ + vector result; + for(int i = 0; i < 2000; ++i) + { + double tx = rand()%((int) (2*r+1))-r; + double ty = rand()%((int) (2*r+1))-r; + if(tx*tx + ty * ty > r*r) + continue; + tx += x; + ty += y; + Particle temp = Particle(tx, ty, 1, 100, 100, 100); + double vb = (rand()%5000)/30.0; + double vp = ((rand()%10000)/10000.0)*2*M_PI; + if(!mode) + { + temp.vx = cos(vp)*vb+vxb; + temp.vy = sin(vp)*vb+vyb; + } + else + { + temp.vx = cos(vp)*vb+vxb*(rand()%1000)/1000.0; + temp.vy = sin(vp)*vb+vyb*(rand()%1000)/1000.0; + } + temp.life = (rand()%100)/50.0; + result.push_back(temp); + } + return result; +} + +double x_pos = 0; +double x_vel = 0; +double ground_level = 20; +double y_vel = 0; +double ball_rad = 8; +double y_pos = ground_level + ball_rad; +double g = 300.0; +double c_r = 0.004; +double highscore = 1000; +double position_multiplier = 3; +double win_bonus = 5000; +bool hardcore = false; + +int main() +{ + string name; + cout << "This is a simple jump and run" << endl; + cout << "Please enter your name:"; + cin >> name; + cout << "Please enter difficulty (0-100):" << endl; + int difficulty; + cin >> difficulty; + cout << "Do you want to play in hardcore mode?" << endl; + cin >> hardcore; + if(hardcore) + { + highscore *= 1.5; + cout << "You are insane. +50% score!" << endl; + } + else + cout << "Very sane decision..." << endl; + 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; + 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; + return 0; + } + screen = SDL_SetVideoMode(xres,yres,32,SDL_SWSURFACE); + if(!screen) + { + cout << "could not initialize screen" << endl; + return 0; + } + /* + if(TTF_Init() == -1) + { + cout << "could not initialize True Fonts" << endl; + return 0; + }*/ + SDL_WM_SetCaption("Ballroller - v0.1", NULL); + + + 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; + particles.push_back(p); + } + int opos = 100; + 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)); + } + if(hardcore) + for(int i = 1; i < 110; ++i) + { + Obstacle tempo = Obstacle(i*100,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; + while(!Game_Quit) + { + steps++; + Uint32 start = SDL_GetTicks(); + if(!lost) + highscore--; + blacken(screen); + SDL_Event event; + while(SDL_PollEvent(&event)) + { + if(event.type == SDL_QUIT) + { + Game_Quit = true; + quickquit = 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%100) && 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)); + 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 < 10; ++i) + { + Particle p = Particle(-300 + rand()%10000,ground_level + rand()%((int) (yres- ground_level)),3,255,255,255); + p.vy = -6; + 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, abs(yres-y_pos), ball_rad, 100, 100, 100); + } + + right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; + if(!right) + { + 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; + } + if(y_pos <= ground_level+ball_rad) + { + 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; + } + 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; + 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; + SDL_Flip(screen); + Uint32 time = SDL_GetTicks()-start; + if(1000/60.0 - time > 0) + SDL_Delay(1000/60.0 - time); + } + if(!quickquit) + { + ifstream ins; + ins.open("highscore.dat"); + vector > highlist; + for(int i = 0; i < 10; ++i) + { + pair temp; + ins >> temp.first; + if(!ins.good()) + break; + ins >> temp.second; + if(ins.good()) + { + highlist.push_back(temp); + } + 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); + sort(highlist.begin(), highlist.end()); + ofstream ofs; + ofs.open("highscore.dat"); + if(highlist.size() == 11) + { + 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) + { + ofs << highlist[i].first << " " << highlist[i].second << endl; + } + } + + 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; + } + + atexit(SDL_Quit); + + return 0; +} -- cgit v1.2.3