在Python中使用街机库显示降雪
在本文中,我们将使用街机Python包显示降雪。 Arcade库是一个现代Python模块,广泛用于开发具有引人入胜的图形和声音的 2D 视频游戏。它是一个面向对象的库,可以像任何其他Python包一样安装。
循序渐进的方法:
- 本系列的第一步也是最重要的一步是在您的Python IDE 中导入Arcade库。还要导入random和math模块,因为在屏幕上的随机位置显示降雪。
Python3
# Import required modules
import random
import math
import arcade
Python3
# Adjust window attributes
Width = 800
Height = 600
Title = "SnowFall"
Python3
class snow_fall:
def __init__(self):
self.x = 0
self.y = 0
def reset_snow(self):
# Reset flake to random position above screen
self.y = random.randrange(Height, Height + 100)
self.x = random.randrange(Width)
Python3
class snowfall(arcade.Window):
def __init__(self, width, height, title):
# Calls "__init__" of parent class
# (arcade.Window) to setup screen
super().__init__(width, height, title)
def start_snowfall(self):
# Set up snowfall and initialize variables.
self.snowfall_list = []
for i in range(50):
# Create snowfall instance
snowfall = snow_fall()
# Randomly position snowfall
snowfall.x = random.randrange(Width)
snowfall.y = random.randrange(Height + 200)
# Set other variables for the snowfall
snowfall.size = random.randrange(8)
snowfall.speed = random.randrange(20, 40)
snowfall.angle = random.uniform(math.pi, math.pi * 2)
# Add snowflake to snowfall list
self.snowfall_list.append(snowfall)
Python3
# Set the background color
arcade.set_background_color(arcade.color.BLUE)
Python3
def on_draw(self):
# This command is necessary before drawing
arcade.start_render()
# Draw the current position of each snowfall
for snowfall in self.snowfall_list:
arcade.draw_circle_filled(snowfall.x, snowfall.y,
snowfall.size, arcade.color.WHITE)
def on_update(self, delta_time):
# Animate all the snowfall falling
for snowfall in self.snowfall_list:
snowfall.y -= snowfall.speed * delta_time
# Check if snowfall has fallen below screen
if snowfall.y < 0:
snowfall.reset_snow()
# Some math to make the snowfall move side to side
snowfall.x += snowfall.speed * \
math.cos(snowfall.angle) * delta_time
snowfall.angle += 1 * delta_time
Python3
# Driver Code
if __name__ == "__main__":
screen = snowfall(800, 600, "Snow")
screen.start_snowfall()
arcade.run()
Python3
# Import required modules
import random
import math
import arcade
# Adjust window attributes
Width = 800
Height = 600
Title = "SnowFall"
class snow_fall:
def __init__(self):
self.x = 0
self.y = 0
def reset_snow(self):
# Reset flake to random position above screen
self.y = random.randrange(Height, Height + 100)
self.x = random.randrange(Width)
class snowfall(arcade.Window):
def __init__(self, width, height, title):
# Calls "__init__" of parent class
# (arcade.Window) to setup screen
super().__init__(width, height, title)
def start_snowfall(self):
# Set up snowfall and initialize variables.
self.snowfall_list = []
for i in range(50):
# Create snowfall instance
snowfall = snow_fall()
# Randomly position snowfall
snowfall.x = random.randrange(Width)
snowfall.y = random.randrange(Height + 200)
# Set other variables for the snowfall
snowfall.size = random.randrange(8)
snowfall.speed = random.randrange(20, 40)
snowfall.angle = random.uniform(math.pi, math.pi * 2)
# Add snowflake to snowfall list
self.snowfall_list.append(snowfall)
# Set the background color
arcade.set_background_color(arcade.color.BLUE)
def on_draw(self):
# This command is necessary before drawing
arcade.start_render()
# Draw the current position of each snowfall
for snowfall in self.snowfall_list:
arcade.draw_circle_filled(snowfall.x, snowfall.y,
snowfall.size, arcade.color.WHITE)
def on_update(self, delta_time):
# Animate all the snowfall falling
for snowfall in self.snowfall_list:
snowfall.y -= snowfall.speed * delta_time
# Check if snowfall has fallen below screen
if snowfall.y < 0:
snowfall.reset_snow()
# Some math to make the snowfall move side to side
snowfall.x += snowfall.speed * \
math.cos(snowfall.angle) * delta_time
snowfall.angle += 1 * delta_time
# Driver Code
if __name__ == "__main__":
screen = snowfall(800, 600, "Snow")
screen.start_snowfall()
arcade.run()
- 指定输出屏幕的屏幕宽度、高度和标题的值。
蟒蛇3
# Adjust window attributes
Width = 800
Height = 600
Title = "SnowFall"
- 定义一个由__init__方法和一个将降雪重置到屏幕上方随机位置的函数组成的类snow_fall 。
蟒蛇3
class snow_fall:
def __init__(self):
self.x = 0
self.y = 0
def reset_snow(self):
# Reset flake to random position above screen
self.y = random.randrange(Height, Height + 100)
self.x = random.randrange(Width)
- 现在,定义另一个类snowfall()和函数来随机定位降雪。
蟒蛇3
class snowfall(arcade.Window):
def __init__(self, width, height, title):
# Calls "__init__" of parent class
# (arcade.Window) to setup screen
super().__init__(width, height, title)
def start_snowfall(self):
# Set up snowfall and initialize variables.
self.snowfall_list = []
for i in range(50):
# Create snowfall instance
snowfall = snow_fall()
# Randomly position snowfall
snowfall.x = random.randrange(Width)
snowfall.y = random.randrange(Height + 200)
# Set other variables for the snowfall
snowfall.size = random.randrange(8)
snowfall.speed = random.randrange(20, 40)
snowfall.angle = random.uniform(math.pi, math.pi * 2)
# Add snowflake to snowfall list
self.snowfall_list.append(snowfall)
- 设置背景颜色。
蟒蛇3
# Set the background color
arcade.set_background_color(arcade.color.BLUE)
- 现在,定义用于动画降雪和显示每个降雪的当前位置的函数。
蟒蛇3
def on_draw(self):
# This command is necessary before drawing
arcade.start_render()
# Draw the current position of each snowfall
for snowfall in self.snowfall_list:
arcade.draw_circle_filled(snowfall.x, snowfall.y,
snowfall.size, arcade.color.WHITE)
def on_update(self, delta_time):
# Animate all the snowfall falling
for snowfall in self.snowfall_list:
snowfall.y -= snowfall.speed * delta_time
# Check if snowfall has fallen below screen
if snowfall.y < 0:
snowfall.reset_snow()
# Some math to make the snowfall move side to side
snowfall.x += snowfall.speed * \
math.cos(snowfall.angle) * delta_time
snowfall.angle += 1 * delta_time
- 最后一步是创建一个街机对象并调用所需的方法。
蟒蛇3
# Driver Code
if __name__ == "__main__":
screen = snowfall(800, 600, "Snow")
screen.start_snowfall()
arcade.run()
以下是基于上述方法的完整程序:
蟒蛇3
# Import required modules
import random
import math
import arcade
# Adjust window attributes
Width = 800
Height = 600
Title = "SnowFall"
class snow_fall:
def __init__(self):
self.x = 0
self.y = 0
def reset_snow(self):
# Reset flake to random position above screen
self.y = random.randrange(Height, Height + 100)
self.x = random.randrange(Width)
class snowfall(arcade.Window):
def __init__(self, width, height, title):
# Calls "__init__" of parent class
# (arcade.Window) to setup screen
super().__init__(width, height, title)
def start_snowfall(self):
# Set up snowfall and initialize variables.
self.snowfall_list = []
for i in range(50):
# Create snowfall instance
snowfall = snow_fall()
# Randomly position snowfall
snowfall.x = random.randrange(Width)
snowfall.y = random.randrange(Height + 200)
# Set other variables for the snowfall
snowfall.size = random.randrange(8)
snowfall.speed = random.randrange(20, 40)
snowfall.angle = random.uniform(math.pi, math.pi * 2)
# Add snowflake to snowfall list
self.snowfall_list.append(snowfall)
# Set the background color
arcade.set_background_color(arcade.color.BLUE)
def on_draw(self):
# This command is necessary before drawing
arcade.start_render()
# Draw the current position of each snowfall
for snowfall in self.snowfall_list:
arcade.draw_circle_filled(snowfall.x, snowfall.y,
snowfall.size, arcade.color.WHITE)
def on_update(self, delta_time):
# Animate all the snowfall falling
for snowfall in self.snowfall_list:
snowfall.y -= snowfall.speed * delta_time
# Check if snowfall has fallen below screen
if snowfall.y < 0:
snowfall.reset_snow()
# Some math to make the snowfall move side to side
snowfall.x += snowfall.speed * \
math.cos(snowfall.angle) * delta_time
snowfall.angle += 1 * delta_time
# Driver Code
if __name__ == "__main__":
screen = snowfall(800, 600, "Snow")
screen.start_snowfall()
arcade.run()
输出: