今回はプログラミング言語の「Python」を使って作成しました。
脳トレアプリを開発しました。前回のものにタイマーを実装してみました。
■ プログラム
import traceback try: import myUI import pygame import sys from pygame.locals import * import random inputBtn = None selectBtn = None calcBtns = None text = None PHASE = 0 def phase(pygame, screen): global calcBtns, selectBtn, text, PHASE, inputBtn if PHASE == 1: if selectBtn: # 数字ボタンがクリックされているなら inputBtn.number = selectBtn.number inputBtn.setText(selectBtn.number) inputBtn.draw() selectBtn.col = (255, 255, 0) inputBtn.col = (0, 255, 0) inputBtn = calcBtns[3] inputBtn.col = (255, 255, 0) PHASE = 2 elif PHASE == 2: inputBtn = calcBtns[3] if selectBtn: # 数字ボタンがクリックされているなら inputBtn.number = selectBtn.number inputBtn.setText(selectBtn.number) inputBtn.col = (0, 255, 0) inputBtn = None #--------------------------------------- # 計算開始 #--------------------------------------- # 文字連結 if calcBtns[2] == 0: answerNum = calcBtns[3].number else: answerNum = int(str(calcBtns[2].number) + str(calcBtns[3].number)) if (calcBtns[0].number * calcBtns[1].number) == answerNum: text.setText('正解!!') PHASE = 3 else: text.setText('不正解!!') PHASE = 3 elif PHASE == 3: # 計算画面ボタン初期化 inputBtn = calcBtns[2] inputBtn.col = (255, 255, 0) inputBtn.setText('') calcBtns[3].setText('') text.setText('空白に何が入る?') selectBtn.col = (0, 255, 0) selectBtn = None calcBtns[0].number = random.randint(1, 9) calcBtns[0].setText(calcBtns[0].number) calcBtns[1].number = random.randint(1, 9) calcBtns[1].setText(calcBtns[1].number) PHASE = 0 def main(): global inputBtn, selectBtn, PHASE, text, calcBtns pygame.init() # Pygameを初期化 screen = pygame.display.set_mode((600, 500)) # 画面作成 running = True # 実行継続フラグ # ボタンインスタンス作成 buttons = [[0 for i in range(3)] for j in range(4)] for i in range(len(buttons)): for j in range(len(buttons[i])): if i*3+j == 10 or i*3+j == 11: button = myUI.Button(j*150 + 75, i*50 + 260, 150, 50, '', 40, (0, 255, 0), screen, pygame) buttons[i][j] = button else: button = myUI.Button(j*150 + 75, i*50 + 260, 150, 50, i*3 + j, 40, (0, 255, 0), screen, pygame) buttons[i][j] = button # フレームインスタンス作成 frame = myUI.Frame(25, 40, 550, 200, (0, 255, 0), screen, pygame) # テキストインスタンス作成 text = myUI.Text(25, 45, 550, 220, '空白に何が入る?', 0, 0, 40, (0, 255, 0), True, screen, pygame) text2 = myUI.Text(25, 20, 550, 0, '「脳トレゲーム 脳を鍛えよう!」', 0, 0, 20, (0, 255, 0), True, screen, pygame) text3 = myUI.Text(25, 40, 550, 320, '', 0, 0, 30, (0, 255, 0), True, screen, pygame) offsetX = 140 # 計算画面ボタン calcBtn_00 = myUI.Button(offsetX, 65, 60, 60, random.randint(1, 9), 40, (0, 255, 0), screen, pygame) # 計算画面テキスト calcText_00 = myUI.Text(offsetX+60 , 65, 60, 60, 'x', 0, 0, 40, (0, 255, 0), True, screen, pygame) # 計算画面ボタン calcBtn_01 = myUI.Button(offsetX+120, 65, 60, 60, random.randint(1, 9), 40, (0, 255, 0), screen, pygame) # 計算画面テキスト calcText_01 = myUI.Text(offsetX+180, 65, 60, 60, '=', 0, 0, 40, (0, 255, 0), True, screen, pygame) # 計算画面ボタン calcBtn_02 = myUI.Button(offsetX+240, 65, 40, 60, '', 40, (255, 255, 0), screen, pygame) # 計算画面ボタン calcBtn_03 = myUI.Button(offsetX+280, 65, 40, 60, '', 40, (0, 255, 0), screen, pygame) inputBtn = calcBtn_02 calcBtns = [0 for j in range(4)] calcBtns = [calcBtn_00, calcBtn_01, calcBtn_02, calcBtn_03] while running: pygame.display.update() # 画面を更新 screen.fill(pygame.Color(0, 0, 0)) # 画面を塗りつぶす # ボタン描画 for items in buttons: for button in items: button.draw() frame.draw() # 枠描画 text.draw() # テキスト描画 text2.draw() # テキスト描画 text3.setText('Time ' + str(pygame.time.get_ticks()//1000 // 60) + ' : ' + str(pygame.time.get_ticks()//1000 % 60)+ ' : ' + str(pygame.time.get_ticks() % 1000)) text3.draw() # テキスト描画 for calcBtn in calcBtns: calcBtn.draw() # 計算画面ボタン描画 calcText_00.draw() # 計算画面テキスト描画 calcText_01.draw() # 計算画面テキスト描画 for event in pygame.event.get(): # イベント if event.type == pygame.QUIT: running = False # 終了 if event.type == pygame.MOUSEBUTTONDOWN: if PHASE == 0: for items in buttons: for button in items: if button.collider(event): selectBtn = button selectBtn.col = (255, 255, 0) PHASE = 1 phase(pygame, screen) break else: continue break elif PHASE == 2: for items in buttons: for button in items: if button.collider(event): selectBtn.col = (0, 255, 0) selectBtn = button selectBtn.col = (255, 255, 0) phase(pygame, screen) break else: continue break elif PHASE == 3: phase(pygame, screen) pygame.display.flip() # 画面フリップ pygame.quit() # Pygameを終了 if __name__=="__main__": main() except Exception as e: # 文字列を取得する format_exec()メソッド print("エラー情報\n" + traceback.format_exc()) input() |
from PIL import Image, ImageFont, ImageDraw import numpy as np from matplotlib import pyplot as plt from matplotlib.ticker import AutoMinorLocator fontUrl = "PixelMplus12-Regular.ttf" class Button: # コンストラクタ def __init__(self, bx, by, bw, bh, text, fsize, col, screen, pygame): # creates a rect object self.button = pygame.Rect(bx, by, bw, bh) # フォントの用意 self.font = pygame.font.Font(fontUrl, fsize) # テキストの設定 self.text = self.font.render(str(text), True, col) # テキスト幅 textW, textH = self.getTextSize(str(text), fsize) # ボタンを中央に配置する self.tx = (bw - textW)/2 + bx self.ty = (bh - textH)/2 + by self.screen = screen self.pygame = pygame self.col = col self.bx = bx self.by = by self.bw = bw self.bh = bh self.fsize = fsize if type(text) == str: self.number = 0 else: self.number = text def setText(self, text): # フォントの用意 self.font = self.pygame.font.Font(fontUrl, self.fsize) # テキストの設定 self.text = self.font.render(str(text), True, self.col) # テキスト幅 textW, textH = self.getTextSize(str(text), self.fsize) # ボタンを中央に配置する self.tx = (self.bw - textW)/2 + self.bx self.ty = (self.bh - textH)/2 + self.by def draw(self): self.pygame.draw.rect(self.screen, self.col, self.button, 5) self.screen.blit(self.text, (self.tx, self.ty)) def getTextSize(self, text, fsize): img = np.zeros((30, 50, 3), np.uint8) # 新規黒塗画像を作成 message = text # 文字列を定義 img = Image.fromarray(img) # cv2(NumPy)型の画像をPIL型に変換 draw = ImageDraw.Draw(img) # .textlengthで文字列のピクセルサイズを取得 font_size = fsize font = ImageFont.truetype(fontUrl, size=font_size) txw = draw.textlength(message, font=font) txh = font_size return(txw, txh) def collider(self, event): if self.button.collidepoint(event.pos): return True else: return False class Frame: # コンストラクタ def __init__(self, fx, fy, fw, fh, col, screen, pygame): self.background = pygame.Rect(fx, fy, fw, fh) self.screen = screen self.pygame = pygame self.col = col def draw(self): self.pygame.draw.rect(self.screen, self.col, self.background, 10) class Text: # コンストラクタ def __init__(self, bx, by, bw, bh, text, tx, ty, fsize, col, center, screen, pygame): # フォントの用意 self.font = pygame.font.Font(fontUrl, fsize) # テキストの設定 self.text = self.font.render(text, True, col) if center == True: # テキスト幅 textW, textH = self.getTextSize(text, fsize) # テキストを中央に配置する self.tx = (bw - textW)/2 + bx self.ty = (bh - textH)/2 + by elif center == False: self.tx = tx self.ty = ty self.screen = screen self.pygame = pygame self.fsize = fsize self.col = col self.bx = bx self.by = by self.bw = bw self.bh = bh self.center = center def getTextSize(self, text, fsize): img = np.zeros((30, 50, 3), np.uint8) # 新規黒塗画像を作成 message = text # 文字列を定義 img = Image.fromarray(img) # cv2(NumPy)型の画像をPIL型に変換 draw = ImageDraw.Draw(img) # .textlengthで文字列のピクセルサイズを取得 font_size = fsize font = ImageFont.truetype(fontUrl, size=font_size) txw = draw.textlength(message, font=font) txh = font_size return(txw, txh) def setText(self, text): # フォントの用意 self.font = self.pygame.font.Font(fontUrl, self.fsize) # テキストの設定 self.text = self.font.render(text, True, self.col) if self.center == True: # テキスト幅 textW, textH = self.getTextSize(text, self.fsize) # テキストを中央に配置する self.tx = (self.bw - textW)/2 + self.bx self.ty = (self.bh - textH)/2 + self.by elif self.center == False: self.tx = self.tx self.ty = self.ty def draw(self): self.screen.blit(self.text, (self.tx, self.ty)) |
■ ゲーム用ライブラリ
「Pygame」
■ プログラミング言語
「Python」