Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Rhodonea Curves and Maurer Rose in Python
Next article icon

Not everybody must have witnessed Snowfall personally but wait a minute, What if you can see the snowfall right on your screen by just a few lines of creativity and Programming.

 Before starting the topic, it is highly recommended revising the basics ofPygame. 

Steps for snowfall creation

1. Importing modules

First, we need to import the Pygame module by using the command.

import pygame
 

Also, along with Pygame, we will also need random module. Python has a built-in module that you can use to make random numbers just by importing random module.

import random
 

2. Initialize the game engine 

It simply means choose the colors you want to use. In programming World, Whatever you can think you can make. At the end of the article, you will find green snowfall on the white background.

Python3
# initializepygame.init()# chosen colours will be used# to display the outputWHITE=[255,255,255]GREEN=[0,255,0]

3.  Specify the size of the screen 

 It can be a new number depending upon the resolution of your system.

Python3
# specify the sizeSIZE=[400,400]screen=pygame.display.set_mode(SIZE)

4. Assign a name to your snowfall window screen

The name given can be seen on the left corner of the output window.

Python3
# caption for output windowpygame.display.set_caption("Programming World of GFG")

5. Create an empty array for your snowfall 

Python3
snowFall=[]

 
6. Looping to get snowfall positions

Make a loop and run to 50 times and add a snowfall in a random x,y position using random Module. 

Python3
foriinrange(50):x=random.randrange(0,400)y=random.randrange(0,400)snowFall.append([x,y])

7. Track time

Create an object to help track time

Python3
# object to track timeclock=pygame.time.Clock()

8. Set criteria for snowfall occurrence

 Snowfall should occur until the user presses the close button and for this insidewhileloop, use afor loop.

Python3
# loop till the close button is presseddone=Falsewhilenotdone:# User did somethingforeventinpygame.event.get():# If user clicked closeifevent.type==pygame.QUIT:# Flag that we are done so# we exit this loopdone=True

9. Set the screen background :

Python3
screen.fill(WHITE)

 
10. Process the snowfall

Now use a for loop to process each Snowfall in the list : 

Python3
foriinrange(len(snowFall)):

11. Draw the snowfall 

Python3
pygame.draw.circle(screen,GREEN,snowFall[i],2)

      

12. Adding movement

Python3
# Move the snowFall down one pixelsnowFall[i][1]+=1# If the snowFall has moved off the bottom of the screenifsnowFall[i][1]>400:# Reset it just above the topy=random.randrange(-50,-10)snowFall[i][1]=y# Give it a new x positionx=random.randrange(0,400)snowFall[i][0]=x# Go ahead and update the screen with what we've drawn.pygame.display.flip()clock.tick(20)pygame.quit()

And Yes, Green snowfall has started!!

Complete Program

Python3
importpygameimportrandompygame.init()WHITE=[255,255,255]GREEN=[0,255,0]SIZE=[400,400]screen=pygame.display.set_mode(SIZE)pygame.display.set_caption("Programming World of GFG")snowFall=[]foriinrange(50):x=random.randrange(0,400)y=random.randrange(0,400)snowFall.append([x,y])clock=pygame.time.Clock()done=Falsewhilenotdone:foreventinpygame.event.get():ifevent.type==pygame.QUIT:done=Truescreen.fill(WHITE)foriinrange(len(snowFall)):pygame.draw.circle(screen,GREEN,snowFall[i],2)snowFall[i][1]+=1ifsnowFall[i][1]>400:y=random.randrange(-50,-10)snowFall[i][1]=yx=random.randrange(0,400)snowFall[i][0]=xpygame.display.flip()clock.tick(20)pygame.quit()

Output


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp