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()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Let's typing")
font1 = pygame.font.SysFont("MS UI Gothic", 50)
font1.set_underline(True)
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))
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()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
else:
if count == 0:
itme_diff = 0
OKpoint = 0
inputkey = getInputkey(pygame.key.name(event.key))
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))
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))
count = 2
elif count == 2:
themeTextLine = font1.render("キーを押してスタート", True ,(255,255,255))
count = 0
count1 = 0
count2 = 0
inputkeys = ""
if __name__ == "__main__":
main()