简体   繁体   中英

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. The other is a learning management system(LMS) written in PHP/HTML/CSS. 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.

I have been trying to figure out the method and have tried using exec() and 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. 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). 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM