Kaktusen

Untitled

Oct 19th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.12 KB | None | 0 0
  1. //including libraries
  2. //Cpu.score < 2000 || (Cpu.score > 2500 && Cpu.score < 15000) || (Cpu.score > 18000 && Cpu.score < 60000)
  3. #include "SDL/SDL.h"
  4. #include "SDL/SDL_ttf.h"
  5. #include "SDL/SDL_mixer.h";
  6.  
  7. #include <ctime>
  8. #include <stdlib.h>
  9. #include <cstdlib>
  10. #include <iostream>
  11. #include <string>
  12. #include <sstream>
  13.  
  14. #include "cpu.h"
  15. #include "player.cpp"
  16. #include "monster.cpp"
  17.  
  18. #define BLACK {0, 0, 0}
  19.  
  20. using namespace std;
  21.  
  22. //Surfaces
  23. SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  24. SDL_Surface *background = SDL_LoadBMP("data/images/background.bmp");
  25. SDL_Surface *tileset = SDL_LoadBMP("data/images/tileset.bmp");
  26.  
  27. void apply_text(SDL_Surface *destination, string msg, int x, int y, int size, SDL_Color color) {
  28.     TTF_Font *font;
  29.     font = TTF_OpenFont("data/font.ttf", size);
  30.  
  31.     SDL_Rect rect;
  32.     rect.x = x;
  33.     rect.y = y;
  34.  
  35.     SDL_Surface *message = NULL;
  36.     message = TTF_RenderText_Solid(font, msg.c_str(), color);
  37.  
  38.     SDL_BlitSurface(message, NULL, destination, &rect);
  39.  
  40.     TTF_CloseFont(font);
  41.     SDL_FreeSurface(message);
  42. }
  43.  
  44. void apply_int(SDL_Surface* destination, int number, int x, int y, int size, SDL_Color color) {
  45.     TTF_Font *font;
  46.     font = TTF_OpenFont("data/font.ttf", size);
  47.  
  48.     string s;
  49.     stringstream out;
  50.     out << number;
  51.     s = out.str();
  52.  
  53.     SDL_Rect rect;
  54.     rect.x = x;
  55.     rect.y = y;
  56.  
  57.     SDL_Surface *message = NULL;
  58.     message = TTF_RenderText_Solid(font, s.c_str(), color);
  59.  
  60.     SDL_BlitSurface(message, NULL, destination, &rect);
  61.  
  62.     TTF_CloseFont(font);
  63.     SDL_FreeSurface(message);
  64. }
  65.  
  66. void checkCpu() {
  67.     Cpu.gameLoop = true;
  68.     Cpu.fps = 30;
  69.     Cpu.currentTick = SDL_GetTicks();
  70.     Cpu.score = 0;
  71.     Cpu.time = 10;
  72.     Cpu.lvl = 1;
  73.     Cpu.kill = 0;
  74.     Cpu.shoot = 0;
  75.     Cpu.bossfight = false;
  76. }
  77.  
  78. void load_sprite(int x, int y, SDL_Surface *source, SDL_Surface *destination, int bx, int by, int width, int height) {
  79.     SDL_Rect rect;
  80.     SDL_Rect offset;
  81.  
  82.     offset.x = x;
  83.     offset.y = y;
  84.  
  85.     rect.x = bx;
  86.     rect.y = by;
  87.     rect.w = width;
  88.     rect.h = height;
  89.  
  90.     SDL_BlitSurface(source, &rect, destination, &offset);
  91. }
  92.  
  93. void load_sound() {
  94.     Mix_Music *songName = songName = Mix_LoadMUS("data/sound.mp3");
  95.     Mix_VolumeMusic(25);
  96.  
  97.     Mix_PlayMusic(songName, -1);
  98. }
  99.  
  100. void game() {
  101.     SDL_Init(SDL_INIT_EVERYTHING);
  102.     SDL_WM_SetCaption("Bullshoo - V 0.7.4 - BETA!", NULL);
  103.     TTF_Init();
  104.     Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
  105.     srand(time(NULL));
  106.  
  107.     SDL_Rect player;
  108.  
  109.     SDL_Rect monster1[10];
  110.     SDL_Rect monster2[5];
  111.     SDL_Rect monster3[8];
  112.     SDL_Rect monster4[15];
  113.  
  114.     SDL_Rect boss1;
  115.     SDL_Rect boss2;
  116.      
  117.     SDL_Rect bullet[100];
  118.  
  119.     checkCpu();
  120.     cPlayer.checkVars();
  121.     checkBullet();
  122.  
  123.     for(int i=0; i<10; i++) {
  124.         cMonster1[i].checkMonster1();
  125.     }
  126.  
  127.     for(int i=0; i<5; i++) {
  128.         cMonster2[i].checkMonster2();
  129.     }
  130.  
  131.     for(int i=0; i<8; i++) {
  132.         cMonster3[i].checkMonster3();
  133.     }
  134.  
  135.     for(int i=0; i<15; i++) {
  136.         cMonster4[i].checkMonster4();
  137.     }
  138.  
  139.     cBoss1.checkBoss1();
  140.     cBoss2.checkBoss2();
  141.  
  142.     Uint32 colorkey = SDL_MapRGB(tileset -> format, 255, 0, 255);
  143.     SDL_SetColorKey(tileset, SDL_SRCCOLORKEY, colorkey);
  144.  
  145.     load_sound();
  146.  
  147.     //Runs the game
  148.     while(Cpu.gameLoop) {
  149.         if(Cpu.time>0) {
  150.             Cpu.time--;
  151.         }
  152.    
  153.         //player_rect
  154.         player.x = cPlayer.x;
  155.         player.y = cPlayer.y;
  156.         player.w = cPlayer.w;
  157.         player.h = cPlayer.h;
  158.        
  159.         //boss1_rect
  160.         boss1.x = cBoss1.x;
  161.         boss1.y = cBoss1.y;
  162.         boss1.w = cBoss1.w;
  163.         boss1.h = cBoss1.h;
  164.        
  165.         boss2.x = cBoss2.x;
  166.         boss2.y = cBoss2.y;
  167.         boss2.w = cBoss2.w;
  168.         boss2.h = cBoss2.h;
  169.    
  170.         //monster1_rect
  171.         for(int i=0; i<10; i++) {
  172.           monster1[i].x = cMonster1[i].x;
  173.           monster1[i].y = cMonster1[i].y;
  174.           monster1[i].w = 32;
  175.           monster1[i].h = 32;
  176.         }
  177.    
  178.         //monster2_rect
  179.         for(int i=0; i<5; i++) {
  180.           monster2[i].x = cMonster2[i].x;
  181.           monster2[i].y = cMonster2[i].y;
  182.           monster2[i].w = cMonster2[i].w;
  183.           monster2[i].h = cMonster2[i].h;
  184.         }
  185.  
  186.         //monster3_rect
  187.         for(int i=0; i<8; i++) {
  188.           monster3[i].x = cMonster3[i].x;
  189.           monster3[i].y = cMonster3[i].y;
  190.           monster3[i].w = cMonster3[i].w;
  191.           monster3[i].h = cMonster3[i].h;
  192.         }
  193.  
  194.     //monster4_rect
  195.         for(int i=0; i<15; i++) {
  196.           monster4[i].x = cMonster4[i].x;
  197.           monster4[i].y = cMonster4[i].y;
  198.           monster4[i].w = cMonster4[i].w;
  199.           monster4[i].h = cMonster4[i].h;
  200.         }
  201.  
  202.         //bullet_rect
  203.         for(int i=0; i<100; i++) {
  204.           bullet[i].x = cBullet[i].A.x;
  205.           bullet[i].y = cBullet[i].A.y;
  206.           bullet[i].w = cBullet[i].w;
  207.           bullet[i].h = cBullet[i].h;
  208.         }
  209.  
  210.         Cpu.currentTick = SDL_GetTicks();
  211.         cPlayer.input();
  212.  
  213.    
  214.         //MoveMonsters
  215.         if(Cpu.bossfight == false) {
  216.             for(int i=0; i<10; i++) {
  217.                 cMonster1[i].moveMonster();
  218.             }
  219.  
  220.             if(Cpu.score >= 250) {
  221.                 for(int i=0; i<5; i++) {
  222.                     cMonster2[i].moveMonster2();
  223.                 }
  224.             }
  225.      
  226.             if(Cpu.score >= 800) {
  227.                 for(int i=0; i<8; i++) {
  228.                     cMonster3[i].moveMonster3();
  229.                 }
  230.             }
  231.      
  232.             if(Cpu.score >= 1500) {
  233.                 for(int i=0; i<15; i++) {
  234.                     cMonster4[i].moveMonster4();
  235.                 }
  236.             }
  237.         }
  238.    
  239.         //checks if player is hit by monsters
  240.         if(Cpu.bossfight == false) {
  241.             for(int i=0; i<10; i++) {
  242.                 if(cPlayer.collision(player, monster1[i])) {
  243.                     Cpu.gameLoop = false;
  244.                 }
  245.             }
  246.  
  247.             for(int i=0; i<5; i++) {
  248.                 if(cPlayer.collision(player, monster2[i])) {
  249.                     Cpu.gameLoop = false;
  250.                 }
  251.             }
  252.  
  253.             for(int i=0; i<8; i++) {
  254.                 if(cPlayer.collision(player, monster3[i])) {
  255.                     Cpu.gameLoop = false;
  256.                 }
  257.             }
  258.  
  259.             for(int i=0; i<15; i++) {
  260.                 if(cPlayer.collision(player, monster4[i])) {
  261.                     Cpu.gameLoop = false;
  262.                 }
  263.             }
  264.         }
  265.    
  266.         //if player is hit by boss1
  267.         if(cPlayer.collision(player, boss1)) {
  268.             Cpu.gameLoop = false;
  269.         }
  270.  
  271.         //if player is hit by boss2
  272.         if(cPlayer.collision(player, boss2)) {
  273.             Cpu.gameLoop = false;
  274.         }
  275.    
  276.         //checks if monster is hit by bullet
  277.         //monster1
  278.         if(Cpu.bossfight == false) {
  279.             for(int i=0; i<10; i++) {
  280.                 for(int i2=0; i2<100; i2++) {
  281.                     if(cPlayer.collision(monster1[i], bullet[i2])) {
  282.                         cMonster1[i].x = rand() % 1500 + 640;
  283.                         cMonster1[i].x = rand() % 1230 + 640;
  284.                         Cpu.score += 10;
  285.                         Cpu.kill += 1;
  286.                         cBullet[i2].alive = false;
  287.                         cBullet[i2].A.x = 0;
  288.                         cBullet[i2].A.y = 0;
  289.                     }
  290.                 }      
  291.             }
  292.         }
  293.    
  294.         //boss1
  295.         for(int i2=0; i2<100; i2++) {
  296.             if(Cpu.score >= 2000 && Cpu.score <= 10000) {
  297.                 if(cPlayer.collision(boss1, bullet[i2])) {
  298.                     cBoss1.life--;
  299.                     cBullet[i2].alive = false;
  300.                     cBullet[i2].A.x = 0;
  301.                     cBullet[i2].A.y = 0;
  302.                 }
  303.             }
  304.         }
  305.  
  306.         //boss2
  307.         for(int i2=0; i2<100; i2++) {
  308.             if(Cpu.score >= 15000 && Cpu.score <= 20000) {
  309.                 if(cPlayer.collision(boss2, bullet[i2])) {
  310.                     cBoss2.life--;
  311.                     cBullet[i2].alive = false;
  312.                     cBullet[i2].A.x = 0;
  313.                     cBullet[i2].A.y = 0;
  314.                 }
  315.             }
  316.         }
  317.    
  318.         if(Cpu.bossfight == false) {
  319.             if(Cpu.score >= 250) {
  320.                 for(int i=0; i<5; i++) {
  321.                     for(int i2=0; i2<100; i2++) {
  322.                         if(cPlayer.collision(monster2[i], bullet[i2])) {
  323.                             cMonster2[i].x = rand() % 1300 + 640;
  324.                             cMonster2[i].y = rand() % 1430 + 640;
  325.                             Cpu.score += 15;
  326.                             Cpu.kill+=1;
  327.                             cBullet[i2].alive = false;
  328.                             cBullet[i2].A.x = 0;
  329.                             cBullet[i2].A.y = 0;
  330.          
  331.                         }
  332.                     }
  333.                 }
  334.             }
  335.    
  336.             for(int i=0; i<8; i++) {
  337.                 for(int i2=0; i2<100; i2++) {
  338.                     if(Cpu.score >= 800) {
  339.                         if(cPlayer.collision(monster3[i], bullet[i2])) {
  340.                             cMonster3[i].life--;
  341.    
  342.                             cBullet[i2].alive = false;
  343.                             cBullet[i2].A.x = 0;
  344.                             cBullet[i2].A.y = 0;
  345.                         }
  346.                     }
  347.                 }
  348.             }
  349.    
  350.             if(Cpu.score >= 1500) {
  351.                 for(int i=0; i<15; i++) {
  352.                     for(int i2=0; i2<100; i2++) {
  353.                         if(cPlayer.collision(monster4[i], bullet[i2])) {
  354.                             cMonster4[i].life--;
  355.            
  356.                             cBullet[i2].alive = false;
  357.                             cBullet[i2].A.x = 0;
  358.                             cBullet[i2].A.y = 0;
  359.                         }
  360.                     }
  361.                 }
  362.             }
  363.         }
  364.  
  365.         for(int i=0; i<8; i++) {
  366.             if(cMonster3[i].life<=0) {
  367.                 cMonster3[i].x = rand() % 1150 + 640;
  368.                 cMonster3[i].x = rand() % 1005 + 480;
  369.                 Cpu.score += 20;
  370.                 Cpu.kill+=1;
  371.      
  372.                 cMonster3[i].life = 3;
  373.             }
  374.         }
  375.      
  376.  
  377.         for(int i=0; i<15; i++) {
  378.             if(cMonster4[i].life<=0) {
  379.  
  380.                 cMonster4[i].x = rand() % 2000 + 640;
  381.                 cMonster4[i].y = rand() % (480-50) + 10;
  382.      
  383.                 cMonster4[i].life = 2;
  384.                 Cpu.score += 50;
  385.                 Cpu.kill+=1;
  386.             }
  387.         }
  388.    
  389.         load_sprite(0, 0, background, screen, 0, 0, 640, 480);
  390.    
  391.         //print out bullet and stuff for it
  392.         for(int i=0; i<100; i++) {
  393.             if(cBullet[i].alive == true) {
  394.                 cBullet[i].A.x += 5;
  395.                 cBullet[i].A.w = cBullet[i].w;
  396.                 cBullet[i].A.h = cBullet[i].h;
  397.    
  398.                 SDL_FillRect(screen, &cBullet[i].A, 0xFFFF66);
  399.    
  400.                 if(cBullet[i].A.x > 640) {
  401.                     cBullet[i].alive = false;
  402.                 }
  403.             }
  404.         }
  405.    
  406.    
  407.         //Print out things to screen
  408.         //player
  409.         load_sprite(cPlayer.x, cPlayer.y, tileset, screen, cPlayer.bx, cPlayer.by, cPlayer.w, cPlayer.h);
  410.    
  411.         if(Cpu.bossfight == false) {
  412.             //monster1
  413.             for(int i=0; i<10; i++) {
  414.                 load_sprite(cMonster1[i].x, cMonster1[i].y, tileset, screen, cMonster1[i].bx, cMonster1[i].by, cMonster1[i].w, cMonster1[i].h);
  415.             }
  416.             //monster2
  417.             if(Cpu.score >= 250) {
  418.                 for(int i=0; i<5; i++) {
  419.                     load_sprite(cMonster2[i].x, cMonster2[i].y, tileset, screen, cMonster2[i].bx, cMonster2[i].by, cMonster2[i].w, cMonster2[i].h);
  420.                 }
  421.             }
  422.    
  423.             //monster3
  424.             if(Cpu.score >= 800) {
  425.                 for(int i=0; i<8; i++) {
  426.                     load_sprite(cMonster3[i].x, cMonster3[i].y, tileset, screen, cMonster3[i].bx, cMonster3[i].by, cMonster3[i].w, cMonster3[i].h);
  427.                 }
  428.             }
  429.  
  430.             //monster 4
  431.             if(Cpu.score >= 1500) {
  432.                 for(int i=0; i<15; i++) {
  433.                     load_sprite(cMonster4[i].x, cMonster4[i].y, tileset, screen, cMonster4[i].bx, cMonster4[i].by, cMonster4[i].w, cMonster4[i].h);
  434.                 }
  435.             }
  436.         }
  437.  
  438.         if((Cpu.bossfight == true && (Cpu.score >= 2000 && Cpu.score <= 2100)) || (Cpu.bossfight == true && (Cpu.score >= 15000 && Cpu.score <= 15100))) {
  439.             for(int i=0; i<10; i++) {
  440.                 cMonster1[i].moveBack1();
  441.             }
  442.    
  443.             for(int i=0; i<5; i++) {
  444.                 cMonster2[i].moveBack2();
  445.             }
  446.    
  447.             for(int i=0; i<8; i++) {
  448.                 cMonster3[i].moveBack3();
  449.             }
  450.    
  451.             for(int i=0; i<15; i++) {
  452.                 cMonster4[i].moveBack4();
  453.             }
  454.         }
  455.  
  456.    
  457.         //boss1
  458.         if(Cpu.score >= 2000 && cBoss1.life >= 1) {
  459.             cBoss1.moveBoss1();
  460.             Cpu.bossfight = true;
  461.             apply_int(screen, cBoss1.life, cBoss1.x, cBoss1.y-55, 50, BLACK);
  462.             load_sprite(cBoss1.x, cBoss1.y, tileset, screen, cBoss1.bx, cBoss1.by, cBoss1.w, cBoss1.h);    
  463.         }else if(Cpu.score >= 15000 && cBoss2.life >= 1) {
  464.             cBoss2.moveBoss2();
  465.             Cpu.bossfight = true;
  466.             apply_int(screen, cBoss2.life, cBoss2.x, cBoss2.y-55, 50, BLACK);
  467.             load_sprite(cBoss2.x, cBoss2.y, tileset, screen, cBoss2.bx, cBoss2.by, cBoss2.w, cBoss2.h);
  468.         } else {
  469.             Cpu.bossfight = false;
  470.         }
  471.  
  472.  
  473.         //if bosses is dead
  474.         if(cBoss1.life <= 0 || cBoss2.life <= 0) {
  475.             Cpu.bossfight = false;
  476.         } else {
  477.             Cpu.bossfight = true;
  478.         }
  479.    
  480.         //checks so the boss is dead and then move it out of the game
  481.         if(cBoss1.life <= 0 && cBoss1.life >= -2) {
  482.             cBoss1.x = 640+100;
  483.             Cpu.score += 10000;
  484.             cBoss1.life = -5;
  485.         }
  486.  
  487.         if(cBoss2.life <= 0 && cBoss2.life >= -2) {
  488.             cBoss2.x = 640+100;
  489.             Cpu.score += 50000;
  490.             cBoss2.life = -5;
  491.         }
  492.    
  493.         //check wich level it is!
  494.         if(Cpu.score >= 100) {
  495.             Cpu.lvl = 2;
  496.         }
  497.         if(Cpu.score >= 250) {
  498.             Cpu.lvl = 3;
  499.         }
  500.  
  501.         if(Cpu.score >= 12000) {
  502.             Cpu.lvl = 4;
  503.         }
  504.    
  505.         if(Cpu.score >= 60000) {
  506.             Cpu.lvl = 5;
  507.         }
  508.    
  509.         apply_text(screen, "Score: ", 0, 0, 50, BLACK);
  510.         apply_int(screen, Cpu.score, 65, 0, 50, BLACK);
  511.    
  512.         apply_text(screen, "Level: ", 0, 40, 50, BLACK);
  513.         apply_int(screen, Cpu.lvl, 65, 40, 50, BLACK);
  514.    
  515.         apply_text(screen, "Kills: ", 0, 80, 50, BLACK);
  516.         apply_int(screen, Cpu.kill, 65, 80, 50, BLACK);
  517.    
  518.         apply_text(screen, "Bullet: ", 0, 120, 50, BLACK);
  519.         apply_int(screen, Cpu.shoot, 65, 120, 50, BLACK);
  520.    
  521.         apply_text(screen, "Life: ", 0, 160, 50, BLACK);
  522.         apply_int(screen, cPlayer.life, 65, 160, 50, BLACK);
  523.    
  524.    
  525.         if(SDL_GetTicks() - Cpu.currentTick < 1000 / Cpu.fps) {
  526.             SDL_Delay(((1000/Cpu.fps)-(SDL_GetTicks() - Cpu.currentTick)));
  527.         }
  528.    
  529.         SDL_Flip(screen);
  530.     }
  531.     SDL_CloseAudio();
  532. }
Advertisement
Add Comment
Please, Sign In to add comment