AllenYuan

Untitled

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