Advertisement
Guest User

Untitled

a guest
Jan 13th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2.  
  3.  
  4. sf::Color gray = sf::Color(211, 211, 211);
  5.  
  6. struct Segment {
  7.     int y;
  8.     int x;
  9.     int z;
  10.     int width;
  11. };
  12. struct Road {
  13.     int height; //number of elements
  14.     struct Segment *segments; //array of segments
  15.     float playerx;
  16.     int width;
  17. };
  18.  
  19. void gen_road(struct Road* road) {
  20.     int v = road->height;
  21.     for (int i = 1;i <= road->height;i++) {
  22.         road->segments[i - 1].width = road->width;
  23.         road->segments[i - 1].z = i;
  24.         road->segments[i - 1].y = 100;
  25.         road->segments[i - 1].x = 0;
  26.         v--;
  27.     }
  28. }
  29.  
  30. void draw_road(sf::RenderWindow *window, struct Road* road) {
  31.     int i = 0;
  32.     for (int v = road->height-1;v > 0;v--) { //draws each rectangle from furthest to closest
  33.         int s = road->segments[v].width - v*2;
  34.         sf::RectangleShape rs(sf::Vector2f(s, 20)); //makes a rectangle of width s and height 20
  35.         rs.setFillColor(gray);
  36.         rs.setOutlineThickness(1);
  37.         rs.setOutlineColor(sf::Color(250, 150, 100));
  38.         int xoffset = ( (road->segments[v].x + 192) - s / 2) - (road->playerx * s); //displayed x position of rectangle (based on z and x axis)
  39.         int yoffset = road->segments[v].y - road->segments[v].z; //displayed y position of rectangle (based on z and y axis)
  40.         rs.setPosition( xoffset , yoffset);
  41.         window->draw(rs); //displays the rectangle
  42.         i++;
  43.     }
  44. }
  45.  
  46. int main() {
  47.     sf::RenderWindow window(sf::VideoMode(384, 216), "KraD");
  48.     struct Road road;
  49.     road.height = 20;
  50.     road.playerx = 0;
  51.     road.width = 100;
  52.     road.segments = (struct Segment*)malloc(road.height * sizeof(struct Segment));
  53.     gen_road(&road);
  54.     while (window.isOpen())
  55.     {
  56.         sf::Event event;
  57.         while (window.pollEvent(event)) {
  58.             if (event.type == sf::Event::Closed) {
  59.                 window.close();
  60.             }
  61.             if (event.type == sf::Event::KeyPressed)
  62.             {
  63.                 if (event.key.code == sf::Keyboard::D)
  64.                 {
  65.                     road.playerx -= 0.1;
  66.                 }
  67.                 else if (event.key.code == sf::Keyboard::A)
  68.                 {
  69.                     road.playerx += 0.1;
  70.                 }
  71.                 else if (event.key.code == sf::Keyboard::W)
  72.                 {
  73.                     for (int i = 0;i < road.height;i++) {
  74.                         road.segments[i].x -= 10;
  75.                     }
  76.                 }
  77.                 else if (event.key.code == sf::Keyboard::S)
  78.                 {
  79.                     for (int i = 0;i < road.height;i++) {
  80.                         road.segments[i].x += 10;
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         window.clear(sf::Color::Black);
  86.         draw_road(&window, &road);
  87.         window.display();
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement