Advertisement
AllenYuan

paltforms

May 25th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3. using namespace sf;
  4.  
  5. struct point
  6. { int x,y; };
  7.  
  8. int main()
  9. {
  10.   srand(time(0));
  11.   RenderWindow app(VideoMode(400, 533), "Doodle Game!"); // render app
  12.   app.setFramerateLimit(60);
  13.  
  14.   Texture t1,t2,t3;
  15.   t1.loadFromFile("/Users/alleyuan/code-snippets/16_games/02 Doodle Jump/images/background.png");
  16.   t2.loadFromFile("/Users/alleyuan/code-snippets/16_games/02 Doodle Jump/images/platform.png");
  17.   t3.loadFromFile("/Users/alleyuan/code-snippets/16_games/02 Doodle Jump/images/doodle.png");
  18.  
  19.   Sprite sBackground(t1), sPlat(t2), sPers(t3);
  20.  
  21.   point plat[20]; // platform locations
  22.  
  23.   for (int i = 0; i < 10; i++) // set platform locations
  24.   {
  25.     plat[i].x = rand()%400;
  26.     plat[i].y = rand()%533;
  27.   }
  28.  
  29.   int x = 100, y = 100, h = 200;
  30.   float dx = 0, dy = 0;
  31.  
  32.   while (app.isOpen())
  33.   {
  34.     Event e;
  35.     while (app.pollEvent(e))
  36.     {
  37.       if (e.type == Event::Closed) // close window
  38.         app.close();
  39.     }
  40.  
  41.     if (Keyboard::isKeyPressed(Keyboard::Right)) x += 3; // go right
  42.     if (Keyboard::isKeyPressed(Keyboard::Left)) x -= 3; // go left
  43.  
  44.     dy += 0.2; // jump w/ gravity
  45.     y += dy;
  46.     if (y > 500) dy = -10;
  47.  
  48.     if (y < h)
  49.     for (int i = 0; i < 10; i++) // move platforms and re-render once they're out of screen
  50.     {
  51.       y = h;
  52.       plat[i].y = plat[i].y - dy;
  53.       if (plat[i].y > 533) { plat[i].y = 0; plat[i].x = rand() % 400; }
  54.     }
  55.  
  56.     for (int i = 0; i < 10; i++) // platform jumping
  57.     if ((x+50 > plat[i].x) && (x+20 < plat[i].x + 68)
  58.     && (y + 70 > plat[i].y) && (y + 70 < plat[i].y + 14) && (dy > 0)) dy = -10;
  59.  
  60.     sPers.setPosition(x, y);
  61.  
  62.     app.draw(sBackground);
  63.     app.draw(sPers);
  64.     for (int i = 0; i < 10; i++) // draw platforms
  65.     {
  66.       sPlat.setPosition(plat[i].x, plat[i].y);
  67.       app.draw(sPlat);
  68.     }
  69.  
  70.     app.display();
  71.   }
  72.  
  73.   return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement