Chrome恐龙游戏AI代理优化:动态调参循环实现需求
Solution for Adapting Chrome Dino AI to Increasing Game Speed with Self-Learning
Let's fix your bot so it can keep up as the game speeds up and even learn from its mistakes. The main issues with your current code are the fixed check interval and static jump parameters—we'll replace those with dynamic adjustments and a basic self-learning loop.
Key Improvements We'll Make
- Dynamic Check Interval: As the game speeds up, obstacles move faster, so we'll shorten the time between bot checks to react quicker.
- Adaptive Jump Parameters: The
a,b,c,d, andposWidthvalues will adjust based on the current game speed and whether the bot's previous jumps were successful. - Basic Self-Learning: We'll track when the bot crashes and tweak parameters to fix the issue (e.g., jump earlier if it's crashing into obstacles that get too close, jump later if it's jumping too soon).
Modified Code with Explanations
document.getElementById("botStatus").addEventListener("change", function() { if (this.checked === true) { // Centralize bot state and adjustable parameters const botState = { params: { a: 0.1, b: 5, c: 35, d: 160, posWidth: 20, baseCheckInterval: 2, // Base interval when game speed is 1x }, lastObstacle: null, lastJumpSuccess: true, // Track if the last jump cleared the obstacle timeoutId: null, gameOverHandler: null, }; // Learn from crashes: adjust parameters when the bot hits an obstacle botState.gameOverHandler = () => { const runner = Runner.instance_; const lastObstacle = botState.lastObstacle; if (lastObstacle && !botState.lastJumpSuccess) { const speed = runner.currentSpeed; const obstacleEdge = lastObstacle.xPos + lastObstacle.width; const triggerThreshold = ((speed - botState.params.a) - botState.params.b) * botState.params.c + botState.params.d; if (obstacleEdge < botState.params.posWidth) { // Crashed because we jumped too late: increase trigger distance botState.params.d += 5; botState.params.c += 1; } else if (obstacleEdge > triggerThreshold + 20) { // Crashed because we jumped too early: decrease trigger distance botState.params.d -= 3; botState.params.c -= 0.5; } // Keep parameters within reasonable bounds to avoid extreme values botState.params.d = Math.max(120, Math.min(200, botState.params.d)); botState.params.c = Math.max(30, Math.min(40, botState.params.c)); botState.params.a = Math.max(0.05, Math.min(0.2, botState.params.a)); } botState.lastJumpSuccess = true; }; // Attach game over listener to learn from mistakes Runner.instance_.on('gameOver', botState.gameOverHandler); // Core bot logic: runs recursively with dynamic intervals const runBot = () => { const runner = Runner.instance_; const tRex = runner.tRex; const obstacles = runner.horizon.obstacles; if (!tRex.jumping && obstacles.length > 0) { const currentObstacle = obstacles[0]; const obstacleEdge = currentObstacle.xPos + currentObstacle.width; const speed = runner.currentSpeed; // Calculate dynamic jump trigger threshold based on current speed const triggerThreshold = ((speed - botState.params.a) - botState.params.b) * botState.params.c + botState.params.d; if (obstacleEdge <= triggerThreshold && obstacleEdge > botState.params.posWidth) { tRex.startJump(); botState.lastObstacle = currentObstacle; botState.lastJumpSuccess = false; // Mark jump as pending verification } else if (obstacles[0].xPos > runner.canvas.width) { // Last obstacle passed successfully botState.lastJumpSuccess = true; } } // Adjust check interval: faster game = shorter interval (min 1ms to avoid performance hits) const nextInterval = Math.max(1, botState.params.baseCheckInterval / runner.currentSpeed); botState.timeoutId = setTimeout(runBot, nextInterval); }; // Start the bot runBot(); window.tRexBot = botState; } else { // Clean up bot resources when disabled if (window.tRexBot) { clearTimeout(window.tRexBot.timeoutId); Runner.instance_.off('gameOver', window.tRexBot.gameOverHandler); delete window.tRexBot; } } });
How It Works
- Dynamic Interval: Instead of a fixed
INTERVAL, we calculatenextIntervalbased on the current game speed. Faster speeds mean shorter intervals, so the bot checks for obstacles more frequently. - Adaptive Jump Threshold: The trigger threshold for jumping uses the current game speed directly (no
parseIntrounding) to make the jump timing more accurate as speed increases. - Self-Learning: When the bot crashes, we analyze whether it jumped too early or too late, then tweak the
dandcparameters to adjust the jump trigger distance. We also clamp parameters to prevent them from becoming extreme. - State Tracking: We track the last obstacle and whether the jump cleared it, so we only adjust parameters when a crash is caused by a bad jump.
Extra Optimization Tips
- Obstacle Type Awareness: You can extend the logic to check obstacle height (e.g., birds) and adjust jump height with
tRex.setJumpVelocity()to avoid unnecessary high jumps. - Smoother Parameter Adjustments: Use a learning rate (e.g., multiply adjustments by 0.8 each time) to make parameter changes more gradual and stable over time.
- Speed-Based Initial Params: Set initial
canddbased on the starting speed (e.g.,c = 30 + runner.currentSpeed * 0.3) to get a better starting point.
内容的提问来源于stack exchange,提问作者Kye Atkinson
相关产品推荐
相关产品推荐

