Python儿童数字消消乐源代码,小朋友认数字游戏源代码,通过消消乐游戏的方式来让小朋友来学习数字,让孩子在玩中学数字
核心代码
import pygame
from time import sleep
from pygame.locals import *
import sys
from utils import *
from cube import *
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("数字消消乐_Python代码大全")
bg_image = pygame.image.load("./imgs/bg.png")
BG_MUSIC.play(-1)
BG_MUSIC.set_volume(0.2)
game_level = 1
current_choose = 1
first = None
game_stage = 0 # 0:ready 1:playing 2:game pass 3:all_game_pass
game_left_block = Text(LEFT_BLOCK_TEXT, font_size=35, color=(253, 196, 11), pos=(888, 38))
game_left_block.update(MAX_BLOCK_PAIR)
game_click_error = Text(CLICK_ERROR_TEXT, font_size=20, color=(253, 196, 11), pos=(146, 29))
click_error_time = 0
game_click_error.update(0)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
x, y = event.pos
if game_stage == 1:
for cube in player_group:
if cube.rect.collidepoint(x, y):
music_play = pygame.mixer.Sound("./sound/shuzi/" + str(cube.label) + ".wav")
if current_choose == 1:
music_play.play()
cube.float(True)
first = cube
current_choose += 1
elif current_choose == 2:
first_id = first.get_id()
if cube.get_label() == first.get_label() and \
cube.is_not_same(first_id):
cube.float(True)
music_play.play()
cube.yes()
first.ready_to_kill()
cube.ready_to_kill()
emotion_flag = 0
count -= 1
well_done = WellDone()
well_dones.add(well_done)
else:
emotion_flag = 1
first.float(False)
music_play.play()
click_error_time += 1
current_choose = 1
elif game_stage == 2:
if replay_again.collidepoint(x, y):
game_level = game_level
elif next_level.collidepoint(x, y):
game_level += 1
game_stage = 0
elif game_stage == 3:
if return_first_level.collidepoint(x, y):
game_level = 1
game_stage = 0
elif game_exit.collidepoint(x, y):
sys.exit()
if game_stage == 0:
count = MAX_BLOCK_PAIR
click_error_time = 0
sound_play = False
face_emotion = Emotion(764, 32)
emotion_flag = 2
game_level_show = "第" + str(game_level) + "关" + " "
number_start = 1 + (game_level-1)*4
number_end = number_start + 7
game_level_show += "( " + str(number_start) + "~" + str(number_end) + " )"
if game_level <= 3:
game_level_display = Text(game_level_show, font_size=16, color=(253, 196, 11), pos=(99, 75))
elif game_level > 3:
game_level_display = Text(game_level_show, font_size=13, color=(253, 196, 11), pos=(95, 78))
label = load_map(game_level)
player_group = pygame.sprite.Group()
well_dones = pygame.sprite.Group()
for row in range(0, CUBE_MAX_ROW):
for col in range(0, CUBE_MAX_COL):
player = Word(col, row, label[row][col])
player_group.add(player)
game_stage = 1
if game_stage in (1, 2):
screen.blit(bg_image, (0, 0))
player_group.update()
for p in player_group:
p.draw(screen)
well_dones.update()
game_left_block.update(count)
game_left_block.draw(screen)
game_click_error.update(click_error_time)
game_click_error.draw(screen)
face_emotion.update(emotion_flag)
face_emotion.draw(screen)
game_level_display.draw(screen)
if count == 0:
game_stage = 2
if game_level == MAX_GAME_LEVEL:
if count == 0:
game_stage = 3
if game_stage == 2:
if not sound_play:
sound_play = True
GAME_PASS_MUSIC.play()
screen.blit(GAME_PASS, (350, 300))
if game_stage == 3:
screen.blit(GAME_ALL_PASS_BG, (0, 0))
pygame.draw.rect(screen, "gray", (5, 105, WIDTH - 10, HEIGHT - 115), 3)
pygame.display.update()
完整代码下载地址:
https://download.csdn.net/download/weixin_42756970/87263935