前言
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"
# 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 = 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 = RUN_TRAIN_MODE
# stage = RUN_HAPPY_MODE
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()
# 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)
``
读者福利:如果大家对Python感兴趣,这套python学习资料一定对你有用
对于0基础小白入门:
如果你是零基础小白,想快速入门Python是可以考虑的。
一方面是学习时间相对较短,学习内容更全面更集中。
二方面是可以根据这些资料规划好学习计划和方向。
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习、Python量化交易等习教程。带你从零基础系统性的学好Python!
零基础Python学习资源介绍
① Python所有方向的学习路线图,清楚各个方向要学什么东西
② 600多节Python课程视频,涵盖必备基础、爬虫和数据分析
③ 100多个Python实战案例,含50个超大型项目详解,学习不再是只会理论
④ 20款主流手游迫解 爬虫手游逆行迫解教程包
⑤ 爬虫与反爬虫攻防教程包,含15个大型网站迫解
⑥ 爬虫APP逆向实战教程包,含45项绝密技术详解
⑦ 超300本Python电子好书,从入门到高阶应有尽有
⑧ 华为出品独家Python漫画教程,手机也能学习
⑨ 历年互联网企业Python面试真题,复习时非常方便
👉Python学习路线汇总👈
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取哈)
👉Python必备开发工具👈
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
👉实战案例👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉100道Python练习题👈
检查学习结果。
👉面试刷题👈
资料领取
上述这份完整版的Python全套学习资料已经上传CSDN官方,朋友们如果需要可以微信扫描下方CSDN官方认证二维码 即可领取↓↓↓