Difference between revisions of "Pygame In Coder Merlin"

From Coder Merlin
m (Editorial review and minor corrections)
 
Line 1: Line 1:
== How to run Pygame in Coder Merlin ==
== How to Run Pygame in Coder Merlin ==


=== Prerequisites ===
=== Prerequisites ===
Line 7: Line 7:
* Learn how to use Google
* Learn how to use Google


=== Good things to know ===
=== Good Things to Know ===
* How to use VaporShell (outlined below)
* How to use VaporShell (outlined below)
* How to use Pygbag (how we're going to display in the web, outlined below)
* How to use Pygbag (how we are going to display in the Web, outlined below)


== Steps ==
== Steps ==


=== Step One ===
=== Step One: Prepare ===
Have your game/project ready. Know that ''the main loop has to be asynchronous'' to work so that [https://pypi.org/project/pygbag/ Pygbag] can work properly.
Have your game/project ready. Know that ''the main loop has to be asynchronous'' to work so that [https://pypi.org/project/pygbag/ Pygbag] can work properly.


Line 64: Line 64:
</syntaxhighlight>
</syntaxhighlight>


Some important things to note are that <syntaxhighlight lang="python" inline>main()</syntaxhighlight> is asynchronous and <syntaxhighlight lang="python" inline>asyncio.sleep(0)</syntaxhighlight> is used. The reason why this is used is so that the function "gives up control" and can run other things like the webpage. See more about that [https://stackoverflow.com/questions/65973786/why-does-asyncio-sleep0-make-my-code-faster here].
Some important things to note are that <syntaxhighlight lang="python" inline>main()</syntaxhighlight> is asynchronous and <syntaxhighlight lang="python" inline>asyncio.sleep(0)</syntaxhighlight> is used. This is used so that the function "gives up control" and can run other things like the Web page. For more information, see the [https://stackoverflow.com/questions/65973786/why-does-asyncio-sleep0-make-my-code-faster Why does asyncio.sleep(0) make my code faster?] page on Stackoverflow.


=== Step Two. Get Vapor Ready ===
=== Step Two: Get Vapor Ready ===
We're using Vapor Client because it allows for more than [https://rocketmedia.com/blog/static-vs-dynamic-websites#:~:text=Contact%20us%20today!-,What%20is%20a%20static%20website%3F,only%20the%20actual%20homepage%20file. static HTML] and bare javascript. Therefore, we need to download it.
We are using Vapor Client because it allows for more than [https://rocketmedia.com/blog/static-vs-dynamic-websites#:~:text=Contact%20us%20today!-,What%20is%20a%20static%20website%3F,only%20the%20actual%20homepage%20file. static HTML] and bare javascript. Therefore, we need to download it.


First, git clone [https://www.codermerlin.academy/wiki/index.php/Vapor VaporClient].
First, git clone [https://www.codermerlin.academy/wiki/index.php/Vapor VaporClient].
Line 76: Line 76:
{{ConsoleLine|nikola-tesla@codermerlin:~/VaporShell$ |run}}
{{ConsoleLine|nikola-tesla@codermerlin:~/VaporShell$ |run}}


The server will then answer at https://www.codermerlin.com/vapor/(user-name)/
The server will then answer at https://www.codermerlin.com/vapor/(user-name)/.


(The above is from the [https://www.codermerlin.academy/wiki/index.php/Vapor Vapor Wiki])
(The above is from the [https://www.codermerlin.academy/wiki/index.php/Vapor Vapor Wiki])


After running it, take note of the port it says it's running on. This is important because any website that you run on that port will load in VaporClient on the "answer" URL. Now that you have VaporClient ready, let's move to step three!
After running it, note the port it says it is running on. This is important because any website you run on that port will load in VaporClient on the "answer" URL. Now that you have VaporClient ready, let's move on to step three!


=== Step Three, install pygbag===
=== Step Three: Install Pygbag===
You can do this via:
You can do this via:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 88: Line 88:
</syntaxhighlight>
</syntaxhighlight>


=== Step Four, run pygbag ===
=== Step Four: Run Pygbag ===
Run the below in the directory of your game/project.  
Run the below in the directory of your game/project.  
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 95: Line 95:
''Note: If python is not found, it might be labeled as "python3"''
''Note: If python is not found, it might be labeled as "python3"''


It will create the folder {{Pathname|Build}} that contains a zip of your project in web form. This zip should be called {{Pathname|web.zip}}.
It creates the folder {{Pathname|Build}} that contains a zip of your project in web form. This zip should be called {{Pathname|web.zip}}.
This isn't too important though because that build will already be unzipped in the {{Pathname|web}} directory. <syntaxhighlight lang="bash" inline>cd web</syntaxhighlight> into that directory and then run the web server with:
This is not too important though because that build will already be unzipped in the {{Pathname|web}} directory. <syntaxhighlight lang="bash" inline>cd web</syntaxhighlight> into that directory and then run the web server with:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
python -m http.server [port_number_here] &
python -m http.server [port_number_here] &
Line 107: Line 107:
This allows us to display the game at the vapor URL: https://www.codermerlin.academy/vapor/firstname-lastname/
This allows us to display the game at the vapor URL: https://www.codermerlin.academy/vapor/firstname-lastname/
{{Caution|
{{Caution|
Make sure to have the slash at the end of the URL
Make sure to have the slash at the end of the URL.
}}
}}


The <syntaxhighlight lang="bash" inline>&</syntaxhighlight> allows us to run the server in the background. It's not required but it's good if you want to work on other things while the server is running. To stop the server from running do:
The <syntaxhighlight lang="bash" inline>&</syntaxhighlight> allows us to run the server in the background. It is not required but it is good if you want to work on other things while the server is running. To stop the server from running do:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
jobs
jobs
Line 123: Line 123:
OSError: [Errno 98] Address already in use   
OSError: [Errno 98] Address already in use   
</syntaxhighlight>
</syntaxhighlight>
Then you already have a job running at that address. You need to kill it like killing the server.
This means you already have a job running at that address. You need to kill it like killing the server.


== Sources ==
== Sources ==

Latest revision as of 11:49, 21 April 2024

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

How to Run Pygame in Coder Merlin[edit]

Prerequisites[edit]

  • Learn the basics of Python
  • Basics of Linux Commands (most important)
  • Learn the basics of Pygame
  • Learn how to use Google

Good Things to Know[edit]

  • How to use VaporShell (outlined below)
  • How to use Pygbag (how we are going to display in the Web, outlined below)

Steps[edit]

Step One: Prepare[edit]

Have your game/project ready. Know that the main loop has to be asynchronous to work so that Pygbag can work properly.

Here's an example of a basic Pygbag project from the Pygbag pypi

import asyncio

# Try to declare all your globals at once to facilitate compilation later.
COUNT_DOWN = 3

# Do init here
# Load any assets right now to avoid lag at runtime or network errors.


async def main():
    global COUNT_DOWN

    # avoid this kind declaration, prefer the way above
    COUNT_DOWN = 3

    while True:

        # Do your rendering here, note that it's NOT an infinite loop,
        # and it is fired only when VSYNC occurs
        # Usually 1/60 or more times per seconds on desktop
        # could be less on some mobile devices

        print(f"""

            Hello[{COUNT_DOWN}] from Python

""")
        # pygame.display.update() should go right next line

        await asyncio.sleep(0)  # Very important, and keep it 0

        if not COUNT_DOWN:
            return

        COUNT_DOWN = COUNT_DOWN - 1

# This is the program entry point:
asyncio.run(main())

# Do not add anything from here, especially sys.exit/pygame.quit
# asyncio.run is non-blocking on pygame-wasm and code would be executed
# right before program start main()

Some important things to note are that main() is asynchronous and asyncio.sleep(0) is used. This is used so that the function "gives up control" and can run other things like the Web page. For more information, see the Why does asyncio.sleep(0) make my code faster? page on Stackoverflow.

Step Two: Get Vapor Ready[edit]

We are using Vapor Client because it allows for more than static HTML and bare javascript. Therefore, we need to download it.

First, git clone VaporClient.

nikola-tesla@codermerlin:~$  git clone https://github.com/TheCoderMerlin/VaporShell

Then build and run it:

nikola-tesla@codermerlin:~/VaporShell$  build

nikola-tesla@codermerlin:~/VaporShell$  run

The server will then answer at https://www.codermerlin.com/vapor/(user-name)/.

(The above is from the Vapor Wiki)

After running it, note the port it says it is running on. This is important because any website you run on that port will load in VaporClient on the "answer" URL. Now that you have VaporClient ready, let's move on to step three!

Step Three: Install Pygbag[edit]

You can do this via:

pip install pygbag

Step Four: Run Pygbag[edit]

Run the below in the directory of your game/project.

python -m pygbag --archive .

Note: If python is not found, it might be labeled as "python3"

It creates the folder Build that contains a zip of your project in web form. This zip should be called web.zip. This is not too important though because that build will already be unzipped in the web directory. cd web into that directory and then run the web server with:

python -m http.server [port_number_here] &

Example:

python -m http.server 11937 &

This allows us to display the game at the vapor URL: https://www.codermerlin.academy/vapor/firstname-lastname/

CautionWarnIcon.png

Make sure to have the slash at the end of the URL.

The & allows us to run the server in the background. It is not required but it is good if you want to work on other things while the server is running. To stop the server from running do:

jobs

And note which job number the server is. Then do:

kill %(job number)

If it gives an error similar to:

OSError: [Errno 98] Address already in use

This means you already have a job running at that address. You need to kill it like killing the server.

Sources[edit]

  1. pygbag
  2. Pygame Web
  3. Why is asyncio.sleep(0) used?