Snake semplice gioco in Javascript
Snake semplice gioco in Javascript (scroll down for english)
Ho provato a sperimentare con diversi framework e game engine in vari linguaggi (qui un esempio in Python) e volevo quindi provare a creare un semplice gioco in javascript puro.
A parte la libreria howler.js per evitare problemi di accesso cors non ho usato altro.
Il bello di Javascript è proprio che non servono compilatori e quindi basta il browser per eseguire il proprio codice.
Ho usato questo ottimo algoritmo di Marco Palladino per la gestione del serpente: https://www.marcopalladino.it/it/snake-in-javascript/
Il meccanismo aggiunge le nuove coordinate della testa ad un array di variabili, rimuove l’ultimo elemento e sposta tutti i valori(shift) per aggiornare la posizione sullo schermo.
Ho aggiunto dei commenti, un pò di grafica, suoni, opzioni di debug e logiche di gameover.
Ho inoltre abbozzato un sistema di ridimensionamento dinamico e dei controlli on-screen in modo che sia abbastanza fruibile anche da dispositivi mobile.
L’idea in realtà è quella di avere una sorta di ‘template’ per sperimentare altri piccoli giochi.
Ho cercato di distribuire il codice su più file .js per avere un codice più leggibile almeno per il ciclo di gioco principale.
Non essendo un grafico ho preso immagini e suoni dall’ottimo sito: https://opengameart.org
Trovate qui la versione online:
https://www.robertozanardo.com/snake/snake.html
E qui il codice completo su github:
https://github.com/Rohzen/snakeinJS
Buon divertimento!
Notes on first release:
——————————–
– Comments on code source
– Option for game pause by pressing spacebar
– Add some graphics
– Added score management
– Added css styling
– Dynamic resizing (beta)
– Sounds and muzak
– Game over management
– Added basic support for mobile devices
– Added some more game logic:stars(50pt) and mushrooms(Gameover)
Controls
——–
Arrows for Left Right Up Down
Spacebar for Pause
M to mute the score
D for debug data and gameover bypass
Snake simple game in Javascript
I’ve tried experimenting with different kinds of frameworks and game engines in various languages and therefore wanted to try to create a simple game in pure javascript.
Except from the howler.js library to avoid cors access problems I have not used anything else.
The beauty of Javascript is that you don’t need compilers and therefore you just need a browser to run your code.
I used as a basis for this excellent algorithm by Marco Palladino (https://www.marcopalladino.it/it/snake-in-javascript/) for the management of the snake.
The mechanism adds the new head coordinates to an array of variables, removes the last element and shifts all values to update the position on the screen.
I’ve added some comments, some graphics, sounds, debug options and gameover logic.
I also sketched out a dynamic resizing system and on-screen controls so that it can be used on mobile devices.
The idea is actually to have a kind of ‘template’ to experiment with other small games.
I have tried to distribute the code across multiple .js files to have more readable code at least for the main game cycle.
Not being a graphic designer I took pictures and sounds from the excellent site: https://opengameart.org
Enjoy!
Online version:
https://www.robertozanardo.com/snake/snake.html