{"id":19431,"date":"2022-11-06T17:15:22","date_gmt":"2022-11-06T11:45:22","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/"},"modified":"2022-11-06T17:15:22","modified_gmt":"2022-11-06T11:45:22","slug":"solved-collisions-arent-registering-with-python-turtles","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/","title":{"rendered":"[Solved] Collisions aren&#8217;t registering with Python Turtles"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-50514807\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"50514807\" data-parentid=\"50510385\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I think your primary issue is you&#8217;re updating the bullet between every enemy update instead of once for all the the enemy updates.  This creates a skew between the visual and what&#8217;s really happening.  Also, you don&#8217;t need to define the Pythagorean formula, turtles already know it!  Below is my rework of your code to address the above and rework the the coding style and efficiency a bit:<\/p>\n<pre><code>from turtle import Turtle, Screen\nfrom random import randint\n\nNUMBER_OF_ENEMIES = 5\nENEMY_JUMP = 40\n\nBULLET_SPEED = 20\nPLAYER_SPEED = 15\n\nSAFETY_DISTANCE = 15\n\nGALLERY_WIDTH, GALLERY_HEIGHT = 560, 550\nGALLERY_BORDER = 80\n\n# player movement\ndef move_left():\n    x = player.xcor() - PLAYER_SPEED\n\n    if x &lt; -GALLERY_WIDTH \/ 2:\n        x = -GALLERY_WIDTH \/ 2\n\n    player.setx(x)\n\ndef move_right():\n    x = player.xcor() + PLAYER_SPEED\n\n    if x &gt; GALLERY_WIDTH \/ 2:\n        x = GALLERY_WIDTH \/ 2\n\n    player.setx(x)\n\ndef fire_bullet():\n    global bulletstate\n\n    if bulletstate == 'ready':\n        bulletstate=\"fire\"\n        bullet.setposition(player.position())\n        bullet.forward(BULLET_SPEED \/ 2)\n        bullet.showturtle()\n\ndef isCollision(t1, t2):\n    return t1.distance(t2) &lt; SAFETY_DISTANCE\n\ndef move():\n    global enemy_speed, bulletstate\n\n    screen.tracer(False)\n\n    for enemy in enemies:\n        x = enemy.xcor() + enemy_speed\n\n        enemy.setx(x)\n\n        if x &gt; GALLERY_WIDTH \/ 2:\n            for e in enemies:\n                y = e.ycor() - ENEMY_JUMP\n                e.sety(y)\n\n            enemy_speed *= -1\n\n        elif x &lt; -GALLERY_WIDTH \/ 2:\n            for e in enemies:\n                y = e.ycor() - ENEMY_JUMP\n                e.sety(y)\n\n            enemy_speed *= -1\n\n        if isCollision(player, enemy):\n            player.hideturtle()\n            enemy.hideturtle()\n            print(\"Get To The Doctors! You're Riddled!\")\n            screen.update()\n            return\n\n        if isCollision(bullet, enemy):\n            # reset the bullet\n            bullet.hideturtle()\n            bullet.setposition(0, -GALLERY_HEIGHT)\n            bulletstate=\"ready\"\n\n            # reset the enemy\n            enemy.hideturtle()\n            x = randint(GALLERY_BORDER - GALLERY_WIDTH \/ 2, GALLERY_WIDTH \/ 2 - GALLERY_BORDER)\n            y = randint(GALLERY_HEIGHT \/ 2 - 175, GALLERY_HEIGHT \/ 2 - 25)\n            enemy.setposition(x, y)\n            enemy.showturtle()\n\n    if bulletstate == 'fire':\n        bullet.forward(BULLET_SPEED)\n\n        # check to see if the bullets has gone to the top\n        if bullet.ycor() &gt; GALLERY_HEIGHT \/ 2:\n            bullet.hideturtle()\n            bulletstate=\"ready\"\n\n    screen.tracer(True)\n\n    screen.ontimer(move, 25)\n\nscreen = Screen()\nscreen.setup(GALLERY_WIDTH + GALLERY_BORDER, GALLERY_HEIGHT + GALLERY_BORDER)\nscreen.bgcolor('black')\n\nplayer = Turtle('turtle', visible=False)\nplayer.speed('fastest')\nplayer.color('red')\nplayer.penup()\nplayer.setheading(90)\nplayer.sety(-250)  # need to define this\nplayer.showturtle()\n\nenemies = []\n\nenemy_speed = 1\n\nfor _ in range(NUMBER_OF_ENEMIES):\n    enemy = Turtle('circle', visible=False)\n    enemy.speed('fastest')\n    enemy.color('green')\n    enemy.penup()\n\n    x = randint(GALLERY_BORDER - GALLERY_WIDTH \/ 2, GALLERY_WIDTH \/ 2 - GALLERY_BORDER)\n    y = randint(GALLERY_HEIGHT \/ 2 - 175, GALLERY_HEIGHT \/ 2 - 25)  # define these!\n    enemy.setposition(x, y)\n    enemy.showturtle()\n\n    enemies.append(enemy)\n\n# create the player's spunk shots\nbullet = Turtle('triangle', visible=False)\nbullet.speed('fastest')\nbullet.shapesize(0.5)\nbullet.color('white')\nbullet.penup()\nbullet.setheading(90)\n\n# define bullet state\n# ready - ready to fire\n# fire - bullet is firing\nbulletstate=\"ready\"\n\nscreen.onkey(move_left, 'Left')\nscreen.onkey(move_right, 'Right')\nscreen.onkey(fire_bullet, 'space')\n\nscreen.listen()\n\nmove()\n\nscreen.mainloop()\n<\/code><\/pre>\n<p>I guessed at the missing pieces.  See how this plays for you.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Collisions aren&#8217;t registering with Python Turtles <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I think your primary issue is you&#8217;re updating the bullet between every enemy update instead of once for all the the enemy updates. This creates a skew between the visual and what&#8217;s really happening. Also, you don&#8217;t need to define the Pythagorean formula, turtles already know it! Below is my rework of your code &#8230; <a title=\"[Solved] Collisions aren&#8217;t registering with Python Turtles\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\" aria-label=\"More on [Solved] Collisions aren&#8217;t registering with Python Turtles\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[3781,349,1862,1790],"class_list":["post-19431","post","type-post","status-publish","format-standard","hentry","category-solved","tag-collision","tag-python","tag-python-3-6","tag-turtle-graphics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Collisions aren&#039;t registering with Python Turtles - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Collisions aren&#039;t registering with Python Turtles - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I think your primary issue is you&#8217;re updating the bullet between every enemy update instead of once for all the the enemy updates. This creates a skew between the visual and what&#8217;s really happening. Also, you don&#8217;t need to define the Pythagorean formula, turtles already know it! Below is my rework of your code ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-06T11:45:22+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Collisions aren&#8217;t registering with Python Turtles\",\"datePublished\":\"2022-11-06T11:45:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\"},\"wordCount\":101,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"collision\",\"python\",\"python-3.6\",\"turtle-graphics\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\",\"name\":\"[Solved] Collisions aren't registering with Python Turtles - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-06T11:45:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Collisions aren&#8217;t registering with Python Turtles\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Collisions aren't registering with Python Turtles - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Collisions aren't registering with Python Turtles - JassWeb","og_description":"[ad_1] I think your primary issue is you&#8217;re updating the bullet between every enemy update instead of once for all the the enemy updates. This creates a skew between the visual and what&#8217;s really happening. Also, you don&#8217;t need to define the Pythagorean formula, turtles already know it! Below is my rework of your code ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/","og_site_name":"JassWeb","article_published_time":"2022-11-06T11:45:22+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Collisions aren&#8217;t registering with Python Turtles","datePublished":"2022-11-06T11:45:22+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/"},"wordCount":101,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["collision","python","python-3.6","turtle-graphics"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/","url":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/","name":"[Solved] Collisions aren't registering with Python Turtles - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-06T11:45:22+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-collisions-arent-registering-with-python-turtles\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Collisions aren&#8217;t registering with Python Turtles"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=19431"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19431\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=19431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=19431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=19431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}