Przeglądarkowa odsłona kultowej gry SNAKE.
Zagrać można pod tym adresem: http://fx-team.fulara.com/team/adam/snake
Plik index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!DOCTYPE html> <html lang="pl"> <head> <meta charset="utf-8"> <title>SNAKE</title> <style> html,body { margin:0 auto; font-family: Arial, Helvetica, sans-serif; } canvas { display: block; margin:0 auto; border:2px solid black; } </style> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/5.1.3/pixi.min.js"></script> <script src="js/keyboard.js"></script> <script src="js/app.js"></script> </body> </html> |
Plik app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | const app = new PIXI.Application({ width: 950, height: 950, backgroundColor: 0x1099bb, resolution: window.devicePixelRatio || 1, }); document.body.appendChild(app.view); const container = new PIXI.Container(); app.stage.addChild(container); container.x = app.screen.width / 2; container.y = app.screen.height / 2; const texture_snakeBrick = PIXI.Texture.from('img/snakeBrick.png'); const texture_apple = PIXI.Texture.from('img/apple.png'); class Snake { constructor() { this._segments = []; this._direction = 1; this._speed = 50; this._controled = false; //Dodaj trzy fragmenty węża na początku gry for(let i=0;i<3;i++) { this.addSegment(); } //Początkowa pozycja głowy węża this._segments[0].x = 0; this._segments[0].y = 0; } //Dodaj segment do węża addSegment() { let sprite = new PIXI.Sprite(texture_snakeBrick); sprite.anchor.set(0.5); sprite.tint = Math.random() * 0xFFFFFF; sprite.visible = false; if(this._segments.length) { sprite.x = this._segments[0].x; sprite.y = this._segments[0].y; } container.addChild(sprite); this._segments.push(sprite); } moveSnake() { this._controled = false; for(let i=this._segments.length-1;i>=0;i--) { this._segments[i].visible = true; if(i==0) { if(this._direction == 1) { this._segments[i].y -= this._speed; } else if(this._direction == 2) { this._segments[i].x += this._speed; } else if(this._direction == 3) { this._segments[i].y += this._speed; } else if(this._direction == 4) { this._segments[i].x -= this._speed; } if(this._segments[i].y < -450) { this._segments[i].y = 450; } else if(this._segments[i].y > 450) { this._segments[i].y = -450; } if(this._segments[i].x < -450) { this._segments[i].x = 450; } else if(this._segments[i].x > 450) { this._segments[i].x = -450; } break; } else { this._segments[i].x = this._segments[i-1].x; this._segments[i].y = this._segments[i-1].y; } } this.detectCollision(); this.eatApple(); } eatApple() { if(this._segments[0].y == apple._sprite.y && this._segments[0].x == apple._sprite.x) { this.addSegment(); apple.newCords(); } } control() { if(!this._controled) { if (up.isDown && this._direction!=3) { this._controled = true; this._direction = 1; } if (right.isDown && this._direction!=4) { this._controled = true; this._direction = 2; } if (down.isDown && this._direction!=1) { this._controled = true; this._direction = 3; } if (left.isDown && this._direction!=2) { this._controled = true; this._direction = 4; } } } detectCollision() { for(let i=this._segments.length-1;i>0;i--) { if(this._segments[0].x == this._segments[i].x && this._segments[0].y == this._segments[i].y) { game = false; const gameOver = new PIXI.Text('GAME OVER!'); gameOver.anchor.set(.5); gameOver.x = 0; gameOver.y = 200; container.addChild(gameOver); console.log("GAME OVER!"); } } } } class Apple { constructor(cords) { this._sprite = new PIXI.Sprite(texture_apple); this._sprite.anchor.set(0.5); this._sprite.x = cords.x; this._sprite.y = cords.y; container.addChild(this._sprite); } newCords() { this._sprite.x = getRandomInt(-9, 9)*50; this._sprite.y = getRandomInt(-9, 9)*50; } } let snake = new Snake(); let apple = new Apple({x:0, y:-200}); var game = true; // Listen for animate update app.ticker.add((delta) => { if(game) { snake.control(); } }); setInterval(function() { if(game) { snake.moveSnake(); } }, 300); function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } |