GithubHelp home page GithubHelp logo

corentinth / mini-snake Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 13 KB

A 402 bytes long snake game in pure HTML/JS code golf

Home Page: https://mini-snake.corentin.tech

License: MIT License

HTML 100.00%
bytes code-golf game javascript short snake snake-game

mini-snake's Introduction

Snake, in the browser, in only 402 bytes

Live demo available here! (You may need to disable your adb locker because of csp and inline scripts)

This is a simple snake game in code golf, written in pure HTML and JavaScript. It's only 404 bytes long, and it's playable in the browser.

  • Move the snake with the arrow keys
  • Eat the apples to grow
  • Don't hit yourself -> it reset the length
  • The snake can go through the walls
  • Use any other key to "pause" the game

Code

Available in the index.html file.

<canvas id=c><script>d=c.getContext`2d`,x=a=b=k=[0,1],t=[],l=5,w=20,s=c.width=c.height=w*w,R=t=>Math.random()*w|0,E=t=>t+t==x+x,F=t=>d.fillStyle=t,S=t=>d.fillRect(t[0]*w,t[1]*w,w,w),onkeydown=t=>k=[[-1,0],[0,-1],[1,0],b][t.which-37],setInterval(e=>{x=b.map(t=>(x[t]+k[t]+w)%w),F`tan`,d.fillRect(0,0,s,s),F`red`,t.map(S),t.some(E)&&(l=5),t.push(x),t=t.slice(-l),E(a)&&(a=b.map(R),l++),S(a)},90)</script>

Readable version

Auto formatted + added comments

<canvas id="c">
<script>
  // Get the canvas element and its context
  d = c.getContext`2d`;

  // x the position of the head
  // a the position of the apple
  // b an useful array, used for directions and iterations
  // k the direction
  x = a = b = k = [0, 1];

  // t the tail
  t = [];

  // l the length of the tail
  l = 5;

  // w the width of a tile
  w = 20;

  // s the size of the canvas
  s = c.width = c.height = w * w;

  // Get a random number, cannot use new Date&w because of setInterval, it's recomputed at the same time
  R = _ => Math.random() * w | 0;
  
  // E the equality function, check if the provided coordinates are the same as the head (using array stringification)
  E = a => a + a == x + x;

  // F the fillStyle function to set the colors
  F = c => d.fillStyle = c;

  // S the fillRect function to draw a tile
  S = e => d.fillRect(e[0] * w, e[1] * w, w, w);

  // Set the direction on keydown
  onkeydown = e => k = [[-1, 0], [0, -1], [1, 0], b][e.which - 37];

  setInterval(_ => {
    // Move the head and check if it's out of bounds
    x = b.map(i => (x[i] + k[i] + w) % w);

    // Set the background color
    F`tan`; // Using tan because it's shorter than short hexadecimal colors (like #000)

    // Clear the canvas
    d.fillRect(0, 0, s, s);

    // Set the snake color
    F`red`; // Using red because it's shorter than short hexadecimal colors (like #f00)

    // Draw the tail
    t.map(S);

    // if the head is on the tail, reset the length
    t.some(E) && (l = 5);

    // Move the tail by adding the head to it and removing the last element
    t.push(x);
    t = t.slice(-l);

    // If the head is on the apple, set a new apple and increase the length
    E(a) && (a = b.map(R), l++);

    // Draw the apple
    S(a);
  }, 90);
</script>

But, why?

🤷 I don't know, I kinda like code golf.

Contributing

Feel free to open an issue or a pull request if you have any idea to improve this project.

Local setup

You can use pnpm dev to start a local vite server with HMR to ease the development.

Credits

Coded with ❤️ by Corentin Thomasset.

If you want more, you can try jugly.io a javascript code golfing platform I made.

License

This project is under the MIT license.

mini-snake's People

Contributors

corentinth avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.