首頁 >web前端 >js教程 >創建自動跳躍恐龍腳本的旅程

創建自動跳躍恐龍腳本的旅程

Susan Sarandon
Susan Sarandon原創
2024-11-04 17:42:02728瀏覽

The Journey of Creating an Auto-Jump Dino Script

我睡不著。也許是因為我之前喝了三杯咖啡,也許是因為我的思緒飛快運轉。不管怎樣,我發現自己焦躁不安,無法入睡。我沒有與失眠作鬥爭,而是決定編碼。還有什麼比創建一個讓 Chrome 離線遊戲中的恐龍自行跳躍的腳本更好的方式來花費這些精力呢?

這是一個關於一個想法的小火花如何導致數小時的調整、測試,並最終為霸王龍打造一個功能齊全的自動跳躍系統的故事。

開始:一個簡單的想法

首先,我從最基本的概念開始。我希望只要障礙物在範圍內,恐龍就能自動跳躍。當時的挑戰似乎很簡單。經過一番思考,我寫了一個簡單的腳本:

// Auto jump function
function autoJump() {
  const checkObstacle = setInterval(() => {
    const tRex = Runner.instance_.tRex;

    // Check if an obstacle is near
    const obstacles = Runner.instance_.horizon.obstacles;

    if (obstacles.length > 0) {
      const obstacle = obstacles[0];

      // If the obstacle is close and within jumpable range, make the Dino jump
      if (obstacle.xPos < 70 && obstacle.xPos > 20 && !tRex.jumping) {
        tRex.startJump();
      }
    }
  }, 10); // Check every 10ms
}

// Start auto jump
autoJump();

第一個版本完成了這項工作。恐龍會偵測障礙物,並在接近障礙物時自動跳躍。然而,仍有許多需要改進的地方。它給人的感覺是機器人和有限的——它可以跳躍,但沒有蹲下或考慮遊戲的速度如何影響恐龍的反應。我想要一些更具適應性和動態性的東西。

進化:自適應自動跳躍和蹲伏

我花了接下來的幾個小時完善程式碼,添加了更多功能,例如根據遊戲速度進行自適應跳躍,以及在翼手龍飛過時整合蹲伏機制。結果是一個感覺更符合實際遊戲的腳本。最終版本讓恐龍不僅可以根據固定距離做出反應,還可以根據遊戲本身的速度進行調整。

這是我的想法:

// Create the button to toggle auto-jump
const toggleButton = createToggleButton();
document.body.appendChild(toggleButton);

let autoJumpActive = false; // Auto-jump initially inactive
let autoJumpInterval = null; // Store interval ID
let jumpCount = 0; // Count the number of jumps
let obstacleCount = 0; // Count the number of obstacles encountered
let isCrouching = false; // Track crouch state

// Function to create the toggle button
function createToggleButton() {
  const button = document.createElement('button');
  button.innerText = 'Activate Auto-Jump';
  styleToggleButton(button);
  button.addEventListener('click', toggleAutoJump);
  return button;
}

// Function to style the toggle button
function styleToggleButton(button) {
  button.style.position = 'fixed';
  button.style.top = '10px';
  button.style.left = '10px';
  button.style.padding = '10px';
  button.style.zIndex = '1000';
  button.style.backgroundColor = '#4CAF50';
  button.style.color = '#fff';
  button.style.border = 'none';
  button.style.cursor = 'pointer';
}

// Function to simulate a key press
function simulateKeyPress(keyCode, key) {
  const event = new KeyboardEvent('keydown', {
    keyCode: keyCode,
    code: key,
    key: key,
    bubbles: true,
    cancelable: true,
  });
  document.dispatchEvent(event);
}

// Function to simulate a key release
function simulateKeyRelease(keyCode, key) {
  const event = new KeyboardEvent('keyup', {
    keyCode: keyCode,
    code: key,
    key: key,
    bubbles: true,
    cancelable: true,
  });
  document.dispatchEvent(event);
}

// Function to calculate adaptive distances for jumping and crouching based on speed
function calculateAdaptiveDistance(baseDistance) {
  const speed = Runner.instance_.currentSpeed;
  return baseDistance + speed * 3; // Adjust the multiplier as needed for more precision
}

// Function to start auto-jumping and crouching
function startAutoJump() {
  autoJumpInterval = setInterval(() => {
    const tRex = Runner.instance_.tRex;
    const obstacles = Runner.instance_.horizon.obstacles;
    const speed = Runner.instance_.currentSpeed; // Get the current speed of the game

    if (obstacles.length > 0) {
      const obstacle = obstacles[0];
      const distanceToObstacle = obstacle.xPos - tRex.xPos; // Distance from Dino to the obstacle

      // Dynamically calculate the adaptive jump and crouch distances based on game speed
      const jumpDistance = calculateAdaptiveDistance(100); // Base distance is 100, adjusted by speed
      const crouchDistance = calculateAdaptiveDistance(50); // Base crouch distance is 50
      const safeDistance = 40; // Minimum safe distance to avoid jumping too early

      // Check if the Dino needs to jump or crouch
      if (distanceToObstacle < jumpDistance && distanceToObstacle > safeDistance) {
        if (!tRex.jumping && !isCrouching) { // Ensure Dino is not crouching or jumping
          jumpCount++; // Increment jump count
          simulateKeyPress(32, ' '); // Simulate jump (spacebar)
        }
      } else if (distanceToObstacle <= crouchDistance && distanceToObstacle > safeDistance && !tRex.jumping) {
        // Only crouch if the Dino is not jumping
        simulateKeyPress(40, 'ArrowDown'); // Simulate crouch (down arrow)
        isCrouching = true; // Set crouch state to true
      } else if (obstacle.typeConfig.type === 'PTERODACTYL' && obstacle.yPos < 70) {
        // Crouch if the obstacle is a Pterodactyl flying high
        simulateKeyPress(40, 'ArrowDown'); // Simulate crouch (down arrow)
        isCrouching = true; // Set crouch state to true
      }

      // Release crouch when the obstacle is passed (Dino's xPos is greater than obstacle's xPos)
      if (tRex.xPos > obstacle.xPos && isCrouching) {
        simulateKeyRelease(40, 'ArrowDown'); // Release crouch (down arrow)
        isCrouching = false; // Reset crouch state
      }
    }

    // Update obstacle count
    obstacleCount = Runner.instance_.horizon.obstacles.length;

  }, 50); // Reduced interval time to 50ms for more frequent checks
}

// Function to stop auto-jumping
function stopAutoJump() {
  clearInterval(autoJumpInterval);
  autoJumpActive = false; // Reset auto-jump state
  toggleButton.innerText = 'Activate Auto-Jump';
  toggleButton.style.backgroundColor = '#4CAF50';
}

// Function to toggle auto-jump
function toggleAutoJump() {
  if (autoJumpActive) {
    stopAutoJump();
  } else {
    startAutoJump();
    toggleButton.innerText = 'Deactivate Auto-Jump';
    toggleButton.style.backgroundColor = '#f44336';
  }
  autoJumpActive = !autoJumpActive; // Toggle the state
}

// Detecting game over
const originalGameOver = Runner.prototype.gameOver;
Runner.prototype.gameOver = function() {
  stopAutoJump(); // Stop auto-jumping on game over
  originalGameOver.apply(this, arguments); // Call the original game over function
}

// Detecting when the game restarts
const originalStartGame = Runner.prototype.startGame;
Runner.prototype.startGame = function() {
  originalStartGame.apply(this, arguments);
  if (autoJumpActive) {
    startAutoJump(); // Restart auto-jump on game restart
  }
}

對編碼之旅的思考

從一個不眠之夜到一個功能齊全的自動跳轉腳本的旅程既有趣又充滿挑戰。最初的簡單想法逐漸演變成更為複雜的想法,需要注重細節、適應力和願意嘗試。每次迭代都讓腳本更像是遊戲的一個組成部分,而不是一個簡單的附加元件。

這就是編碼的美妙之處:你從一個想法開始,透過堅持和創造力,你最終會得到超越你最初願景的東西。有時,這一切都始於喝了太多杯咖啡!

以上是創建自動跳躍恐龍腳本的旅程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn