AllenYuan

Untitled

May 30th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <math.h>
  3. using namespace sf;
  4.  
  5. // car encoded by position, speed, angle w/ a move method
  6. struct Car
  7. {
  8. float x, y, speed, angle;
  9.  
  10. Car() {
  11. speed = 2;
  12. angle = 0;
  13. }
  14.  
  15. void move()
  16. {
  17. x += sin(angle) * speed;
  18. y -= cos(angle) * speed;
  19. }
  20. };
  21.  
  22. int main()
  23. {
  24. RenderWindow app(VideoMode(640, 480), "Car Racing Game!");
  25. app.setFramerateLimit(60);
  26.  
  27. Texture t1, t2;
  28. t1.loadFromFile("/Users/alleyuan/code-snippets/16_games/07 Top Down Racing/images/background.png");
  29. t2.loadFromFile("/Users/alleyuan/code-snippets/16_games/07 Top Down Racing/images/car.png");
  30.  
  31. Sprite sBackground(t1), sCar(t2);
  32. sCar.setPosition(300, 300);
  33. sCar.setOrigin(22,22);
  34.  
  35. // set ai cars and starting positions
  36. const int N = 5;
  37. Car car[N];
  38. for (int i = 0; i < N; i++)
  39. {
  40. car[i].x = 300 + i*50;
  41. car[i].y = 1700 + i*80;
  42. car[i].speed = 7 + i;
  43. }
  44.  
  45. float x=300, y=300;
  46. float speed = 0, angle = 0; // initial speed/angle (angle=direciton from car center)
  47. float maxSpeed = 12.0;
  48. float acc = 0.2, dec = 0.3; // acceleration.deceleration
  49. float turnSpeed = 0.08; // angular velocity when turning
  50.  
  51. int offsetX = 0, offsetY = 0;
  52.  
  53. while (app.isOpen())
  54. {
  55. Event e;
  56. while (app.pollEvent(e))
  57. {
  58. if (e.type == Event::Closed)
  59. app.close();
  60. }
  61.  
  62. // set directions
  63. bool Up = 0, Right = 0, Down = 0, Left = 0;
  64. if (Keyboard::isKeyPressed(Keyboard::Up)) Up = 1;
  65. if (Keyboard::isKeyPressed(Keyboard::Right)) Right = 1;
  66. if (Keyboard::isKeyPressed(Keyboard::Down)) Down = 1;
  67. if (Keyboard::isKeyPressed(Keyboard::Left)) Left = 1;
  68.  
  69. // CAR MOVEMENT
  70. // positive speed
  71. if (Up && speed < maxSpeed)
  72. {
  73. if (speed < 0) speed += dec;
  74. else speed += acc;
  75. }
  76.  
  77. // negative speed
  78. if (Down && speed > -maxSpeed)
  79. {
  80. if (speed > 0) speed -= dec;
  81. else speed -= acc;
  82. }
  83.  
  84. // friction
  85. if (!Up && !Down)
  86. {
  87. if (speed - dec > 0) speed -= dec;
  88. else if (speed + dec < 0) speed += dec;
  89. else speed = 0;
  90. }
  91.  
  92. // angle
  93. if (Right && speed != 0) angle += turnSpeed * speed/maxSpeed;
  94. if (Left && speed != 0) angle -= turnSpeed * speed/maxSpeed;
  95.  
  96. // set new position and orientation
  97. car[0].speed = speed;
  98. car[0].angle = angle;
  99.  
  100. for (int i = 0; i < N; i++) car[i].move();
  101.  
  102. if (car[0].x > 320) offsetX = car[0].x - 320;
  103. if (car[0].y > 240) offsetY = car[0].y - 240;
  104.  
  105. if (x > 320) offsetX = car[0].x - 320;
  106. if (y > 240) offsetY = car[0].y - 240;
  107.  
  108. app.clear(Color::White);
  109. sBackground.setPosition(-offsetX, -offsetY); // follow the car w/ the camera
  110. app.draw(sBackground);
  111.  
  112. Color colors[10] = { Color::Red, Color::Green, Color::Magenta, Color::Blue, Color::White};
  113.  
  114. for (int i = 0; i < N; i++)
  115. {
  116. sCar.setPosition(car[i].x - offsetX, car[i].y - offsetY);
  117. sCar.setRotation(car[i].angle*180/3.141592);
  118. sCar.setColor(colors[i]);
  119. app.draw(sCar);
  120. }
  121.  
  122. app.display();
  123. }
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment