AllenYuan

Untitled

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