Snake game is a classic mobile phone game, both simple and easy to play. Eat eggs by controlling the direction of the snake, making the snake grow longer to gain points. In the Nokia era, swept the entire mobile phone industry, today we come to see alternative, how to use PHP to achieve the greedy snake games
Nonsense not much to say, code to serve:
control.php
main.php
tcs.php
<?php session_start ();
$_session[' ud '];
$_session[' LR '];
$im = Imagecreatetruecolor (300,300);
$blue = Imagecolorallocate ($im, 0,0,255);
$red = Imagecolorallocate ($im, 255,0,0);
$c 1 = imagecolorallocate ($im, 200,200,200);
Imagefill ($im, 0,0, $c 1);
if (!isset ($_session[' a ')) &&!isset ($_session[' B '])) {$_session[' a '] =rand (0,290);
$_session[' B '] =rand (0,290); } if (Isset ($_session[' a ']) &&isset ($_session[' B ')) {Imagerectangle ($im, $_session[' a '],$_session[' B '],$_
session[' A ']+10,$_session[' B ']+10, $red); } if (ABS ($_session[' LR ']-$_session[' a ')) <=5&&abs ($_session[' ud ']-$_session[' B ')) <=5) {unset
Session[' a ']);
Unset ($_session[' B '));
$_session[' count ']++;
Switch ($_session[' con ']) {case "up";
if ($_session[' ud ']>10) $_session[' ud ']-=10;
Else $_session[' ud ']=0;
Break
Case "Down": if ($_session[' ud ']<=280) $_session[' ud ']+=10; else $_sesSion[' ud ']=290;
Break
Case "left": if ($_session[' LR ']>10) $_session[' LR ']-=10;
else $_session[' LR ']=0;
Break
Case "Right": if ($_session[' LR ']<=280) $_session[' LR ']+=10;
else $_session[' LR ']=290;
Break For ($i =0 $i <=$_session[' count '; $i + +) {switch ($_session[' con ')} {case "up": $_session[' udnext '] =
$_session[' ud ']+10* $i;
Imagerectangle ($im, $_session[' lr '],$_session[' udnext '],$_session[' lr ']+10,$_session[' udnext ', $blue); Imagerectangle ($im, $_session[' lr '],$_session[' Udtime '][time ()-$i],$_session[' lr ']+10,$_session[' udtime ' [
Time ()-$i]+10, $blue);
Break
Case "Down": $_session[' udnext '] = $_session[' ud ']-10* $i;
Imagerectangle ($im, $_session[' lr '],$_session[' udnext '],$_session[' lr ']+10,$_session[' udnext ', $blue); Imagerectangle ($im, $_session[' lr '],$_session[' Udtime '][time () + $i],$_session[' lr ']+10,$_session[' udtime '][tIME () + $i]+10, $blue);
Break
Case "left": $_session[' lrnext '] = $_session[' LR ']+10* $i;
Imagerectangle ($im, $_session[' lrnext '],$_session[' ud '],$_session[, Lrnext ']+10,$_session[' ud ')]+10, $blue); Imagerectangle ($im, $_session[' Lrtime '][time ()-$i],$_session[' ud ',],$_session[' lrtime ' ()-$i][time
Session[' ud ']+10, $blue);
Break
Case "right": $_session[' lrnext '] = $_session[' LR ']-10* $i;
Imagerectangle ($im, $_session[' lrnext '],$_session[' ud '],$_session[, Lrnext ']+10,$_session[' ud ')]+10, $blue); Imagerectangle ($im, $_session[' Lrtime '][time () + $i],$_session[' ud "],$_session[' Lrtime '"][time "() + $i]+10,$_
Session[' ud ']+10, $blue);
Break
}//imagerectangle ($im, $_session[' LR '],$_session[' ud '],$_session[' LR ']+10,$_session[' ud ']+10, $blue);
Header ("Content-type:image/jpeg");
Imagejpeg ($im);
Imagedestroy ($im);
?>