欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php怎样写单机小游戏贪吃蛇

程序员文章站 2022-04-11 14:41:02
...

php能写单机游戏,其实现方法如:首先创建一个“control.php”以及“main.php”文件;然后创建一个“tcs.php”文件;最后通过session记录位置来制作单机游戏即可。

php怎样写单机小游戏贪吃蛇

php能写游戏吗?一定有小伙伴会有这样的疑问。答案是可以,虽然php写大型游戏还是欠缺,但写一些小型的单机游戏的话还是可以的,比如说一些休闲游戏,智力游戏!本文给大家分享的是使用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['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);  
?>

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php实现断点续传之文件分割合并详解

php 实现微信开发获取用户信息

php实现验证码小程序的方法

以上就是php怎样写单机小游戏贪吃蛇的详细内容,更多请关注其它相关文章!