"""二维码的梦.py 操作一个二维码图片闯关找到扫描枪的小游戏。 """ from turtle import * 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() def start_game(): """生成二维码对象和扫描枪对象""" global qrcode,scanner screen.onkeypress(None,"return") 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" screen = init_screen() screen.onkeypress(start_game,"Return") screen.listen() screen.mainloop()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)