{"id":8587,"date":"2021-02-15T06:46:30","date_gmt":"2021-02-15T05:46:30","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=8587"},"modified":"2021-02-27T07:38:55","modified_gmt":"2021-02-27T06:38:55","slug":"particles-snow-with-pygame","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/particles-snow-with-pygame\/","title":{"rendered":"Particles&#8230; snow (with pygame)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Make some particles to simulate snow with pygame. Particles is an effect to see some visual effects made with tiny images. This is an &#8220;adaptation&#8221; of the code bt <strong><a href=\"https:\/\/github.com\/Rabbid76\/PyGameExamplesAndAnswers\">Rabbid76<\/a><\/strong>.<\/p>\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=\"\"># pygame.math module\n# https:\/\/www.pygame.org\/docs\/ref\/math.html\n#\n# Pygame swap text with another text\n# https:\/\/stackoverflow.com\/questions\/60944070\/pygame-swap-text-with-another-text\/60953697#60953697\n#\n# GitHub - PyGameExamplesAndAnswers - Draw 2D - Particles\n# https:\/\/github.com\/Rabbid76\/PyGameExamplesAndAnswers\/blob\/master\/documentation\/pygame\/pygame_2D.md\n\nimport pygame\nimport random\n\npygame.init()\nwindow = pygame.display.set_mode((300, 300))\nclock = pygame.time.Clock()\n\n# list of particles\nparticles = []\n\ndef starting_point():\n\t\" to make a flame like particles [150, 20] # flame, so that all circles with start at the same point\"\n\treturn random.randint(0, 300)\n\t# return 150\n\n# the surface with the snow as a background\nsnow = pygame.image.load(\"snow.png\")\n\ndef particles_generator():\n    \"This makes the circles move rfrom random start position at the top until bottome and the disappear\"\n\n    # Every particle starts at a random horizontal position at the top\n    particles.append([\n    \t[starting_point(), 0],\n    \t[random.randint(0, 20) \/ 10 - 1, 2],\n    \trandom.randint(4, 6)])\n\n    # Every particle  moves... if particles[2] (the radius) is >= than 0 it is removed\n    for particle in particles[:]:\n        particle[0][0] += particle[1][0]\n        particle[0][1] += particle[1][1]\n        particle[2] -= 0.005 # how fast circles shrinks\n        particle[1][1] += 0.01 # circles speed\n        if particle[2] &lt;= 0:\n            particles.remove(particle)\n\n    # draws a circle on the screen white, at x y corrds and with a ray of particle[2]\n    for particle in particles:\n        pygame.draw.circle(\n        \twindow,\n        \t(255, 255, 255),\n        \t(\n        \t\tround(particle[0][0]),\n        \t\tround(particle[0][1])),\n        \t\tround(particle[2]))\n\ndef main():\n    run = True\n    while run:\n        clock.tick(60)\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n        # it adds particles until they are removed\n        # if the size - [2] - is smaller than 0\n        window.blit(snow, (0,0))\n        particles_generator()\n        pygame.display.flip()\n\n    pygame.quit()\n    exit()\n\nmain()<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-52.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-52.png\" alt=\"\" class=\"wp-image-8695\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-52.png 300w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/image-52-150x150.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption>A background: call it snow.png and put it in the folder of the script<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"640\" style=\"aspect-ratio: 640 \/ 640;\" width=\"640\" controls src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2021\/02\/pygame-window-2021-02-15-06-41-07.mp4\"><\/video><figcaption>Video with particles simulating snow<\/figcaption><\/figure>\n\n\n","protected":false},"excerpt":{"rendered":"Make some particles to simulate snow with pygame. Particles is an effect to see some visual effects made with tiny images. This is \n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/particles-snow-with-pygame\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":8590,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[789,194],"class_list":["post-8587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","tag-particles","tag-pygame"],"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\/8587","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=8587"}],"version-history":[{"count":7,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/8587\/revisions"}],"predecessor-version":[{"id":8698,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/8587\/revisions\/8698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/8590"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=8587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=8587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=8587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}