Pygameでシンプルなタイピング練習ゲーム #python
import pygame
from pygame.key import key_code
from pygame.locals import *
import sys
import time
#押されたキーの先頭の一文字を設定する
def getInputkey(key_code):
inputkey = key_code
if inputkey == "" :
inputkey = "-" #変換キーを押すと""が入力されるため
if inputkey == "space":
inputkey = " "
result = inputkey[0]
return result
#メイン
def main():
pygame.init() # Pygameの初期化
screen = pygame.display.set_mode((640, 480)) # 大きさ640*480の画面を生成
pygame.display.set_caption("Let's typing") # タイトルバーに表示する文字
#STEP1.フォントの用意
font1 = pygame.font.SysFont("MS UI Gothic", 50)
font1.set_underline(True)
#STEP2.テキストの設定
starttext = "キーを押してスタート"
themetext = ("arigatou","thank you","konnichiwa","hello","ohayou","good morning","youkoso","welcome","iitennki","nice day")
themeTextLine = font1.render(starttext, True, (255,255,255))
inputTextLine = font1.render("Press the key to start", True, (255,0,0))
pointTextLine = font1.render("point", True, (255,0,0))
timeTextLine = font1.render("time", True, (255,0,0))
scoreTextLine = font1.render("score", True, (255,0,0))
inputkeys = ""
OKpoint = 0
count = 0
count1 = 0
count2 = 0
sum = 0
while (1):
screen.fill((0,0,0)) # 画面を黒色(#000000)に塗りつぶし
# STEP3.テキストを描画
screen.blit(themeTextLine, (40,30))
screen.blit(inputTextLine, (80,180))
screen.blit(pointTextLine, (120,240))
screen.blit(timeTextLine, (120,290))
screen.blit(scoreTextLine, (120,340))
pygame.display.update() # 画面を更新
pygame.time.wait(30) # 更新時間間隔
# イベント処理
for event in pygame.event.get():
if event.type == QUIT: # 閉じるボタンが押されたら終了
pygame.quit() # Pygameの終了(画面閉じられる)
sys.exit()
# キーを押したとき
if event.type == KEYDOWN:
# ESCキーなら終了
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
else:
#画面遷移
if count == 0: #0ならスタート画面。1ならタイピング中の画面。2ならスタート画面に戻る準備
itme_diff = 0
OKpoint = 0
inputkey = getInputkey(pygame.key.name(event.key))
#inputkeyが空でなければお題の文字列を表示してタイピング中画面へ
if inputkey != "":
count = 1
starttime = time.time()
themeTextLine = font1.render(themetext[count1], True ,(255,255,255))
pointTextLine = font1.render("point = ", True, (255,0,0))
timeTextLine = font1.render("time = ",True,(255,0,0))
scoreTextLine = font1.render("score = ",True,(255,0,0))
elif count == 1:
themeTextLine = font1.render(themetext[count1], True ,(255,255,255))
inputTextLine = font1.render(str(inputkeys) , True, (255,0,0))
inputkey = getInputkey(pygame.key.name(event.key))
inputkeys = str(inputkeys) + inputkey
#入力が空でなければ
if inputkeys != "":
themeTextLine = font1.render(themetext[count1], True ,(255,255,255))
inputTextLine = font1.render(str(inputkeys) , True, (255,0,0))
endtime = time.time()
itme_diff = endtime - starttime
timeTextLine = font1.render("time = " + str(round(itme_diff,2)),True,(255,0,0)) ####
#Count1の文字位置が同じかどうか調べる
if count1 < len(themetext):
if inputkeys[count2] == themetext[count1][count2]:
OKpoint = OKpoint + 1
pointTextLine = font1.render("point = " + str(OKpoint), True, (255,0,0))
count2 = count2 + 1
if len(inputkeys) >= len(themetext[count1]):
inputkeys = ""
count1 = count1 + 1
count2 = 0
if count1 < len(themetext):
themeTextLine = font1.render(themetext[count1], True ,(255,255,255))
inputTextLine = font1.render(str(inputkeys) , True, (255,0,0))
else:
count = 0
themeTextLine = font1.render("結果", True ,(255,255,255))
inputTextLine = font1.render(str(inputkeys) , True, (255,0,0))
endtime = time.time()
itme_diff = endtime - starttime
timeTextLine = font1.render("time = " + str(round(itme_diff,2)),True,(255,0,0)) #######################
sum = (100 - itme_diff) * OKpoint
scoreTextLine = font1.render("score = " + str(round(sum,0)),True,(255,0,0))
#text1 = font1.render("キーを押してスタート", True ,(255,255,255))
count = 2
elif count == 2:
themeTextLine = font1.render("キーを押してスタート", True ,(255,255,255))
count = 0
count1 = 0
count2 = 0
inputkeys = ""
if __name__ == "__main__":
main()
この記事が気に入ったらサポートをしてみませんか?