前提・実現したいこと
Pythonのpygameを利用して、カウントダウンタイマーを作っています。
システムの流れは下記の通りです
(1)初期画面に3分、6分のボタンがある(カーソルを合わせるとボタンの色が変わる仕様)
(2)いずれかのボタンをクリックするとカウントダウンが始まる
(3)カウントダウンが終わると「終了」という表示がでる
※背景画像の上にキャラクター+フキダシ画像を置き、更にそのフキダシの上に文字やボタンを置いています
起動して放置しておけば最後まで作動するのですが、
(2)のカウントダウン中に、ウィンドウ内外関係なくどこかをクリックすると停止し、応答なしとなってしまいます。
Pythonの基本を終えたばかりで原因もよくわからず、昨日1日中いろいろ試してみましたが自力では解決できませんでした。
どうかお知恵を貸していただけると助かります、よろしくお願いします。
該当のソースコード
Python
1import pygame 2import sys 3 4GLAY = (171, 171, 171) #文字色 5 6def main(): 7 pygame.init() 8 pygame.display.set_caption('タイマー') 9 clock = pygame.time.Clock() 10 screen = pygame.display.set_mode((800, 600)) 11 font = pygame.font.Font('APJapanesefontT.ttf', 35) 12 13 frag = 0 #フラグ管理 14 15 img_bg = pygame.image.load('bg.png') #背景画像 16 img_1 = pygame.image.load('hukidashi_1.png') #フキダシ画像1(初期画面、カウントダウン時) 17 img_2 = pygame.image.load('hukidashi_2.png') #フキダシ画像2(完成時) 18 button_off = pygame.image.load('button_off.png') #ボタン 19 button_on = pygame.image.load('button_on.png') #ボタン(カーソルを合わせた時) 20 21 while True: 22 for event in pygame.event.get(): 23 if event.type == pygame.QUIT: 24 pygame.quit() 25 sys.exit() 26 27 #<0> 初期画面 28 if frag == 0: 29 screen.blit(img_bg, [0, 0]) #画像 30 screen.blit(img_1, [0, 0]) 31 screen.blit(button_off, [100, 220]) 32 screen.blit(button_off, [240, 220]) 33 34 txt_Q = font.render('時間は?', True, GLAY) #テキスト 35 txt_3 = font.render('3分', True, GLAY) 36 txt_6 = font.render('6分', True, GLAY) 37 screen.blit(txt_Q, [117, 160]) 38 screen.blit(txt_3, [130, 248]) 39 screen.blit(txt_6, [270, 248]) 40 41 mouseX, mouseY = pygame.mouse.get_pos() #マウス座標、クリック判定 42 click, a, b = pygame.mouse.get_pressed() 43 44 #3分を選択した時 45 if (110 <= mouseX <= 220) and (230 <= mouseY <= 300): 46 screen.blit(button_on, [100, 220]) 47 screen.blit(txt_3, [130, 248]) 48 if click == 1: 49 frag = 1 50 51 #6分を選択した時 52 if (250 <= mouseX <= 360) and (210 <= mouseY <= 280): 53 screen.blit(button_on, [240, 220]) 54 screen.blit(txt_6, [270, 248]) 55 if click == 1: 56 frag = 2 57 58 pygame.display.update() 59 clock.tick(10) 60 61 #<1> カウントダウン(3分) 62 if frag == 1: 63 minute = 3 64 second = 0 65 loop = True 66 67 while loop: 68 for num in range(59, -1, -1): 69 if (minute == 0) and (second == 0): 70 frag = 3 71 loop = False 72 break 73 74 if second == 0: #カウント 75 minute -= 1 76 second = num 77 78 screen.blit(img_bg, [0, 0]) #画像 79 screen.blit(img_1, [0, 0]) 80 81 if 0 <= second < 10: #テキスト 82 txt_0 = font.render('残り{}分0{}秒'.format(minute, second), True, GLAY) 83 screen.blit(txt_0, [105, 200]) 84 else: 85 txt_10 = font.render('残り{}分{}秒'.format(minute, second), True, GLAY) 86 screen.blit(txt_10, [105, 200]) 87 88 pygame. display.update() 89 clock.tick(1) 90 91 #<2> カウントダウン(6分) 92 if frag == 2: 93 minute = 6 94 second = 0 95 loop = True 96 97 while loop: 98 for num in range(59, -1, -1): 99 if (minute == 0) and (second == 0): 100 frag = 3 101 loop = False 102 break 103 104 if second == 0: #カウント 105 minute -= 1 106 second = num 107 108 screen.blit(img_bg, [0, 0]) #画像 109 screen.blit(img_1, [0, 0]) 110 111 if 0 <= second < 10: #テキスト 112 txt_0 = font.render('残り{}分0{}秒'.format(minute, second), True, GLAY) 113 screen.blit(txt_0, [105, 200]) 114 else: 115 txt_10 = font.render('残り{}分{}秒'.format(minute, second), True, GLAY) 116 screen.blit(txt_10, [105, 200]) 117 118 pygame. display.update() 119 clock.tick(1) 120 121 #<3> 完成 122 if frag == 3: 123 screen.blit(img_bg, [0, 0]) #画像 124 screen.blit(img_2, [0, 0]) 125 126 txt = font.render('終了', True, GLAY) #テキスト 127 screen.blit(txt, [105, 200]) 128 129 pygame.display.update() 130 clock.tick(10) 131 132 133if __name__ == '__main__': 134 main() 135 136
補足情報(FW/ツールのバージョンなど)
Windows10
VisualStudio2019
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/09 01:23