Advertisement
Guest User

Corsair CUE Game - t3hlazy1

a guest
Nov 25th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.37 KB | None | 0 0
  1. int main()
  2. {
  3.     CorsairPerformProtocolHandshake();
  4.     if (const auto error = CorsairGetLastError()) {
  5.         std::cout << "Handshake failed: " << toString(error) << std::endl;
  6.         getchar();
  7.         return -1;
  8.     }
  9.     MEMORYSTATUSEX memInfo;
  10.    
  11.  
  12.     const auto ledPositions = CorsairGetLedPositions();
  13.     if (ledPositions && ledPositions->numberOfLed > 0) {
  14.  
  15.         std::cout << "Working... Press Escape to close program...";
  16.  
  17.         // Variables
  18.         srand(time(NULL));                            // Initialize Random
  19.         int numEnemies = 0;                           // Number of enemies
  20.         int heights[6] = { 30, 50, 80, 95, 120, 135}; // Possible heights for enemies
  21.         int enemies[100][3];                          // [EnemyNumber][] = {Top, Left, Speed]
  22.         int charPos = 2;                              // 0 - 4
  23.         int collision = 0;                            // Game status (0 = good; 1 = just ended; > 1 = game over)
  24.         int tick = 0;                                 // Time in the game
  25.         CorsairLedId charId1, charId2;                // Character positions
  26.  
  27.         // Loop until Escape is pressed
  28.         for (auto n = 0; !GetAsyncKeyState(VK_ESCAPE); n++) {
  29.  
  30.             std::vector<CorsairLedColor> vec; // LedColors to push to keyboard
  31.  
  32.             // If not colliding
  33.             // -> Main Gameplay
  34.             if (collision == 0) {
  35.  
  36.                 // Set Character Positions
  37.                 switch (charPos) {
  38.                 case 0:
  39.                     charId1 = CLK_G3;
  40.                     charId2 = CLK_G6;
  41.                     break;
  42.                 case 1:
  43.                     charId1 = CLK_G6;
  44.                     charId2 = CLK_G9;
  45.                     break;
  46.                 case 2:
  47.                     charId1 = CLK_G9;
  48.                     charId2 = CLK_G12;
  49.                     break;
  50.                 case 3:
  51.                     charId1 = CLK_G12;
  52.                     charId2 = CLK_G15;
  53.                     break;
  54.                 case 4:
  55.                     charId1 = CLK_G15;
  56.                     charId2 = CLK_G18;
  57.                 }
  58.  
  59.                 // 10% chance to spawn an enemy per tick
  60.                 if (rand() % 100 + 1 > 90) {
  61.  
  62.                     enemies[numEnemies][0] = heights[rand() % 6]; // Top
  63.                     enemies[numEnemies][1] = 500;                 // Left
  64.                     enemies[numEnemies][2] = rand() % 6 + 3;      // Speed
  65.                     numEnemies++;                                 // Increase enemy count
  66.  
  67.                 }
  68.  
  69.                 // Loop through all LEDs on keyboard
  70.                 for (auto i = 0; i < ledPositions->numberOfLed; i++) {
  71.  
  72.                     // Set up ledColor variable
  73.                     const auto ledPos = ledPositions->pLedPosition[i];
  74.                     auto ledColor = CorsairLedColor();
  75.                     ledColor.ledId = ledPos.ledId;
  76.  
  77.                     // Loop through all enemies
  78.                     for (auto j = 0; j < numEnemies && ledColor.ledId != CLK_MR; j++) {
  79.  
  80.                         // If LED coordinates contain this enemy
  81.                         if ((float)ledPos.left <= (float)enemies[j][1] && (float)ledPos.left + (float)ledPos.width >= (float)enemies[j][1] && (float)ledPos.top <= (float)enemies[j][0] && (float)ledPos.top + (float)ledPos.height >= (float)enemies[j][0]) {
  82.  
  83.                             // Check for enemy-character collision
  84.                             if (ledColor.ledId == charId1 || ledColor.ledId == charId2) {
  85.                                 //std::cout << "COLLISION\n\n";
  86.                                 collision = 1;
  87.                             }
  88.  
  89.                             // Set color to red
  90.                             ledColor.r = 255;
  91.                             ledColor.g = 0;
  92.                             ledColor.b = 0;
  93.                         }
  94.                         // Else set color to grey
  95.                         else {
  96.                             ledColor.r = 40;
  97.                             ledColor.g = 40;
  98.                             ledColor.b = 40;
  99.                         }
  100.  
  101.                     }
  102.  
  103.                     // Push color to list if not on character or MR key
  104.                     if (ledColor.ledId != charId1 && ledColor.ledId != charId2 && ledColor.ledId != CLK_MR) {
  105.                         vec.push_back(ledColor);
  106.                     }
  107.  
  108.                 }
  109.  
  110.  
  111.                 // Move characer up
  112.                 if (GetAsyncKeyState(VK_UP) & 0x8000) {
  113.                     charPos--;
  114.                     if (charPos < 0)
  115.                         charPos = 0;
  116.                 }
  117.  
  118.                 // Move character down
  119.                 if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
  120.                     charPos++;
  121.                     if (charPos > 4)
  122.                         charPos = 4;
  123.                 }
  124.  
  125.                 // Set character position's LED's colors
  126.                 auto ledColor = CorsairLedColor();
  127.                 auto ledColor2 = CorsairLedColor();
  128.  
  129.                 ledColor.r = 0;
  130.                 ledColor.b = 0;
  131.                 ledColor.g = 255;
  132.  
  133.                 ledColor2.r = 0;
  134.                 ledColor2.b = 0;
  135.                 ledColor2.g = 255;
  136.  
  137.                 ledColor.ledId = charId1;
  138.                 ledColor2.ledId = charId2;
  139.  
  140.                 // Push to list if not colliding
  141.                 if (collision == 0) {
  142.                     vec.push_back(ledColor);
  143.                     vec.push_back(ledColor2);
  144.                 }
  145.  
  146.                 // Set game score on MR Key
  147.                 ledColor.ledId = CLK_MR;
  148.                 ledColor.g = (int)((float)tick / (float)2000 * (float)255); // = ((tick/2000)*255)
  149.                 if (ledColor.g > 255)
  150.                     ledColor.g = 255;
  151.                 vec.push_back(ledColor); // Push to list
  152.  
  153.                 //std::cout << "SIZE: " << vec.size() << "\n";
  154.  
  155.                 // Push list to keyboard
  156.                 CorsairSetLedsColors(vec.size(), vec.data());
  157.  
  158.                 // Update enemies
  159.                 for (auto j = 0; j < numEnemies; j++) {
  160.                     //std::cout << "Update Enemy: " << j << " " << enemies[j][1] << " " << enemies[j][0] << "\n";
  161.                    
  162.                     // Update speed
  163.                     enemies[j][1] = enemies[j][1] - enemies[j][2];
  164.  
  165.                     // ENEMY DELETION/REPLACEMENT
  166.                     // If enemy leaves left side of keyboard
  167.                     // -> Replace with last enemy
  168.                     if (enemies[j][1] < 1) {
  169.                         // std::cout << "Deleted enemy\n";
  170.  
  171.                         // If enemy is not last enemy
  172.                         // -> Replace with last enemy
  173.                         if (j < numEnemies - 1) {
  174.                             enemies[j][0] = enemies[numEnemies - 1][0];
  175.                             enemies[j][1] = enemies[numEnemies - 1][1];
  176.                             enemies[j][2] = enemies[numEnemies - 1][2];
  177.                         }
  178.  
  179.                         // Decrement number of enemies
  180.                         numEnemies--;
  181.  
  182.                     }
  183.  
  184.                 }
  185.                
  186.                 tick++; // Increase tick
  187.             }
  188.             // If collision occured last tick
  189.             else if (collision == 1) {
  190.  
  191.                 // Clear list of LEDs
  192.                 // Note: This appears to be useless now, but could be useful in future
  193.                 //   if doing on same tick as collision is detected
  194.                 vec.empty();
  195.                 vec.clear();
  196.  
  197.                 // Loop through all LEDs
  198.                 for (auto i = 0; i < ledPositions->numberOfLed; i++) {
  199.  
  200.                     // Set all LEDs to Red
  201.                     const auto ledPos = ledPositions->pLedPosition[i];
  202.                     auto ledColor = CorsairLedColor();
  203.                     ledColor.ledId = ledPos.ledId;
  204.                     ledColor.r = 255;
  205.                     ledColor.g = 0;
  206.                     ledColor.b = 0;
  207.  
  208.                     vec.push_back(ledColor); // Push to list
  209.                 }
  210.  
  211.                 // Change game state
  212.                 collision = 2;
  213.  
  214.                 // Push to keyboard
  215.                 CorsairSetLedsColors(vec.size(), vec.data());
  216.  
  217.             }
  218.             // Reset game after period of time
  219.             else if (collision > 15) {
  220.                
  221.                 numEnemies = 0;
  222.                 charPos = 2;
  223.                 collision = 0;
  224.                 tick = 0;
  225.  
  226.             }
  227.             // Continue with game over
  228.             else {
  229.  
  230.                 collision++;
  231.  
  232.             }
  233.            
  234.             // Sleep for 75ms
  235.             std::this_thread::sleep_for(std::chrono::milliseconds(75));
  236.  
  237.         }
  238.        
  239.     }
  240.  
  241.     // End program
  242.     return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement