mirror of
				https://github.com/flynx/Course-JavaScript.git
				synced 2025-10-31 03:50:10 +00:00 
			
		
		
		
	refactored simple snake to a separate module...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									0bba41095b
								
							
						
					
					
						commit
						1d56dfbb1c
					
				
							
								
								
									
										85
									
								
								simplesnake/simplesnake.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								simplesnake/simplesnake.html
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,85 @@ | |||||||
|  | <!DOCTYPE html> | ||||||
|  | <html> | ||||||
|  | <head>  | ||||||
|  | <title>Simple Snake</title>  | ||||||
|  | 
 | ||||||
|  | <style> | ||||||
|  | 
 | ||||||
|  | body.hints:before, | ||||||
|  | body.hints:after { | ||||||
|  | 	display: block; | ||||||
|  | 	position: absolute; | ||||||
|  | 	left: 0; | ||||||
|  | 	top: 12.5%; | ||||||
|  | 	bottom: 12.5%; | ||||||
|  | 	z-index: 100; | ||||||
|  | } | ||||||
|  | body.hints:before { | ||||||
|  | 	content: ""; | ||||||
|  | 	width: 100%; | ||||||
|  | 	border-top: dashed 0.5vmin red; | ||||||
|  | 	border-bottom: dashed 0.5vmin red; | ||||||
|  | } | ||||||
|  | body.hints:after { | ||||||
|  | 	content: "↺↻"; | ||||||
|  | 	width: 37vmin; | ||||||
|  | 	right: 50%; | ||||||
|  | 	left: auto; | ||||||
|  | 	overflow: visible; | ||||||
|  | 	border-right: dashed 0.5vmin red; | ||||||
|  | 
 | ||||||
|  | 	color: rgba(255, 0, 0, 0.5); | ||||||
|  | 	font-size: 15vmin; | ||||||
|  | 	line-height: 70vmin; | ||||||
|  | 	white-space: pre; | ||||||
|  | 	letter-spacing: 50vmin; | ||||||
|  | } | ||||||
|  | .hints .simplesnake { | ||||||
|  | 	opacity: 0.3; | ||||||
|  | } | ||||||
|  | .hints .simplesnake:before, | ||||||
|  | .hints .simplesnake:after { | ||||||
|  | 	position: absolute; | ||||||
|  | 	display: block; | ||||||
|  | 	text-align: center; | ||||||
|  | 	width: 100vw; | ||||||
|  | 	color: red; | ||||||
|  | 	font-size: 5vh; | ||||||
|  | 	line-height: 10vh; | ||||||
|  | 	z-index: 100; | ||||||
|  | } | ||||||
|  | .hints .simplesnake:before { | ||||||
|  | 	content: "new level"; | ||||||
|  | } | ||||||
|  | .hints .simplesnake:after { | ||||||
|  | 	bottom: 0; | ||||||
|  | 	content: "pause"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | body { | ||||||
|  | 	font-family: sans-serif; | ||||||
|  | 	overflow: hidden; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | .simplesnake .field { | ||||||
|  | 	position: relative; | ||||||
|  | 	left: 50%; | ||||||
|  | 	margin-left: -45vmin; | ||||||
|  | 
 | ||||||
|  | 	width: 90vmin; | ||||||
|  | 	height: 90vmin; | ||||||
|  | 	border: solid 1px silver; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | </style> | ||||||
|  | 
 | ||||||
|  | <script src="simplesnake.js"></script> | ||||||
|  | 
 | ||||||
|  | </head> | ||||||
|  | <body onload="setup()" onclick="clearHints()" class="hints"> | ||||||
|  | 
 | ||||||
|  | <div class="simplesnake"> </div> | ||||||
|  | 
 | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
|  | <!-- vim:set ts=4 sw=4 spell : --> | ||||||
							
								
								
									
										122
									
								
								jsssnake/simplesnake.html → simplesnake/simplesnake.js
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										122
									
								
								jsssnake/simplesnake.html → simplesnake/simplesnake.js
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							| @ -1,76 +1,33 @@ | |||||||
| <!DOCTYPE html> | /********************************************************************** | ||||||
| <html> | *  | ||||||
| <style> | * Simple Snake  | ||||||
| 
 | * | ||||||
| body.hints:before, | * This code is designed to illustrate the non-intuitive approach to an | ||||||
| body.hints:after { | * implementation, building a snake game as a cellular automaton rather | ||||||
| 	display: block; | * than the more intuitive, a set of entities (OOP) or a number of sets  | ||||||
| 	position: absolute; | * of procedures and data structures, directly emulating the "tactile"  | ||||||
| 	left: 0; | * perception of the game, i.e. independent field, snakes, walls, apples | ||||||
| 	top: 12.5%; | * and their interactions. | ||||||
| 	bottom: 12.5%; | * | ||||||
| 	z-index: 100; | * In this approach there are no entities, no snakes, no apples, no  | ||||||
| } | * walls, just a set of cells in a field and cell behaviours per game  | ||||||
| body.hints:before { | * step: | ||||||
| 	content: ""; | * 	- empty cells, apples and walls just sit there | ||||||
| 	width: 100%; | * 	- "snake" cells: | ||||||
| 	border-top: dashed 0.5vmin red; | * 		- decrement age | ||||||
| 	border-bottom: dashed 0.5vmin red; | * 		- if age is 0 clear cell | ||||||
| } | * 		- if cell has direction (i.e. snake head) | ||||||
| body.hints:after { | * 			- if target cell is red (apple) increment age | ||||||
| 	content: "↺↻"; | * 			- color new cell in direction: | ||||||
| 	width: 37vmin; | * 				- set age on to current age + 1 | ||||||
| 	right: 50%; | * 				- set direction to current | ||||||
| 	left: auto; | * 			- clear direction | ||||||
| 	overflow: visible; | * | ||||||
| 	border-right: dashed 0.5vmin red; | * NOTE: that in the above description some details are omitted for  | ||||||
| 
 | * 		clarity... | ||||||
| 	color: rgba(255, 0, 0, 0.5); | * | ||||||
| 	font-size: 15vmin; | * | ||||||
| 	line-height: 70vmin; | **********************************************************************/ | ||||||
| 	white-space: pre; |  | ||||||
| 	letter-spacing: 50vmin; |  | ||||||
| } |  | ||||||
| .hints .simplesnake { |  | ||||||
| 	opacity: 0.3; |  | ||||||
| } |  | ||||||
| .hints .simplesnake:before, |  | ||||||
| .hints .simplesnake:after { |  | ||||||
| 	position: absolute; |  | ||||||
| 	display: block; |  | ||||||
| 	text-align: center; |  | ||||||
| 	width: 100vw; |  | ||||||
| 	color: red; |  | ||||||
| 	font-size: 5vh; |  | ||||||
| 	line-height: 10vh; |  | ||||||
| 	z-index: 100; |  | ||||||
| } |  | ||||||
| .hints .simplesnake:before { |  | ||||||
| 	content: "new level"; |  | ||||||
| } |  | ||||||
| .hints .simplesnake:after { |  | ||||||
| 	bottom: 0; |  | ||||||
| 	content: "pause"; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| body { |  | ||||||
| 	font-family: sans-serif; |  | ||||||
| 	overflow: hidden; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| .simplesnake .field { |  | ||||||
| 	position: relative; |  | ||||||
| 	left: 50%; |  | ||||||
| 	margin-left: -45vmin; |  | ||||||
| 
 |  | ||||||
| 	width: 90vmin; |  | ||||||
| 	height: 90vmin; |  | ||||||
| 	border: solid 1px silver; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| </style> |  | ||||||
| <script> |  | ||||||
| //---------------------------------------------------------------------
 |  | ||||||
| 
 | 
 | ||||||
| function makeEvent(handler_attr){ | function makeEvent(handler_attr){ | ||||||
| 	return function(func){ | 	return function(func){ | ||||||
| @ -350,7 +307,8 @@ var Snake = { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| //---------------------------------------------------------------------
 | 
 | ||||||
|  | /*********************************************************************/ | ||||||
| 
 | 
 | ||||||
| var HANDLER_SET = false | var HANDLER_SET = false | ||||||
| var KEY_CONFIG = { | var KEY_CONFIG = { | ||||||
| @ -361,6 +319,7 @@ var KEY_CONFIG = { | |||||||
| 	Left: ['left'], | 	Left: ['left'], | ||||||
| 	Right: ['right'], | 	Right: ['right'], | ||||||
| } | } | ||||||
|  | 
 | ||||||
| function makeKeyboardHandler(snake){ | function makeKeyboardHandler(snake){ | ||||||
| 	return function(event){ | 	return function(event){ | ||||||
| 		clearHints() | 		clearHints() | ||||||
| @ -422,16 +381,5 @@ function setup(snake, timer, size){ | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| //---------------------------------------------------------------------
 | /********************************************************************** | ||||||
| </script> | * vim:set ts=4 sw=4 :                                                */ | ||||||
| 
 |  | ||||||
| <head> <title>Simple Snake</title> </head> |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| <body onload="setup()" onclick="clearHints()" class="hints"> |  | ||||||
| 
 |  | ||||||
| <div class="simplesnake"> </div> |  | ||||||
| 
 |  | ||||||
| </body> |  | ||||||
| </html> |  | ||||||
| <!-- vim:set ts=4 sw=4 spell : --> |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user