Python创意多关卡游戏之二维码的梦

python二维码的梦闯关游戏

"""二维码的梦.py
   操作一个二维码图片闯关找到扫描枪的小游戏。
"""
from square import *

def init_audio():
    fail_sound , success_sound = None,None
    try:
        import pygame
        pygame.mixer.init()
    except:
        print("pygame初始化不成功,请用pip install pygame --user重新安装pygame")
        
    if "pygame" in dir():   #如果 pygame标识符在全局名字空间列表中
        try:
            pygame.mixer.music.load("audios/TheAvengers.wav")
            pygame.mixer.music.play(-1,0)
        except:
            print("背景音乐播放不成功!")
        try:
            fail_sound = pygame.mixer.Sound("audios/Fail.wav")
        except:
            print("加载失败音效不成功!请检测aiduos/Fail.wav是否存在.")
        try:
            success_sound = pygame.mixer.Sound("audios/success.wav")
        except:
            print("加载成功音效不成功!请检测audios/success.wav是否存在.")
    return fail_sound,success_sound

def init_screen():
    """初始化屏幕"""
    screen = Screen()              #新建屏幕对象
    screen.delay(0)                #延时
    screen.setup(500,500)          #屏幕大小
    screen.title(game_name)        #标题
    screen.addshape(qrcode_image)  #增加二维码图像,这里引用的是全局变量
    screen.addshape(gun_image)     #扫描枪gif图
    screen.addshape(cry_image)     #哭脸gif图
    screen.bgpic(cover_image)      #封面png图
    
    return screen

def produce_scanner():
    """产生scanner对象"""
    scanner = Turtle(visible = False)
    scanner.shape(gun_image)      #形状为扫描枪
    scanner.penup()               #抬笔
    scanner.goto(0,150)           #坐标定位
    return scanner
    

def scanner_alt_status():
    """每隔10秒秒通二维码的关卡设定隐藏/显示状态"""
    if qrcode.level_number < 3 :   #前三关一直都在等
        scanner.hideturtle()        
    else:
        scanner.showturtle()       #到了第四关显示出来
        
    screen.ontimer(scanner_alt_status,10)
        
def check_condition():
    """到第四关时如果二维码和扫描枪距离小于30并且是可见的,那么表示闯关成功"""
    condition = qrcode.distance(scanner)<30 and scanner.isvisible() 
    if condition == True :         #碰到了扫码枪则隐藏
        try:success_sound.play()
        except:pass
        scanner.hideturtle()
        qrcode.hideturtle()
        qrcode.dead = True
        screen.clear()
        screen.bgpic("images/二维码.png")
        screen.title("请帮忙实现二维码的梦,拿出手机扫一扫!")
        return
    "如果qrcode闯入禁区,那么显示哭脸"
    if qrcode.dead == True:
        try:fail_sound.play()
        except:pass
        screen.clear()
        screen.bgcolor("black")
        screen.title(game_name)
        Turtle(shape=cry_image)
        return
    screen.ontimer(check_condition,10)

You must log in to read the rest of this article. Please log in or register as a user.