Game - SNAKE!

Κώδικας, πληροφορίες, ερωτήσεις και απαντήσεις σχετικές με την JavaScript.

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
Άβαταρ μέλους
Basilakis
PHP Moderator
Δημοσιεύσεις: 8574
Εγγραφή: 17 Νοέμ 2003 13:03
Τοποθεσία: Womans' Brain
Επικοινωνία:

Game - SNAKE!

Δημοσίευση από Basilakis » 30 Ιαν 2006 17:31

Κώδικας: Επιλογή όλων

<!-- THREE STEPS TO INSTALL SNAKE&#58;

  1.  Put the first code in your opening HTMl document -->
  2.  Save the remaining code to a new file, save as snake.html  -->
  3.  Upload all the snake images to the same web site directory -->

<!-- STEP ONE&#58; Paste this code into the opening HTML document  -->

<center>
<form name="open-snake">
<input type=button value="Play Snake" onClick="window.open&#40;'snake.html','snake','top=100,left=100,width=575,height=400'&#41;;">
</form>
</center>

<!-- STEP TWO&#58; Paste this code into a new document, save it as snake.html  -->

<HEAD>

<STYLE TYPE="text/css">
.item&#123;&#125;
.worm1 &#123;
  font-weight&#58;bold;
  font-size&#58;12pt;
  color&#58;blue;
  background&#58;white;
  width&#58;150px;
  BORDER-BOTTOM&#58; #ffffff solid 0px; 
  BORDER-TOP&#58; #ffffff solid 0px; 
  BORDER-LEFT&#58; #ffffff solid 0px; 
  BORDER-RIGHT&#58; #ffffff solid 0px; 
&#125;
.worm2 &#123;
  font-weight&#58;bold;
  font-size&#58;12pt;
  color&#58;red;
  background&#58;white;
  width&#58;150px;
  BORDER-BOTTOM&#58; #ffffff solid 0px; 
  BORDER-TOP&#58; #ffffff solid 0px; 
  BORDER-LEFT&#58; #ffffff solid 0px; 
  BORDER-RIGHT&#58; #ffffff solid 0px; 
&#125;
.info &#123;
  font-weight&#58;bold;
  font-size&#58;10pt;
  color&#58;black;
  background&#58;white;
  width&#58;150px;
  height&#58;100px;
  BORDER-BOTTOM&#58; #ffffff solid 0px; 
  BORDER-TOP&#58; #ffffff solid 0px; 
  BORDER-LEFT&#58; #ffffff solid 0px; 
  BORDER-RIGHT&#58; #ffffff solid 0px; 
&#125;
</style>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original&#58;  Clayton Carlson &#40;clayton@awebmaker.com&#41; -->
<!-- Web Site&#58;  http&#58;//www.awebmaker.com -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http&#58;//javascript.internet.com -->

<!-- Begin
var height = 20; //height of the game area
var width = 20;	//width of the game area
var speed = 100	//speed, higher = slower
width += 2;
var a = 0;
var b = 0;
document.write&#40;"<table bgcolor=white bordercolor=black "&#41;;
document.write&#40;"align=center border=1 cellpadding=0 cellspacing=0><tr><td>"&#41;;
for &#40;b = 0; b < height+2; b++&#41; &#123;
document.write&#40;"<img src=end.gif width=0 height=0>"&#41;;
for &#40;a = 0; a < width- 2; a++&#41; &#123;
if &#40;&#40;b == 0&#41; || &#40;b == height+1&#41;&#41; &#123;
document.write&#40;"<img src=end.gif width=0 height=0>"&#41;;
&#125;
else &#123;
document.write&#40;"<img src=blank.gif width=15 height=15>"&#41;;
   &#125;
&#125;
document.write&#40;"<img src=end.gif width=0 height=0><br>"&#41;;
&#125;
document.write&#40;"</td>"&#41;;
document.write&#40;"<td valign=top align=center bgcolor=white>"&#41;;
document.write&#40;"<p align=center><strong><font color=blue>Snake</font></strong></p>"&#41;;
document.write&#40;"<br>"&#41;;
document.write&#40;"<form name=info>"&#41;;
document.write&#40;"<input type=button size=28 value=0 class=worm1></form>"&#41;;
document.write&#40;"<br><font size=-2>Press any arrow key to start</font></td></tr></table>"&#41;;

