Python: Game: Test Typing speed

Source: Internet
Author: User

Now the person who wrote the book is a bit of a sense of responsibility, recently read a few books, where the code is wrong.

Recently obsessed with Python games, bought "Getting started with Python game programming" [US] Jonathan S. Harbour a book to see.

In the fourth chapter: Bomb catcher game, the program code to test typing speed is seriously wrong.

Change the program screen randomly display a letter, the key to enter the letter randomly displayed next, calculate the average one minute can enter how many letters, the original code in the calculation of the speed of a piece of error, with my revised code

ImportSYSImportRandomImport TimeImportPygame fromPygame.localsImport*defPrint_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=True whileTrue: forEventinchpygame.event.get ():ifEvent.type = =QUIT:sys.exit ()elifEvent.type = =Keydown:key_flag=TrueelifEvent.type = =Keyup:key_flag=False Keys= Pygame.key.get_pressed ()#Keys is a tuple, poor up all the keys, not pressed to 0, press 1    ifKeys[k_escape]: sys.exit ()ifKeys[k_return]:ifGame_over:game_over=False Clock_start=Time.time () score=0 seconds= 11 Speed=0 Clock=Clock_startif  notgame_over:current= Time.time ()-Clock_startifSeconds <Current:game_over=TrueElse:            ifKeys[correct_answer]: Correct_answer= Random.randint (97, 122) Clock_start=Time.time () score+ = 1 Speed= * Score/(Clock_start-clock) Screen.fill ((0,100, 0)) Print_text (font1, 0, 0,"Let's see how fast can type!") Print_text (font1, 0,20,"Try to keep up for ten seconds ...")    ifKey_flag:print_text (Font1,500, 0,"<key>")    if  notGame_over:print_text (font1, 0,80,"Time :"+ str (int (seconds-Current)) ) Print_text (font1, 0,100,"Speed :"+ str (int (speed)) +"Letters/min")    ifGame_over:print_text (font1, 0,160,"Press Enter to start ...") Print_text (font2, 0,, Chr (correct_answer-32), yellow) pygame.display.update ()

Python: Game: Test Typing speed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.