Skip to content

Instantly share code, notes, and snippets.

View ssaurel's full-sized avatar

Sylvain Saurel ssaurel

View GitHub Profile
@ssaurel
ssaurel / JavaHTTPServer.java
Created April 28, 2018 18:08
Simple HTTP Web Server made in Java for a tutorial on the SSaurel's Blog
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
@ssaurel
ssaurel / snake.htm
Created January 18, 2024 15:58
Complete Snake Source Code in HTML5/JavaScript for SSaurel's Blog
<html>
<head>
<title>Snake on SSaurel's Blog</title>
<style type="text/css">
#title {
width: 400px;
text-align: center;
margin: auto;
margin-top: 20px;
@ssaurel
ssaurel / gameloop.js
Created January 18, 2024 15:51
Step 6 for creating a Snake on SSaurel's Blog
gameloop(timestamp) {
// Calculate the number of seconds passed since the last frame
secondspassed = (timestamp - oldtimestamp) / 1000;
oldtimestamp = timestamp;
// Calculate fps
realfps = Math.round(1 / secondspassed);
if (this.keyup) {
this.dirx = 0;
@ssaurel
ssaurel / move.js
Created January 18, 2024 15:45
Step 5 for creating a Snake on SSaurel's Blog
move() {
var dirx = this.dirx;
var diry = this.diry;
// if the snake eats the food
if (this.head.x == this.food.x && this.head.y == this.food.y) {
// add a new tail
var newtail = { x: this.head.x - dirx, y: this.head.y - diry };
this.elements.unshift(newtail);
this.tail = newtail;
@ssaurel
ssaurel / draw.js
Created January 18, 2024 15:34
Step 4 for creating a Snake on SSaurel's Blog
draw(realfps) {
this.ctx.fillStyle = "#EFEFEF";
this.ctx.fillRect(0, 0, canvas.width, canvas.height);
this.ctx.font = "20px Arial";
this.ctx.fillStyle = "black";
//this.ctx.fillText("FPS: " + realfps, 10, 30);
this.ctx.fillText("Points: " + this.points, 10, 30);
this.ctx.fillStyle = "green";
@ssaurel
ssaurel / generatefood.js
Created January 18, 2024 15:29
Step 3 for creating a Snake on SSaurel's Blog
generatefood(nbx, nby) {
// check food is not on the snake
var self = this;
var touch = true;
var food = null;
while (touch) {
food = { x: randomInteger(0, this.nbx), y: randomInteger(0, this.nby) };
this.elements.every((element) => {
@ssaurel
ssaurel / snake_step2.js
Created January 18, 2024 15:11
Step 2 for creating a Snake on the SSaurel's Blog
class Snake {
constructor(ctx, bw, bh, nbx, nby, fps) {
this.ctx = ctx;
this.bw = bw;
this.bh = bh;
this.eltw = bw / nbx;
this.elth = bh / nby;
this.nbx = nbx;
this.nby = nby;
this.dirx = 1;
@ssaurel
ssaurel / snake_step1.htm
Created January 18, 2024 15:07
Step 1 for creating a Snake on SSaurel's Blog
<html>
<head>
<title>Snake on SSaurel's Blog</title>
<style type="text/css">
#title {
width: 400px;
text-align: center;
margin: auto;
margin-top: 20px;
@ssaurel
ssaurel / india_map.html
Last active January 7, 2024 15:17
India Interactive Map for a tutorial on the SSaurel's Blog
<!DOCTYPE html>
<html lang="en">
<head>
<title>India Interactive SVG Map</title>
<style type="text/css">
#title {
font-size: 25px;
fill: black;
@ssaurel
ssaurel / showhidetooltip.html
Created January 7, 2024 15:16
Show Hide Tooltip on SSaurel's Blog
function showTooltip(evt, mouseovertext) {
tooltip.setAttributeNS(null,"x",evt.clientX + 18);
tooltip.setAttributeNS(null,"y",evt.clientY + 32);
tooltip.firstChild.data = mouseovertext;
tooltip.setAttributeNS(null,"visibility","visible");
length = tooltip.getComputedTextLength();
tooltip_bg.setAttributeNS(null,"width",length + 20);
tooltip_bg.setAttributeNS(null,"x",evt.clientX + 8);
tooltip_bg.setAttributeNS(null,"y",evt.clientY + 14);