php实现贪吃蛇小游戏
程序员文章站
2024-04-03 19:45:22
贪吃蛇游戏是经典手机游戏,既简单又耐玩。通过控制蛇头方向吃蛋,使得蛇变长,从而获得积分。在诺基亚时代,风靡整个手机界,今天我们来看看另类的,如何使用php来实现贪吃蛇小游戏...
贪吃蛇游戏是经典手机游戏,既简单又耐玩。通过控制蛇头方向吃蛋,使得蛇变长,从而获得积分。在诺基亚时代,风靡整个手机界,今天我们来看看另类的,如何使用php来实现贪吃蛇小游戏
废话不多说,代码奉上:
control.php
<!doctype html> <html> <head> <meta charset="utf-8"/> </head> <body> <a href="control.php?a=up">上<a/> <?session_start(); echo $_session['count'];?>分<br/> <a href="control.php?a=left">左<a/> <a href="control.php?a=right">右<a/><br/> <a href="control.php?a=down">下<a/> <? switch($_get['a']){ case "up"; $_session['con']="up"; break; case "down": $_session['con']="down"; break; case "left": $_session['con']="left"; break; case "right": $_session['con']="right"; break; } ?> </body> </html>
main.php
<!doctype html> <html> <head> <meta charset="utf-9"/> <meta http-equiv="refresh" content="0.5"/> </head> <frameset rows="95,*" border="1" noresize > <frame src="control.php" name="top"/> <frame src="./tcs.php" name="menu"/> </frameset> </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); $c1 = imagecolorallocate($im,200,200,200); imagefill($im,0,0,$c1); 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']+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); ?>