Skip to content

Instantly share code, notes, and snippets.

@Dev-Dipesh
Last active April 26, 2024 16:52
Show Gist options
  • Save Dev-Dipesh/d992f796a8796a0d0a7e99f5c6efc670 to your computer and use it in GitHub Desktop.
Save Dev-Dipesh/d992f796a8796a0d0a7e99f5c6efc670 to your computer and use it in GitHub Desktop.
CleanShot 2024-04-26 at 21 05 31@2x

Hack for the Game

Remember to change the query selector before running the script in your browser.

Laymen Explanation

  • Game Character = sleeping emoji
  • Obstacle = Notification emoji

Here's what each part of the code does:

Configuration:

  1. It knows that the sleeping emoji is always 50 pixels from the left side of the screen.
  2. It is set to make the sleeping emoji jump when the obstacle is 70 pixels away from it — this is like seeing something coming and deciding when to jump to avoid it.
  3. It checks the position of the obstacle every 100 milliseconds, which is pretty quick (ten times a second).

Starting the Game Automation:

  1. It starts its watch, constantly checking for obstacles.
  2. If it sees an obstacle getting too close (less than 70 pixels away), it tells the sleeping emoji to jump.
  3. It keeps watching and jumping as needed, repeating this check every 100 milliseconds.

Jumping:

  1. When it's time to jump, the code sends a signal as if the "up arrow" key on your keyboard was pressed, which is the command for the emoji to jump.

Stopping and Starting:

  1. The code will keep playing the game non-stop, but if you want to take a break and stop it, you press the "Escape" key on your keyboard.
  2. Pressing the "Escape" key again will make the robot start playing the game again.
// Configuration
const sleepingEmojiLeft = 50; // The fixed position from the left for the sleeping emoji
const jumpThreshold = 70; // Distance at which to jump to avoid collision
const checkInterval = 100; // How often to check the position (in milliseconds)
let intervalId = null; // To store the interval ID
let automationActive = true; // Flag to control the automation
function startGameAutomation() {
// This function will be called periodically to check the position
const checkForCollision = () => {
if (!automationActive) {
return;
}
const notificationEmoji = document.querySelector(
"div.css-yz4fex img:nth-of-type(4)"
);
const notificationLeft = parseInt(
window.getComputedStyle(notificationEmoji).left,
10
);
// When the notification is within the threshold, simulate the up arrow key press
if (notificationLeft - sleepingEmojiLeft < jumpThreshold) {
simulateUpArrowKeyPress();
}
};
// Start the interval
intervalId = setInterval(checkForCollision, checkInterval);
}
function stopGameAutomation() {
if (intervalId) {
clearInterval(intervalId);
}
automationActive = false; // Set the flag to false to stop automation
}
function simulateUpArrowKeyPress() {
const event = new KeyboardEvent("keydown", {
key: "ArrowUp",
code: "ArrowUp",
keyCode: 38,
which: 38,
bubbles: true,
cancelable: true,
});
document.dispatchEvent(event);
}
// Toggle the automation on and off with a specific key press, e.g., 'Escape'
document.addEventListener("keydown", function (event) {
if (event.key === "Escape") {
if (automationActive) {
stopGameAutomation();
console.log("Automation stopped.");
} else {
startGameAutomation();
console.log("Automation started.");
}
}
});
// Call this function to start the automation
startGameAutomation();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment