
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Snow Effect Using the Arcade Module in Python
We've all wanted to add additional effects to our presentation or to a video. These effects help us in better present our product or helps increase user experience. And in this tutorial, you will learn how to implement the snow effect using the arcade module.
You can use this in your games to create a snow drizzle or a rain drop effect. You can even go ahead and set it up as a screen timeout effect.
That being said, let us get started!
Getting Started
Throughout this tutorial, we will be using the arcade module that helps users create game related functions with ease.
This module does not come pre-installed with Python. Which means we'll be using the pip package manager to install it.
To do this, use the below command.
pip3 install arcade
Once you have the arcade module installed, it's time we import it's various methods to our script.
To do this, we use the import keyword followed by the module name in Python.
import arcade
And that's it! You are all set to start working on the task at hand now.
Creating Snow Effect Using the Arcade Module
We need to import the random and the math modules along with the arcade module. You'll understand why soon.
import random import math import arcade
Now, first we have to define each snowflake. In this script, each snow flake is a dot on the screen. So we will be considering the x and y variable for its position on the output screen.
We also define a function reset_pos that we will be using to define the position of the snow flake once it reaches the end of the window.
class Snow: def __init__(self, height, width): self.x = 0 self.y = 0 def reset_pos(self, height, width): self.y = random.randrange(height, height + 100) self.x = random.randrange(width)
And that's it. We now have a defined snow ptutorial. Now, we need to start working on the output window the pattern by which the snow ptutorials fall.
Firstly, we define the essential functions to draw the output screen using the arcade module.
class MyGame(arcade.Window): def __init__(self, width, height): super().__init__(width, height) self.stream = None
Now, we define a start function that runs when we launch the script.
This function will be responsible for describing the movement of each of these ptutorials. The angle at which they fall, the speed at which they fall, the size of the ptutorial and it's position on the screen with respect to the output window.
And here is where the random import comes into play. We use the random module to regulate the random generation of these ptutorials.
def start(self, height, width): self.stream = [] for i in range(100): # Increase to increase number of snow ptutorials. snow = Snow(800, 600) snow.x = random.randrange(width) snow.y = random.randrange(height + 200) snow.size = random.randrange(2) snow.speed = random.randrange(20, 100) snow.angle = random.uniform(math.pi, math.pi * 2) self.stream.append(snow) # define bg colour arcade.set_background_color(arcade.color.BLACK)
Now, we need to use the default on_draw function that will generate the ptutorials on the output window.
def on_draw(self): # This is a default function. arcade.start_render() for snow in self.stream: arcade.draw_circle_filled(snow.x, snow.y, snow.size, arcade.color.WHITE)
We will also need to define the on_update function that will define the position of the ptutorial once it crosses the limit of the output window boundary on the y direction.
def on_update(self, delta_time): # This is a default function. for snow in self.stream: snow.y = snow.y - snow.speed * delta_time if snow.y < 0: snow.reset_pos(800, 600) snow.x = snow.x + snow.speed * math.cos(snow.angle) * delta_time snow.angle = snow.angle + delta_time
And that's it! You now have the complete script ready! Let us use the main function and define the borders for the window and run arcade functions.
if __name__ == "__main__": window = MyGame(800, 600) window.start(800, 600) arcade.run()
It will produce the following output ?
Complete Script
Here is the complete code ?
import random import math import arcade class Snow: def __init__(self, height, width): self.x = 0 self.y = 0 def reset_pos(self, height, width): self.y = random.randrange(height, height + 100) self.x = random.randrange(width) class MyGame(arcade.Window): def __init__(self, width, height): super().__init__(width, height) self.stream = None def start(self, height, width): self.stream = [] for i in range(100): snow = Snow(800, 600) snow.x = random.randrange(width) snow.y = random.randrange(height + 200) snow.size = random.randrange(2) snow.speed = random.randrange(20, 100) snow.angle = random.uniform(math.pi, math.pi * 2) self.stream.append(snow) arcade.set_background_color(arcade.color.BLACK) def on_draw(self): # This is a default function. arcade.start_render() for snow in self.stream: arcade.draw_circle_filled(snow.x, snow.y, snow.size, arcade.color.WHITE) def on_update(self, delta_time): # This is a default function. for snow in self.stream: snow.y = snow.y - snow.speed * delta_time if snow.y < 0: snow.reset_pos(800, 600) snow.x = snow.x + snow.speed * math.cos(snow.angle) * delta_time snow.angle = snow.angle + delta_time if __name__ == "__main__": window = MyGame(800, 600) window.start(800, 600) arcade.run()
Conclusion
You now know how to create snow tutorial effect on your GUI output screen using the Arcade module in Python. You can go ahead and experiment with it by changing some of the values.