Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- CorsairPerformProtocolHandshake();
- if (const auto error = CorsairGetLastError()) {
- std::cout << "Handshake failed: " << toString(error) << std::endl;
- getchar();
- return -1;
- }
- MEMORYSTATUSEX memInfo;
- const auto ledPositions = CorsairGetLedPositions();
- if (ledPositions && ledPositions->numberOfLed > 0) {
- std::cout << "Working... Press Escape to close program...";
- // Variables
- srand(time(NULL)); // Initialize Random
- int numEnemies = 0; // Number of enemies
- int heights[6] = { 30, 50, 80, 95, 120, 135}; // Possible heights for enemies
- int enemies[100][3]; // [EnemyNumber][] = {Top, Left, Speed]
- int charPos = 2; // 0 - 4
- int collision = 0; // Game status (0 = good; 1 = just ended; > 1 = game over)
- int tick = 0; // Time in the game
- CorsairLedId charId1, charId2; // Character positions
- // Loop until Escape is pressed
- for (auto n = 0; !GetAsyncKeyState(VK_ESCAPE); n++) {
- std::vector<CorsairLedColor> vec; // LedColors to push to keyboard
- // If not colliding
- // -> Main Gameplay
- if (collision == 0) {
- // Set Character Positions
- switch (charPos) {
- case 0:
- charId1 = CLK_G3;
- charId2 = CLK_G6;
- break;
- case 1:
- charId1 = CLK_G6;
- charId2 = CLK_G9;
- break;
- case 2:
- charId1 = CLK_G9;
- charId2 = CLK_G12;
- break;
- case 3:
- charId1 = CLK_G12;
- charId2 = CLK_G15;
- break;
- case 4:
- charId1 = CLK_G15;
- charId2 = CLK_G18;
- }
- // 10% chance to spawn an enemy per tick
- if (rand() % 100 + 1 > 90) {
- enemies[numEnemies][0] = heights[rand() % 6]; // Top
- enemies[numEnemies][1] = 500; // Left
- enemies[numEnemies][2] = rand() % 6 + 3; // Speed
- numEnemies++; // Increase enemy count
- }
- // Loop through all LEDs on keyboard
- for (auto i = 0; i < ledPositions->numberOfLed; i++) {
- // Set up ledColor variable
- const auto ledPos = ledPositions->pLedPosition[i];
- auto ledColor = CorsairLedColor();
- ledColor.ledId = ledPos.ledId;
- // Loop through all enemies
- for (auto j = 0; j < numEnemies && ledColor.ledId != CLK_MR; j++) {
- // If LED coordinates contain this enemy
- 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]) {
- // Check for enemy-character collision
- if (ledColor.ledId == charId1 || ledColor.ledId == charId2) {
- //std::cout << "COLLISION\n\n";
- collision = 1;
- }
- // Set color to red
- ledColor.r = 255;
- ledColor.g = 0;
- ledColor.b = 0;
- }
- // Else set color to grey
- else {
- ledColor.r = 40;
- ledColor.g = 40;
- ledColor.b = 40;
- }
- }
- // Push color to list if not on character or MR key
- if (ledColor.ledId != charId1 && ledColor.ledId != charId2 && ledColor.ledId != CLK_MR) {
- vec.push_back(ledColor);
- }
- }
- // Move characer up
- if (GetAsyncKeyState(VK_UP) & 0x8000) {
- charPos--;
- if (charPos < 0)
- charPos = 0;
- }
- // Move character down
- if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
- charPos++;
- if (charPos > 4)
- charPos = 4;
- }
- // Set character position's LED's colors
- auto ledColor = CorsairLedColor();
- auto ledColor2 = CorsairLedColor();
- ledColor.r = 0;
- ledColor.b = 0;
- ledColor.g = 255;
- ledColor2.r = 0;
- ledColor2.b = 0;
- ledColor2.g = 255;
- ledColor.ledId = charId1;
- ledColor2.ledId = charId2;
- // Push to list if not colliding
- if (collision == 0) {
- vec.push_back(ledColor);
- vec.push_back(ledColor2);
- }
- // Set game score on MR Key
- ledColor.ledId = CLK_MR;
- ledColor.g = (int)((float)tick / (float)2000 * (float)255); // = ((tick/2000)*255)
- if (ledColor.g > 255)
- ledColor.g = 255;
- vec.push_back(ledColor); // Push to list
- //std::cout << "SIZE: " << vec.size() << "\n";
- // Push list to keyboard
- CorsairSetLedsColors(vec.size(), vec.data());
- // Update enemies
- for (auto j = 0; j < numEnemies; j++) {
- //std::cout << "Update Enemy: " << j << " " << enemies[j][1] << " " << enemies[j][0] << "\n";
- // Update speed
- enemies[j][1] = enemies[j][1] - enemies[j][2];
- // ENEMY DELETION/REPLACEMENT
- // If enemy leaves left side of keyboard
- // -> Replace with last enemy
- if (enemies[j][1] < 1) {
- // std::cout << "Deleted enemy\n";
- // If enemy is not last enemy
- // -> Replace with last enemy
- if (j < numEnemies - 1) {
- enemies[j][0] = enemies[numEnemies - 1][0];
- enemies[j][1] = enemies[numEnemies - 1][1];
- enemies[j][2] = enemies[numEnemies - 1][2];
- }
- // Decrement number of enemies
- numEnemies--;
- }
- }
- tick++; // Increase tick
- }
- // If collision occured last tick
- else if (collision == 1) {
- // Clear list of LEDs
- // Note: This appears to be useless now, but could be useful in future
- // if doing on same tick as collision is detected
- vec.empty();
- vec.clear();
- // Loop through all LEDs
- for (auto i = 0; i < ledPositions->numberOfLed; i++) {
- // Set all LEDs to Red
- const auto ledPos = ledPositions->pLedPosition[i];
- auto ledColor = CorsairLedColor();
- ledColor.ledId = ledPos.ledId;
- ledColor.r = 255;
- ledColor.g = 0;
- ledColor.b = 0;
- vec.push_back(ledColor); // Push to list
- }
- // Change game state
- collision = 2;
- // Push to keyboard
- CorsairSetLedsColors(vec.size(), vec.data());
- }
- // Reset game after period of time
- else if (collision > 15) {
- numEnemies = 0;
- charPos = 2;
- collision = 0;
- tick = 0;
- }
- // Continue with game over
- else {
- collision++;
- }
- // Sleep for 75ms
- std::this_thread::sleep_for(std::chrono::milliseconds(75));
- }
- }
- // End program
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement