PHP version of the snake game source code

source code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

<?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();

code description

Open and run without relying on any extensions
1. Copy the code into the text and change the suffix to php
2. Put it into the project or www, and access the php file on the browser
3. It can be run.

Guess you like

Origin blog.csdn.net/winkexin/article/details/131150239