AllenYuan

lateral

May 25th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 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.     sPers.setPosition(x, y);
  49.  
  50.     app.draw(sBackground);
  51.     app.draw(sPers);
  52.     for (int i = 0; i < 10; i++) // draw platforms
  53.     {
  54.       sPlat.setPosition(plat[i].x, plat[i].y);
  55.       app.draw(sPlat);
  56.     }
  57.  
  58.     app.display();
  59.   }
  60.  
  61.   return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment