Advertisement
Guest User

Untitled

a guest
Jan 6th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_image.h>
  5. #include <SDL2/SDL_mixer.h>
  6. #include <SDL2/SDL_ttf.h> // wszelkie potrzebne biblioteki
  7. #include <sstream>
  8. #include "car1.png"
  9. #include "car2.png"
  10. #include "route.png"
  11.  
  12. using namespace std;
  13.  
  14. #include "fps.cpp"
  15. #include "klasy.cpp"
  16.  
  17. SDL_Window * okno;
  18. SDL_Renderer * render;
  19. SDL_Event zdarzenie;
  20. SDL_Rect rect1,rect2;
  21.  
  22. const Uint8*keystate;
  23.  
  24. SDL_Texture*loadTexture(std::string path)
  25. {
  26.    SDL_Texture * newTexture = NULL;
  27.    SDL_Surface * loadedSurface - IMG_Load( path.c_str());
  28.    if(loadedSurface == NULL)
  29.    {
  30.        printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str() IMG_getError());
  31.    }
  32.    else
  33.    {
  34.        newTexture = SDL_CreateTextureFromSurface(render, loadedSurface);
  35.        if(newTexture == NULL)
  36.        {
  37.            printf("Unable to create texture from %s! SDL Error: %s\n" path.c_str(), SDL_GetError());
  38.        }
  39.        SDL_FreeSurface(loadedSurface);
  40.    }
  41.    return newTexture;
  42. }
  43.  
  44. int main(int argc, char*args[])
  45. {
  46.     SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"1");
  47.     okno = SDL_CreateWindow("Babel Racer",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1366,768,0);  // rozdzielczoœæ gry
  48.     render = SDL_CreateRenderer(okno,-1,SDL_RENDERER_ACCELERATED);
  49.  
  50.     klasa_fps fps
  51.  
  52.     TTF_Init();
  53.  
  54.     TTF_Font * arial = TTF_OpenFont("C:/Windows/Fonts/arial.ttf",30);
  55.  
  56.     SDL_Surface * Surface_tekst;
  57.     SDL_Texture * texture_tekst;
  58.  
  59.     SDL_Texture*route = loadTexture("route.png"); // ³adowanie trasy
  60.     SDL_Texture*car1 = loadTexture("car1.png"); // ³adowanie samochodu gracza
  61.     SDL_Texture*car2 = loadTexture("car2.png"); // ³adowanie samochodu przeciwnika
  62.  
  63.     int klatka=0;
  64.  
  65.     float posY=360;
  66.     float velY=0;
  67.     int punkty=0
  68.  
  69.     vector < klasa_car2 > v_car2;
  70.  
  71.     while(true)
  72.     {
  73.         fps.start();
  74.         klatka++;
  75.         keystate = SDL_GetKeyBoardState(NULL);
  76.  
  77.         while(SDL_PollEvent(&zdarzenie))
  78.         {
  79.             if(zdarzenie.type==SDL_QUIT)
  80.             {
  81.                 exit(0);
  82.             }
  83.  
  84.         {//wydarzenia
  85.             if(klatka%10==0)
  86.             {
  87.                 punkty=punkty+1;
  88.             }
  89.  
  90.             if(velY<1&&velY>-1)
  91.             {
  92.                   if(klatka%3==0)
  93.                   {
  94.                       punkty=punkty+1;
  95.                   }
  96.             }
  97.  
  98.         }
  99.  
  100.         {
  101.             //render
  102.             SDL_Color kolor = {255,255,255};
  103.             stringstream ss;
  104.             ss<<punkty;
  105.             surface_tekst = TTF_RenderText_Blended(arial,ss.str().c_str(),kolor);
  106.  
  107.             texture_tekst = SDL_CreateTextureFromSurface(render,Surface_tekst);
  108.  
  109.             rect2.x=0;rect2.y=0;rect2.w=300,rect2.h=100;
  110.  
  111.             SDL_RenderCopy(render,texture_tekst,NULL,&rect2);
  112.  
  113.             SDL_DestroySurface(surface_tekst);
  114.             SDL_FreeTexture(texture_tekst);
  115.         }
  116.     }
  117.  
  118.     { //spawnowanie samochodow przeciwnika
  119.         if(klatka%15==0)
  120.         {
  121.             klasa_car2.obj;
  122.             obj.init();
  123.             v_car2.push_back(obj);
  124.         }
  125.         for(int loop=0;loop<v_car2.size();loop++)
  126.         {
  127.             if(v_car2.at(loop).aktywny==false)
  128.             {
  129.                 if aktywny==true;
  130.                 posX=posX-10;
  131.  
  132.  
  133.                     if(posX<0)
  134.                     {
  135.                         aktywny=false;
  136.                     }
  137.  
  138.                     SDL_Rect rect;
  139.                     rect.x=posX;
  140.                     rect.y=posY-20;
  141.                     rect.w=80;
  142.                     rect.h=40;
  143.                     SDL_RenderCopy(render,tekstura,NULL,&rect);
  144.             }
  145.         }
  146.  
  147.     }
  148.  
  149.     {
  150.         //sterowanie
  151.         if(keystate[SDL_SCANCODE_DOWN])
  152.         {
  153.             if(velY<25)
  154.             {
  155.                 velY=velY+0.6;
  156.             }
  157.         }
  158.         if(keystate[SDL_SCANCODE_UP])
  159.         {
  160.             if(velY>-25)
  161.             {
  162.                 velY=velY-0.6;
  163.             }
  164.         }
  165.  
  166.         velY=velY*0.9;
  167.     }
  168.  
  169.         { // Fizyka
  170.             posY=posY+velY;
  171.  
  172.             if(posY<100) // poza trasa
  173.             {
  174.                 velY=velY+0.1;
  175.                 velY=velY*0.9;
  176.             }
  177.             if(posY>600)
  178.             {
  179.                 velY=velY-1;
  180.                 velY=velY*0.9;
  181.             }
  182.         }
  183.  
  184.         { // Renderowanie
  185.             { // Tlo
  186.                 SDL_RenderClear(render);
  187.             }
  188.  
  189.             { // Droga
  190.              for(int loop=0;loop<1;loop++)
  191.         {
  192.             rect2.x=loop*768-(klatka*7)%200; // szerokosc trasy + szybkosc poruszania pojazdu
  193.             rect2.y=0;
  194.             rect2.w=200;
  195.             rect2.h=720;
  196.             SDL_RenderCopy(render,droga,NULL,&rect2);
  197.         }
  198.             }
  199.  
  200.             { //car1  gracz
  201.                 rect2.x=100;
  202.                 rect2.y=posY-20;
  203.                 rect2.w=80;
  204.                 rect2.h=40;
  205.                 SDL_RenderCopyEX(render,car1,NULL,&rect2,velY*4,NULL,SDL_FLIP_NONE);
  206.             }
  207.  
  208.             {// car2 - przeciwnik
  209.                 for(int loop=0;loop<v_car2.size();loop++)
  210.                 {
  211.                     v_car2.at(loop).update(render,car2,posY);
  212.                 }
  213.             }
  214.  
  215.  
  216.         }
  217.         SDL_RenderPresent(render);
  218.         fps.end();
  219.     }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement