Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Snake</title>
    <link rel="stylesheet" href="../style.css">
</head>
<body>
  <br>
    <a class="logout" href="logout.php">Abmelden</a>

    <canvas class="box-game" id="canvas" width="800" height="800"></canvas>

    <script>
        let canvas = document.getElementById('canvas');
        let ctx = canvas.getContext('2d');
        let rows = 20;
        let cols = 20;
        let snake = [
            {x: 2, y: 3}
        ];
        let cellWidth = canvas.width / cols;
        let cellHeight = canvas.height / rows;
        let direction = '';
        let foodCollected = false;
        var score = 0;
        //https://developer.mozilla.org/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript/Track_the_score_and_win
        //https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/var


        setInterval(gameLoop, 135); // die funktion gameLoop alle 200 Millisekunden ausführen
        document.addEventListener('keydown', keyDown); // wenn taste gedrückt wird, event starten

        placeFood(); // Food plazieren
        draw(); // Canvas färben

        function draw()
        {
            ctx.fillStyle = 'black'; // Farbe schwarz auswählen
            ctx.fillRect(0, 0, canvas.width, canvas.height); // Ganze leinwand füllen


            ctx.fillStyle = 'white'; // weiß auswählen
            add(130, 170); // füllen
            add(160, 170); // fühlen
            snake.forEach(part => add(part.x, part.y)); // die snake bewegen

            ctx.fillStyle = 'yellow'; // gelb auswählen
            add(food.x, food.y); // food färben
            drawScore();

            requestAnimationFrame(draw); // draw die ganze zeit ausführen
        }
        function placeFood() // Food plazieren
        {
            let randomX = Math.floor(Math.random() * cols); // Zufällige Variable 'X' errechnen
            let randomY = Math.floor(Math.random() * rows); // Zufällige Variable 'Y' errechnen

            food = {
                x: randomX,
                y: randomY
            } // 'X' & 'Y' zusammen rechnen und bereich food bestimmen
        }
        function drawScore() {
            ctx.font = "16px Arial";
            ctx.fillStyle = "#0095DD";
            ctx.fillText("Score: "+score, 8, 20);
            if(foodCollected == true)
            {
                score = score +1;
            }
        }
        function testGameOver()
        {
            let firstPart = snake[0];
            let otherParts = snake.slice(1);
            let duplicatePart = otherParts.find(part => part.x == firstPart.x && part.y == firstPart.y);
            if (snake[0].x < 0)
            {
                snake[0].x = 19;
            }
            if (snake[0].x > cols - 1)
            {
                snake[0].x = 0;
            }
            if (snake[0].y < 0)
            {
                snake[0].y = 19;
            }
            if (snake[0].y > cols - 1)
            {
                snake[0].y = 0;
            }
            if (duplicatePart)
            {
                placeFood();
                snake = [{
                    x: 19,
                    y: 3
                }]
                direction = '';
            }
        }

        function add(x, y)
        {
            ctx.fillRect(x * cellWidth, y * cellHeight, cellWidth - 3, cellHeight - 3);
        }
        function shiftSnake()
        {
            for (let i = snake.length - 1; i > 0; i--) {
                const part = snake[i];
                const lastpart = snake[i - 1];
                part.x = lastpart.x;
                part.y = lastpart.y;

            }
        }
        function gameLoop() // Tasten ausführen
        {
            testGameOver();
            if(foodCollected)
            {
                snake = [{x: snake[0].x, y: snake[0].y}, ...snake];
                foodCollected = false;
            }
            drawScore();
            if(foodCollected)
            {
                score = score + plus
            }
            shiftSnake();
            if(direction == 'LEFT')
            {
                snake[0].x--;
            }
            if(direction == 'RIGHT')
            {
                snake[0].x++;
            }
            if(direction == 'UP')
            {
                snake[0].y--;
            }
            if(direction == 'DOWN')
            {
                snake[0].y++;
            }
            if(snake[0].x == food.x &&  snake[0].y == food.y)
            {
                foodCollected = true;
                placeFood(); // Food neu Plazieren
            }
        }
        function keyDown(e) // Tasten bestimmen
        {
            if(e.keyCode == 37)
            {
                direction = 'LEFT';
            }
            if(e.keyCode == 38)
            {
                direction = 'UP';
            }
            if(e.keyCode == 39)
            {
                direction = 'RIGHT';
            }
            if(e.keyCode == 40)
            {
                direction = 'DOWN';
            }
        }
    </script>
    <h1><center>Drücke die Pfeiltasten um das Spiel zu beginnen!</center></h1>
</body>
</html>

              
            
!

CSS

              
                body{
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  background: #34495e;
}
h1{
  color: white;
  font-weight: 500;
}
.box{
  width: 300px;
  padding: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #191919;
  text-align: center;
}
.box h1{
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}
.box input[type = "text"],.box input[type = "password"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #3498db;
  padding: 14px 10px;
  width: 200px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
}
.box input[type = "text"]:focus,.box input[type = "password"]:focus{
  width: 280px;
  border-color: #2ecc71;
}
.login{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #2ecc71;
  padding: 14px 40px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
  cursor: pointer;
}
.login:hover{
  background: #2ecc71;
}
.box-snake{
  width: 630px;
  padding: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #191919;
  text-align: center;
}
.box-snake h1{
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}
.box-snake input[type = "text"],.box-snake input[type = "password"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #3498db;
  padding: 14px 10px;
  width: 200px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
}
.box-snake input[type = "text"]:focus,.box-snake input[type = "password"]:focus{
  width: 280px;
  border-color: #2ecc71;
}
.box-game{
  width: 600px;
  padding: 80px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #191919;
  text-align: center;
}
.box-game h1{
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}
.box-game input[type = "text"],.box input[type = "password"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #3498db;
  padding: 14px 10px;
  width: 200px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
}
.box-game input[type = "text"]:focus,.box input[type = "password"]:focus{
  width: 280px;
  border-color: #2ecc71;
}
.logout {
  text-align: right;
  text-decoration: none;
  color: white;
  margin: 20px auto;
  padding: 14px 40px
}

              
            
!

JS

              
                
              
            
!
999px

Console