Embedding Python Game Into HTML Using Skulpt

2.4k Views Asked by At

I have written a game in Python using the PyGame library that I am trying to embed into an HTML page to allow me to play in a web browser.

I am attempting to do this using the JavaScript library Skulpt. I have attached a test script below that successfully outputs the print statement below.

skulpt.html

<html>
<head>
    <script src="assets/skulpt/skulpt.js" type="text/javascript"></script> 
</head>
<body> 
    <textarea id="pythonCode">
        print "I am python."
    </textarea><br /> 
    <pre id="output"></pre> 

    <script type="text/javascript">
        function outf(text) { 
            var mypre = document.getElementById("output"); 
            mypre.innerHTML = mypre.innerHTML + text; 
        } 

        var code = document.getElementById("pythonCode").value; 
        Sk.configure({output:outf}); 
        eval(Sk.importMainWithBody("<stdin>",false,code)); 
    </script>
</body>
</html>

Output of skulpt.html:

enter image description here

The issue that I am having is that when I use my game code instead of the simple print statement shown above it produces the error seen below;

enter image description here

I have included all relevant images to my web servers' directory at the correct path. I am unsure of why this error is being produced. Any help would be much appreciated, thanks!

Also, here is the attached Python game code (and a live demo of the error):

http://nicolasward.com/portfolio/skulpt.html

1

There are 1 best solutions below

6
On

You have a lot of indentation on line 1 -> remember, in python, indentation always matters. Take away all those spaces/tabs on the first line and it should run.