diff options
author | Gregor Kleen <gkleen@praseodym.org> | 2014-12-02 17:08:03 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@praseodym.org> | 2014-12-02 17:08:03 +0000 |
commit | 6f84f40ed345d5e0e224990f03613695851c8b1a (patch) | |
tree | 01ca8d8072f489d1fe1e170a27c50fe3594d2fe1 | |
download | SpaceCannon-6f84f40ed345d5e0e224990f03613695851c8b1a.tar SpaceCannon-6f84f40ed345d5e0e224990f03613695851c8b1a.tar.gz SpaceCannon-6f84f40ed345d5e0e224990f03613695851c8b1a.tar.bz2 SpaceCannon-6f84f40ed345d5e0e224990f03613695851c8b1a.tar.xz SpaceCannon-6f84f40ed345d5e0e224990f03613695851c8b1a.zip |
Initial commit
-rw-r--r-- | JnR.cpp | 559 |
1 files changed, 559 insertions, 0 deletions
@@ -0,0 +1,559 @@ | |||
1 | #include <iostream> | ||
2 | #include <math.h> | ||
3 | #include <stdio.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <SDL/SDL.h> | ||
6 | #include <algorithm> | ||
7 | #include <fstream> | ||
8 | #include <sstream> | ||
9 | #include <ctime> | ||
10 | //#include "/usr/include/SDL/SDL_image.h" | ||
11 | #include <cstdio> | ||
12 | #include <vector> | ||
13 | #include <list> | ||
14 | |||
15 | |||
16 | using namespace std; | ||
17 | |||
18 | /* | ||
19 | class Label | ||
20 | { | ||
21 | protected: | ||
22 | string caption; | ||
23 | SDL_Surface* image; | ||
24 | // int textSize; | ||
25 | SDL_Color textColor; | ||
26 | int render(); | ||
27 | public: | ||
28 | SDL_Rect pos; | ||
29 | Label(); | ||
30 | Label(string ncaption, SDL_Rect& npos); | ||
31 | ~Label(); | ||
32 | void draw(SDL_Surface* screen); | ||
33 | int setTextSize(int ntextSize); | ||
34 | void setTextColor(SDL_Color textColor); | ||
35 | int setCaption(string ncaption); | ||
36 | }; | ||
37 | Label::Label() | ||
38 | { | ||
39 | pos.x = 0; | ||
40 | pos.y = 0; | ||
41 | pos.w = 0; | ||
42 | pos.h = 14; | ||
43 | caption = ""; | ||
44 | textColor.r = 255; | ||
45 | textColor.g = 255; | ||
46 | textColor.b = 255; | ||
47 | render(); | ||
48 | } | ||
49 | |||
50 | Label::Label(string ncaption, SDL_Rect& npos) | ||
51 | { | ||
52 | pos = npos; | ||
53 | textColor.r = 255; | ||
54 | textColor.g = 255; | ||
55 | textColor.b = 255; | ||
56 | caption = ncaption; | ||
57 | npos.w = render(); | ||
58 | } | ||
59 | |||
60 | int Label::setTextSize(int ntextSize) | ||
61 | { | ||
62 | pos.h = ntextSize; | ||
63 | SDL_FreeSurface(image); | ||
64 | return render(); | ||
65 | } | ||
66 | |||
67 | void Label::setTextColor(SDL_Color ntextColor) | ||
68 | { | ||
69 | textColor = ntextColor; | ||
70 | SDL_FreeSurface(image); | ||
71 | render(); | ||
72 | } | ||
73 | |||
74 | int Label::setCaption(string ncaption) | ||
75 | { | ||
76 | if(caption == ncaption) | ||
77 | return pos.w; | ||
78 | caption = ncaption; | ||
79 | SDL_FreeSurface(image); | ||
80 | return render(); | ||
81 | } | ||
82 | |||
83 | int Label::render() | ||
84 | { | ||
85 | TTF_Font *font = TTF_OpenFont("data/fonts/OpenSans-Semibold.ttf",pos.h); | ||
86 | image = NULL; | ||
87 | image = TTF_RenderText_Solid(font, caption.c_str(),textColor); | ||
88 | if(caption.size() == 0) | ||
89 | cout << "empty caption!!!" << endl; | ||
90 | if(image == NULL) | ||
91 | { | ||
92 | cout << "Error rendering Label with caption " << caption << endl; | ||
93 | return 0; | ||
94 | } | ||
95 | TTF_CloseFont(font); | ||
96 | pos.w = image->w; | ||
97 | return image->w; | ||
98 | } | ||
99 | |||
100 | |||
101 | void Label::draw(SDL_Surface* screen) | ||
102 | { | ||
103 | SDL_Rect temp = pos; | ||
104 | SDL_BlitSurface(image, NULL, screen, &temp); | ||
105 | } | ||
106 | |||
107 | Label::~Label() | ||
108 | { | ||
109 | SDL_FreeSurface(image); | ||
110 | } | ||
111 | */ | ||
112 | vector<int> dirx = {1,0,-1,0}; | ||
113 | vector<int> diry = {0,1,0,-1}; | ||
114 | |||
115 | |||
116 | int xres = 600; | ||
117 | int yres = 450; | ||
118 | SDL_Surface *screen; | ||
119 | |||
120 | bool inBounds(int x, int y) | ||
121 | { | ||
122 | if(x >= 0 && x < xres && y >= 0 && y < yres) | ||
123 | return true; | ||
124 | return false; | ||
125 | } | ||
126 | |||
127 | void drawPixel(SDL_Surface *screen,int x, int y, Uint8 R, Uint8 G, Uint8 B) | ||
128 | { | ||
129 | Uint32 color = SDL_MapRGB(screen->format, R, G, B); | ||
130 | |||
131 | SDL_Rect r; | ||
132 | r.x = x; | ||
133 | r.y = y; | ||
134 | r.w = 1; | ||
135 | r.h = 1; | ||
136 | SDL_FillRect(screen,&r,color); | ||
137 | } | ||
138 | |||
139 | void drawCircle(SDL_Surface* screen, double x, double y, double radius, Uint8 R, Uint8 G, Uint8 B) | ||
140 | { | ||
141 | for(int i = x-radius; i <= x+radius; ++i) | ||
142 | for(int j = y-radius; j <= y+radius; ++j) | ||
143 | if(((i-x)*(i-x)+(j-y)*(j-y) <= radius*radius) && inBounds(i,j)) | ||
144 | { | ||
145 | drawPixel(screen, i,j,R,G,B); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | void blacken(SDL_Surface* screen) | ||
150 | { | ||
151 | SDL_FillRect(screen,NULL,0); | ||
152 | } | ||
153 | |||
154 | |||
155 | class Obstacle | ||
156 | { | ||
157 | public: | ||
158 | SDL_Rect r; | ||
159 | int vx; | ||
160 | Obstacle(double xn, double yn, double width, double height) | ||
161 | { | ||
162 | r.x = xn; | ||
163 | r.y = yn; | ||
164 | r.h = height; | ||
165 | r.w = width; | ||
166 | vx = 0; | ||
167 | } | ||
168 | bool check_death(double x_pos, double y_pos, double radius) | ||
169 | { | ||
170 | 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) | ||
171 | return false; | ||
172 | return true; | ||
173 | } | ||
174 | void draw(SDL_Surface* screen,double levelx, double levely) | ||
175 | { | ||
176 | SDL_Rect temp = r; | ||
177 | temp.x -= levelx; | ||
178 | temp.y -= levely; | ||
179 | temp.y = yres - temp.y- temp.h; | ||
180 | SDL_FillRect(screen, &temp, SDL_MapRGB(screen->format, 255, 255, 255)); | ||
181 | } | ||
182 | void step(double time) | ||
183 | { | ||
184 | r.x += time*vx; | ||
185 | } | ||
186 | }; | ||
187 | |||
188 | class Particle | ||
189 | { | ||
190 | public: | ||
191 | double x; | ||
192 | double y; | ||
193 | double vx; | ||
194 | double vy; | ||
195 | double life; | ||
196 | // 0 = static, 1 = strife, 2 = randomwalk, 3 = xrandomwalk | ||
197 | int mode; | ||
198 | Uint8 R,G,B; | ||
199 | Particle() | ||
200 | { | ||
201 | x = rand()%xres; | ||
202 | y = rand()%yres; | ||
203 | life = rand()%20; | ||
204 | R = rand()%256; | ||
205 | G = rand()%256; | ||
206 | B = rand()%256; | ||
207 | } | ||
208 | Particle(double xn, double yn, int nmode, Uint8 Rn, Uint8 Gn, Uint8 Bn) | ||
209 | { | ||
210 | mode = nmode; | ||
211 | x = xn; | ||
212 | y = yn; | ||
213 | R = Rn; | ||
214 | G = Gn; | ||
215 | B = Bn; | ||
216 | life = rand()%20; | ||
217 | } | ||
218 | void draw(SDL_Surface* screen,double levelx,double levely) | ||
219 | { | ||
220 | if(inBounds(x-levelx,y-levely)) | ||
221 | drawPixel(screen, x-levelx, yres- (y-levely), R, G, B); | ||
222 | } | ||
223 | bool step(double time) | ||
224 | { | ||
225 | life -= time; | ||
226 | // 0 = static, 1 = strife, 2 = randomwalk, 3 = xrandomwalk | ||
227 | if(mode == 1){ | ||
228 | x += vx*time; | ||
229 | y += vy*time; | ||
230 | } | ||
231 | else if(mode == 2) | ||
232 | { | ||
233 | x += (rand()%3-1)/3.0; | ||
234 | y += (rand()%3-1)/3.0; | ||
235 | } | ||
236 | else if(mode == 3) | ||
237 | { | ||
238 | x += (rand()%3-1)/3.0; | ||
239 | y += vy*time; | ||
240 | } | ||
241 | |||
242 | return (life >= 0); | ||
243 | } | ||
244 | }; | ||
245 | |||
246 | vector<Particle> makeDeathAnimation(double x, double y, double r, double vxb, double vyb, int mode) | ||
247 | { | ||
248 | vector<Particle> result; | ||
249 | for(int i = 0; i < 2000; ++i) | ||
250 | { | ||
251 | double tx = rand()%((int) (2*r+1))-r; | ||
252 | double ty = rand()%((int) (2*r+1))-r; | ||
253 | if(tx*tx + ty * ty > r*r) | ||
254 | continue; | ||
255 | tx += x; | ||
256 | ty += y; | ||
257 | Particle temp = Particle(tx, ty, 1, 100, 100, 100); | ||
258 | double vb = (rand()%5000)/30.0; | ||
259 | double vp = ((rand()%10000)/10000.0)*2*M_PI; | ||
260 | if(!mode) | ||
261 | { | ||
262 | temp.vx = cos(vp)*vb+vxb; | ||
263 | temp.vy = sin(vp)*vb+vyb; | ||
264 | } | ||
265 | else | ||
266 | { | ||
267 | temp.vx = cos(vp)*vb+vxb*(rand()%1000)/1000.0; | ||
268 | temp.vy = sin(vp)*vb+vyb*(rand()%1000)/1000.0; | ||
269 | } | ||
270 | temp.life = (rand()%100)/50.0; | ||
271 | result.push_back(temp); | ||
272 | } | ||
273 | return result; | ||
274 | } | ||
275 | |||
276 | double x_pos = 0; | ||
277 | double x_vel = 0; | ||
278 | double ground_level = 20; | ||
279 | double y_vel = 0; | ||
280 | double ball_rad = 8; | ||
281 | double y_pos = ground_level + ball_rad; | ||
282 | double g = 300.0; | ||
283 | double c_r = 0.004; | ||
284 | double highscore = 1000; | ||
285 | double position_multiplier = 3; | ||
286 | double win_bonus = 5000; | ||
287 | bool hardcore = false; | ||
288 | |||
289 | int main() | ||
290 | { | ||
291 | string name; | ||
292 | cout << "This is a simple jump and run" << endl; | ||
293 | cout << "Please enter your name:"; | ||
294 | cin >> name; | ||
295 | cout << "Please enter difficulty (0-100):" << endl; | ||
296 | int difficulty; | ||
297 | cin >> difficulty; | ||
298 | cout << "Do you want to play in hardcore mode?" << endl; | ||
299 | cin >> hardcore; | ||
300 | if(hardcore) | ||
301 | { | ||
302 | highscore *= 1.5; | ||
303 | cout << "You are insane. +50% score!" << endl; | ||
304 | } | ||
305 | else | ||
306 | cout << "Very sane decision..." << endl; | ||
307 | if(difficulty > 0) | ||
308 | cout << "solvability not guarenteed! Have fun!" << endl; | ||
309 | if(difficulty < 0) | ||
310 | cout << "This should be a piece of cake and you should feel bad!" << endl; | ||
311 | difficulty = max(-50,difficulty); | ||
312 | if(difficulty > 100) | ||
313 | difficulty = 100; | ||
314 | srand(2334508 + difficulty); | ||
315 | cout << "difficulty = " << difficulty << endl; | ||
316 | position_multiplier += difficulty/40.0; | ||
317 | // char l; | ||
318 | if(SDL_Init(SDL_INIT_VIDEO) == -1) | ||
319 | { | ||
320 | cout << "Error: Could not initialize SDL" << endl; | ||
321 | return 0; | ||
322 | } | ||
323 | screen = SDL_SetVideoMode(xres,yres,32,SDL_SWSURFACE); | ||
324 | if(!screen) | ||
325 | { | ||
326 | cout << "could not initialize screen" << endl; | ||
327 | return 0; | ||
328 | } | ||
329 | /* | ||
330 | if(TTF_Init() == -1) | ||
331 | { | ||
332 | cout << "could not initialize True Fonts" << endl; | ||
333 | return 0; | ||
334 | }*/ | ||
335 | SDL_WM_SetCaption("Ballroller - v0.1", NULL); | ||
336 | |||
337 | |||
338 | bool Game_Quit = false; | ||
339 | int down = 0; | ||
340 | int right = 0; | ||
341 | int space = 0; | ||
342 | vector<Obstacle> obstacles; | ||
343 | obstacles.push_back(Obstacle(-50,ground_level,10,200)); | ||
344 | vector<Particle> particles; | ||
345 | bool quickquit = false; | ||
346 | for(int i = 0; i < 10*60*5; ++i) | ||
347 | { | ||
348 | Particle p = Particle(-300 + rand()%10000,ground_level + rand()%((int) (yres- ground_level)),3,255,255,255); | ||
349 | p.vy = -2; | ||
350 | particles.push_back(p); | ||
351 | } | ||
352 | int opos = 100; | ||
353 | while(opos < 10000) | ||
354 | { | ||
355 | opos += rand()%(900-6*difficulty); | ||
356 | int blub = rand()%180 + 3; | ||
357 | obstacles.push_back(Obstacle(opos,ground_level,blub,200/2-blub/2)); | ||
358 | } | ||
359 | if(hardcore) | ||
360 | for(int i = 1; i < 110; ++i) | ||
361 | { | ||
362 | Obstacle tempo = Obstacle(i*100,ground_level, 10, 10); | ||
363 | tempo.vx = -60; | ||
364 | obstacles.push_back(tempo); | ||
365 | } | ||
366 | vector<Particle> deathanimation; | ||
367 | bool lost = false; | ||
368 | long long steps = 0; | ||
369 | SDL_Rect lpos; | ||
370 | lpos.x = 100; | ||
371 | lpos.y = 10; | ||
372 | lpos.w = 300; | ||
373 | lpos.h = 20; | ||
374 | while(!Game_Quit) | ||
375 | { | ||
376 | steps++; | ||
377 | Uint32 start = SDL_GetTicks(); | ||
378 | if(!lost) | ||
379 | highscore--; | ||
380 | blacken(screen); | ||
381 | SDL_Event event; | ||
382 | while(SDL_PollEvent(&event)) | ||
383 | { | ||
384 | if(event.type == SDL_QUIT) | ||
385 | { | ||
386 | Game_Quit = true; | ||
387 | quickquit = true; | ||
388 | break; | ||
389 | } | ||
390 | } | ||
391 | Uint8 *keyState = SDL_GetKeyState(NULL); | ||
392 | |||
393 | down = -keyState[SDLK_UP]+keyState[SDLK_DOWN]; | ||
394 | |||
395 | vector<Particle> nparticles; | ||
396 | for(int i = 0; i < (int) particles.size(); ++i) | ||
397 | { | ||
398 | if(particles[i].step(1/60.0) && particles[i].y > ground_level) | ||
399 | { | ||
400 | particles[i].draw(screen,x_pos-xres/4,0); | ||
401 | nparticles.push_back(particles[i]); | ||
402 | } | ||
403 | } | ||
404 | if(!(steps%100) && hardcore) | ||
405 | { | ||
406 | Obstacle tempo = Obstacle(110*100,ground_level, 10, 10); | ||
407 | tempo.vx = -60; | ||
408 | obstacles.push_back(tempo); | ||
409 | } | ||
410 | for(int i = 0; i < (int) obstacles.size(); ++i) | ||
411 | { | ||
412 | obstacles[i].step(1/60.0); | ||
413 | obstacles[i].draw(screen,x_pos-xres/4,0); | ||
414 | if(!lost) | ||
415 | if(obstacles[i].check_death(x_pos,y_pos,ball_rad)) | ||
416 | { | ||
417 | cout << "Collision: You lost the game" << endl; | ||
418 | cout << "Better luck next time!" << endl; | ||
419 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier)); | ||
420 | cout << "Your highscore: " << highscore << endl; | ||
421 | lost = true; | ||
422 | deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,1); | ||
423 | } | ||
424 | } | ||
425 | particles = nparticles; | ||
426 | for(int i = 0; i < 10; ++i) | ||
427 | { | ||
428 | Particle p = Particle(-300 + rand()%10000,ground_level + rand()%((int) (yres- ground_level)),3,255,255,255); | ||
429 | p.vy = -6; | ||
430 | particles.push_back(p); | ||
431 | } | ||
432 | if(!lost) | ||
433 | { | ||
434 | x_pos = x_pos + x_vel/60.0; | ||
435 | y_pos = y_pos + y_vel/60.0; | ||
436 | drawCircle(screen, xres/4, abs(yres-y_pos), ball_rad, 100, 100, 100); | ||
437 | } | ||
438 | |||
439 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | ||
440 | if(!right) | ||
441 | { | ||
442 | right = -keyState[SDLK_LEFT]+keyState[SDLK_RIGHT]; | ||
443 | } | ||
444 | space = keyState[SDLK_SPACE]; | ||
445 | if(space) | ||
446 | { | ||
447 | cout << "x-Position = " << x_pos << endl; | ||
448 | } | ||
449 | |||
450 | if(y_pos > ground_level+ball_rad) | ||
451 | { | ||
452 | y_vel = y_vel*(1.0-c_r) - g/60.0; | ||
453 | x_vel = x_vel*(1.0-c_r) + right*80/60.0; | ||
454 | } | ||
455 | if(y_pos <= ground_level+ball_rad) | ||
456 | { | ||
457 | x_vel = x_vel*(1.0-c_r) + right*140./60.0; | ||
458 | y_pos = ground_level+ball_rad; | ||
459 | y_vel = fabs(y_vel); | ||
460 | if(down == -1) | ||
461 | y_vel += 900; | ||
462 | y_vel *= 0.25; | ||
463 | } | ||
464 | if(!lost) | ||
465 | if(highscore+x_pos*position_multiplier <= 0) | ||
466 | { | ||
467 | cout << "TIMEOUT: You lost the game." << endl; | ||
468 | cout << "Better luck next time!" << endl; | ||
469 | quickquit = true; | ||
470 | Game_Quit = true; | ||
471 | deathanimation = makeDeathAnimation(x_pos,y_pos,ball_rad,x_vel, y_vel,0); | ||
472 | } | ||
473 | if(x_pos >= 10000) | ||
474 | { | ||
475 | cout << "You win the game!" << endl; | ||
476 | highscore = (int)((1+0.5*hardcore)*(highscore + x_pos*position_multiplier + win_bonus)); | ||
477 | cout << "Your highscore: " << highscore << endl; | ||
478 | Game_Quit = true; | ||
479 | } | ||
480 | for(int i = 0; i < xres; ++i) | ||
481 | drawPixel(screen,i,yres-ground_level,255,255,255); | ||
482 | bool animation_term = true; | ||
483 | for(int i = 0; i < (int) deathanimation.size(); ++i) | ||
484 | { | ||
485 | if(deathanimation[i].step(1/60.0)) | ||
486 | { | ||
487 | animation_term = false; | ||
488 | deathanimation[i].draw(screen,x_pos-xres/4,0); | ||
489 | } | ||
490 | } | ||
491 | if(animation_term && lost) | ||
492 | Game_Quit = true; | ||
493 | SDL_Flip(screen); | ||
494 | Uint32 time = SDL_GetTicks()-start; | ||
495 | if(1000/60.0 - time > 0) | ||
496 | SDL_Delay(1000/60.0 - time); | ||
497 | } | ||
498 | if(!quickquit) | ||
499 | { | ||
500 | ifstream ins; | ||
501 | ins.open("highscore.dat"); | ||
502 | vector<pair<int, string> > highlist; | ||
503 | for(int i = 0; i < 10; ++i) | ||
504 | { | ||
505 | pair<int, string> temp; | ||
506 | ins >> temp.first; | ||
507 | if(!ins.good()) | ||
508 | break; | ||
509 | ins >> temp.second; | ||
510 | if(ins.good()) | ||
511 | { | ||
512 | highlist.push_back(temp); | ||
513 | } | ||
514 | else | ||
515 | break; | ||
516 | } | ||
517 | ins.close(); | ||
518 | sort(highlist.begin(), highlist.end()); | ||
519 | if(highlist.size() < 10 || highlist[0].first < highscore) | ||
520 | { | ||
521 | cout << "Congratulations! You made a new highscore." << endl; | ||
522 | pair<int, string> youscore; | ||
523 | youscore.first = highscore; | ||
524 | youscore.second = name; | ||
525 | highlist.push_back(youscore); | ||
526 | sort(highlist.begin(), highlist.end()); | ||
527 | ofstream ofs; | ||
528 | ofs.open("highscore.dat"); | ||
529 | if(highlist.size() == 11) | ||
530 | { | ||
531 | for(int i = 0; i < 10; ++i) | ||
532 | { | ||
533 | ofs << highlist[i+1].first << " " << highlist[i+1].second << endl; | ||
534 | } | ||
535 | } | ||
536 | else | ||
537 | { | ||
538 | for(int i = 0; i < (int)highlist.size(); ++i) | ||
539 | { | ||
540 | ofs << highlist[i].first << " " << highlist[i].second << endl; | ||
541 | } | ||
542 | } | ||
543 | |||
544 | ofs.close(); | ||
545 | } | ||
546 | cout << "\n**HIGHSCORE**\n"; | ||
547 | for(int i = min(10,(int) highlist.size()-1); i > 0; --i) | ||
548 | { | ||
549 | cout << highlist[i].second << ": " << highlist[i].first << '\n'; | ||
550 | } | ||
551 | if(highlist.size() < 11) | ||
552 | cout << highlist[0].second << ": " << highlist[0].first << '\n'; | ||
553 | cout << endl; | ||
554 | } | ||
555 | |||
556 | atexit(SDL_Quit); | ||
557 | |||
558 | return 0; | ||
559 | } | ||