This article to you to share is the use of PHP implementation of the Snake Game code, the main idea is through the session record location to make, should say the idea is very ingenious, recommended to everyone
Snake game is a classic mobile games, both simple and resistant to play. By controlling the direction of the snake head to eat eggs, so that the snake becomes longer, thus obtaining points. In the Nokia era, the entire mobile phone industry, today we look at alternative, how to use PHP to realize the snake games
Needless to say, the code is on:
control.php
<! DOCTYPE html>
main.php
<! DOCTYPE html>
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 ') = $_s Ession[' ud ']+10* $i; Imagerectangle ($im, $_session[' lr '],$_session[' udnext '],$_session[' lr ']+10,$_session[' Udnext ']+10, $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 ']+10, $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 '][time ()-$i]+10,$_ 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); ?>
Summary: The above is the entire content of this article, I hope to be able to help you learn.