Rumah  >  Artikel  >  php教程  >  php版贪吃蛇

php版贪吃蛇

WBOY
WBOYasal
2016-08-04 08:53:354807semak imbas

本文给大家分享的是使用php实现贪吃蛇小游戏的代码,其主要思路是通过session记录位置来制作,应该说思路非常巧妙,推荐给大家

贪吃蛇游戏是经典手机游戏,既简单又耐玩。通过控制蛇头方向吃蛋,使得蛇变长,从而获得积分。在诺基亚时代,风靡整个手机界,今天我们来看看另类的,如何使用php来实现贪吃蛇小游戏

废话不多说,代码奉上:

control.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>
     <a href="control.php?a=up">上<a/>       <?session_start(); echo $_SESSION[&#39;count&#39;];?>分<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[&#39;a&#39;]){
        case "up";
          $_SESSION[&#39;con&#39;]="up";
        break;
        case "down":
          $_SESSION[&#39;con&#39;]="down";
        break;
        case "left":
          $_SESSION[&#39;con&#39;]="left";
        break;
        case "right":
          $_SESSION[&#39;con&#39;]="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[&#39;ud&#39;];
  $_SESSION[&#39;lr&#39;];  
  $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[&#39;a&#39;])&&!isset($_SESSION[&#39;b&#39;])){
    $_SESSION[&#39;a&#39;] =rand(0,290);
    $_SESSION[&#39;b&#39;] =rand(0,290);
  }
  if(isset($_SESSION[&#39;a&#39;])&&isset($_SESSION[&#39;b&#39;])){
    imagerectangle($im,$_SESSION[&#39;a&#39;],$_SESSION[&#39;b&#39;],$_SESSION[&#39;a&#39;]+10,$_SESSION[&#39;b&#39;]+10,$red); 
  }
  if(abs(($_SESSION[&#39;lr&#39;]-$_SESSION[&#39;a&#39;]))<=5&&abs($_SESSION[&#39;ud&#39;]-$_SESSION[&#39;b&#39;])<=5){
    unset($_SESSION[&#39;a&#39;]);
    unset($_SESSION[&#39;b&#39;]);
    $_SESSION[&#39;count&#39;]++;
  }
  switch($_SESSION[&#39;con&#39;]){
    case "up";
      if($_SESSION[&#39;ud&#39;]>10)
        $_SESSION[&#39;ud&#39;]-=10;
      else
        $_SESSION[&#39;ud&#39;]=0;
    break;
    case "down":
      if($_SESSION[&#39;ud&#39;]<=280)
        $_SESSION[&#39;ud&#39;]+=10;
      else
        $_SESSION[&#39;ud&#39;]=290;
    break;
    case "left":
      if($_SESSION[&#39;lr&#39;]>10)
        $_SESSION[&#39;lr&#39;]-=10;
      else
        $_SESSION[&#39;lr&#39;]=0;
    break;
    case "right":
      if($_SESSION[&#39;lr&#39;]<=280)
        $_SESSION[&#39;lr&#39;]+=10;
      else
        $_SESSION[&#39;lr&#39;]=290;
    break;
  }
  for($i=0;$i<=$_SESSION[&#39;count&#39;];$i++){
    switch($_SESSION[&#39;con&#39;]){
      case "up":
        $_SESSION[&#39;udnext&#39;] = $_SESSION[&#39;ud&#39;]+10*$i;
        imagerectangle($im,$_SESSION[&#39;lr&#39;],$_SESSION[&#39;udnext&#39;],$_SESSION[&#39;lr&#39;]+10,$_SESSION[&#39;udnext&#39;]+10,$blue);
        // imagerectangle($im,$_SESSION[&#39;lr&#39;],$_SESSION[&#39;udtime&#39;][time()-$i],$_SESSION[&#39;lr&#39;]+10,$_SESSION[&#39;udtime&#39;][time()-$i]+10,$blue);
      break;
      case "down":
        $_SESSION[&#39;udnext&#39;] = $_SESSION[&#39;ud&#39;]-10*$i;
        imagerectangle($im,$_SESSION[&#39;lr&#39;],$_SESSION[&#39;udnext&#39;],$_SESSION[&#39;lr&#39;]+10,$_SESSION[&#39;udnext&#39;]+10,$blue);
        // imagerectangle($im,$_SESSION[&#39;lr&#39;],$_SESSION[&#39;udtime&#39;][time()+$i],$_SESSION[&#39;lr&#39;]+10,$_SESSION[&#39;udtime&#39;][time()+$i]+10,$blue);
      break;
      case "left":
        $_SESSION[&#39;lrnext&#39;] = $_SESSION[&#39;lr&#39;]+10*$i;
        imagerectangle($im,$_SESSION[&#39;lrnext&#39;],$_SESSION[&#39;ud&#39;],$_SESSION[&#39;lrnext&#39;]+10,$_SESSION[&#39;ud&#39;]+10,$blue);
        // imagerectangle($im,$_SESSION[&#39;lrtime&#39;][time()-$i],$_SESSION[&#39;ud&#39;],$_SESSION[&#39;lrtime&#39;][time()-$i]+10,$_SESSION[&#39;ud&#39;]+10,$blue);
      break;
      case "right":
        $_SESSION[&#39;lrnext&#39;] = $_SESSION[&#39;lr&#39;]-10*$i;
        imagerectangle($im,$_SESSION[&#39;lrnext&#39;],$_SESSION[&#39;ud&#39;],$_SESSION[&#39;lrnext&#39;]+10,$_SESSION[&#39;ud&#39;]+10,$blue);
        // imagerectangle($im,$_SESSION[&#39;lrtime&#39;][time()+$i],$_SESSION[&#39;ud&#39;],$_SESSION[&#39;lrtime&#39;][time()+$i]+10,$_SESSION[&#39;ud&#39;]+10,$blue);
      break;
    }
    //imagerectangle($im,$_SESSION[&#39;lr&#39;],$_SESSION[&#39;ud&#39;],$_SESSION[&#39;lr&#39;]+10,$_SESSION[&#39;ud&#39;]+10,$blue);
  }
  header("Content-Type: image/jpeg");
  imagejpeg($im);
  imagedestroy($im);  
?>

以上就是这篇文章的内容,希望对你有所帮助!

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3.php图文教程

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Artikel berkaitan

Lihat lagi