Python:遊戲:測試打字速度

來源:互聯網
上載者:User

標籤:col   second   apt   har   enter   一個   return   spl   一點   

現在寫書的人真是一點責任心都沒有,最近看了幾本書,其中的代碼都存在錯誤。

最近迷戀 Python 遊戲,買了《Python遊戲編程入門》[美] Jonathan S·Harbour 著 一書來看。

其中第四章:Bomb Catcher遊戲中,測試打字速度的程式碼嚴重有誤。

改程式螢幕上隨機顯示一個字母,按鍵輸入該字母后隨機顯示下一個,計算平均一分鐘可以輸入多少個字母,原代碼中計算速度的一塊有誤,附上我修改後的代碼

import sysimport randomimport timeimport pygamefrom 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)key_flag = Falsecorrect_answer = 97     # aseconds = 11score = 0speed = 0clock_start = 0game_over = Truewhile True:    for event in pygame.event.get():        if event.type == QUIT:            sys.exit()        elif event.type == KEYDOWN:            key_flag = True        elif event.type == KEYUP:            key_flag = False    keys = pygame.key.get_pressed()     # keys 是一個元組,窮舉了所有的按鍵,未按下為 0,按下為 1    if keys[K_ESCAPE]:        sys.exit()    if keys[K_RETURN]:        if game_over:            game_over = False            clock_start = time.time()            score = 0            seconds = 11            speed = 0            clock = clock_start    if not game_over:        current = time.time() - clock_start        if seconds < current:            game_over = True        else:            if keys[correct_answer]:                correct_answer = random.randint(97, 122)                clock_start = time.time()                score += 1                speed = 60 * score / (clock_start - clock)    screen.fill((0, 100, 0))    print_text(font1, 0, 0, "Let‘s 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, "<key>")    if not game_over:        print_text(font1, 0, 80, "Time: " + str(int(seconds-current)))    print_text(font1, 0, 100, "Speed: " + str(int(speed)) + " letters/min")    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:遊戲:測試打字速度

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.