{"id":1033,"date":"2018-08-10T12:43:22","date_gmt":"2018-08-10T10:43:22","guid":{"rendered":"http:\/\/pythonprogramming.altervista.org\/?p=1033"},"modified":"2018-08-27T07:54:05","modified_gmt":"2018-08-27T05:54:05","slug":"ipad-and-pythonista-make-a-game-2","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/ipad-and-pythonista-make-a-game-2\/","title":{"rendered":"Pythonista and Ipad: make a game 2"},"content":{"rendered":"<h2>Add sprites to the backgroud<\/h2>\n<p><a href=\"http:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1052\" src=\"http:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/py.png\" alt=\"\" width=\"327\" height=\"102\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/py.png 327w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/py-320x100.png 320w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><\/a><\/p>\n<p>Your dreams can come alive with pythonista: create a game with ipad and in python. What is better to use the best language of the world on the best tablet? You can take advantage of the ipad input and all the feature that it has with python. So, let&#8217;s see how easy it is.<\/p>\n<p>In the previous post we&#8217;ve seen how to start a game with pythonista on IOS, importing the scene module and starting the Game class, that inherit from the Scene class. With the function setup we defined the color and with the run function we started the Game class.<br \/>\nNow ew create the ground istance of Node, then with create a SpriteNode istance called tile that we attach it to the ground for all the width of the screen, incrementing the x position of the tile of 64 that is the width of each sprite.<\/p>\n<pre class=\"lang:default decode:true\"># coding: utf-8\r\n\r\nfrom scene import *\r\n\r\nclass Game(Scene):\r\n\tdef setup(self):\r\n\t\tself.background_color = \"#fc9525\"\r\n\r\n\t\tground = Node(parent=self)\r\n\t\tx = 0\r\n\t\twhile x &lt;= self.size.x +64:\r\n\t\t\ttile = SpriteNode('plf:Tile_Bush', position=(x,30))\r\n\t\t\tground.add_child(tile)\r\n\t\t\tx += 64\r\n        self.player = SpriteNode('plf:AlienBeige_front')\r\n        self.player.anchor_point = (0.5,0)\r\n        self.add_child(self.player)\r\n\t\t\r\nrun(Game())<\/pre>\n<p>At the end of the while cycle, we create another SpriteNode that is the player istance of the class containing the image of the sprite that you control. The we place it on the screen at the middle, with the anchor point to the feet of the sprite and we add it to the screen to be visible with add_child.<\/p>\n<p>The anchor is by default to the center of the sprite. If (0,0) is the bottom-left, (1,1) is the top-right, so (0.5, 0) is bottom, middle.<\/p>\n<p>The result is better explained by the following images:<\/p>\n<figure id=\"attachment_1046\" aria-describedby=\"caption-attachment-1046\" style=\"width: 695px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/002.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1046\" src=\"http:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/002.png\" alt=\"pythonista games screen\" width=\"695\" height=\"521\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/002.png 695w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2018\/08\/002-320x240.png 320w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/a><figcaption id=\"caption-attachment-1046\" class=\"wp-caption-text\">pythonista games screen<\/figcaption><\/figure>\n<svg width=\"60\" height=\"60\">\r\n  <rect width=\"60\" height=\"60\" style=\"fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)\" \/>\r\n<circle cx=\"0\" cy=\"60\" r=\"4\" stroke=\"red\" stroke-width=\"3\" fill=\"red\" \/>\r\n<\/svg> (0, 0)\r\n<svg width=\"60\" height=\"60\">\r\n  <rect width=\"60\" height=\"60\" style=\"fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)\" \/>\r\n<circle cx=\"60\" cy=\"0\" r=\"4\" stroke=\"red\" stroke-width=\"3\" fill=\"red\" \/>\r\n<\/svg> (1,1)\r\n<svg width=\"60\" height=\"60\">\r\n  <rect width=\"60\" height=\"60\" style=\"fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)\" \/>\r\n<circle cx=\"30\" cy=\"60\" r=\"4\" stroke=\"red\" stroke-width=\"3\" fill=\"red\" \/>\r\n<\/svg> (0.5, 0)\n<p>The result being the following:<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"400\" src=\"https:\/\/www.youtube.com\/embed\/XMXTOP9_c3A\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<h2>Video in italian (video in italiano)<\/h2>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"400\" src=\"https:\/\/www.youtube.com\/embed\/TiaGPvD7wTc\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n\t<script>\r\n\t\tvar title = \"Game on Ipad with Pythonista\";\r\n\t\tvar links = [\r\n\t\t\t\/\/ ['final part of the address'],['title']\r\n\t\t\t[\"https:\/\/pythonprogramming.altervista.org\/ipad-and-python-make-a-game\/\",\"The background of the scene\"],\r\n\t\t\t[\"https:\/\/pythonprogramming.altervista.org\/ipad-and-pythonista-make-a-game-2\",\"Draw the sprites\"],\r\n\t\t\t[\"https:\/\/pythonprogramming.altervista.org\/create-a-game-with-an-ipad-and-python-video-1-and-2\/\",\"Pythonista game p-1\"],\r\n\t\t\t[\"https:\/\/pythonprogramming.altervista.org\/pythonista-game-on-ipad-part-2\/\",\"Pythonista game p.2\"],\r\n\t\t\t]\r\n\t\t];\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>\r\n\r\n\t<h3>External links<\/h3>\r\n\t<a href=\"https:\/\/youtu.be\/kBnt7kiZf1o\">Video about pythonista in classroom<\/a>\r\n\r\n\n","protected":false},"excerpt":{"rendered":"Adding the ground and the player sprite to the game.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/ipad-and-pythonista-make-a-game-2\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":1046,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,154,230],"tags":[158,225,4,226],"class_list":["post-1033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-games","category-pythonista-ipad","tag-games","tag-ipad","tag-python","tag-pythonista"],"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\/1033","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=1033"}],"version-history":[{"count":12,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1033\/revisions\/1212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/1046"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}