{"id":12367,"date":"2022-12-17T17:15:42","date_gmt":"2022-12-17T16:15:42","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=12367"},"modified":"2022-12-17T17:15:45","modified_gmt":"2022-12-17T16:15:45","slug":"make-a-sound-when-balls-collides-in-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/make-a-sound-when-balls-collides-in-python\/","title":{"rendered":"Make a sound when balls collides in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Third part of this game made with Python. In previous post we&#8217;ve added a second ball. Now we add a sound when the balls collides.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/12\/ball.ogg\"><\/audio><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pygame\n\n# Initialize Pygame and set up the display\npygame.init()\nscreen = pygame.display.set_mode((640, 480))\n\n# Initialize the mixer and load a sound file\npygame.mixer.init()\ncollision_sound = pygame.mixer.Sound('ball.ogg')\nclock = pygame.time.Clock()\n# Set up the first ball's initial position and velocity\nx1 = 320\ny1 = 240\nvx1 = 1\nvy1 = 1\n\n# Set up the second ball's initial position and velocity\nx2 = 100\ny2 = 100\nvx2 = 2\nvy2 = 2\n\n# Set the balls' radius\nradius = 20\n\n# Run the game loop\nrunning = True\nwhile running:\n    # Handle user input\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            running = False\n        elif event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_LEFT:\n                vx1 = -2\n            elif event.key == pygame.K_RIGHT:\n                vx1 = 2\n            elif event.key == pygame.K_UP:\n                vy1 = -2\n            elif event.key == pygame.K_DOWN:\n                vy1 = 2\n        elif event.type == pygame.KEYUP:\n            vx1 = 0\n            vy1 = 0\n    \n    # Update the balls' positions\n    x1 += vx1\n    y1 += vy1\n    x2 += vx2\n    y2 += vy2\n    \n    # Check if the balls are out of bounds and reverse their velocities if necessary\n    if x1 - radius &lt; 0 or x1 + radius > 640:\n        vx1 = -vx1\n    if y1 - radius &lt; 0 or y1 + radius > 480:\n        vy1 = -vy1\n    if x2 - radius &lt; 0 or x2 + radius > 640:\n        vx2 = -vx2\n    if y2 - radius &lt; 0 or y2 + radius > 480:\n        vy2 = -vy2\n    \n    # Check if the balls collide and reverse their velocities if necessary\n    dx = x1 - x2\n    dy = y1 - y2\n    distance = (dx ** 2 + dy ** 2) ** 0.5\n    if distance &lt; 2 * radius:\n        vx1, vx2 = vx2, vx1\n        vy1, vy2 = vy2, vy1\n        collision_sound.play()  # Play the collision sound\n    \n    # Draw the balls and update the display\n    screen.fill((0, 0, 0))\n    pygame.draw.circle(screen, (255, 255, 255), (x1, y1), radius)\n    pygame.draw.circle(screen, (255, 255, 0), (x2, y2), radius)\n    pygame.display.flip()\n    clock.tick(60)\n<\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"480\" style=\"aspect-ratio: 640 \/ 480;\" width=\"640\" controls src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2022\/12\/pygame-window-2022-12-17-17-12-38.mp4\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"Third part of this game made with Python. In previous post we&#8217;ve added a second ball. Now we add a sound when the \n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/make-a-sound-when-balls-collides-in-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":12370,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[191],"tags":[194,4],"class_list":["post-12367","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pygame","tag-pygame","tag-python"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"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\/12367","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=12367"}],"version-history":[{"count":1,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/12367\/revisions"}],"predecessor-version":[{"id":12371,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/12367\/revisions\/12371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/12370"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=12367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=12367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=12367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}