core: add Snake using Typescript #7
							
								
								
									
										25
									
								
								src/snake.ts
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								src/snake.ts
									
									
									
									
									
								
							| @ -68,12 +68,8 @@ class SnakeCore { | ||||
|         this.width = 25; | ||||
|         this.height = 15; | ||||
|         this.board = []; | ||||
|         for (let i = 0; i < this.height; i++) { | ||||
|         for (let i = 0; i < this.height; i++) | ||||
|             this.board.push(new Array(this.width)); | ||||
|             for (let j = 0; j < this.width; j++) { | ||||
|                 this.board[i][j] = 0; | ||||
|             } | ||||
|         } | ||||
|         this.body = []; | ||||
|         this.gameover = false; | ||||
|         this.foodAte = true; | ||||
| @ -86,19 +82,22 @@ class SnakeCore { | ||||
|     } | ||||
| 
 | ||||
|     reset() { | ||||
|         snake.gameover = false; | ||||
|         snake.foodAte = true; | ||||
|         this.gameover = false; | ||||
|         this.foodAte = true; | ||||
|         for (let i = 0; i < this.height; i++) { | ||||
|             for (let j = 0; j < this.width; j++) { | ||||
|                 this.board[i][j] = 0; | ||||
|             } | ||||
|         } | ||||
|         snake.context.fillStyle = "black"; | ||||
|         for (let i = 0; i < snake.height; i++) { | ||||
|             for (let j = 0; j < snake.width; j++) { | ||||
|                 snake.context.fillRect(j * snake.grid, i * snake.grid, snake.grid, snake.grid); | ||||
|             } | ||||
|         } | ||||
|         while (this.body.length > 0) | ||||
|             this.body.pop(); | ||||
|         this.startRandom(); | ||||
|         this.foodRegen(); | ||||
|         this.draw(); | ||||
|     } | ||||
| 
 | ||||
|     startRandom() { | ||||
|         this.body.push(new Point(Math.floor(Math.random() * this.width), Math.floor(Math.random() * this.height))); | ||||
|     } | ||||
| 
 | ||||
|     foodRegen() { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user