AllenYuan

Untitled

May 30th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <math.h>
  3. using namespace sf;
  4.  
  5. // checkpoints
  6. const int num = 8;
  7. int points[num][2] = {
  8. 300, 610,
  9. 1270, 430,
  10. 1380, 2380,
  11. 1900, 2460,
  12. 1970, 1700,
  13. 2550, 1680,
  14. 2560, 3150,
  15. 500, 3300};
  16.  
  17. // car encoded by position, speed, angle w/ a move method
  18. struct Car
  19. {
  20. float x, y, speed, angle;
  21. int n;
  22.  
  23. Car()
  24. {
  25. speed = 2;
  26. angle = 0;
  27. n = 0; // first waypoint for ai cars
  28. }
  29.  
  30. void move()
  31. {
  32. x += sin(angle) * speed;
  33. y -= cos(angle) * speed;
  34.  
  35. // target point
  36. float tx = points[n][0];
  37. float ty = points[n][1];
  38.  
  39. // point the car towards the target
  40. float beta = angle - atan2(tx - x, -ty + y); // difference between current angle and target angle
  41. if (sin(beta) < 0)
  42. angle += 0.08;
  43. else
  44. angle -= 0.08; // move angle towards target angle
  45.  
  46. // if a car gets within 25 pixels of a way point, point it to the next waypoint.
  47. if ((x - tx) * (x - tx) + (y - ty) * (y - ty) < 25 * 25)
  48. n = (n + 1) % num;
  49. }
  50. };
  51.  
  52. int main()
  53. {
  54. RenderWindow app(VideoMode(640, 480), "Car Racing Game!");
  55. app.setFramerateLimit(60);
  56.  
  57. Texture t1, t2;
  58. t1.loadFromFile("/Users/alleyuan/code-snippets/16_games/07 Top Down Racing/images/background.png");
  59. t2.loadFromFile("/Users/alleyuan/code-snippets/16_games/07 Top Down Racing/images/car.png");
  60.  
  61. Sprite sBackground(t1), sCar(t2);
  62. sBackground.scale(2, 2);
  63. sCar.setPosition(300, 300);
  64. sCar.setOrigin(22, 22);
  65.  
  66. // set ai cars and starting positions
  67. const int N = 5;
  68. Car car[N];
  69. for (int i = 0; i < N; i++)
  70. {
  71. car[i].x = 300 + i * 50;
  72. car[i].y = 1700 + i * 80;
  73. car[i].speed = 7 + i;
  74. }
  75.  
  76. float x = 300, y = 300;
  77. float speed = 0, angle = 0; // initial speed/angle (angle=direciton from car center)
  78. float maxSpeed = 12.0;
  79. float acc = 0.2, dec = 0.3; // acceleration.deceleration
  80. float turnSpeed = 0.08; // angular velocity when turning
  81.  
  82. int offsetX = 0, offsetY = 0;
  83.  
  84. while (app.isOpen())
  85. {
  86. Event e;
  87. while (app.pollEvent(e))
  88. {
  89. if (e.type == Event::Closed)
  90. app.close();
  91. }
  92.  
  93. // set directions
  94. bool Up = 0, Right = 0, Down = 0, Left = 0;
  95. if (Keyboard::isKeyPressed(Keyboard::Up))
  96. Up = 1;
  97. if (Keyboard::isKeyPressed(Keyboard::Right))
  98. Right = 1;
  99. if (Keyboard::isKeyPressed(Keyboard::Down))
  100. Down = 1;
  101. if (Keyboard::isKeyPressed(Keyboard::Left))
  102. Left = 1;
  103.  
  104. // CAR MOVEMENT
  105. // positive speed
  106. if (Up && speed < maxSpeed)
  107. {
  108. if (speed < 0)
  109. speed += dec;
  110. else
  111. speed += acc;
  112. }
  113.  
  114. // negative speed
  115. if (Down && speed > -maxSpeed)
  116. {
  117. if (speed > 0)
  118. speed -= dec;
  119. else
  120. speed -= acc;
  121. }
  122.  
  123. // friction
  124. if (!Up && !Down)
  125. {
  126. if (speed - dec > 0)
  127. speed -= dec;
  128. else if (speed + dec < 0)
  129. speed += dec;
  130. else
  131. speed = 0;
  132. }
  133.  
  134. // angle
  135. if (Right && speed != 0)
  136. angle += turnSpeed * speed / maxSpeed;
  137. if (Left && speed != 0)
  138. angle -= turnSpeed * speed / maxSpeed;
  139.  
  140. // set new position and orientation
  141. car[0].speed = speed;
  142. car[0].angle = angle;
  143.  
  144. // move all cars
  145. for (int i = 0; i < N; i++)
  146. car[i].move();
  147.  
  148. float R = 22; // car radius
  149.  
  150. // collision
  151. for (int i = 0; i < N; i++)
  152. for (int j = 0; j < N; j++)
  153. {
  154. int dx = car[i].x - car[j].x;
  155. int dy = car[i].y - car[j].y;
  156. if (dx * dx + dy * dy < 4 * R * R) // upon collision, nudge each car away from the collision
  157. {
  158. car[i].x += dx / 10;
  159. car[i].y += dy / 10;
  160. car[j].x -= dx / 10;
  161. car[j].y -= dy / 10;
  162. }
  163. }
  164.  
  165. // camera offset
  166. if (car[0].x > 320)
  167. offsetX = car[0].x - 320;
  168. if (car[0].y > 240)
  169. offsetY = car[0].y - 240;
  170.  
  171. if (x > 320)
  172. offsetX = car[0].x - 320;
  173. if (y > 240)
  174. offsetY = car[0].y - 240;
  175.  
  176. app.clear(Color::White);
  177. sBackground.setPosition(-offsetX, -offsetY); // follow the car w/ the camera
  178. app.draw(sBackground);
  179.  
  180. Color colors[10] = {Color::Red, Color::Green, Color::Magenta, Color::Blue, Color::White};
  181.  
  182. // re-render cars
  183. for (int i = 0; i < N; i++)
  184. {
  185. sCar.setPosition(car[i].x - offsetX, car[i].y - offsetY);
  186. sCar.setRotation(car[i].angle * 180 / 3.141592);
  187. sCar.setColor(colors[i]);
  188. app.draw(sCar);
  189. }
  190.  
  191. app.display();
  192. }
  193.  
  194. return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment