Chrome浏览器在没有联网的时候,可以玩一个恐龙跳障碍的小游戏,可能很多人都玩过(地址:chrome://dino/),昨天听一个朋友说起可以用js给这个游戏写一个外挂,在听完他的思路之后,我就跃跃欲试了,今天总算是给搞出来。
function config() {
let canvas = document.querySelector('.runner-canvas')
let context = canvas.getContext('2d')
let checkPoint = {
x: canvas.width * 0.2,
y: canvas.height * 0.75,
}
return {
canvas,
context,
checkPoint,
}
}
function pixelData(checkPoint) {
let data = config().context.getImageData(checkPoint.x, checkPoint.y, 1, 1)
return {
r: data.data[0],
g: data.data[1],
b: data.data[2],
}
}
function isObstruction(pixelData) {
let {r, g, b} = pixelData
if (r === 83 && g === 83 && b === 83) {
return true
} else {
return false
}
}
function isObstructionComing() {
let point = config().checkPoint
let data = pixelData(point)
let check = isObstruction(data)
return check
}
function keyEvent(type, keyCode) {
let event = document.createEvent('Events')
event.initEvent(type, true, true)
event.keyCode = keyCode
event.which = keyCode
document.dispatchEvent(event)
}
function jump() {
keyEvent('keydown', 32)
}
function check() {
if (isObstructionComing()) {
jump()
}
}
function __main() {
setInterval(check, 1)
}
__main()