Github Snake

17.11.2020, 16:25:10

17.11.2020 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name        Github Snake
// @namespace   Violentmonkey Scripts
// @match       https://github.com/*
// @grant       none
// @version     1.1
// @author      -
// @description 17.11.2020, 16:25:10
// ==/UserScript==

const lime = "var(--color-calendar-graph-day-L1-bg)"
const silver = "var(--color-calendar-graph-day-bg)"
const darkGreen = "var(--color-calendar-graph-day-L2-bg)"
const appleColor = "var(--color-calendar-graph-day-L4-bg)"

const cells = document.querySelectorAll("rect")
clear()

let snakeLen = 10
let last = 5, pos = 5
let dir = "right"
let lastMove
let tick = 100
let snakeGameStarted = false
let apple

const snake = [8, 15, 22, 23, 24, 31, 38]
const legend = document.querySelectorAll(".legend li")

const one = {
  right: i => cells[i + 7] ? i + 7 : null,
  left: i => cells[i - 7] ? i - 7 : null,
  up: i => i % 7 ? i - 1 : null,
  down: i => (i + 1) % 7 && i != 366 ? i + 1 : null
}

legend[0].onclick = () => tick = 1000
legend[1].onclick = () => tick = 500
legend[2].onclick = () => tick = 300
legend[3].onclick = () => tick = 180
legend[4].onclick = () => tick = 100

function drawSnake() {
  clear()
  snake.forEach(i => cells[i].style.fill = lime)
  cells[snake[snake.length - 1]].style.fill = darkGreen
}

function clear() {
  cells.forEach(rect => rect.style.fill = silver)
}

function moveSnake(dir) {
  const head = one[dir](snake[snake.length - 1])
  if (head !== null) {
    if (head == apple) {
      generateApple()
    } else {
      snake.shift()
    }
    snake.push(head)
  } else if (snake.length > 2) {
    snake.shift()
  }
}

function drawApple() {
  cells[apple].style.fill = appleColor
}

function draw() {
  clear()
  drawSnake()
  drawApple()
}

function generateApple() {
  apple = Math.floor(Math.random() * cells.length)
}

function render(timestamp) {
  if (!lastMove) {
    lastMove = timestamp
  } else {
    const diff = timestamp - lastMove
    if (diff > tick) {
      let shift = Math.floor(diff / tick)
      lastMove = timestamp - diff % tick
      
      while (shift--) {
        moveSnake(dir)
      }
      
      draw()
    }
  }
  
  requestAnimationFrame(render)
}

addEventListener("keydown", e => {
  if (snakeGameStarted && e.key.startsWith("Arrow")) {
    e.preventDefault()
    dir = e.key.slice(5).toLowerCase()
  }
})

generateApple()
draw()

setTimeout(() => {
  requestAnimationFrame(render)
  snakeGameStarted = true
  document.documentElement.style.scrollBehavior = "smooth"
  document.querySelector(".mt-4.position-relative").scrollIntoView()
}, 8000)