AllenYuan

Untitled

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