時計を作ろう(連続モードのプログラム)

連続モードのプログラム

下記のプログラムにおいて、
冒頭の[A]の部分はプログラム開始時に1回だけ実行されます。
while ループ中の [B]の部分はプログラム終了まで繰り返し実行されます。

import pygame
import sys
from pygame.locals import *

#関数の定義はここに書く

#[A]この部分はプログラム開始時に1回だけ実行される
pygame.init() #モジュール初期化
screen = pygame.display.set_mode( (400,330) ) #ウィンドウ生成

t = 0
while True:

    #[B]この部分はプログラム終了まで繰り返し実行される
    screen.fill( (0,0,0) ) #画面を黒く塗りつぶす
    pygame.draw.rect(screen,(255,0,0),(20+t,70,10,180)) #赤い四角を描く
    t = t + 0.1
    pygame.display.update() #画面表示を更新

    for event in pygame.event.get():
        if event.type == QUIT: #終了処理
            pygame.quit() #モジュールの終了
            sys.exit() #プログラム終了

Bの部分に処理を描くことで、「プログラム終了まで動き続けるプログラム」を作成できます。
ここでは、現在時刻を表示する、アナログ時計を作成します。

画面の中心から放射状の線を描く関数

hline(m, r, width, length, color)
#画面中心から決められた方向に放射状の線を描く
#m 方向 (0~60, 上方向を0として、時計の「分」で方向を指定。
#r 線の開始半径(pixel単位)
#width 線の幅(分単位)
#length 線の長さ(pixel単位)
#color 色
#(screen はグローバル定数として定義)

def hline(m, r, width, length, color=(255,255,255)):
    #放射状の線を描く
    
    xcenter = screen.get_width()/2
    ycenter = screen.get_height()/2

    c1 = (xcenter+r*math.cos(math.radians(90.0-6.0*(m-width/2.0))), ycenter-r*math.sin(math.radians(90.0-6.0*(m-width/2.0))))
    c2 = (xcenter+r*math.cos(math.radians(90.0-6.0*(m+width/2.0))), ycenter-r*math.sin(math.radians(90.0+6.0*(m+width/2.0))))
    c3 = (xcenter+(r+length)*math.cos(math.radians(90.0-6.0*(m-width/2.0))), ycenter-(r+length)*math.sin(math.radians(90.0-6.0*(m-width/2.0))))
    c4 = (xcenter+(r+length)*math.cos(math.radians(90.0-6.0*(m+width/2.0))), ycenter-(r+length)*math.sin(math.radians(90.0+6.0*(m+width/2.0))))

    coords = [c1, c2, c4, c3]

    pygame.draw.polygon(screen, color, coords, 0)

特定の方向に文字を描く関数

moji(h, r, s, color)
#画面中心から決められた方向、距離に文字を描く
#h 方向(0~12, 上方向を0として、時計の「時」で方向を指定
#r 中心から文字の中心までの距離(pixel)
#s 描く文字
#color 色
#(screenはグローバル変数として定義)

def moji(h, r, s, color=(255, 255, 255)):
    #特定の方向と距離に文字を描く 

    font1 = pygame.font.SysFont("PlemolJP", 50) #フォントとサイズの指定 

    xcenter = screen.get_width()/2 #画面中心を取得 
    ycenter = screen.get_height()/2 

    text1 = font1.render(s, True, color) #文字をテキストオブジェクトに出力 
    fontsize = font1.size(str(h)) #テキストの大きさを取得 (テキストの幅, テキストの高さ) 
    #画面にテキストオブジェクトを描画 
    #指定した位置にテキストの中心が来るよう位置をずらす 
    screen.blit(text1,(xcenter+r*math.cos(math.radians(90.0-30.0*h))-fontsize[0]/2, ycenter-r*math.sin(math.radians(90.0-30.0*h))-fontsize[1]/2))

時計を動かす

import pygame #モジュールpygameの読み込み
import sys
import math
import datetime #時間を扱うモジュール
from pygame.locals import *

#ここに関数の定義を入れる

pygame.init() #pygameモジュールの初期化
screen = pygame.display.set_mode( (800,600) ) #ウィンドウの表示

#プログラムの終了処理
while True: #無限ループ
    screen.fill( (0,0,0) ) #画面を黒く塗りつぶす

    #文字盤を描く関数を呼び出す

    dt_now = datetime.datetime.now() #現在の時間の取得
    hline(dt_now.second, 0, 0.3, 200) #秒針を描く
    #分針を描く
    #時針を描く

    pygame.display.update() #画面を更新

    for event in pygame.event.get(): #pygameからくるイベントを順に取り出す
        if event.type == QUIT: #もしイベントがQUITなら
            pygame.image.save(screen,"tokei.png") #画面をpngファイルとしてセーブ
            pygame.quit() #pygameモジュールの終了
            sys.exit() #プログラムの強制終了

出力結果