{"id":1912,"date":"2019-04-28T07:06:37","date_gmt":"2019-04-28T05:06:37","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=1912"},"modified":"2019-04-28T12:01:34","modified_gmt":"2019-04-28T10:01:34","slug":"pythonista-game-15-meteor-hits","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/pythonista-game-15-meteor-hits\/","title":{"rendered":"Pythonista game 15 &#8211; Meteor hits"},"content":{"rendered":"<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/04\/015.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1914\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/04\/015.png\" alt=\"\" width=\"571\" height=\"322\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/04\/015.png 571w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/04\/015-320x180.png 320w\" sizes=\"auto, (max-width: 571px) 100vw, 571px\" \/><\/a>We&#8217;re almost done.<\/p>\n<p><iframe loading=\"lazy\" title=\"Pythonissta game 15 - Meteor&acirc;&#128;&#153;s hit\" width=\"747\" height=\"560\" src=\"https:\/\/www.youtube.com\/embed\/xpevESx3wkc?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<pre class=\"lang:default decode:true \"># coding: utf-8\r\n\r\nfrom scene import *\r\nimport sound\r\nimport random\r\n\r\n\r\n# standing and walking texture\r\nstanding = Texture('plf:AlienGreen_front')\r\nwalking = [Texture('plf:AlienGreen_walk1'), Texture('plf:AlienGreen_walk2')]\r\nhit_texture = Texture('plf:AlienGreen_duck')\r\n\r\nclass Coin(SpriteNode):\r\n\tdef __init__(self, **kwargs):\r\n\t\tSpriteNode.__init__(self, 'plf:Item_CoinGold', **kwargs)\r\n\r\nclass Meteor(SpriteNode):\r\n\tdef __init__(self, **kwargs):\r\n\t\timg = random.choice(['spc:MeteorBrownBig1','spc:MeteorBrownBig2'])\r\n\t\tSpriteNode.__init__(self, img, **kwargs)\r\n\r\n\r\nclass Game(Scene):\r\n\t\r\n\tdef setup(self):\r\n\t\tself.background_color = \"#3b21b5\"\r\n\t\tground = Node(parent=self)\r\n\t\tx = 0\r\n\t\twhile x &lt;= self.size.w +64:\r\n\t\t\ttile = SpriteNode('plf:Ground_Dirt', position=(x,10))\r\n\t\t\tground.add_child(tile)\r\n\t\t\tx += 64\r\n\t\t# create player sprite\r\n\t\tself.player = SpriteNode('plf:AlienGreen_front')\r\n\t\t# position\r\n\t\tself.player.position = (self.size.w\/2, 41)\r\n\t\t# anchor\r\n\t\tself.player.anchor_point = (0.5, 0)\r\n\t\t# attach the player the ground (make it visible)\r\n\t\tground.add_child(self.player)\r\n\t\t# Add score\r\n\t\tself.label_score = LabelNode('0', ('futura',40), parent=self)\r\n\t\tself.label_score.position = (self.size.w \/ 2, self.size.h -40)\r\n\t\tself.list_of_items = []\r\n\t\tself.new_game()\r\n\t\t\r\n\tdef new_game(self):\r\n\t\tfor item in self.list_of_items:\r\n\t\t\titem.remove_from_parent()\r\n\t\tself.score = 0\r\n\t\tself.walk_state = -1\r\n\t\tself.list_of_items = []\r\n\t\tself.game_over = False\r\n\t\tself.label_score.text = '0'\r\n\t\tself.player.position = self.size.w \/ 2, 41\r\n\t\tself.speed = 1\r\n\t\tself.player.texture = standing\r\n\t\t\r\n\tdef update(self):\r\n\t\tif self.game_over:\r\n\t\t\treturn\r\n\t\tif random.random() &lt; .05:\r\n\t\t\tself.spawn_items()\r\n\t\tself.update_player()\r\n\t\t #look for collisions\r\n\t\tself.collisions_with_items()\r\n\t\t\r\n\tdef update_player(self):\r\n\t\tg = gravity()\r\n\t\t\r\n\t\tself.player.x_scale = ((g.x &gt; 0) - (g.x &lt; 0))\r\n\t\t\r\n\t\tif abs(g.x) &gt; 0.05:\r\n\t\t\tspeed = g.x * 50\r\n\t\t\tx = self.player.position.x\r\n\t\t\tx = max(0, min(self.size.w, x + speed))\r\n\t\t\tself.player.position = x, 41\r\n\t\t\tstep = int(self.player.position.x \/ 40) % 2\r\n\t\t\tif step != self.walk_state:\r\n\t\t\t\tself.player.texture = walking[step]\r\n\t\t\t\tsound.play_effect('rpg:Footstep00', 0.05, 1.0 + .5 * g.x)\r\n\t\t\t\tself.walk_state = step\r\n\t\t\t\t\r\n\t\telse:\r\n\t\t\tself.player.texture = standing\r\n\t\t\tself.walk_state = -1\r\n\t\t\r\n\tdef spawn_items(self):\r\n\t\tif random.random() &lt; 0.3:\r\n\t\t\tmeteor = Meteor(parent=self)\r\n\t\t\tmeteor.position = random.uniform(20, self.size.w), self.size.h\r\n\t\t\tduration = random.uniform(2,4)\r\n\t\t\tmeteor.run_action(\r\n\t\t\t\tAction.sequence(\r\n\t\t\t\t\tAction.move_to(0,-1000, duration),\r\n\t\t\t\t\tAction.remove()\r\n\t\t\t\t\t)\r\n\t\t\t\t)\r\n\t\t\t# list of coins falling\r\n\t\t\tself.list_of_items.append(meteor)\r\n\t\t\r\n\t\telse:\r\n\t\t\tcoin = Coin(parent=self)\r\n\t\t\tcoin.position = random.uniform(20, self.size.w), self.size.h\r\n\t\t\tduration = random.uniform(2,4)\r\n\t\t\tcoin.run_action(\r\n\t\t\t\tAction.sequence(\r\n\t\t\t\t\tAction.move_by(0,-1000, duration),\r\n\t\t\t\t\tAction.remove()\r\n\t\t\t\t\t)\r\n\t\t\t\t)\r\n\t\t\t# list of coins falling\r\n\t\t\tself.list_of_items.append(coin)\r\n\t\t\t\r\n\t\t\r\n\t# Coins collision intersection\r\n\tdef collisions_with_items(self):\r\n\t\tp_box = Rect(self.player.position.x-20, 32, 40, 65)\r\n\t\tfor item in self.list_of_items:\r\n\t\t\tif item.frame.intersects(p_box):\r\n\t\t\t\tif isinstance(item, Coin):\r\n\t\t\t\t\tsound.play_effect('arcade:Coin_2')\r\n\t\t\t\t\titem.remove_from_parent()\r\n\t\t\t\t\tself.list_of_items.remove(item)\r\n\t\t\t\t\tself.score += 10\r\n\t\t\t\t\tself.label_score.text = str(self.score)\r\n\t\t\t\telse:\r\n\t\t\t\t\tself.player_hit()\r\n\t\t\t\t\t\r\n\t\r\n\tdef player_hit(self):\r\n\t\tself.game_over = True\r\n\t\tsound.play_effect('arcade:Explosion_6')\r\n\t\tself.player.texture = hit_texture\r\n\t\tself.player.run_action(Action.move_by(0,-150))\r\n\t\tself.run_action(Action.sequence(Action.wait(2*self.speed), Action.call(self.new_game)))\r\n\t\t\r\n\t\t\r\n\t\r\n\t\r\n\t\t\t\r\n\t\t\t\t\r\n\t\r\n\tdef touch_began(self, touch):\r\n\t\t# load the sprite\r\n\t\tlaser = SpriteNode('plf:LaserPurpleDot',\r\n\t\t\tposition=self.player.position,\r\n\t\t\tz_position= -1,\r\n\t\t\tparent=self)\r\n\t\t\t\r\n\t\t # moving the laser\r\n\t\tlaser.run_action(Action.sequence(\r\n\t\t\tAction.move_by(0,1000),\r\n\t\t\tAction.remove()))\r\n\t\t\r\n\t\t# sound to the laser\r\n\t\t\r\n\t\tsound.play_effect('arcade:Laser_1')\r\n\t\t\r\n\t\t\r\n\r\n\t\t\r\n\t\t\t\t\t\t\r\nrun(Game(), PORTRAIT)\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"This is what happens when meteors hit the player in the just one before last video tutorial about making games on the ipad with the incredible python language and pytonista app\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/pythonista-game-15-meteor-hits\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":1914,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1912","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"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\/1912","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=1912"}],"version-history":[{"count":3,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1912\/revisions"}],"predecessor-version":[{"id":1920,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1912\/revisions\/1920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/1914"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}