1. Basic code

The following code completes the random generation of a new letter at the top of the interface every second

# -* -coding =utf-8 -* -import pygame from pygame.locals import KEYDOWN import random w,h = 800,600 pygame.init() screen = pygame.display.set_mode((w, H)) white=255,255,255 black=0,0,0 myfont = pygame.font.Font(None,80) word_diff_ticks = 1000 word_ticks = pygame.time.get_ticks() + word_diff_ticks def get_random_word(): Color = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255)) Y = 0 word = random. Randint (65, 90) return x,y,word,color arr=[] arr.append(get_random_word()) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() screen.fill((255, 255, 255)) for i in range(len(arr)): [I] textImage = myfont. Render (CHR (word), True, c) screen.blit(x, x) y)) if pygame.time.get_ticks()>=word_ticks: Word_ticks +=word_diff_ticks arr.append(get_random_word()) pygame.display.update()Copy the code

2. Move letters

Add a timer and set the letter to move one space in 20 milliseconds

diff_ticks = 20
ticks = pygame.time.get_ticks() + diff_ticks
Copy the code

Add moving code to the main loop

    if pygame.time.get_ticks() >= ticks:
        ticks += diff_ticks
        for i in range(len(arr)):
            x, y, word, c = arr[i]
            arr[i] = (x, y+1, word, c)
Copy the code

3. Eliminate letters

Add keyboard letter judgments to event handling code

For event in pygame.event.get():... If len(arr)>0 and event.type == KEYDOWN: if event.key == arr[0][2]+32: arr.pop(0)Copy the code

Specify that each elimination must be the first one, so if the first letter is pressed correctly, the first letter is removed

4. Increase the difficulty level

Add a variable clear_word to record the number of letters removed, add a variable level to record the current level, and set the title of the interface to show the current level

clear_word=0
level = 1
pygame.display.set_caption('typing level:%d'%level)
Copy the code

Adding technique and deciding whether to increase difficulty after correctly pressing the letter, because DIFF_ticks and word_DIFF_ticks are respectively the time interval for moving the letter and the time interval for adding a new letter, processing these two variables by *0.9 reduces the time interval and increases the difficulty

for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if len(arr)>0 and event.type == KEYDOWN: if event.key == arr[0][2]+32: Pop (0) clear_word += 1 if clear_word >= level*10: Pygame.display. Set_caption ('typing level:%d' % level) Diff_ticks = DIFF_ticks *0.9 Word_diff_ticks = word_diff_ticks * 0.9Copy the code

5. Game state

Add a variable game_state to keep track of the game state

Game_state =1 # 1. In progress 2. Game failedCopy the code

Display when the game fails

if game_state == 2: TextImage = myfont. Render ("Level%d fail"% Level, True, (255,0,0)) sw,sh = textimage.get_size () screen.blit(textImage, ((W-sw)/2, (H-SH)/2)) # center displayCopy the code

Move the letter code to make a change, put the code into a game_state of 1 judgment, and use arr[0][1] > h to check if the bottom letter has gone off-screen

if game_state == 1: if pygame.time.get_ticks()>=word_ticks: Word_ticks +=word_diff_ticks arr.append(get_random_word()) if pygame.time.get_ticks() >= ticks: ticks += diff_ticks for i in range(len(arr)): x, y, word, c = arr[i] arr[i] = (x, y+1, word, c) if len(arr) > 0 and arr[0][1] > h: game_state=2Copy the code

The key judgment processing of letters also added under the game state judgment, to avoid the end of the game can also eliminate letters

for event in pygame.event.get(): if event.type == pygame.QUIT: Quit () exit() if game_state==1 and len(arr)>0 and event.type == KEYDOWN:...Copy the code

6. Add a flicker effect to the first letter

To increase the display effect, add a flashing effect to the first letter to remind you to hit the corresponding letter. The idea is to make the letter change color randomly every time you move a space, in order to achieve flashing. Define a sign variable to switch colors

sign=1
Copy the code

Add the switch of sign variable in the place of moving letters 1-sign realizes the switch of 0 and 1 values

If game_state == 1:... If pygame.time.get_ticks() >= ticks: ticks += diff_ticks sign=1-sign......Copy the code

Add “sign” to the place where the letter is drawn. If it is the first letter and sign is not 0, color the letter randomly

    for i in range(len(arr)):
        x, y, word, c = arr[i]
        if i==0 and sign:
            c = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

        textImage = myfont.render(chr(word), True, c)
        screen.blit(textImage, (x, y))
Copy the code

7. Final renderings

8. Complete code

# -* -coding =utf-8 -* -import pygame from pygame.locals import KEYDOWN import random w,h = 800,600 pygame.init() screen = pygame.display.set_mode((w, Background-color (None,80) diff_ticks = 20 ticks = pygame.time.get_ticks() + diff_ticks word_diff_ticks = 1000 word_ticks = pygame.time.get_ticks() + word_diff_ticks def get_random_word(): Color = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255)) Y = 0 word = random. Randint (65, 90) return x,y,word,color arr=[] arr.append(get_random_word()) clear_word=0 level = 1 pygame.display.set_caption('typing  level:%d'%level) game_state=1 # 1. Sign =1 while True: for event in Pygame.event.get (): if event.type == PyGame.quit: pygame.quit() exit() if game_state==1 and len(arr)>0 and event.type == KEYDOWN: if event.key == arr[0][2]+32: Pop (0) clear_word += 1 if clear_word >= level*10: Level +=1 Pygame.display. set_caption('typing level:%d' % level) Diff_ticks = Diff_ticks *0.9 Word_diff_ticks =word_diff_ticks*0.95 screen.fill((255, 255, 255)) for I in range(len(arr)): X, y, word, c = arr[I] if I ==0 and sign: c = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) textImage = myfont.render(chr(word), True, c) screen.blit(textImage, (x, y)) if game_state == 2: TextImage = myfont. Render ("Level%d fail"% Level, True, (255,0,0)) sw,sh = textimage.get_size () screen.blit(textImage, If game_state == 1: if pygame.time.get_ticks()>=word_ticks: ((w-sw)/2, (h-sh)/2)) Word_ticks +=word_diff_ticks arr.append(get_random_word()) if pygame.time.get_ticks() >= ticks: ticks += diff_ticks sign=1-sign for i in range(len(arr)): x, y, word, c = arr[i] arr[i] = (x, y+1, word, c) if len(arr) > 0 and arr[0][1] > h: game_state=2 pygame.display.update()Copy the code