哈喽哈喽!又到了今日更新啦
我相信在疫情期间小伙伴们都和小编一样,除了上班就是宅在家里为祖国做贡献,在大家的努力
下,疫情得到遏制,趋势大大好转,每个人都振奋十足!
但海外疫情却日益严峻
近日我国多地陆续出现境外输入病例
再次提醒我们防控不可松动
少外出、不聚集,做好个人防护
小编认为我们宅在家的同时,不妨静下心来多看书,提升自己的价值!又或许在闲暇之余学习防疫
知识,科学正确学会个人防护!
小编闲暇之余,还为大家写了一款Python版的“消灭病毒小游戏”,无聊的时候大家一起动动手玩一
下~
正文 一、小简介新型冠状病毒泡泡堂游戏.py
本程序借用了pygame的混音器模块,所以需要安装pygame模块。
需要精灵模块1.33版支持请卸载老版本,重新安装最新版本的精灵模块。
安装方法 pip install sprites 程序中迷宫房间是画出来的,当然虫子是不能穿越过去,也不能碰到病毒...
按上下左右键 *** 作虫子移动,按空格键放炸d即可。
二、环境准备(仅展示部分素材)
三、代码演示from sprites import * from pygame import mixer def draw_frame(obj,margin): """用obj角色画边框,margin为边距 本函数用for循环直接画一个正方形也可以。 """ sw = obj.screen.window_width() sh = obj.screen.window_height() bug.topleft() # 移到左上角 bug.addy(-margin) x,y = bug.position() cors = [(x+sw,y),(x+sw,y+margin),(x,y+margin)] f1 = bug.polygon(cors) bug.bottomleft() # 移到左下角 x,y = bug.position() cors = [(x+sw,y),(x+sw,y+margin),(x,y+margin)] f2 = bug.polygon(cors) bug.addy(margin) x,y = bug.position() cors = [(x+margin,y),(x+margin,y+(sh-2*margin)),(x,y+(sh-2*margin))] f3 = bug.polygon(cors) bug.addx(sw-margin) x,y = bug.position() cors = [(x+margin,y),(x+margin,y+(sh-2*margin)),(x,y+(sh-2*margin))] f4 = bug.polygon(cors) return [f1,f2,f3,f4] def draw_cross(obj,length): """画十字架""" d = length/2 bug.goto(-d,d) x,y = bug.position() cors = [(x+length,y),(x+length,y+2*length),(x,y+2*length)] p1 = bug.polygon(cors) bug.goto(-d-2*length,-d) x,y = bug.position() cors = [(x+5*length,y),(x+5*length,y+length),(x,y+length)] p2 = bug.polygon(cors) bug.goto(-d,-d-2*length) x,y = bug.position() cors = [(x+length,y),(x+length,y+2*length),(x,y+2*length)] p3 = bug.polygon(cors) return [p1,p2,p3] def draw_squares(obj,length): """画4个正方形""" items = [] for _ in range(4): i = bug.polygon(4,length) items.append(i) bug.fd(length*5) bug.rt(90) return items width = 800 height = 600 screen = Screen() screen.bgcolor('black') screen.setup(width,height) screen.title('新型冠状病毒泡泡堂游戏') screen.bgpic('封面.png') leftkey = Key('Left') rightkey = Key('Right') upkey = Key('Up') downkey = Key('Down') spacekey = Key('space') screen.listen() PlaySound('audios/泡堂开场音乐.wav',SND_ASYNC) starttime = time.time() while time.time() - starttime < 6 : screen.update() if spacekey.down():break screen.bgpic('nopic') PlaySound(None,SND_PURGE) PlaySound('audios/泡泡堂小区音乐.wav',SND_ASYNC|SND_LOOP) mixer.init() # 初始化混音器 bombsound = mixer.Sound('audios/BOMB1.wav') placesound = mixer.Sound('audios/zoop.wav') bomb = Sprite('bombs',visible=False) bomb.scale(0.5) w = 100 bug = Sprite(visible=False) bug.color('pink','pink') items1 = draw_frame(bug,w/2) # 画紧挨着屏幕的边框 items2 = draw_cross(bug,w/1.5) # 画十字架 bug.goto(-w/2 - w,w/3 + w) items3 = draw_squares(bug,w/1.5) # 画四个正方形 items1.extend(items2) items1.extend(items3) print(items1) # items1是障碍物列表的项目编号 frames = items1 # 取一个别名 bug.color('gray') bug.goto(0,240) info = '''本游戏代码有210行,Python精灵模块开发,作者:李兴球, 迷宫是画出来的,按上下左右键操作虫子移动,按空格键放炸弹。 ''' bug.write(info,align='center') bug.color('black') bug.goto(0,-290) info = '''Python精灵模块基于海龟画图模块开发, 作者亦是李兴球。博客:www.lixingqiu.com''' bug.write(info,align='center') bug.goto(100,100) clock = Clock() bug.show() cors = [(-100,100),(-100,-100),(265,2),(179,-107),(300, 101), (0,-219),(273,-213),(263, -140),(-258, 0),(-116,222)] vsitems = [] # 存储每个病毒项目编号的列表 vsgroup = Group('virus') # 病毒组 for cor in cors: virus = Sprite(shape='res/病毒.png',pos=cor,tag='virus') virus.rotatemode(1) vsitems.append(virus.turtle._item) result = None # 描述游戏成功或失败 running = True # 描述游戏主循环 allow = True # 是否能放炸药? counter = 0 # 炸药的造型计数器 def check_bomb(): global counter,allow,running if running == False:return if bomb.isvisible(): if counter == 0 : screen.ontimer(check_bomb,1000) counter += 1 elif counter < len(bomb._costumes): bomb.nextcostume() if counter < 3: screen.ontimer(check_bomb,1000) else: if counter==3:bombsound.play() # 爆炸声 screen.ontimer(check_bomb,100) counter += 1 elif counter == len(bomb._costumes): bomb.hide() bomb.shapeindex(0) allow = True counter = 0 screen.ontimer(check_bomb,100) else: screen.ontimer(check_bomb,100) check_bomb() while running: if leftkey.down(): bug.setheading(180) bug.fd(5) if bug.overlap_with(items1):bug.bk(5) if rightkey.down(): bug.setheading(0) bug.fd(5) if bug.overlap_with(items1):bug.bk(5) if upkey.down(): bug.setheading(90) bug.fd(5) if bug.overlap_with(items1):bug.bk(5) if downkey.down(): bug.setheading(-90) bug.fd(5) if bug.overlap_with(items1):bug.bk(5) if spacekey.down() and allow == True : print('放炸弹') placesound.play() bomb.goto(bug.position()) bomb.show() allow = False for v in list(vsgroup): # 防止动态改变vsgroup所以加list v.fd(2) if v.overlap_with(frames):v.right(180) if v.overlap_with(bomb) and bomb._costume_index>2: v.remove() if bug.overlap_with(bomb) and bomb._costume_index > 2: running = False result = False if bug.overlap_with(vsitems): running = False result = False if len(vsgroup) == 0 : running = False result = True screen.update() clock.tick(60) print(result) bug.wait(0.5) if result: print('显示成功的封面') screen.clear() screen.bgpic('成功.png') else: print('显示失败的封面') screen.clear() screen.bgpic('失败.png') screen.mainloop()四、效果展示 1)截图展示——
游戏界面
游戏界面
炸d
游戏结束
小结目前疫情还未结束,仍不能掉以轻心!尽量少外出、不聚集,做好个人防护。坚定信心,毫不放松
抓紧抓实抓细各项防控工作,外防输入,内防扩散,坚决打赢这场战役。
完整的素材等:滴滴我吖!
还有更多更多源码等你来领区啦! 文章汇总——汇总: Python文章合集 | (入门到实战、游戏、Turtle、案例等)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)