Python版打字练习软件源代码,键盘练习软件源代码,含娱乐模式和训练模式

科技   科技   2023-06-29 18:04   云南  

Python版打字练习软件源代码,键盘练习软件源代码,含娱乐模式和训练模式
按ESC切换左手练习,右手练习、双手练习


核心代码:

import randomimport pygamefrom pygame.locals import *from kb import *from alpha import *from player import *from button import *from cloud import *import sysfrom setting import *
pygame.init()pygame.mixer.init()ADDBIRD = pygame.USEREVENT + 1pygame.time.set_timer(ADDBIRD, 1500)
ADDCLOUD = pygame.USEREVENT + 2pygame.time.set_timer(ADDCLOUD, 20000)COUNTDOWN = pygame.USEREVENT + 3pygame.time.set_timer(COUNTDOWN, 1000)

boom_file = "./sound/boom.wav"# print(file)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 = 0happy_score = 0count = 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 = RUN_TRAIN_MODE# stage = RUN_HAPPY_MODEstage = MENU_MODEsubstage = RUN_LEFT_MODEkb_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() # print("add cloud") clouds.add(cloud) if event.type == COUNTDOWN: if stage == RUN_TRAIN_MODE: if count <= 0: old_stage = stage stage = GAME_OVER_MODE # RUN_TRAIN_MODE_FLAG = True 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 # kb_pressd = KB_Pressed() 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)) # screen.fill("black") clouds.update() clouds.draw(screen) # return to menu button return_menu_mode.update() return_menu_mode.draw(screen) # screen.blit(BG_IMG, (0, 0)) screen.blit(keyboard, KB_POS) hit_alpha = kb_pressd.update() # 得到一个按下的字母 kb_pressd.draw(screen) # hit_alpha 得到按下的字母 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(): # 如果向左的字母 和 按下的字母是一样的 # print(hit_alpha) 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(): # 如果向左的字母 和 按下的字母是一样的 # 1. 移动player的位置到,show_bird,这一行 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_text(text_font, SHOW_HANDS_INFO[substage], (20, 900), text_color) 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代码大全,海量代码任你下载

Python代码大全
Python源程序、源代码、源码分享,Python代码大全,Python源代码学习,Python入门,Python基础教程。
 最新文章