var points = 0;
var go = 1;
var di = 0;
var x = 0;
var y = 0;
var n = 0;
document.images&#91;1&#93;.src = "blank.gif"; 
var blank = document.images&#91;1&#93;.src;
var hw = &#40;height * width&#41;;
var o = Math.floor&#40;Math.random&#40;&#41; * hw - 2&#41;;
do &#123;
o = Math.floor&#40;Math.random&#40;&#41; * hw-2&#41;;
&#125; while&#40;document.images&#91;o&#93;.src != blank&#41;;
var i = o;
var food = 0;
do &#123;
food = Math.floor&#40;Math.random&#40;&#41; * hw-2&#41;;
&#125; while &#40;document.images&#91;food&#93;.src != blank&#41;;
document.images&#91;i&#93;.src = "worm.gif";
document.images&#91;width-1&#93;.src="end.gif";
var end = document.images&#91;width-1&#93;.src;
var file = document.images&#91;i&#93;.src;
var length = 1;
var worm = new Array&#40;&#41;;
var k = 0;
var ie = document.all ? 1 &#58; 0;
var  enableScroll = &#40;&#40;navigator.appName == "Microsoft Internet Explorer"&#41; && &#40;parseInt&#40;navigator.appVersion&#41; >3&#41;&#41; ;
var height = document.images&#91;0&#93;.height;
var tScroll;
var d = 0;
function runTimer&#40;&#41; &#123;
if &#40;d != 0&#41; &#123; n++; &#125;
if &#40;d == 1&#41; &#123; i--; &#125;
if &#40;d == 2&#41; &#123; i++; &#125;
if &#40;d == 3&#41; &#123; i += width; &#125;
if &#40;d == 4&#41; &#123; i -= width; &#125;
if &#40;document.images&#91;i&#93;.src == end&#41; &#123;
speed -= 400; i = worm&#91;n-1&#93;; di = 1; die&#40;&#41;;
&#125;
worm&#91;n&#93; = i;
if&#40;i == food&#41; &#123;
length++; points += &#40;10*length&#41;;
do &#123;
food = Math.floor&#40;Math.random&#40;&#41; * hw-2&#41;;
&#125; while &#40;document.images&#91;food&#93;.src != blank&#41;;
if &#40;di == 0&#41; &#123;
document.info.elements&#91;0&#93;.value = points;
   &#125;
&#125;
if &#40;n > length&#41;&#123;
o = worm&#91;n-length&#93;;
&#125;
if &#40;&#40;document.images&#91;i&#93;.src == file&#41; && &#40;n > 1&#41;&#41; &#123;
speed -= 400; d = 0; di = 1; die&#40;&#41;;
&#125;
if&#40;di == 0&#41; &#123;
document.images&#91;o&#93;.src = "blank.gif";
document.images&#91;i&#93;.src = "worm.gif";
document.images&#91;food&#93;.src = "food.gif";
tScroll = window.setTimeout&#40;"runTimer&#40;&#41;;", speed&#41;;
   &#125;
&#125;
if &#40;enableScroll&#41;&#123;
if &#40;ie&#41; window.onload = runTimer;
if &#40;ie&#41; window.onunload = new Function&#40;"clearTimeout&#40;tScroll&#41;"&#41;;
&#125;
systm = "";
ver = navigator.appVersion;
len = ver.length;
for &#40;iln = 0;iln < len; iln++&#41; if &#40;ver.charAt&#40;iln&#41; == "&#40;"&#41; break;
systm = ver.charAt&#40;iln+1&#41;.toUpperCase&#40;&#41;;
document.onkeydown = keyDown;
if &#40;systm != "C"&#41; &#123;
document.captureEvents&#40;Event.KEYDOWN&#41;;
&#125;
function keyDown&#40;DnEvents&#41; &#123;
if &#40;systm != "C"&#41; &#123;
k = DnEvents.which;
&#125; else &#123;
k = window.event.keyCode;
&#125;
if &#40;k == 37&#41; &#123; d = 1; &#125;
if &#40;k == 39&#41; &#123; d = 2; &#125;
if &#40;k == 40&#41; &#123; d = 3; &#125;
if &#40;k == 38&#41; &#123; d = 4; &#125;
&#125;
function die&#40;&#41; &#123;
i = 0;
o = 0;
food = 0;
document.info.elements&#91;0&#93;.value = "Died with " + document.info.elements&#91;0&#93;.value;
var restart=confirm&#40;"Play Again?"&#41;;
if&#40;restart&#41;&#123; location.reload&#40;&#41;; &#125;
&#125;
// End -->
</script>
Σας δίνω το game όπως το βρήκα. Εσείς πρέπει να δείτε να βάλετε εικόνες που χρειάζετε δηλαδή το food snake kai blank. Και μόλις βρώ λίγο χρόνο θα σας πώ και τι κάνει το κάθε κομμάτι του κώδικα και κάθε function. ;)

Άβαταρ μέλους
skeftomilos
Script Master
Δημοσιεύσεις: 2888
Εγγραφή: 07 Ιαν 2005 07:22
Τοποθεσία: Αθήνα

Game - SNAKE!

Δημοσίευση από skeftomilos » 31 Ιαν 2006 01:08

Το ανέβασα εδώ: snake online. :)

Θέλει IE.
The pure and simple truth is rarely pure and never simple. Ο μη νους δε σκέπτεται μη σκέψεις για το τίποτα.

Άβαταρ μέλους
Basilakis
PHP Moderator
Δημοσιεύσεις: 8574
Εγγραφή: 17 Νοέμ 2003 13:03
Τοποθεσία: Womans' Brain
Επικοινωνία:

Game - SNAKE!

Δημοσίευση από Basilakis » 31 Ιαν 2006 16:09

Thanks! ;)

Απάντηση

Επιστροφή στο “JavaScript και Frameworks”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες