Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include "Player.h"
  3. #include <time.h>
  4. #include <math.h>
  5. #include <iostream>
  6. #include <array>
  7. #include <algorithm>
  8.  
  9. void poll_player_controller(std::array<Player, 2> arr)
  10. {
  11. unsigned int counter = 0;
  12.  
  13. for(Player &p : arr)
  14. {
  15. if (sf::Joystick::isConnected(counter))
  16. {
  17. // joystick number 0 is connected
  18. float axis_x = sf::Joystick::getAxisPosition(0, sf::Joystick::X) / 100.0;
  19. float axis_y = sf::Joystick::getAxisPosition(0, sf::Joystick::Y) / 100.0;
  20. axis_x += sf::Joystick::getAxisPosition(0, sf::Joystick::PovX) / 100.0;
  21. axis_y += sf::Joystick::getAxisPosition(0, sf::Joystick::PovY) / 100.0;
  22.  
  23. axis_x += sf::Joystick::isButtonPressed(counter, 19) - sf::Joystick::isButtonPressed(counter, 18);
  24. axis_y += sf::Joystick::isButtonPressed(counter, 17) - sf::Joystick::isButtonPressed(counter, 16);
  25.  
  26.  
  27. std::cout << axis_x << std::endl;
  28. std::cout << axis_y << std::endl;
  29.  
  30. p.direction = atan2(axis_y, axis_x);
  31.  
  32.  
  33. axis_x + axis_y != 0.0 ? p.speed = 0.2 : p.speed = 0.0;
  34.  
  35. }
  36. counter++;
  37. }
  38. }
  39.  
  40. void update_player(Player &p, sf::RenderWindow &w)
  41. {
  42. // Trig functions to move at angle and speed
  43. p.x += cos(p.direction) * p.speed;
  44. p.y += sin(p.direction) * p.speed;
  45.  
  46. unsigned int window_width = w.getSize().x;
  47. unsigned int window_height = w.getSize().y;
  48.  
  49. // Out of bounds wrapping
  50. if(p.x >= window_width){p.x -= window_width;}
  51. if(p.y >= window_height){p.y -= window_height;}
  52. if(p.x < 0){p.x += window_width;}
  53. if(p.y < 0){p.y += window_height;}
  54.  
  55. // Update the circle child object
  56. p.circle.setRadius(p.radius);
  57. p.circle.setPosition(p.x - p.radius, p.y - p.radius);
  58. }
  59.  
  60. void draw_player(Player &p, sf::RenderWindow &window)
  61. {
  62. window.draw(p.circle);
  63. }
  64.  
  65. int main()
  66. {
  67. // Create the main window
  68. sf::RenderWindow main_window(sf::VideoMode(800, 600),
  69. "KingOfCircles98DoubleSpecialEdition");
  70.  
  71. srand(time(NULL));
  72. std::array<Player, 2> game_players;
  73. for (unsigned char i = 0; i < 2; i++)
  74. {
  75. char r = rand() % 256;
  76. char g = rand() % 256;
  77. char b = rand() % 256;
  78.  
  79. unsigned int rand_x = rand() % main_window.getSize().x;
  80. unsigned int rand_y = rand() % main_window.getSize().y;
  81. float temp_direction = rand() * 6.28;
  82. float temp_speed = 0.0; //.1;
  83.  
  84. game_players[i] = Player(i, rand_x, rand_y, 64, temp_speed,
  85. temp_direction);
  86. sf::Color temp_color(r, g, b, 200);
  87. game_players[i].circle.setFillColor(temp_color);
  88. }
  89.  
  90. // Start the game loop
  91. while (main_window.isOpen())
  92. {
  93. // Process events
  94. sf::Event event;
  95.  
  96. while (main_window.pollEvent(event))
  97. {
  98. // Close window : exit
  99. if (event.type == sf::Event::Closed)
  100. main_window.close();
  101. }
  102.  
  103. // Clear screen
  104. main_window.clear();
  105. poll_player_controller(game_players);
  106. for(Player &i : game_players)
  107. {
  108. update_player(i, main_window);
  109. draw_player(i, main_window);
  110. }
  111. // Update the window
  112. main_window.display();
  113. }
  114.  
  115. return EXIT_SUCCESS;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement