{"id":775,"date":"2018-07-27T18:14:08","date_gmt":"2018-07-27T16:14:08","guid":{"rendered":"http:\/\/pythonprogramming.altervista.org\/?p=775"},"modified":"2019-05-04T06:00:35","modified_gmt":"2019-05-04T04:00:35","slug":"pygame-zero-another-example","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/pygame-zero-another-example\/","title":{"rendered":"Pygame zero: another example"},"content":{"rendered":"<h2>Pygame zero<\/h2>\n<figure id=\"attachment_792\" aria-describedby=\"caption-attachment-792\" style=\"width: 364px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-792\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/pope.png\" alt=\"pygame zero\" width=\"364\" height=\"185\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/pope.png 1073w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/pope-320x163.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/pope-768x391.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/pope-960x488.png 960w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><figcaption id=\"caption-attachment-792\" class=\"wp-caption-text\">pygame zero<\/figcaption><\/figure>\n<p>In a <a href=\"https:\/\/pythonprogramming.altervista.org\/python-making-a-video-game\/\">recent article about pygame<\/a><a href=\"https:\/\/pythonprogramming.altervista.org\/python-making-a-video-game\/\">zero<\/a> we&#8217;ve seen an example.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"zrjnU9JgZ0\"><p><a href=\"https:\/\/pythonprogramming.altervista.org\/python-making-a-video-game\/\">Python: let&#8217;s make a game (Pygame zero)<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Python: let&#8217;s make a game (Pygame zero)&#8221; &#8212; python programming\" src=\"https:\/\/pythonprogramming.altervista.org\/python-making-a-video-game\/embed\/#?secret=RQ1oDSVeHI#?secret=zrjnU9JgZ0\" data-secret=\"zrjnU9JgZ0\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>Now we will have a look at another example and, finally, to a Daniel Pope talk at the EuroPython conference of 2016.<\/p>\n<h2>A circle<\/h2>\n<p>Let&#8217;s draw a circle on the screen with pygame zero.<\/p>\n<pre class=\"lang:default decode:true \">\"\"\"\r\nCreate a circle\r\n\r\nThis creates a circle\r\n\r\n\"\"\"\r\n\r\n\r\nimport pgzrun\r\n\r\nWIDTH = 800\r\nHEIGHT = 600\r\n\r\ndef draw():\r\n    screen.clear()\r\n    screen.draw.circle((400, 300), 30, (255,255,255))\r\n\r\npgzrun.go()<\/pre>\n<p>&nbsp;<\/p>\n<h2>Interacting with the screen<\/h2>\n<p>Now the code will show how to interact with the screen, making it change from red to green.<\/p>\n<p>If you want something to happen when you\u00a0 click the mouse you just have to write\u00a0 a function like this:<\/p>\n<pre class=\"lang:default decode:true\">def on_mouse_down(button):\r\n    print(\"clicked\", button)<\/pre>\n<p>In this case, when you press the mouse button, it will print if it is the left mouse button or the right one (mouse.left).<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">\"\"\"Create a circle\r\nThis creates a circle in the middle of the screen\r\nif you click, background colors changes\r\n\"\"\"\r\nimport pgzrun\r\n\r\n\r\n# Two colors and the default background color\r\nRED = 150, 0, 0\r\nGREEN = 0, 128, 0\r\nbg = RED\r\n \r\n# screen size\r\nWIDTH = 800\r\nHEIGHT = 600\r\n \r\ndef draw(): # This draws a circle on the screen\r\n    screen.fill(bg)\r\n    screen.draw.circle((400, 300), 30, (255,255,255))\r\n \r\ndef on_mouse_down(): # when you click the mouse\r\n\tglobal bg\r\n\tbg = GREEN\r\n \r\ndef on_mouse_up(): # when you release the mouse\r\n\tglobal bg\r\n\tbg = RED\r\n \r\npgzrun.go()<\/pre>\n<p><iframe loading=\"lazy\" title=\"Make a game with Python and Pygame zero - part 1\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/dKG7ZwpUDZo?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h2>Daniel Pope: pygame zero presentation at EuroPython conference<\/h2>\n<p>For Daniel Pope Pygame zero was made to make easier to see sprite moving on the screen moving and interacting to fill the gap with Scratch that allows that immediately. To have a visual impact of your code is very important to teach computer science to the younger. Another positive aspect is that pgzero is written i python 3. Teacher wanted to teach just one language, Python 3. This module make it possible because it solves the problems with the installation of pygame for python 3.<\/p>\n<figure id=\"attachment_967\" aria-describedby=\"caption-attachment-967\" style=\"width: 1187px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-967\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popecode.png\" alt=\"pygame zero code example\" width=\"1187\" height=\"783\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popecode.png 1187w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popecode-320x211.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popecode-768x507.png 768w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popecode-960x633.png 960w\" sizes=\"auto, (max-width: 1187px) 100vw, 1187px\" \/><figcaption id=\"caption-attachment-967\" class=\"wp-caption-text\">pygame zero code example<\/figcaption><\/figure>\n<figure id=\"attachment_968\" aria-describedby=\"caption-attachment-968\" style=\"width: 481px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-968\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popegame.png\" alt=\"Pygame zero game\" width=\"481\" height=\"376\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popegame.png 481w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/07\/popegame-320x250.png 320w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><figcaption id=\"caption-attachment-968\" class=\"wp-caption-text\">Pygame zero game<\/figcaption><\/figure>\n<p><iframe loading=\"lazy\" title=\"Daniel Pope - Pygame Zero\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/Pzs-c-B3RiM?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n\t<script>\r\nvar title = \"Pygame zero\";\r\n\t\tvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/pong-v-1-0-pygame-example\/\",\"Pong v.1.0 (mouse) - Arkanoid beta\"],\r\n['https:\/\/pythonprogramming.altervista.org\/python-making-a-video-game\/', 'Example 1: intro'],\r\n['https:\/\/pythonprogramming.altervista.org\/pygame-zero-another-example\/', 'Example 2: a simple interaction'],\r\n['https:\/\/pythonprogramming.altervista.org\/pygame-zero-3-some-more-funny-games\/', 'Example 3: a simple game'],\r\n['https:\/\/pythonprogramming.altervista.org\/pygame-zero-4-catch-my-fall\/', 'Example 4: Alien Fall'],\r\n['https:\/\/pythonprogramming.altervista.org\/pygame-zero-5-improving-graphic-and-adding-sounds\/', 'Example 5: Alien Fall (2) - graphic and sounds'],\r\n['https:\/\/pythonprogramming.altervista.org\/pygame-zero-lander-by-daniel-pope\/', 'Lander: game 1'],\r\n['https:\/\/pythonprogramming.altervista.org\/pygame-zero-games-examples\/', 'Repository pygame and examples']\r\n];\r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n","protected":false},"excerpt":{"rendered":"pgzrun\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/pygame-zero-another-example\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":792,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,191,198],"tags":[194,192,4],"class_list":["post-775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-pygame","category-pygame-zero","tag-pygame","tag-pygame-zero","tag-python"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=775"}],"version-history":[{"count":8,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":1950,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/775\/revisions\/1950"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/792"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}