Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <stdio.h> /* printf, scanf, puts, NULL */
- #include <stdlib.h> /* srand, rand */
- #include <time.h>
- sf::Color gray = sf::Color(211, 211, 211);
- struct Road {
- int height; //number of elements
- int *segments; //holds width of each element
- float playerx;
- };
- void gen_road(struct Road* road) {
- int base_width = 100; //width of the bottom element
- for (int y = 1;y <= road->height;y++) {
- int z = base_width - y*4; //decreases width by 4 each time
- road->segments[y - 1] = z;
- }
- }
- void draw_road(sf::RenderWindow *window, struct Road* road) {
- int i = 0;
- for (int v = road->height-1;v > 0;v--) { //draws each rectangle from furthest to closest
- int s = road->segments[v];
- sf::RectangleShape rs(sf::Vector2f(s, 30)); //makes a rectangle of width s and height 30
- rs.setFillColor(gray);
- rs.setOutlineThickness(1);
- rs.setOutlineColor(sf::Color(250, 150, 100));
- int x = ( (384 - s) / 2 ) - (road->playerx * s); //sets position of rectangle relative to x position and to position in segments array
- rs.setPosition( x , i*10); //where i is the index of the rectangle in the segments array
- window->draw(rs); //displays the rectangle
- i++;
- }
- }
- int main() {
- sf::RenderWindow window(sf::VideoMode(384, 216), "KraD");
- struct Road road;
- road.height = 20;
- road.playerx = 0;
- road.segments = (int*)malloc(road.height * sizeof(int));
- gen_road(&road);
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- if (event.type == sf::Event::KeyPressed)
- {
- if (event.key.code == sf::Keyboard::D)
- {
- road.playerx -= 0.1;
- }
- else if (event.key.code == sf::Keyboard::A)
- {
- road.playerx += 0.1;
- }
- }
- }
- window.clear(sf::Color::Black);
- draw_road(&window, &road);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement