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)
  
def start_game():
    """生成二维码对象和扫描枪对象"""
    global qrcode,scanner
    screen.onkeypress(None,"return")
    screen.bgpic("nopic")    
    qrcode = Square(-100,0,30,30,qrcode_image,bglist,bump_area_condition,backlist,forwardlist)
    scanner = produce_scanner()
    
    scanner_alt_status()     #根据二维码所在的关卡号显示或隐藏
    check_condition()        #检测一些条件是否成立,决定显示什么内容   
    
if __name__ == "__main__":

    game_name = "二维码的梦,作者:李兴球"
    fail_sound,success_sound = init_audio()
    qrcode,scanner = None,None
    qrcode_image = "images/二维码.gif"
    gun_image   = "images/gun.gif"
    cry_image = "images/哭脸.gif"
    cover_image = "images/封面.png"    
  
    bglist = ['images/level1.png','images/level2.png','images/level3.png','images/level4.png']
    bump_area_condition = []                           #禁区的x和y坐标条件列表
    bump_area_condition.append("y >50 or y < -50 or x < - 200")
    bump_area_condition.append("y >50 or x >50 or  ( x<-50 and y<-50) ")
    bump_area_condition.append(" (x<-50 and y>50) or x > 50 or y <-50")
    bump_area_condition.append("y < -50 or x < -50 or y >200 or (x>50 and y>50)")
    backlist = ["False","x<-250","y>250","x>250"]      #第一关无法回到上一关,所以第一个值为False
    forwardlist = ["x>250","y<-250","x<-250","False"]  #最后一关到底了,所以最后的值为False

    screen = init_screen()
    screen.onkeypress(start_game,"Return")
    screen.listen()
    screen.mainloop()

python二维码的梦闯关游戏