Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_image.h>
- #include <SDL2/SDL_mixer.h>
- #include <SDL2/SDL_ttf.h> // wszelkie potrzebne biblioteki
- #include <sstream>
- #include "car1.png"
- #include "car2.png"
- #include "route.png"
- using namespace std;
- #include "fps.cpp"
- #include "klasy.cpp"
- SDL_Window * okno;
- SDL_Renderer * render;
- SDL_Event zdarzenie;
- SDL_Rect rect1,rect2;
- const Uint8*keystate;
- SDL_Texture*loadTexture(std::string path)
- {
- SDL_Texture * newTexture = NULL;
- SDL_Surface * loadedSurface - IMG_Load( path.c_str());
- if(loadedSurface == NULL)
- {
- printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str() IMG_getError());
- }
- else
- {
- newTexture = SDL_CreateTextureFromSurface(render, loadedSurface);
- if(newTexture == NULL)
- {
- printf("Unable to create texture from %s! SDL Error: %s\n" path.c_str(), SDL_GetError());
- }
- SDL_FreeSurface(loadedSurface);
- }
- return newTexture;
- }
- int main(int argc, char*args[])
- {
- SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"1");
- okno = SDL_CreateWindow("Babel Racer",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,1366,768,0); // rozdzielczoϾ gry
- render = SDL_CreateRenderer(okno,-1,SDL_RENDERER_ACCELERATED);
- klasa_fps fps
- TTF_Init();
- TTF_Font * arial = TTF_OpenFont("C:/Windows/Fonts/arial.ttf",30);
- SDL_Surface * Surface_tekst;
- SDL_Texture * texture_tekst;
- SDL_Texture*route = loadTexture("route.png"); // ³adowanie trasy
- SDL_Texture*car1 = loadTexture("car1.png"); // ³adowanie samochodu gracza
- SDL_Texture*car2 = loadTexture("car2.png"); // ³adowanie samochodu przeciwnika
- int klatka=0;
- float posY=360;
- float velY=0;
- int punkty=0
- vector < klasa_car2 > v_car2;
- while(true)
- {
- fps.start();
- klatka++;
- keystate = SDL_GetKeyBoardState(NULL);
- while(SDL_PollEvent(&zdarzenie))
- {
- if(zdarzenie.type==SDL_QUIT)
- {
- exit(0);
- }
- {//wydarzenia
- if(klatka%10==0)
- {
- punkty=punkty+1;
- }
- if(velY<1&&velY>-1)
- {
- if(klatka%3==0)
- {
- punkty=punkty+1;
- }
- }
- }
- {
- //render
- SDL_Color kolor = {255,255,255};
- stringstream ss;
- ss<<punkty;
- surface_tekst = TTF_RenderText_Blended(arial,ss.str().c_str(),kolor);
- texture_tekst = SDL_CreateTextureFromSurface(render,Surface_tekst);
- rect2.x=0;rect2.y=0;rect2.w=300,rect2.h=100;
- SDL_RenderCopy(render,texture_tekst,NULL,&rect2);
- SDL_DestroySurface(surface_tekst);
- SDL_FreeTexture(texture_tekst);
- }
- }
- { //spawnowanie samochodow przeciwnika
- if(klatka%15==0)
- {
- klasa_car2.obj;
- obj.init();
- v_car2.push_back(obj);
- }
- for(int loop=0;loop<v_car2.size();loop++)
- {
- if(v_car2.at(loop).aktywny==false)
- {
- if aktywny==true;
- posX=posX-10;
- if(posX<0)
- {
- aktywny=false;
- }
- SDL_Rect rect;
- rect.x=posX;
- rect.y=posY-20;
- rect.w=80;
- rect.h=40;
- SDL_RenderCopy(render,tekstura,NULL,&rect);
- }
- }
- }
- {
- //sterowanie
- if(keystate[SDL_SCANCODE_DOWN])
- {
- if(velY<25)
- {
- velY=velY+0.6;
- }
- }
- if(keystate[SDL_SCANCODE_UP])
- {
- if(velY>-25)
- {
- velY=velY-0.6;
- }
- }
- velY=velY*0.9;
- }
- { // Fizyka
- posY=posY+velY;
- if(posY<100) // poza trasa
- {
- velY=velY+0.1;
- velY=velY*0.9;
- }
- if(posY>600)
- {
- velY=velY-1;
- velY=velY*0.9;
- }
- }
- { // Renderowanie
- { // Tlo
- SDL_RenderClear(render);
- }
- { // Droga
- for(int loop=0;loop<1;loop++)
- {
- rect2.x=loop*768-(klatka*7)%200; // szerokosc trasy + szybkosc poruszania pojazdu
- rect2.y=0;
- rect2.w=200;
- rect2.h=720;
- SDL_RenderCopy(render,droga,NULL,&rect2);
- }
- }
- { //car1 gracz
- rect2.x=100;
- rect2.y=posY-20;
- rect2.w=80;
- rect2.h=40;
- SDL_RenderCopyEX(render,car1,NULL,&rect2,velY*4,NULL,SDL_FLIP_NONE);
- }
- {// car2 - przeciwnik
- for(int loop=0;loop<v_car2.size();loop++)
- {
- v_car2.at(loop).update(render,car2,posY);
- }
- }
- }
- SDL_RenderPresent(render);
- fps.end();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement