再び WPF/IronPythonで、 Snow (Gestalt Particles Pack)サンプルを実行する。
Gestalt ⇒ samples ⇒ Particles Pack ⇒ view 1http://www.visitmix.com/labs/gestalt/samples/
暑い夏に、清涼感を感じてください...
Let it grow, let it grow,
Let it blossom, let it flow.
In the sun, the rain, the snow,
Love is lovely, let it grow.
#
# snow.py
# from: particles.simple.python.html
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase') # for Point
from System.Windows.Markup import XamlReader
from System import Object, Random, Math
from System.Windows import Window, Application, Point
from System.Windows.Controls import Canvas, UserControl
from System.Windows.Media import CompositionTarget, Brushes
#from System.Windows.Media.Animation import Storyboard
xaml_SnowFlake_str = """
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="20" Height="20">
<Grid x:Name="LayoutRoot">
<Ellipse Fill="#FFFFFFFF"></Ellipse>
</Grid>
</UserControl>
"""
class Snowflake(UserControl):
def __init__(self, xaml, randomNumber):
self.xaml = xaml
self.randomNumber = randomNumber
self.x = 0
self.y = 0
self.xSpeed = 0
self.ySpeed = 0
self.radius = 0
self.scale = 0
self.alpha = 0
self.stageSize = Point()
self.Content = XamlReader.Parse(xaml)
# This method gets called many times a second and is responsible for moving your snowflake around
def MoveSnowFlake(sender, e):
self.x = self.x + self.xSpeed
self.y = self.y + self.ySpeed
Canvas.SetTop(self, self.y)
Canvas.SetLeft(self, Canvas.GetLeft(self) + self.radius * Math.Cos(self.x))
# Reset the position to go back to the top when the bottom boundary is reached
if (Canvas.GetTop(self) > self.stageSize.Y):
Canvas.SetTop(self, - self.ActualHeight - 10)
self.y = Canvas.GetTop(self)
CompositionTarget.Rendering += MoveSnowFlake # !
def SetInitialProperties(self, stageWidth, stageHeight):
self.xSpeed = self.randomNumber.NextDouble() / 20
self.ySpeed = .01 + self.randomNumber.NextDouble() * 2
self.radius = self.randomNumber.NextDouble()
self.scale = .01 + self.randomNumber.NextDouble() * 2
self.alpha = .1 + self.randomNumber.NextDouble()
# Setting initial position
Canvas.SetLeft(self, self.randomNumber.Next(stageWidth))
Canvas.SetTop(self, self.randomNumber.Next(stageHeight))
self.stageSize = Point(stageWidth, stageHeight)
self.y = Canvas.GetTop(self)
# Setting initial size and opacity
self.Content.Width = 5 * self.scale
self.Content.Height = 5 * self.scale
self.Content.Opacity = self.alpha
class SnowWindow(Window):
def __init__(self):
self.Title="Let It Grow"
self.Width = 600
self.Height= 300
self.Background = Brushes.Black
xaml_str="""
<Canvas x:Name="LayoutRoot"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF00202A" Offset="1"></GradientStop>
<GradientStop Color="#FF000000" Offset="0"></GradientStop>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>"""
canvas = XamlReader.Parse(xaml_str)
self.layoutRoot = canvas.FindName("LayoutRoot")
canvas.Width = 600
canvas.Height = 300
self.Content = canvas
randomNumber = Random()
for i in range(0, 200):
snowflake = Snowflake(xaml_SnowFlake_str, randomNumber)
# 600 and 300 is the width/height of the application
snowflake.SetInitialProperties(600, 300)
self.layoutRoot.Children.Add(snowflake)
if __name__ == "__main__":
win = SnowWindow()
Application().Run(win)
# snow.py
# from: particles.simple.python.html
#
import clr
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReference('WindowsBase') # for Point
from System.Windows.Markup import XamlReader
from System import Object, Random, Math
from System.Windows import Window, Application, Point
from System.Windows.Controls import Canvas, UserControl
from System.Windows.Media import CompositionTarget, Brushes
#from System.Windows.Media.Animation import Storyboard
xaml_SnowFlake_str = """
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="20" Height="20">
<Grid x:Name="LayoutRoot">
<Ellipse Fill="#FFFFFFFF"></Ellipse>
</Grid>
</UserControl>
"""
class Snowflake(UserControl):
def __init__(self, xaml, randomNumber):
self.xaml = xaml
self.randomNumber = randomNumber
self.x = 0
self.y = 0
self.xSpeed = 0
self.ySpeed = 0
self.radius = 0
self.scale = 0
self.alpha = 0
self.stageSize = Point()
self.Content = XamlReader.Parse(xaml)
# This method gets called many times a second and is responsible for moving your snowflake around
def MoveSnowFlake(sender, e):
self.x = self.x + self.xSpeed
self.y = self.y + self.ySpeed
Canvas.SetTop(self, self.y)
Canvas.SetLeft(self, Canvas.GetLeft(self) + self.radius * Math.Cos(self.x))
# Reset the position to go back to the top when the bottom boundary is reached
if (Canvas.GetTop(self) > self.stageSize.Y):
Canvas.SetTop(self, - self.ActualHeight - 10)
self.y = Canvas.GetTop(self)
CompositionTarget.Rendering += MoveSnowFlake # !
def SetInitialProperties(self, stageWidth, stageHeight):
self.xSpeed = self.randomNumber.NextDouble() / 20
self.ySpeed = .01 + self.randomNumber.NextDouble() * 2
self.radius = self.randomNumber.NextDouble()
self.scale = .01 + self.randomNumber.NextDouble() * 2
self.alpha = .1 + self.randomNumber.NextDouble()
# Setting initial position
Canvas.SetLeft(self, self.randomNumber.Next(stageWidth))
Canvas.SetTop(self, self.randomNumber.Next(stageHeight))
self.stageSize = Point(stageWidth, stageHeight)
self.y = Canvas.GetTop(self)
# Setting initial size and opacity
self.Content.Width = 5 * self.scale
self.Content.Height = 5 * self.scale
self.Content.Opacity = self.alpha
class SnowWindow(Window):
def __init__(self):
self.Title="Let It Grow"
self.Width = 600
self.Height= 300
self.Background = Brushes.Black
xaml_str="""
<Canvas x:Name="LayoutRoot"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF00202A" Offset="1"></GradientStop>
<GradientStop Color="#FF000000" Offset="0"></GradientStop>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>"""
canvas = XamlReader.Parse(xaml_str)
self.layoutRoot = canvas.FindName("LayoutRoot")
canvas.Width = 600
canvas.Height = 300
self.Content = canvas
randomNumber = Random()
for i in range(0, 200):
snowflake = Snowflake(xaml_SnowFlake_str, randomNumber)
# 600 and 300 is the width/height of the application
snowflake.SetInitialProperties(600, 300)
self.layoutRoot.Children.Add(snowflake)
if __name__ == "__main__":
win = SnowWindow()
Application().Run(win)
![]() | IronPythonの世界 (Windows Script Programming) |
荒井 省三 | |
ソフトバンク クリエイティブ |
![]() | エキスパートPythonプログラミング |
Tarek Ziade | |
アスキー・メディアワークス |
![]() | Pythonスタートブック |
辻 真吾 | |
技術評論社 |
と記載されていた。
そのページは、 kirupa.com ( Create Falling Snow by kirupa )
http://www.kirupa.com/developer/mx/snow.htm
参考にしてください。 雪のFlash アニメーションがあります。