Mu Way ︰向前?!

如果一個人學會了派生 python 語言,能夠說他掌握了打開程式大門的鑰匙嗎?若是以派生豐富的程式庫而言,這又何嘗不是的呢!

舉例來說,你不止可以用『 PyGame 』︰

Games with PyGame Zero

PyGameZero is a beginner friendly wrapper around the powerful PyGame library for writing video games using Python. It’s extraordinarily easy to write an entertaining game in only a few lines of Python code with PyGameZero. For example, the side scrolling chasing game shown in the video below is only 400 lines of Python which were written using Mu.

Mu’s PyGameZero mode provides five buttons but we need only describe two:

The “Play” button knows how to start your game using the PyGameZero framework and, once clicked, turns into a “Stop” button. When your game starts any textual input or output will be contained in the pane added between the text editor and Mu’s footer. Most importantly, a new game window created by PyGameZero will appear on your desktop. This makes it very easy to see, test and check your game is working how you expected it to!

PyGameZero play

The remaining “Images”, “Fonts”, “Sounds” and “Music” buttons all work in exactly the same way. Your game will need to include assets such as images (for sprites, backgrounds and other things in your game), fonts (so you can write text to the screen in your game), sounds (for sound effects to signal when something has happened in your game) and music (to create an atmosphere for your game). Clicking on each of the four buttons opens the directory into which you should put such game assets (simply drag and drop things into them using your operating system’s file manager).

PyGameZero images

Once your game assets are in the right place, PyGameZero makes it extraordinarily easy to use them in your game!

Now go read the PyGameZero documentation and make something fun!

 

寫小遊戲︰

/paperchase

A simple game set in both the “paper” and “real” world. Created for PyWeek 25.

Paper Chase

A very simple game written for Pyweek 25 by Nicholas H.Tollervey (instructions for installing and running the game are at the end of this document). Read my developer diary for the event online. All game assets were created by the author except for those listed and acknowledged in the various sources.txt files in the asset directories. The game itself is covered by the MIT license (see the LICENSE file in the root of the source repository) and all the non-original game media / assets are covered by “open” licences (see the relevant sources.txt files mentioned above for more details).

The theme for this iteration of PyWeek was “Two Worlds”. In this instance the two worlds are the intergalactic rivals from the worlds of the red biros vs the blue biros (there’s a HHGG reference in there somewhere). 😉

As the blurb from the start of the game says:

“The intergalactic war between the red and blue factions of the biro universe has reached its climax. Each world has sent a stick-figure champion to race in the “Paper chase” for ultimate victory and to decide which colour biro pen teachers should use when marking work.”

This is a two-player side-scrolling chase / avoidance game. You win by getting 200 steps ahead of the other player.

As the figures run across the paper they encounter various obstacles which, if hit, will slow the player down. If you snaffle a Python power-up (to import antigravity) you’ll be able to gain speed by flying (here’s the obvious XKCD reference).

The keys are:

Action Red Blue
Up Up W
Down Down S
Kick Right D
Jump Enter Space

As the game progresses the obstacles come more often and the pace of the game speeds up.

The wider point of the game is educational:

  • I’m using Dan Pope’s amazing PyGame Zero and writing the code using the Mu editor.
  • I aim to create a game that is simple enough that it could be used as a “template” or launch pad for teachers and beginner programmers who want to write their own games.
  • The game will be a single logic file and single data file.
  • The game assets will be generated in as simple a manner as possible (i.e. something kids could easily copy).
  • Post-PyWeek I’m going to write this all up into an educational resource.

Installing / Running the Game

Paper Chase only works with Python 3!

I’ve tested the game works on Windows 10 (64bit), OSX (High Sierra) and Ubuntu Linux (17.10).

Installation of the dependencies is relatively simple:

pip3 install pgzero</pre> <span style="color: #808080;">Installing PyGame Zero will grab PyGame and other associated requirements.</span>  <span style="color: #808080;"><strong>NOTE: As of time of writing, PyGame Zero has a misconfiguration issue in OSX which means that keyboard capture doesn't work if you're running the game within a virtualenv. In this case, just use the system Python 3 and ``pip install pgzero`` globally (sorry).</strong></span>  <span style="color: #808080;">Simply change directories into the root of the project (i.e. the directory in which you'll find this file) and type:</span> <pre class="lang:default decode:true "> pgzrun paper_chase.py

…then grab a friend for the battle of the biroid champions.

Most of all, I hope you have as much fun playing it as I did writing it!

 

※ 註︰

pi@raspberrypi:~ sudo pip3 install pgzero Collecting pgzero   Downloading https://files.pythonhosted.org/packages/48/e5/e5f14292373cb5fc7539aa01307b184c1e3c954d68945d8c44778669dd82/pgzero-1.2-py3-none-any.whl (69kB)     100% |████████████████████████████████| 71kB 583kB/s  Requirement already satisfied: pygame<2.0,>=1.9.2 in /usr/local/lib/python3.5/dist-packages (from pgzero) Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from pgzero) Installing collected packages: pgzero Successfully installed pgzero-1.2 pi@raspberrypi:~ git clone https://github.com/ntoll/paperchase
Cloning into 'paperchase'...
remote: Enumerating objects: 128, done.
remote: Total 128 (delta 0), reused 0 (delta 0), pack-reused 128
Receiving objects: 100% (128/128), 23.35 MiB | 4.81 MiB/s, done.
Resolving deltas: 100% (19/19), done.
pi@raspberrypi:~ cd paperchase/ pi@raspberrypi:~/paperchase ls
docs  fonts  gamedata.py  images  LICENSE  music  paper_chase.py  README.rst
pi@raspberrypi:~/paperchase pgzrun paper_chase.py pygame 1.9.4.post1 Hello from the pygame community. https://www.pygame.org/contribute.html pi@raspberrypi:~/paperchase 

 

 

進一步,你還可以借著

Gym is a toolkit for developing and comparing reinforcement learning algorithms. It supports teaching agents everything from walking to playing games like Pong or Pinball.

 

定義『遊戲環境』,創作『學習過關』之『演算法』哩◎

強化學習

 強化學習英語:Reinforcement learning,簡稱RL)是機器學習中的一個領域,強調如何基於環境而行動,以取得最大化的預期利益 。其靈感來源於心理學中的行為主義理論,即有機體如何在環境給予的獎勵或懲罰的刺激下,逐步形成對刺激的預期,產生能獲得最大利益的習慣性行為。這個方法具有普適性,因此在其他許多領域都有研究,例如博弈論控制論運籌學資訊理論、仿真優化、多主體系統學習群體智能統計學以及遺傳算法。在運籌學和控制理論研究的語境下,強化學習被稱作「近似動態規劃」(approximate dynamic programming,ADP)。在最優控制理論中也有研究這個問題,雖然大部分的研究是關於最優解的存在和特性 ,並非是學習或者近似方面。在經濟學博弈論中,強化學習被用來解釋在有限理性的條件下如何出現平衡。在機器學習問題中,環境通常被規範為馬可夫決策過程(MDP),所以許多強化學習算法在這種情況下使用動態規劃技巧。傳統的技術和強化學習算法的主要區別是,後者不需要關於MDP的知識,而且針對無法找到確切方法的大規模MDP。

強化學習和標準的監督式學習之間的區別在於,它並不需要出現正確的輸入/輸出對,也不需要精確校正次優化的行為。強化學習更加專注於在線規劃,需要在探索(在未知的領域)和遵從(現有知識 )之間找到平衡。強化學習中的「探索-遵從」的交換,在多臂老虎機問題和有限MDP中研究得最多。