Advertisement
Guest User

Untitled

a guest
Jan 12th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <stdio.h>      /* printf, scanf, puts, NULL */
  3. #include <stdlib.h>     /* srand, rand */
  4. #include <time.h>    
  5.  
  6. sf::Color gray = sf::Color(211, 211, 211);
  7.  
  8. struct Road {
  9.     int height; //number of elements
  10.     int *segments; //holds width of each element
  11.     float playerx;
  12. };
  13.  
  14. void gen_road(struct Road* road) {
  15.     int base_width = 100; //width of the bottom element
  16.     for (int y = 1;y <= road->height;y++) {
  17.         int z = base_width - y*4; //decreases width by 4 each time
  18.         road->segments[y - 1] = z;
  19.     }
  20. }
  21.  
  22. void draw_road(sf::RenderWindow *window, struct Road* road) {
  23.     int i = 0;
  24.     for (int v = road->height-1;v > 0;v--) { //draws each rectangle from furthest to closest
  25.         int s = road->segments[v];
  26.         sf::RectangleShape rs(sf::Vector2f(s, 30)); //makes a rectangle of width s and height 30
  27.         rs.setFillColor(gray);
  28.         rs.setOutlineThickness(1);
  29.         rs.setOutlineColor(sf::Color(250, 150, 100));
  30.         int x = ( (384 - s) / 2 ) - (road->playerx * s); //sets position of rectangle relative to x position and to position in segments array
  31.         rs.setPosition( x , i*10); //where i is the index of the rectangle in the segments array
  32.         window->draw(rs); //displays the rectangle
  33.         i++;
  34.     }
  35. }
  36.  
  37. int main() {
  38.     sf::RenderWindow window(sf::VideoMode(384, 216), "KraD");
  39.     struct Road road;
  40.     road.height = 20;
  41.     road.playerx = 0;
  42.     road.segments = (int*)malloc(road.height * sizeof(int));
  43.     gen_road(&road);
  44.     while (window.isOpen())
  45.     {
  46.         sf::Event event;
  47.         while (window.pollEvent(event)) {
  48.             if (event.type == sf::Event::Closed) {
  49.                 window.close();
  50.             }
  51.             if (event.type == sf::Event::KeyPressed)
  52.             {
  53.                 if (event.key.code == sf::Keyboard::D)
  54.                 {
  55.                     road.playerx -= 0.1;
  56.                 }
  57.                 else if (event.key.code == sf::Keyboard::A)
  58.                 {
  59.                     road.playerx += 0.1;
  60.                 }
  61.             }
  62.         }
  63.         window.clear(sf::Color::Black);
  64.         draw_road(&window, &road);
  65.         window.display();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement