我睡不著。也許是因為我之前喝了三杯咖啡,也許是因為我的思緒飛快運轉。不管怎樣,我發現自己焦躁不安,無法入睡。我沒有與失眠作鬥爭,而是決定編碼。還有什麼比創建一個讓 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 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 safeDistance) { if (!tRex.jumping && !isCrouching) { // Ensure Dino is not crouching or jumping jumpCount++; // Increment jump count simulateKeyPress(32, ' '); // Simulate jump (spacebar) } } else if (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 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中文網其他相關文章!

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

10款趣味橫生的jQuery遊戲插件,讓您的網站更具吸引力,提升用戶粘性!雖然Flash仍然是開發休閒網頁遊戲的最佳軟件,但jQuery也能創造出令人驚喜的效果,雖然無法與純動作Flash遊戲媲美,但在某些情況下,您也能在瀏覽器中獲得意想不到的樂趣。 jQuery井字棋遊戲 遊戲編程的“Hello world”,現在有了jQuery版本。 源碼 jQuery瘋狂填詞遊戲 這是一個填空遊戲,由於不知道單詞的上下文,可能會產生一些古怪的結果。 源碼 jQuery掃雷遊戲

本教程演示瞭如何使用jQuery創建迷人的視差背景效果。 我們將構建一個帶有分層圖像的標題橫幅,從而創造出令人驚嘆的視覺深度。 更新的插件可與JQuery 1.6.4及更高版本一起使用。 下載

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

Matter.js是一個用JavaScript編寫的2D剛體物理引擎。此庫可以幫助您輕鬆地在瀏覽器中模擬2D物理。它提供了許多功能,例如創建剛體並為其分配質量、面積或密度等物理屬性的能力。您還可以模擬不同類型的碰撞和力,例如重力摩擦力。 Matter.js支持所有主流瀏覽器。此外,它也適用於移動設備,因為它可以檢測觸摸並具有響應能力。所有這些功能都使其值得您投入時間學習如何使用該引擎,因為這樣您就可以輕鬆創建基於物理的2D遊戲或模擬。在本教程中,我將介紹此庫的基礎知識,包括其安裝和用法,並提供一

本文演示瞭如何使用jQuery和ajax自動每5秒自動刷新DIV的內容。 該示例從RSS提要中獲取並顯示了最新的博客文章以及最後的刷新時間戳。 加載圖像是選擇


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),