diff options
Diffstat (limited to 'JnR.cpp')
-rw-r--r-- | JnR.cpp | 75 |
1 files changed, 60 insertions, 15 deletions
@@ -3,11 +3,12 @@ | |||
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | #include <SDL/SDL.h> | 5 | #include <SDL/SDL.h> |
6 | #include <SDL/SDL_ttf.h> | ||
7 | #include <SDL/SDL_rotozoom.h> | ||
6 | #include <algorithm> | 8 | #include <algorithm> |
7 | #include <fstream> | 9 | #include <fstream> |
8 | #include <sstream> | 10 | #include <sstream> |
9 | #include <ctime> | 11 | #include <ctime> |
10 | //#include "/usr/include/SDL/SDL_image.h" | ||
11 | #include <cstdio> | 12 | #include <cstdio> |
12 | #include <vector> | 13 | #include <vector> |
13 | #include <list> | 14 | #include <list> |
@@ -15,7 +16,14 @@ | |||
15 | 16 | ||
16 | using namespace std; | 17 | using namespace std; |
17 | 18 | ||
18 | /* | 19 | |
20 | string lltostr(const long long& l) | ||
21 | { | ||
22 | stringstream str; | ||
23 | str << l; | ||
24 | return str.str(); | ||
25 | } | ||
26 | |||
19 | class Label | 27 | class Label |
20 | { | 28 | { |
21 | protected: | 29 | protected: |
@@ -82,7 +90,7 @@ int Label::setCaption(string ncaption) | |||
82 | 90 | ||
83 | int Label::render() | 91 | int Label::render() |
84 | { | 92 | { |
85 | TTF_Font *font = TTF_OpenFont("data/fonts/OpenSans-Semibold.ttf",pos.h); | 93 | TTF_Font *font = TTF_OpenFont("fonts/OpenSans-Semibold.ttf",pos.h); |
86 | image = NULL; | 94 | image = NULL; |
87 | image = TTF_RenderText_Solid(font, caption.c_str(),textColor); | 95 | image = TTF_RenderText_Solid(font, caption.c_str(),textColor); |
88 | if(caption.size() == 0) | 96 | if(caption.size() == 0) |
@@ -108,7 +116,7 @@ Label::~Label() | |||
108 | { | 116 | { |
109 | SDL_FreeSurface(image); | 117 | SDL_FreeSurface(image); |
110 | } | 118 | } |
111 | */ | 119 | |
112 | 120 | ||
113 | int xres = 600; | 121 | int xres = 600; |
114 | int yres = 450; | 122 | int yres = 450; |
@@ -148,6 +156,13 @@ void blacken(SDL_Surface* screen) | |||
148 | SDL_FillRect(screen,NULL,0); | 156 | SDL_FillRect(screen,NULL,0); |
149 | } | 157 | } |
150 | 158 | ||
159 | Uint8 playerR = 100; | ||
160 | Uint8 playerG = 100; | ||
161 | Uint8 playerB = 100; | ||
162 | |||
163 | Uint8 obstacleR = 255; | ||
164 | Uint8 obstacleG = 100; | ||
165 | Uint8 obstacleB = 100; | ||
151 | 166 | ||
152 | class Obstacle | 167 | class Obstacle |
153 | { | 168 | { |
@@ -174,7 +189,7 @@ public: | |||
174 | temp.x -= levelx; | 189 | temp.x -= levelx; |
175 | temp.y -= levely; | 190 | temp.y -= levely; |
176 | temp.y = yres - temp.y- temp.h; | 191 | temp.y = yres - temp.y- temp.h; |
177 | SDL_FillRect(screen, &temp, SDL_MapRGB(screen->format, 255, 255, 255)); | 192 | SDL_FillRect(screen, &temp, SDL_MapRGB(screen->format, obstacleR, obstacleG, obstacleB)); |
178 | } | 193 | } |
179 | void step(double time) | 194 | void step(double time) |
180 | { | 195 | { |
@@ -240,6 +255,7 @@ public: | |||
240 | } | 255 | } |
241 | }; | 256 | }; |
242 | 257 | ||
258 | |||
243 | vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, double vyb, int mode) | 259 | vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, double vyb, int mode) |
244 | { | 260 | { |
245 | vector<Particle> result; | 261 | vector<Particle> result; |
@@ -251,7 +267,7 @@ vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, do | |||
251 | continue; | 267 | continue; |
252 | tx += x; | 268 | tx += x; |
253 | ty += y; | 269 | ty += y; |
254 | Particle temp = Particle(tx, ty, 1, 100, 100, 100); | 270 | Particle temp = Particle(tx, ty, 1, playerR, playerG, playerB); |
255 | double vb = (rand()%5000)/30.0; | 271 | double vb = (rand()%5000)/30.0; |
256 | double vp = ((rand()%10000)/10000.0)*2*M_PI; | 272 | double vp = ((rand()%10000)/10000.0)*2*M_PI; |
257 | if(!mode) | 273 | if(!mode) |
@@ -283,16 +299,37 @@ double position_multiplier = 3; | |||
283 | double win_bonus = 5000; | 299 | double win_bonus = 5000; |
284 | bool hardcore = false; | 300 | bool hardcore = false; |
285 | 301 | ||
302 | void loadConstants() | ||
303 | { | ||
304 | ifstream ins; | ||
305 | ins.open("settings.dat"); | ||
306 | int i = 0; | ||
307 | ins >> i; | ||
308 | playerR = i; | ||
309 | ins >> i; | ||
310 | playerG = i; | ||
311 | ins >> i; | ||
312 | playerB = i; | ||
313 | ins >> i; | ||
314 | obstacleR = i; | ||
315 | ins >> i; | ||
316 | obstacleG = i; | ||
317 | ins >> i; | ||
318 | obstacleB = i; | ||
319 | ins.close(); | ||
320 | } | ||
321 | |||
286 | int main() | 322 | int main() |
287 | { | 323 | { |
324 | loadConstants(); | ||
288 | string name; | 325 | string name; |
289 | cout << "This is a simple jump and run" << endl; | 326 | cout << "This is a simple jump and run" << endl; |
290 | cout << "Please enter your name:"; | 327 | cout << "Please enter your name:"; |
291 | cin >> name; | 328 | cin >> name; |
292 | cout << "Please enter difficulty (0-100):" << endl; | 329 | cout << "Please enter difficulty (0-100):"; |
293 | int difficulty; | 330 | int difficulty; |
294 | cin >> difficulty; | 331 | cin >> difficulty; |
295 | cout << "Do you want to play in hardcore mode?" << endl; | 332 | cout << "Do you want to play in hardcore mode(0/1)?"; |
296 | cin >> hardcore; | 333 | cin >> hardcore; |
297 | if(hardcore) | 334 | if(hardcore) |
298 | { | 335 | { |
@@ -315,21 +352,21 @@ int main() | |||
315 | if(SDL_Init(SDL_INIT_VIDEO) == -1) | 352 | if(SDL_Init(SDL_INIT_VIDEO) == -1) |
316 | { | 353 | { |
317 | cout << "Error: Could not initialize SDL" << endl; | 354 | cout << "Error: Could not initialize SDL" << endl; |
318 | return 0; | 355 | return 1; |
319 | } | 356 | } |
320 | screen = SDL_SetVideoMode(xres,yres,32,SDL_SWSURFACE); | 357 | screen = SDL_SetVideoMode(xres,yres,32,SDL_SWSURFACE); |
321 | if(!screen) | 358 | if(!screen) |
322 | { | 359 | { |
323 | cout << "could not initialize screen" << endl; | 360 | cout << "could not initialize screen" << endl; |
324 | return 0; | 361 | return 1; |
325 | } | 362 | } |
326 | /* | 363 | |
327 | if(TTF_Init() == -1) | 364 | if(TTF_Init() == -1) |
328 | { | 365 | { |
329 | cout << "could not initialize True Fonts" << endl; | 366 | cout << "could not initialize True Fonts" << endl; |
330 | return 0; | 367 | return 1; |
331 | }*/ | 368 | } |
332 | SDL_WM_SetCaption("Ballroller - v0.1", NULL); | 369 | SDL_WM_SetCaption("Ballroller - v0.2", NULL); |
333 | 370 | ||
334 | 371 | ||
335 | bool Game_Quit = false; | 372 | bool Game_Quit = false; |
@@ -368,6 +405,8 @@ int main() | |||
368 | lpos.y = 10; | 405 | lpos.y = 10; |
369 | lpos.w = 300; | 406 | lpos.w = 300; |
370 | lpos.h = 20; | 407 | lpos.h = 20; |
408 | Label scoreLabel = Label("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier))), lpos); | ||
409 | |||
371 | while(!Game_Quit) | 410 | while(!Game_Quit) |
372 | { | 411 | { |
373 | steps++; | 412 | steps++; |
@@ -414,6 +453,7 @@ int main() | |||
414 | cout << "Collision: You lost the game" << endl; | 453 | cout << "Collision: You lost the game" << endl; |
415 | cout << "Better luck next time!" << endl; | 454 | cout << "Better luck next time!" << endl; |
416 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)); | 455 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)); |
456 | scoreLabel.setCaption("Score: " + lltostr(highscore)); | ||
417 | cout << "Your highscore: " << highscore << endl; | 457 | cout << "Your highscore: " << highscore << endl; |
418 | lost = true; | 458 | lost = true; |
419 | deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,1); | 459 | deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,1); |
@@ -430,7 +470,8 @@ int main() | |||
430 | { | 470 | { |
431 | x_pos = x_pos + x_vel/60.0; | 471 | x_pos = x_pos + x_vel/60.0; |
432 | y_pos = y_pos + y_vel/60.0; | 472 | y_pos = y_pos + y_vel/60.0; |
433 | drawCircle(screen, xres/4, fabs(yres-y_pos), ball_rad, 100, 100, 100); | 473 | drawCircle(screen, xres/4, fabs(yres-y_pos), ball_rad, playerR, playerG, playerB); |
474 | scoreLabel.setCaption("Score: " + lltostr((long long)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)))); | ||
434 | } | 475 | } |
435 | 476 | ||
436 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | 477 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; |
@@ -472,6 +513,7 @@ int main() | |||
472 | cout << "You win the game!" << endl; | 513 | cout << "You win the game!" << endl; |
473 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier + win_bonus)); | 514 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier + win_bonus)); |
474 | cout << "Your highscore: " << highscore << endl; | 515 | cout << "Your highscore: " << highscore << endl; |
516 | scoreLabel.setCaption("Score: " + lltostr(highscore)); | ||
475 | Game_Quit = true; | 517 | Game_Quit = true; |
476 | } | 518 | } |
477 | for(int i = 0; i < xres; ++i) | 519 | for(int i = 0; i < xres; ++i) |
@@ -487,11 +529,14 @@ int main() | |||
487 | } | 529 | } |
488 | if(animation_term && lost) | 530 | if(animation_term && lost) |
489 | Game_Quit = true; | 531 | Game_Quit = true; |
532 | scoreLabel.draw(screen); | ||
490 | SDL_Flip(screen); | 533 | SDL_Flip(screen); |
491 | Uint32 time = SDL_GetTicks()-start; | 534 | Uint32 time = SDL_GetTicks()-start; |
492 | if(1000/60.0 - time > 0) | 535 | if(1000/60.0 - time > 0) |
493 | SDL_Delay(1000/60.0 - time); | 536 | SDL_Delay(1000/60.0 - time); |
494 | } | 537 | } |
538 | |||
539 | /*The Game has ended*/ | ||
495 | if(!quickquit) | 540 | if(!quickquit) |
496 | { | 541 | { |
497 | ifstream ins; | 542 | ifstream ins; |