使用pygame写一个古诗词填空通关游戏
之前写的诗词填空的游戏支持python2,现在对程序进行了修改,兼容支持python2和python3,附下效果图。
下面是两个主程序
idiom_lib.py代码:
#-*-coding=utf-8-*- importsys importrandom ifsys.version_info<(3,0): reload(sys) sys.setdefaultencoding('utf-8') elifsys.version_info<=(3,3): importimp imp.reload(sys) else: importimportlib importlib.reload(sys) classIdiomInfo(object): def__init__(self,idiom): self.idiom=idiom self.dire=0 self.word_arr=[] defto_str(self): arr=[] forword_infoinself.word_arr: arr.append('%s%s%s'%(word_info.i,word_info.j,word_info.word)) return'%s,%s,%s'%(self.idiom,self.dire,'|'.join(arr)) classWordInfo(object): def__init__(self,word,i,j): self.i=i self.j=j self.word=word self.is_lock=True self.state=-1 self.hide_index=-1 self.op_hide_index=-1 classMatrix(object): rows=0 cols=0 data=[] def__init__(self,rows,cols,data=None): self.rows=rows self.cols=cols ifdataisNone:data=[Noneforiinrange(rows*cols)] self.data=data defset_val(self,x,y,val): self.data[y*self.cols+x]=val defget_val(self,x,y): returnself.data[y*self.cols+x] defexist_val_four_around(self,x,y,ignore_set): move_arr=[(-1,0),(1,0),(0,-1),(0,1)] fordx,dyinmove_arr: tx=x+dx ty=y+dy if(tx,ty)inignore_set:continue iftx<0ortx>=self.colsorty<0orty>=self.rows:continue ifself.data[ty*self.cols+tx]:returnTrue returnFalse classIdiomLib(): def__init__(self,block_num=12): self.word_dic={} self.word_arr=[] self.block_num=block_num self.matrix=Matrix(self.block_num,self.block_num) self.idiom_dic={} self.all_word_num=0 self.hide_arr=[] defload_idiom_from_file(self,filename='poetry.txt'): ifsys.version_info<(3,0): f=open(filename) else: f=open(filename,encoding='UTF-8') all_idiom=f.readlines() f.close() foridiominall_idiom: ifsys.version_info<(3,0): idiom=idiom.strip().decode('utf-8') else: idiom=idiom.strip() forwordinidiom: ifwordnotinself.word_dic: self.word_dic[word]=[idiom] else: self.word_dic[word].append(idiom) self.word_arr=list(self.word_dic.keys()) defcheck_new_idiom(self,new_idiom,new_dire,word_info): windex=new_idiom.index(word_info.word) cx,cy=word_info.i,word_info.j ignore_set=set([(cx,cy)]) new_idiom_word_arr=[] foriinrange(-windex,-windex+len(new_idiom)): ifi==0: new_idiom_word_arr.append(word_info) else: tx=cx+iifnew_dire==0elsecx iftx<0ortx>=self.block_num:returnNone,None ty=cyifnew_dire==0elsecy+i ifty<0orty>=self.block_num:returnNone,None ifself.matrix.exist_val_four_around(tx,ty,ignore_set):returnNone,None old_word_info=self.matrix.get_val(tx,ty) ifold_word_info: returnNone,None new_word_info=WordInfo(new_idiom[i+windex],tx,ty) new_idiom_word_arr.append(new_word_info) returnnew_idiom_word_arr,windex defadd_idiom_to_matrix(self,idiom_num): ifidiom_num==0:return0 foridiom,idiom_infoinself.idiom_dic.items(): dire=idiom_info.dire new_dire=1-dire forword_infoinidiom_info.word_arr: word=word_info.word idiom_list=self.word_dic[word] fornew_idiominidiom_list: ifnew_idiominself.idiom_dic:continue new_idiom_word_arr,windex=self.check_new_idiom(new_idiom,new_dire,word_info) ifnew_idiom_word_arr: new_idiom_info=IdiomInfo(new_idiom) new_idiom_info.dire=new_dire fornew_indexinrange(len(new_idiom_word_arr)): new_word_info=new_idiom_word_arr[new_index] ifnew_index==windex: new_idiom_info.word_arr.append(word_info) else: self.matrix.set_val(new_word_info.i,new_word_info.j,new_word_info) new_idiom_info.word_arr.append(new_word_info) self.idiom_dic[new_idiom]=new_idiom_info returnlen(new_idiom)-1+self.add_idiom_to_matrix(idiom_num-1) return0 defget_idiom_matrix(self,idiom_num): self.idiom_dic={} cx=int(self.block_num/2)-1 cy=int(self.block_num/2)-1 n=random.randint(0,len(self.word_arr)-1) word=self.word_arr[n] idiom=self.word_dic[word][0] wn=len(idiom) self.idiom_dic[idiom]=IdiomInfo(idiom) last_i=-100 foriinrange(len(idiom)): word_info=WordInfo(idiom[i],cx-int(wn/2)+1+i,cy) self.matrix.set_val(cx-int(wn/2)+1+i,cy,word_info) self.idiom_dic[idiom].word_arr.append(word_info) wn+=self.add_idiom_to_matrix(idiom_num-1) returnwn defget_hide_arr(self,percent): self.hide_arr=[] idiom_word_arr=[] fork,vinself.idiom_dic.items(): arr=[] forword_infoinv.word_arr: arr.append(word_info) idiom_word_arr.append([k,arr]) #idiom_word_arr.sort(cmp=lambdax,y:cmp(len(y[-1]),len(x[-1]))) idiom_word_arr.sort(key=lambdax:len(x[-1])) idiom_index=0 whilelen(self.hide_arr)0: tmp_idiom_str+=word iflen(tmp_idiom_str)==len(idiom): state=1iftmp_idiom_str==idiomelse2 else: state=0 forword_infoinword_arr: ifword_info.state!=1:word_info.state=state foridiom,idiom_infoinself.idiom_dic.items(): word_arr=idiom_info.word_arr forword_infoinword_arr: ifword_info.state!=1: returnFalse returnTrue stage=1 definit(self,new_stage): idiom_num=int(new_stage/5)+3 ifnew_stage>100: percent=0.7 else: percent=0.2+(new_stage*1.0/100)*(0.7-0.2) self.matrix=Matrix(self.block_num,self.block_num) self.all_word_num=self.get_idiom_matrix(idiom_num) self.get_hide_arr(percent) self.select_rect=self.hide_arr[0][0],self.hide_arr[0][1] if__name__=='__main__': pass
main.py的代码
#-*-coding=utf-8-*- importsys importrandom importpygame frompygame.localsimport* fromidiom_libimportIdiomLib ifsys.version_info<(3,0): reload(sys) sys.setdefaultencoding('utf-8') elifsys.version_info<=(3,3): importimp imp.reload(sys) else: importimportlib importlib.reload(sys) block_num=12 lib=IdiomLib(block_num=block_num) lib.load_idiom_from_file() header_height=30 main_space=20 block_size=36 bspace=2 space=20 width=block_size*block_num+main_space*2 height=header_height+block_size*block_num+main_space*2+(block_size+space)*3 pygame.init() screen=pygame.display.set_mode((width,height)) screencaption=pygame.display.set_caption(u'诗词填空') font=pygame.font.Font(u'syht.otf',int(block_size*0.8)) dray_gray=50,50,200 white=255,255,255 #textImage=font.render(u'你好',True,white) bg_image=pygame.image.load('bg.jpeg') bg_image=pygame.transform.scale(bg_image,(width,height)) bg2_image=pygame.image.load('bg2.jpeg') bg2_image=pygame.transform.scale(bg2_image,(block_size*block_num,block_size*block_num)) block_bg_image=pygame.image.load('tzg.jpg') block_bg_image=pygame.transform.scale(block_bg_image,(block_size-bspace*2,block_size-bspace*2)) stage=1 lib.init(stage) stage_textImage=pygame.font.Font(u'syht.otf',30).render(u'第%s关'%stage,True,dray_gray) stage_font_width,stage_font_height=stage_textImage.get_size() stage_x=int((width-stage_font_width)/2) stage_y=int((header_height-stage_font_height)/2)+int(main_space/2) whileTrue: foreventinpygame.event.get(): ifevent.type==pygame.QUIT: pygame.quit() exit() ifevent.type==MOUSEBUTTONDOWN: pressed_array=pygame.mouse.get_pressed() ifpressed_array[0]: x,y=pygame.mouse.get_pos() foriinrange(block_num): forjinrange(block_num): bx=main_space+block_size*i+bspace by=header_height+main_space+block_size*j+bspace ifx>=bxandx<=bx+block_size-bspace*2andy>=byandy<=by+block_size-bspace*2: info=lib.matrix.get_val(i,j) ifinfoandinfo.state!=1andinfo.hide_index>=0: ifinfo.op_hide_index>=0: lib.hide_arr[info.op_hide_index][-1]=None info.word='' info.op_hide_index=-1 lib.check_idiom() lib.select_rect=i,j break sx=main_space sy=header_height+main_space+block_size*block_num+space n=0 forhiinrange(len(lib.hide_arr)): tmp_x=sx+(n%block_num)*block_size tmp_y=sy+int(n/block_num)*block_size iflib.hide_arr[hi][-1]isNoneandx>=tmp_xandx<=tmp_x+block_size-bspace*2andy>=tmp_yandy<=tmp_y+block_size-bspace*2: info=lib.matrix.get_val(lib.select_rect[0],lib.select_rect[1]) info.word=lib.hide_arr[hi][2] info.op_hide_index=hi info.state=0 lib.hide_arr[hi][-1]=lib.select_rect lib.select_rect=lib.get_next_select(lib.select_rect[0],lib.select_rect[1]) flag=lib.check_idiom() ifflag: stage+=1 lib.init(stage) stage_textImage=pygame.font.Font(u'syht.otf',30).render(u'第%s关'%stage,True,dray_gray) break n+=1 screen.blit(bg_image,(0,0)) screen.blit(stage_textImage,(stage_x,stage_y)) panel=screen.subsurface((main_space,header_height+main_space,block_size*block_num,block_size*block_num)) panel.blit(bg2_image,(0,0)) foriinrange(block_num): forjinrange(block_num): info=lib.matrix.get_val(i,j) ifinfoisnotNone: bx=block_size*i+bspace by=block_size*j+bspace panel.blit(block_bg_image,(bx,by)) ifinfo.state==1: textImage=font.render(info.word,True,(30,144,30)) elifinfo.is_lock==1: textImage=font.render(info.word,True,(100,100,100)) elifinfo.state==2: textImage=font.render(info.word,True,(255,0,0)) else: textImage=font.render(info.word,True,dray_gray) tw,th=textImage.get_size() dx=int((block_size-bspace*2-tw)/2) dy=int((block_size-bspace*2-th)/2) panel.blit(textImage,(bx+dx,by+dy)) if(i,j)==lib.select_rect: pygame.draw.rect(panel,(255,0,0),(bx,by,block_size-bspace*2,block_size-bspace*2),2) sx=main_space sy=header_height+main_space+block_size*block_num+space n=0 fori,j,word,opinlib.hide_arr: screen.blit(block_bg_image,(sx+(n%block_num)*block_size,sy+int(n/block_num)*block_size)) ifopisNone: textImage=font.render(word,True,dray_gray) tw,th=textImage.get_size() dx=int((block_size-bspace*2-tw)/2) dy=int((block_size-bspace*2-th)/2) screen.blit(textImage,(dx+sx+(n%block_num)*block_size,dy+sy+int(n/block_num)*block_size)) n+=1 pygame.display.update()
代码就这么多了,不过这边用到几个额外的依赖:
bg.jpeg用于做整个界面的背景
bg2.jpeg用于做上半部分的背景
tzg.jpg每个文字的格子的背景
words.txt一个成语的列表文件(每行一条成语),如果换成诗词或者歇后语什么的也是没有问题的
syht.otf一个字体库,用于正常显示中文
运行pythonmain.py即可开始游戏
如果嫌格子太多或者太小,可以调一下这两个参数
block_size=32 block_num=12
block_size表示格子的大小
block_num上半部分的区域横竖最多显示多少个格子
block_size=26,block_num=18的效果图:
block_size=40,block_num=10的效果图:
试试你的诗词水平,看能冲到第几关吧!
完整的资源已经上传本站:
http://xiazai.jb51.net/201912/yuanma/guess_idiom_jb51.rar
也可以直接从github下载
https://github.com/zhangenter/guess_idiom
总结
以上所述是小编给大家介绍的使用pygame写一个古诗词填空通关游戏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。