吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1819|回复: 25
收起左侧

[其他转载] PHP版贪吃蛇小游戏

[复制链接]
qPHPMYSQL 发表于 2022-11-18 09:25
不依赖任何扩展,打开就能运行
1.将代码复制到文本中,后缀改为php
2.放入项目或www中,浏览器上访问这个php文件
3.就可运行了。
[PHP] 纯文本查看 复制代码
<?php
class snake
{
    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->app = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    }

    /**
     * 读取session模拟的虚拟缓存显示到屏幕上
     * [url=home.php?mod=space&uid=155549]@Return[/url] html
     */
    public function print()
    {
        $score = !$this->get("score") ? 0 : $this->get("score"); //得分
        //html长字符串
        $html = <<<MAP_STRING
    <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>snake</title>
<style>
table{
    background-color:#000;
  }
  table td{
    padding:1px 1px 1px 1px;
    width:16px;height:12px;
    color:red;
  }
  table tr{
    background-color:#000;
  }
  .sn{
    background:#fff;
  }
  .bright{background:#fff;}
  button{font-size:20px;}
  marquee{width:400px;height:400px;font-size:50px;color:red;background:#000;text-align:center;}
</style>
<p>php贪吃蛇->[分数:{$score}]</p>
MAP_STRING;
        $map = "<table>";
        //初始化高亮区域$bright,包括蛇体和食物
        if (!$this->get("snake")) {
            $bright = [];
        } else {
            $bright = array_merge($this->get("snake"), [$this->get("food")]);
        }
        //标记蛇体和食物高亮
        for ($i = 0; $i < 30; $i++) {
            $map .= "<tr>";
            for ($j = 0; $j < 30; $j++) {
                if (in_array([$j, $i], $bright)) {
                    $map .= "<td class='bright'></td>";
                } else {
                    $map .= "<td></td>";
                }
            }
            $map .= "</tr>";
        }
        $map .= "</table>";
        //控制区域长字符串
        $controll = <<<CONTROLL
    <a href="{$this->app}?isOn=on"><button>start</button></a>
    ........................
    <a href="{$this->app}?isOn=on&direction=up"><button>up</button></a>
    <br><a href="{$this->app}?isOn=off"><button>stop</button></a>
    ...............
    <a href="{$this->app}?isOn=on&direction=left"><button>left</button></a>
    <a href="{$this->app}?isOn=on&direction=right"><button>right</button></a>
    <br>............................................
    <a href="{$this->app}?isOn=on&direction=down"><button>down</button></a>
CONTROLL;
        if (isset($_GET["isOn"]) && $_GET["isOn"] == "on") {
            header("refresh: 1"); //每一秒刷新一次页面
        }
        if (isset($_GET["msg"])) {
            //收到游戏结束的消息
            echo $html . "<marquee direction=up>{$_GET["msg"]}</marquee>" . $controll;
        } else {
            //游戏画面显示
            echo $html . $map . $controll;
        }
    }

    /**
     * 设置虚拟显存session中的数据
     * [url=home.php?mod=space&uid=952169]@Param[/url] string $k
     * @param string $v
     */
    public function set($k, $v)
    {
        $_SESSION[$k] = $v;
    }

    /**
     * 读取虚拟缓存session中的数据
     * @param  string $k
     * @return string|bool
     */
    public function get($k)
    {
        return isset($_SESSION[$k]) ? $_SESSION[$k] : false;
    }

    /**
     * 贪吃蛇算法,添头去尾、吃食物、撞墙判断、咬自己判断
     * @return void
     */
    public function cpu()
    {
        session_start();
        //游戏若暂停状态则不需计算不需修改虚拟缓存
        if (!(isset($_GET["isOn"]) && $_GET["isOn"] == "on")) {
            return;
        }
        //初始化蛇体和食物
        if (!$this->get("snake")) {
            $this->set("snake", [
                [29, 29]
            ]);
            $this->set("score", 0);
            $this->getFood();
            return;
        }
        //初始化运动方向
        if (!isset($_GET["direction"])) {
            $this->set("direction", "left");
        } else {
            $this->set("direction", $_GET["direction"]);
        }
        $snake = $this->get("snake");
        //计算蛇头坐标
        switch ($this->get("direction")) {
            case "up": {
                    $snakeHead = [
                        $snake[0][0],
                        $snake[0][1] - 1
                    ];
                    break;
                }
            case "down": {
                    $snakeHead = [
                        $snake[0][0],
                        $snake[0][1] + 1
                    ];
                    break;
                }
            case "left": {
                    $snakeHead = [
                        $snake[0][0] - 1,
                        $snake[0][1]
                    ];
                    break;
                }
            case "right": {
                    $snakeHead = [
                        $snake[0][0] + 1,
                        $snake[0][1]
                    ];
                    break;
                }
        }
        //咬到自己,游戏结束
        if (in_array($snakeHead, $snake)) {
            $this->gameOver();
            return;
        }
        //添加蛇头坐标
        array_unshift($snake, $snakeHead);
        //撞墙,游戏结束
        if ($snake[0][0] < 0 || $snake[0][1] < 0 || $snake[0][0] > 29 || $snake[0][1] > 29) {
            $this->gameOver();
            return;
        }
        //咬到食物得一分
        if (in_array($this->get("food"), $snake)) {
            $this->getFood();
            $this->set("score", $this->get("score") + 1);
        } else {
            unset($snake[count($snake) - 1]);
        }
        $this->set("snake", $snake);
    }

    /**
     * 取得食物
     * @return void
     */
    public function getFood()
    {
        $food = [mt_rand(1, 29), mt_rand(1, 29)];
        $this->set("food", $food);
        if (in_array($food, $this->get("snake"))) {
            $this->getFood();
        }
    }
    
    /**
     * 游戏结束
     * @return void
     */
    public function gameOver()
    {
        session_unset();
        header("location:" . $this->app . "?msg=gameover");
    }

    /**
     * 程序入口
     * @return void
     */
    public function main()
    {
        $this->cpu();
        $this->print();
    }
}
(new snake())->main();

免费评分

参与人数 4吾爱币 +4 热心值 +4 收起 理由
960886319 + 1 + 1 我很赞同!
xiaoyangby + 1 + 1 我很赞同!
Ym199628 + 1 + 1 热心回复!
坐久落花多 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| qPHPMYSQL 发表于 2022-11-24 09:05
bdcpc 发表于 2022-11-23 15:57
这个是php几的,为什么我提示
Parse error: syntax error, unexpected 'print' (T_PRINT), expecting ide ...

第16行中print改下名字,和php的冲突了,第220行记着一起改名
bdcpc 发表于 2022-11-23 15:57
这个是php几的,为什么我提示
Parse error: syntax error, unexpected 'print' (T_PRINT), expecting identifier (T_STRING) in /www/wwwroot/pic.2kyb.com/tcs/index.php on line 16
ybss 发表于 2022-11-18 09:46
ybss 发表于 2022-11-18 09:50

装了环境 解决了
屏幕截图 2022-11-18 094941.png
cctv8239 发表于 2022-11-18 09:55
厉害,这应该是诺基亚工程师的水平了,楼主厉害
xiaoyangby 发表于 2022-11-18 09:56
看起来不错的样子哦
puz_zle 发表于 2022-11-18 10:38
优秀啊 这么一下
victor815 发表于 2022-11-18 10:57
wc,厉害啊,这样都能玩起来
CNSL 发表于 2022-11-18 11:01
学习了,我先试下
lingluanlive 发表于 2022-11-18 11:12
优秀啊,我试试
yuancl825765 发表于 2022-11-18 11:12
感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-20 11:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表