Created
March 18, 2015 11:04
-
-
Save zither/126df7399657603fd4ed to your computer and use it in GitHub Desktop.
terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$terminal = new Terminal(); | |
$terminal->saveTTY(); | |
$terminal->initTTY(); | |
$player = new Player($terminal, array("x" =>10, "y" => 27)); | |
$valid = false; | |
while ($char = fread(STDIN, 1)) { | |
if ($char == "[") { | |
$valid = true; | |
continue; | |
} | |
if (in_array($char, ["q", "Q"])) { | |
$terminal->restoreTTY(); | |
exit; | |
} | |
if ($valid) { | |
switch ($char) { | |
case "A" : | |
$player->moveUp(); | |
break; | |
case "B" : | |
$player->moveDown(); | |
break; | |
case "C" : | |
$player->moveRight(); | |
break; | |
case "D" : | |
$player->moveLeft(); | |
break; | |
} | |
$valid = false; | |
} | |
} | |
class Terminal | |
{ | |
public function initTTY() | |
{ | |
$this->clearScreen(); | |
$this->resetCursor(); | |
//http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html#AEN92 | |
system("stty -icanon -echo"); | |
$this->cursorOff(); | |
} | |
public function saveTTY() | |
{ | |
// 保存当前 tty 设置 | |
$this->tty = shell_exec("stty -g"); | |
} | |
public function restoreTTY() | |
{ | |
$this->clearScreen(); | |
$this->resetCursor(); | |
system(sprintf("stty %s", $this->tty)); | |
$this->cursorOn(); | |
} | |
public function cursorOn() | |
{ | |
system("setterm -cursor on"); | |
} | |
public function cursorOff() | |
{ | |
system("setterm -cursor off"); | |
} | |
public function setCursor($x, $y) | |
{ | |
$this->command(sprintf("[%s;%sf", $x, $y)); | |
} | |
public function command($command) | |
{ | |
printf("\033%s", $command); | |
} | |
public function clearScreen() | |
{ | |
$this->command("[2J"); | |
} | |
public function resetCursor() | |
{ | |
$this->command("[H"); | |
} | |
} | |
class Player | |
{ | |
protected $terminal; | |
protected $pos = array("x" => 0,"y" => 0); | |
public function __construct($terminal, $pos = null) | |
{ | |
$this->terminal = $terminal; | |
if (!is_null($pos)) { | |
$this->pos = $pos; | |
} | |
$this->move(); | |
$this->setPlayer(); | |
} | |
public function moveUp() | |
{ | |
$this->clearPos(); | |
$this->pos["x"] -= 1; | |
$this->move(); | |
$this->setPlayer(); | |
} | |
public function moveDown() | |
{ | |
$this->clearPos(); | |
$this->pos["x"] += 1; | |
$this->move(); | |
$this->setPlayer(); | |
} | |
public function moveRight() | |
{ | |
$this->clearPos(); | |
$this->pos["y"] += 1; | |
$this->move(); | |
$this->setPlayer(); | |
} | |
public function moveLeft() | |
{ | |
$this->clearPos(); | |
$this->pos["y"] -= 1; | |
$this->move(); | |
$this->setPlayer(); | |
} | |
public function move() | |
{ | |
$this->terminal->setCursor($this->pos["x"], $this->pos["y"]); | |
} | |
public function clearPos() | |
{ | |
$this->move(); | |
echo " "; | |
} | |
public function setPlayer() | |
{ | |
echo "@"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice