[英]Execute Python game in Pygame from PHP script
Here is a little synopsis. 这里有个小提要。 I have two chunks of code with me. 我有两个代码块。 One is a game written exclusively in python using pygame. 一个是使用pygame专门用python编写的游戏。 The other is a learning management system(LMS) written in PHP/HTML/CSS. 另一个是用PHP / HTML / CSS编写的学习管理系统(LMS)。 The user can click the play game option in LMS, and thus control would be transferred to the python program and would return back after end of game. 用户可以单击LMS中的“玩游戏”选项,从而将控制权转移到python程序,并在游戏结束后返回。
I have been trying to figure out the method and have tried using exec()
and shell_exec()
. 我一直在尝试找出方法,并尝试使用exec()
和shell_exec()
。 They happen to run a simple python script but they do no initiate the pygame engine window which consists of the actual game. 他们碰巧运行了一个简单的python脚本,但没有启动由实际游戏组成的pygame引擎窗口。 I would appreciate if someone who has had experience in this regard can point me in the right direction. 如果有人在这方面有经验,可以为我指出正确的方向,我将不胜感激。
WWW server on locahost is like WWW server on remote computer - it lives in own environment (for security reasons). locahost上的WWW服务器就像远程计算机上的WWW服务器一样-位于安全的环境中。 It can run programs but only in its own environment which has no access to the screen (especially to screen used by other user - like human). 它可以运行程序,但只能在自己的环境中运行,该环境无法访问屏幕(尤其是其他用户(例如人类)使用的屏幕)。 Server doesn't need screen to work. 服务器不需要屏幕即可工作。
EDIT: 编辑:
For local server on linux you can try to set DISPLAY=:0.0
to give server access to local screen 对于Linux上的本地服务器,您可以尝试将DISPLAY=:0.0
设置为允许服务器访问本地屏幕
exec('export DISPLAY=:0.0; python game.py');
To get error messages you could use: 要获取错误消息,您可以使用:
$descriptors = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
$process = proc_open('export DISPLAY=:0.0; python game.py', $descriptors, $pipes, dirname(__FILE__), null);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
echo "stdout: ";
var_dump($stdout);
echo "stderr: ";
var_dump($stderr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.