python编程之键盘响应

            

在学习python键盘响应时,按照教材写了一个小demo, 用于计算打字的速度。对于书上的代码做了调整,认为这样写是正确的。源代码如下:



# -*- coding: utf-8 -*-
"""
Created on Mon Jun 29 21:28:18 2015

@author: liuchang
"""

import sys,random,time,pygame
from pygame.locals import *

def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text,True,color)
    screen.blit(imgText,(x,y))
    
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("keyboard demo")
font1= pygame.font.Font(None,24)
font2= pygame.font.Font(None,200)
white=255,255,255
yellow=255,255,0
pressed=False

key_flag =False
correct_answer = 97 # "a"
seconds = 11
score = 0
clock_start = 0
game_over = True
array= [0 for x in range(0, 1000)]
array[0]=time.clock()
index=1
current=0
speed =0
while True:
   
   
    for event in pygame.event.get():
        
        
        if event.type ==QUIT :
            sys.exit()
        elif event.type == KEYDOWN :
            
            key_flag= True
            keys = pygame.key.get_pressed()
            if keys[correct_answer]:
                #当按键正确的时候记录按键的时间,与上一次正确按下的时间只差是一个字符所消耗的时间
                #用于计算每分钟能打印多少字符
              flag =False
              for i in keys:
                if i == True:
                 flag==True
                 clock_start = time.clock()
                 array[index]=clock_start
                 current =  array[index]-array[index-1]
                 speed = 60/current
                 index+=1
                 print("current",current)
                 break
        elif event.type == KEYUP :
            key_flag = False
        keys = pygame.key.get_pressed()
       
        
        if keys[K_ESCAPE] :
            sys.exit()
        if keys[K_RETURN]:
            if game_over:
                game_over = False
                score = 0
                seconds =11

        
        if current > 10:
            game_over =True
            print("over 10 seconds")
        elif current <=10:
            if keys[correct_answer]:
                print("get new character")
                correct_answer = random.randint(97,122)
                score+=1
       # clean
        screen.fill((0,100,0))
        print_text(font1,0,0,"lets see how fast you can type ")
        print_text(font1,0,20,"try to keep up for 10 seconds..  ")
        
        if key_flag:
            print_text(font1,500,0,"")
        if not game_over :
            print_text(font1,0,80,"time :"+str(int(seconds-current)))
        print_text(font1,0,100,"speed :"+str(speed)+"letters/min")
        print_text(font1,500,30,"score :"+str(score))
        if game_over:
            print_text(font1,0,160,"press enter to start")
        print_text(font2,0,240,chr(correct_answer-32),yellow)
        
        pygame.display.update()
        
                        
            
            

打字的速度由一分钟的六十秒去除以两次正确的按键之间的时间差近似得到。超过十秒钟以后视为游戏结束。

截图:

python编程之键盘响应_第1张图片

python编程之键盘响应_第2张图片

超时:

python编程之键盘响应_第3张图片

分数清空

python编程之键盘响应_第4张图片

你可能感兴趣的:(python,pygame)