Home > other >  Pygame typing game how to generate speed control letters?
Pygame typing game how to generate speed control letters?

Time:09-29

Game description: the top of the screen down different letters, keyboard keys corresponding letter disappear, the letter x position are random, letter is gradually falling speed faster, if 10 letters at the bottom of the screen, the game ends, in order to simplify the game, Mr Into the same one letter (letters A, for example), has achieved, but the generated letters so quickly, how to add the code to control the rate of letters A generation? Seems to add time. Sleep (5) can make the letters whereabouts caton,
Amy polumbo ng:
Here is my code:
1. Alphabet_zoo. Py
 import sys 
The import pygame
The from pygame. Locals import *
The from Settings import Settings
The from letter import letter
The import game_functions as gf
The from pygame. Sprite import Group

Def run_game () :
Pygame. The init ()
Az_settings=Settings ()
Screen=pygame. Display. Set_mode ((0, 0), the RESIZABLE) # ((0, 0), the RESIZABLE) can achieve full screen and can adjust the screen size
Pygame. Display. Set_caption (" Alphabet Zoo ")
Letters=pygame. Sprite. Group ()


While True:
Gf. Letter_generator (az_settings, screen, letters)
Gf. Check_events (letters)
Letters. The update ()
Gf. Update_screen (az_settings, screen, letters)


Run_game ()


2. Settings. Py
 class Settings () : 
Def __init__ (self) :
Self. Bg_color=(0, 0, 0)
Self. Letter_speed_factor=10


3. Game_functions. Py
 import sys 
The import pygame
The import time
The from letter import letter

Def letter_generator (az_settings, screen, letters) :
# time. Sleep (5)
# This setting not only slowdowns the generating of A
# but also stops the falling down of the each A for A short time.
New_letter=Letter (az_settings, screen)
Letters. The add (new_letter)

Def check_events (letters) :
For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
Sys. The exit ()
Elif event. Type==pygame. KEYDOWN:
If the event. The key==pygame. K_a:
Letters. Empty ()


Def update_screen (az_settings, screen, letters) :
Screen. The fill (az_settings. Bg_color)
Letters. The draw (screen)
Letters. The update ()
Pygame. Display. Flip ()


4. Letter. Py
 import pygame 
Import the random
The from pygame. Sprite import Sprite


The class Letter (Sprite) :

Def __init__ (self, az_settings, screen) :
Super () __init__ ()
The self. The screen=screen
Self. Az_settings=az_settings
The self. The image=pygame. Image. The load (' images/Amy polumbo ng)
The self. The rect=self. Image. Get_rect ()
Self. Screen_rect=screen. Get_rect ()

The self. The rect. Centerx=random. Randint (0, the self. Screen_rect. Right)
The self. The rect. Top=self. Screen_rect. Top
Self center=float (self. The rect. Centerx)


Def update (self) :
If the self. The rect. Bottom & lt; Self. Screen_rect. Bottom:
The self. The rect. Centery +=self. Az_settings. Letter_speed_factor

CodePudding user response:

Make a thread to control the letters generated, sleep in the thread, so as not to cause the main interface caton,
  • Related