Python版打字练习软件源代码,键盘练习软件源代码,含娱乐模式和训练模式
按ESC切换左手练习,右手练习、双手练习
核心代码:
import random
import pygame
from pygame.locals import *
from kb import *
from alpha import *
from player import *
from button import *
from cloud import *
import sys
from setting import *
pygame.init()
pygame.mixer.init()
ADDBIRD = pygame.USEREVENT + 1
pygame.time.set_timer(ADDBIRD, 1500)
ADDCLOUD = pygame.USEREVENT + 2
pygame.time.set_timer(ADDCLOUD, 20000)
COUNTDOWN = pygame.USEREVENT + 3
pygame.time.set_timer(COUNTDOWN, 1000)
boom_file = "./sound/boom.wav"
music_boom = pygame.mixer.Sound(boom_file)
music_boom.set_volume(0.4)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
text_font = pygame.font.Font("./font/jiangxizhuokai20.TTF", 30)
text_color = "white"
train_score = 0
happy_score = 0
count = COUNTDOWN_TIME
def print_text(font, text, pos, color):
img_text = font.render(text, True, color)
screen.blit(img_text, pos)
def print_hand_info(text_font, substage, pos , text_color):
print_text(text_font, SHOW_HANDS_INFO[substage], pos, text_color)
hand_image = pygame.image.load("./imgs/" + hand_images[substage])
screen.blit(hand_image, (pos[0] + 180, pos[1]))
running = True
stage = MENU_MODE
substage = RUN_LEFT_MODE
kb_pressd = KB_Pressed()
player = Player(y=300)
bird = MenuBird()
boom_group = pygame.sprite.Group()
bird_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
float_alpha_group = pygame.sprite.Group()
clouds = pygame.sprite.Group()
hit_alpha = None # fix bug, 防止进入娱乐模式后,直接按空格发射子弹,程序崩溃
if __name__ == "__main__":
keyboard = pygame.image.load(KB_IMAGE)
while running:
for event in pygame.event.get():
if event.type == KEYUP:
if event.key == K_ESCAPE:
substage = (substage + 1) % 3
elif event.type == KEYDOWN:
if event.key == K_SPACE and stage == RUN_HAPPY_MODE:
print("hit_alpha", shoot_alpha)
bullet = Bullet(player.rect, shoot_alpha)
bullet.play_shoot()
bullet_group.add(bullet)
if event.type == ADDBIRD:
if stage in (RUN_HAPPY_MODE, RUN_TRAIN_MODE):
bird = Bird(substage, stage)
bird_group.add(bird)
if event.type == ADDCLOUD:
if stage in (RUN_HAPPY_MODE, RUN_TRAIN_MODE):
cloud = Cloud()
clouds.add(cloud)
if event.type == COUNTDOWN:
if stage == RUN_TRAIN_MODE:
if count <= 0:
old_stage = stage
stage = GAME_OVER_MODE
count -= 1
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEBUTTONUP:
x, y = event.pos
if stage == MENU_MODE:
for button_choice in menu_mode_buttons:
if button_choice.rect.collidepoint(x, y):
if button_choice.type == RUN_TRAIN_MODE:
pygame.time.set_timer(ADDBIRD, 2000)
if button_choice.type in (RUN_HAPPY_MODE, RUN_TRAIN_MODE):
train_score = 0
happy_score = 0
count = COUNTDOWN_TIME
substage = RUN_LEFT_MODE
stage = button_choice.type
kb_pressd = KB_Pressed()
boom_group = pygame.sprite.Group()
bird_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
clouds = pygame.sprite.Group()
for cloud_index in range(len(INIT_CLOUD_POS)):
cloud = Cloud(INIT_CLOUD_POS[cloud_index])
clouds.add(cloud)
bird = Bird(substage, stage)
bird_group.add(bird)
elif button_choice.type == GAME_QUIT:
sys.exit()
elif stage in (RUN_HAPPY_MODE, RUN_TRAIN_MODE):
if return_menu_mode.rect.collidepoint(x, y):
return_menu_mode.revert_image()
stage = MENU_MODE
player = Player()
bird = MenuBird()
float_alpha_group = pygame.sprite.Group()
elif stage == GAME_OVER_MODE:
if return_menu.collidepoint(x, y):
stage = MENU_MODE
player = Player()
bird = MenuBird()
elif replay_again.collidepoint(x, y):
if old_stage == RUN_TRAIN_MODE:
train_score = 0
count = COUNTDOWN_TIME
elif old_stage == RUN_HAPPY_MODE:
happy_score = 0
bullet_group = pygame.sprite.Group()
stage = old_stage
substage = RUN_LEFT_MODE
boom_group = pygame.sprite.Group()
bird_group = pygame.sprite.Group()
clouds = pygame.sprite.Group()
float_alpha_group = pygame.sprite.Group()
for cloud_index in range(len(INIT_CLOUD_POS)):
cloud = Cloud(INIT_CLOUD_POS[cloud_index])
clouds.add(cloud)
elif event.type == MOUSEMOTION:
x, y = event.pos
if stage == MENU_MODE:
for button_choice in menu_mode_buttons:
button_choice.do_mouse_on(x, y)
if stage in (RUN_HAPPY_MODE, RUN_TRAIN_MODE):
screen.fill((135, 206, 250))
clouds.update()
clouds.draw(screen)
return_menu_mode.update()
return_menu_mode.draw(screen)
screen.blit(keyboard, KB_POS)
hit_alpha = kb_pressd.update() # 得到一个按下的字母
kb_pressd.draw(screen)
for bird in bird_group:
bird.update()
bird.draw(screen)
if stage == RUN_HAPPY_MODE:
if bird.is_dead():
old_stage = stage
stage = GAME_OVER_MODE
if stage == RUN_TRAIN_MODE: # 训练模式:小鸟原地自爆炸
print_text(text_font, "SCORE: " + str(train_score), (1720, 20), "black")
print_text(text_font, "倒计时: " + str(count), (1520, 20), "black")
if hit_alpha:
float_alpha = FloatAlpha(hit_alpha)
float_alpha_group.add(float_alpha)
for show_bird in bird_group: # 遍历正在向左移动的字母
if hit_alpha == show_bird.get_label(): # 如果向左的字母 和 按下的字母是一样的
train_score += 1
music_boom.play()
x, y = show_bird.get_pos()
boom = Boom(x-10, y-10)
boom_group.add(boom)
show_bird.kill() # 干掉向左移动的字母
break
if stage == RUN_HAPPY_MODE: # 娱乐模式:小鸟被子弹射杀
if hit_alpha:
float_alpha = FloatAlpha(hit_alpha)
float_alpha_group.add(float_alpha)
for show_bird in bird_group: # 遍历正在向左移动的字母
if hit_alpha == show_bird.get_label(): # 如果向左的字母 和 按下的字母是一样的
player.move_y(show_bird.rect.y)
shoot_alpha = hit_alpha
break
collisions = pygame.sprite.groupcollide(bullet_group, bird_group, False, False)
if collisions:
for bullet, birds in collisions.items():
for bird in birds:
if bullet.get_label() == bird.get_label():
happy_score += 1
music_boom.play()
x, y = bird.get_pos()
boom = Boom(x-10, y-10)
boom_group.add(boom)
bullet.kill()
bird.kill()
print_text(text_font, "SCORE: " + str(happy_score), (1720, 20), "black")
player.update()
player.draw(screen)
bullet_group.update()
bullet_group.draw(screen)
float_alpha_group.update()
float_alpha_group.draw(screen)
boom_group.update()
boom_group.draw(screen)
print_hand_info(text_font, substage, (20, 900), text_color)
elif stage == MENU_MODE:
screen.blit(MENU_MODE_BG, (0, 0))
for button in menu_mode_buttons:
if button.type != MENU_MODE:
button.update()
button.draw(screen)
player.random_move()
player.update()
player.draw(screen)
bird.update()
bird.draw(screen)
elif stage == GAME_OVER_MODE:
if old_stage == RUN_TRAIN_MODE:
screen.blit(game_over_bg, (0, 0))
screen.blit(GAME_OVER_IMAGE, (WIDTH//2 - 150, HEIGHT//2 - 250))
elif old_stage == RUN_HAPPY_MODE:
screen.blit(game_over_bg, (0, 0))
screen.blit(GAME_OVER_IMAGE, (WIDTH//2 - 150, HEIGHT//2 - 250))
pygame.display.update()
clock.tick(FPS)
完整代码下载地址:
https://download.csdn.net/download/weixin_42756970/87264079
Python代码大全,海量代码任你下载