溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用php編寫(xiě)一個(gè)貪吃蛇小游戲

發(fā)布時(shí)間:2020-12-15 16:36:46 來(lái)源:億速云 閱讀:224 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)利用php編寫(xiě)一個(gè)貪吃蛇小游戲,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

control.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>
    &nbsp;<a href="control.php?a=up">上<a/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?session_start(); echo $_SESSION['count'];?>分<br/>
    <a href="control.php?a=left">左<a/>&nbsp;<a href="control.php?a=right">右<a/><br/>
    &nbsp;<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);  
?>

看完上述內(nèi)容,你們對(duì)利用php編寫(xiě)一個(gè)貪吃蛇小游戲有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI