Up

流れる背景と回転する円盤

 

すこしずつ異なる画像を連続して提示すると動画が得られる。異なる画像を提示ごとに描画するのではなく、予め作成しておいて順次提示することもできる。図1の画像では、円盤は提示ごとに描いているが、後ろの背景は予め作成しておいて順次提示するという方法を取っている。後ろの背景は、上方に動いている。

 

図1

 

スクリプトはリスト1に示した。スクリプトファイルは圧縮ファイルf_bkgrnd_file.zipとした。

後ろの背景は、次のスクリプトで少しずつシフトしたものをサーフェスt_surfaceに描いてリストsurfacesに格納している。

 

surfaces = []          #    背景のサーフェスのリスト

for i in range(500):

    t_surface = pg.Surface((500, 500))

    s_i = i     #   背景を縦方向にiだけシフトする

    for j in range(500):

        v = s_i + j

        if v >= 500:

            v -= 500

        pg.draw.line(t_surface, v_color(3.0*v/500.0), (0,j), (500,j))

    surfaces.append(t_surface) 

 

この格納されたサーフェスsurfaces[int(v_bg)]を順次スクリーンscreenに映して(blit)用いている。

 

screen.blit(surfaces[int(v_bg)], (0, 0))

 

背景surfaces[int(v_bg)]をスクリーンscreenに映した(blit)後、次のコードで回転した円盤(楕円)をスクリーンscreenに描いている。

 

pg.draw.ellipse(screen, f_color(ck_c), pg.Rect((250-v_w,100),(2*v_w,300)))

 

楕円の描画後、次のスクリプトにより、前回の画像表示から所定の時間後、更新後のスクリーンを画面に表示している。

 

    while time.perf_counter() < ck_t:    #   画面更新の時間調整

        pass

   

    pg.display.flip()                    #    画面の更新

    ck_t = time.perf_counter() + 0.02    #    次の更新は0.02秒後

 

画面の更新後、次のスクリプトによりイベントのチェックを行っている。

 

 

for e in pg.event.get():             #    イベントの取得

    if e.type == pg.QUIT:            #    Windowのクローズボンタが押されたか

        ck = False

    elif e.type == pg.KEYDOWN:       #    押されたキーがあるか

        if e.key == pg.K_c:          #    押されたキーはキーC

            ck_c *= -1

        elif e.key == pg.K_ESCAPE:   #    押されたキーはESCキーか

            ck = False             

 

キーCを押すと。円盤の色が変わる。ウィンドウの閉じるボタンのクリック、あるいはESCキーを押すとckFalseに設定されて、while文が終了する。

 

 

 

 

リスト1 スクリプトファイルは圧縮ファイルf_bkgrnd_file.zipとした。

 

import pygame as pg

import time

import math

 

 

def v_color(x):

    """

            0 < x < 3 を赤から緑、青の色に変換

    """

    if x < 0.0:

        return (0,0,0)

    elif x < 1.0:

        return (int(155*(1.0-x)+100), int(155*x+100), 100)

    elif x < 2.0:

        return (100, int(155*(2.0-x)+100), int(155*(x-1.0)+100))

    elif x <= 3.0:

        return (int(155*(x-2.0)+100), 100, int(155*(3.0-x)+100))

    else:

        return(255,255,255)

 

 

pg.init()              #   pygameの初期化

 

surfaces = []          #    背景のサーフェスのリスト

for i in range(500):

    t_surface = pg.Surface((500, 500))

    s_i = i     #   背景を縦方向にiだけシフトする

    for j in range(500):

        v = s_i + j

        if v >= 500:

            v -= 500

        pg.draw.line(t_surface, v_color(3.0*v/500.0), (0,j), (500,j))

    surfaces.append(t_surface) 

 

screen  = pg.display.set_mode((500, 500))        #   描画スクリーンの設定

pg.display.set_caption('Rotating Disc')

 

ck_t = 0.0       #   描画時間間隔の調整用

i_rot = 0        #   円盤の回転角

v_bg = 0         #   背景色のシフト量

ck_c = 1         #   円盤の色の選択用変数

ck = True        #   while文の制御用

while ck:

    screen.blit(surfaces[int(v_bg)], (0, 0))   #    背景色のパターンで塗り潰す

    v_bg += 3.0                         #    背景色のシフト量の更新

    if v_bg >= 500:

        v_bg -= 500

    v_w = 150 * math.cos(i_rot)         #   円盤の回転角

 

    def f_color(c):                     #   円盤色の選択用関数

        if c > 0:

            return (0,255,0)

        else:

            return (0,0,255)

 

    #    円盤の描画

    pg.draw.ellipse(screen, f_color(ck_c), pg.Rect((250-v_w,100),(2*v_w,300)))

           

    i_rot += math.pi/100     #   回転角の更新  

    if i_rot > math.pi/2:    #   回転角のチェック

        ck_c *= -1           #   色を変える

        i_rot -= math.pi     #   回転角の調整

       

    while time.perf_counter() < ck_t:    #   画面更新の時間調整

        pass

   

    pg.display.flip()                    #    画面の更新

    ck_t = time.perf_counter() + 0.02    #    次の更新は0.02秒後

 

    for e in pg.event.get():             #    イベントの取得

        if e.type == pg.QUIT:            #    Windowのクローズボンタが押されたか

            ck = False

        elif e.type == pg.KEYDOWN:       #    押されたキーがあるか

            if e.key == pg.K_c:          #    押されたキーはキーC

                ck_c *= -1

            elif e.key == pg.K_ESCAPE:   #    押されたキーはESCキーか

                ck = False             

 

 

pg.quit()     #   pygameの終了

 

 

 

Home