実行結果はこのようになります。
以下コード。
pygameでも出来ただろうけど、あえてPyClutterを使っています。
下敷きにしたのは、PyClutterサンプルのflower.py。
先に言い訳を書いておくと、コメントとか一切無いのは書き初めなので一気に書いたから、改行に見苦しい点があるのはpythontidyのせいです。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import clutter
from clutter import CairoTexture
import cairo
import gobject
import sys
import random
BACKGROUND_COLOR = (0xee, 0xee, 0xee, 0xff)
class Snow(CairoTexture):
colors = (
(0.6, 0.7, 0.9, 0.5),
(0.6, 0.7, 1.0, 0.5),
(0.7, 0.9, 0.9, 0.5),
(0.7, 0.7, 1.0, 0.5),
(0.8, 0.7, 0.9, 0.5),
(0.8, 0.9, 1.0, 0.5),
)
def __init__(
self,
x,
y,
translation_velocity,
rotation_velocity,
ground,
):
self.x = x
self.y = y
self.rot = 0
self.v = translation_velocity
self.rv = rotation_velocity
self.ground = ground
CairoTexture.__init__(self, 20, 20)
cr = self.cairo_create()
cr.move_to(0, 0)
cr.rectangle(2, 2, 16, 16)
random_color = Snow.colors[random.randint(0, 5)]
cr.set_source_rgba(*random_color)
cr.paint()
def tick(self, max_width, max_height):
if self.ground - 10 < self.get_y():
return
self.y += self.v
self.rot += self.rv
if self.y > max_height:
self.y = -self.get_height()
self.set_position(self.x, self.y)
self.set_rotation(clutter.Z_AXIS, self.rot, self.get_width()
/ 2, self.get_height() / 2, 0)
class Kakizome(CairoTexture):
def __init__(self):
self.stage = clutter.Stage()
self.stage.set_color(clutter.Color(*BACKGROUND_COLOR))
self.stage.set_size(640, 480)
self.stage.set_title('Kakizome 2010')
self.started = False
self.ground = self.stage.get_height()
self.label = self.create_label('Hello, New Year.')
self.label.set_depth(1.0)
self.stage.add(self.label)
self.snows = list()
self.timeline = clutter.Timeline(500)
self.timeline.set_loop(True)
self.stage.connect('key-press-event', self.on_key_press)
gobject.timeout_add(50, self.tick, self.snows,
self.get_width(), self.stage.get_height())
def create_label(self, message):
label_color = clutter.Color(*BACKGROUND_COLOR)
label = clutter.Text()
label.set_text(message)
label.set_font_name('Mono 32')
label.set_color(label_color)
label_x = (self.stage.get_width() - label.get_width()) - 50
label.set_position(label_x, (self.stage.get_height() / 3) * 2)
return label
def tick(self, snows, height, width):
for snow in snows:
snow.tick(height, width)
if self.ground < 0:
return True
if random.randint(0, 1000) % 125 == 0:
self.ground -= 5
self.fall_snow()
return True
def fall_snow(self):
snow = Snow(random.randint(-10, self.stage.get_width()), -20,
random.randint(1, 5), random.randint(3, 5),
self.ground)
self.stage.add(snow)
snow.set_position(snow.x, snow.y)
self.snows.append(snow)
def run(self):
self.stage.show_all()
self.timeline.start()
clutter.main()
def on_key_press(self, actor, event):
key = event.get_key_symbol()
if key == clutter.keysyms.q:
clutter.main_quit()
if __name__ == '__main__':
app = Kakizome()
app.run()
sys.exit(0)
0 件のコメント:
コメントを投稿