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