当前位置:   article > 正文

【Pygame小游戏】史上最全:《唐诗三百首》合集,每一首都是精华,果断收藏~(学诗+锻炼记忆+Python诗句填空小程序上线啦)_python诗词小游戏

python诗词小游戏

前言

  岁岁年龄岁岁心,不负时光不负卿

哈喽!我是你们的栗子同学,今天给大家来点儿有趣的——

有句话说:“读史使人明智,读诗使人灵秀。”唐诗本来就是中国文化的绚丽瑰宝,是每个人都

该学习的人生必修课。栗子今天特意为大家整理了《唐诗三百首》合集,带你品读千年最美

古诗。赶紧学起来吧!

成语接龙大家都玩过,但诗词填空大家玩过吗?把成语接龙变成填空题。

难度可上涨了不止一个档次呢!这就要考一考你对唐诗三百首的记忆里啦。是不是都还给老师

了~哈哈哈哈。如果你来玩,能完成多少诗句呢?

今天栗子给大家写一个诗句填空的小游戏,看看你能闯关成功吗?

正文

古诗词的赏析——

万紫千红                            草长莺飞

等闲识得东风面,万紫千红总是春。                              草长莺飞二月天,拂堤杨柳醉春烟。

——朱熹《春日》                                                         ——高鼎《村居》    

人面桃花                             寻花问柳

去年今日此门中,人面桃花相映红。                                元戎小队出郊坰,问柳寻花到野亭。

——崔护《题都城南庄》                                                 ——杜甫《严中丞枉驾见过》

比翼连枝                             悲欢离合

在天愿作比翼鸟,在地愿为连理枝。                               人有悲欢离合,月有阴晴圆缺,

天长地久有时尽,此恨绵绵无绝期。                                此事古难全。

——白居易《长恨歌》                                                   ——苏轼《水调歌头》

 一、运行环境

1)前期准备

 Python 3 、Pycharm、pygame。其他内置模块,安装好python环境就可以了。 

 如果需要安装第三方库的话✅下面可以找我拿资料的哈! (win + R 输入cmd 输入安装命令 

pip install 模块名 (如果你觉得安 装速度比较慢, 你可 以切 换国内镜像源

  1. 第三方库的安装: +python安装包 安装教程视频 anaconda5.2.0等等 安装包 +pycharm 社
  2. 区版 专业版 及 激活码免费 (可以找我免费领取的) pip install + 模块名 或者 带镜像源 pip
  3. install -i pypi.douban.com/simple/ +模块名

2)素材资料

二、代码展示

1)主程序:main.py

 
  1. # -*- coding=utf-8 -*-
  2. import sys
  3. import random
  4. import pygame
  5. from pygame.locals import *
  6. from idiom_lib import IdiomLib
  7. if sys.version_info < (3,0):
  8. reload(sys)
  9. sys.setdefaultencoding('utf-8')
  10. elif sys.version_info <= (3,3):
  11. import imp
  12. imp.reload(sys)
  13. else:
  14. import importlib
  15. importlib.reload(sys)
  16. block_num=12
  17. lib = IdiomLib(block_num=block_num)
  18. lib.load_idiom_from_file()
  19. header_height = 30
  20. main_space = 20
  21. block_size = 36
  22. bspace = 2
  23. space = 20
  24. width = block_size * block_num + main_space * 2
  25. height = header_height + block_size * block_num + main_space * 2 + (block_size+space) * 3
  26. pygame.init()
  27. screen = pygame.display.set_mode((width,height))
  28. screencaption = pygame.display.set_caption(u'诗词填空')
  29. font = pygame.font.Font(u'syht.otf', int(block_size*0.8))
  30. dray_gray = 50,50,200
  31. white = 255,255,255
  32. #textImage = font.render(u'你好', True, white)
  33. bg_image = pygame.image.load('bg.jpeg')
  34. bg_image = pygame.transform.scale(bg_image,(width, height))
  35. bg2_image = pygame.image.load('bg2.jpeg')
  36. bg2_image = pygame.transform.scale(bg2_image,(block_size*block_num,block_size*block_num))
  37. block_bg_image = pygame.image.load('tzg.jpg')
  38. block_bg_image = pygame.transform.scale(block_bg_image,(block_size-bspace*2,block_size-bspace*2))
  39. stage = 1
  40. lib.init(stage)
  41. stage_textImage = pygame.font.Font(u'syht.otf', 30).render(u'第%s关'%stage, True, dray_gray)
  42. stage_font_width, stage_font_height = stage_textImage.get_size()
  43. stage_x = int((width - stage_font_width)/2)
  44. stage_y = int((header_height - stage_font_height)/2)+int(main_space/2)
  45. while True:
  46. for event in pygame.event.get():
  47. if event.type == pygame.QUIT:
  48. pygame.quit()
  49. exit()
  50. if event.type == MOUSEBUTTONDOWN:
  51. pressed_array = pygame.mouse.get_pressed()
  52. if pressed_array[0]:
  53. x, y = pygame.mouse.get_pos()
  54. for i in range(block_num):
  55. for j in range(block_num):
  56. bx = main_space + block_size*i+bspace
  57. by = header_height + main_space + block_size*j+bspace
  58. if x >= bx and x <= bx+block_size-bspace*2 and y >= by and y<= by+block_size-bspace*2:
  59. info = lib.matrix.get_val(i, j)
  60. if info and info.state != 1 and info.hide_index >= 0:
  61. if info.op_hide_index>=0:
  62. lib.hide_arr[info.op_hide_index][-1] = None
  63. info.word = ''
  64. info.op_hide_index=-1
  65. lib.check_idiom()
  66. lib.select_rect = i,j
  67. break
  68. sx = main_space
  69. sy = header_height + main_space+ block_size*block_num +space
  70. n = 0
  71. for hi in range(len(lib.hide_arr)):
  72. tmp_x = sx + (n%block_num)*block_size
  73. tmp_y = sy + int(n/block_num)*block_size
  74. if lib.hide_arr[hi][-1] is None and x >= tmp_x and x <= tmp_x+block_size-bspace*2 and y >= tmp_y and y<= tmp_y+block_size-bspace*2:
  75. info = lib.matrix.get_val(lib.select_rect[0],lib.select_rect[1])
  76. info.word = lib.hide_arr[hi][2]
  77. info.op_hide_index = hi
  78. info.state = 0
  79. lib.hide_arr[hi][-1] = lib.select_rect
  80. lib.select_rect = lib.get_next_select(lib.select_rect[0],lib.select_rect[1])
  81. flag = lib.check_idiom()
  82. if flag:
  83. stage += 1
  84. lib.init(stage)
  85. stage_textImage = pygame.font.Font(u'syht.otf', 30).render(u'第%s关'%stage, True, dray_gray)
  86. break
  87. n += 1
  88. screen.blit(bg_image, (0,0))
  89. screen.blit(stage_textImage, (stage_x,stage_y))
  90. panel = screen.subsurface((main_space,header_height+main_space,block_size*block_num,block_size*block_num))
  91. panel.blit(bg2_image, (0,0))
  92. for i in range(block_num):
  93. for j in range(block_num):
  94. info = lib.matrix.get_val(i,j)
  95. if info is not None:
  96. bx = block_size*i+bspace
  97. by = block_size*j+bspace
  98. panel.blit(block_bg_image, (bx,by))
  99. if info.state == 1:
  100. textImage = font.render(info.word, True, (30,144,30))
  101. elif info.is_lock == 1:
  102. textImage = font.render(info.word, True, (100,100,100))
  103. elif info.state == 2:
  104. textImage = font.render(info.word, True, (255,0,0))
  105. else:
  106. textImage = font.render(info.word, True, dray_gray)
  107. tw, th = textImage.get_size()
  108. dx=int((block_size-bspace*2-tw)/2)
  109. dy=int((block_size-bspace*2-th)/2)
  110. panel.blit(textImage, (bx+dx,by+dy))
  111. if (i,j) == lib.select_rect:
  112. pygame.draw.rect(panel,(255,0,0),(bx,by,block_size-bspace*2,block_size-bspace*2),2)
  113. sx = main_space
  114. sy = header_height + main_space+ block_size*block_num +space
  115. n = 0
  116. for i,j,word,op in lib.hide_arr:
  117. screen.blit(block_bg_image, (sx + (n%block_num)*block_size,sy + int(n/block_num)*block_size))
  118. if op is None:
  119. textImage = font.render(word, True, dray_gray)
  120. tw, th = textImage.get_size()
  121. dx=int((block_size-bspace*2-tw)/2)
  122. dy=int((block_size-bspace*2-th)/2)
  123. screen.blit(textImage, (dx+sx+ (n%block_num)*block_size,dy+sy+ int(n/block_num)*block_size))
  124. n+=1
  125. pygame.display.update()

2)程序idiom_lib.py

  1. # -*- coding=utf-8 -*-
  2. import sys
  3. import random
  4. if sys.version_info < (3,0):
  5. reload(sys)
  6. sys.setdefaultencoding('utf-8')
  7. elif sys.version_info <= (3,3):
  8. import imp
  9. imp.reload(sys)
  10. else:
  11. import importlib
  12. importlib.reload(sys)
  13. class IdiomInfo(object):
  14. def __init__(self,idiom):
  15. self.idiom = idiom
  16. self.dire = 0
  17. self.word_arr = []
  18. def to_str(self):
  19. arr = []
  20. for word_info in self.word_arr:
  21. arr.append('%s %s %s'%(word_info.i,word_info.j,word_info.word))
  22. return '%s,%s,%s'%(self.idiom, self.dire, '|'.join(arr))
  23. class WordInfo(object):
  24. def __init__(self, word, i, j):
  25. self.i = i
  26. self.j = j
  27. self.word = word
  28. self.is_lock = True
  29. self.state = -1
  30. self.hide_index = -1
  31. self.op_hide_index = -1
  32. class Matrix(object):
  33. rows = 0
  34. cols = 0
  35. data = []
  36. def __init__(self, rows, cols, data=None):
  37. self.rows = rows
  38. self.cols = cols
  39. if data is None: data = [None for i in range(rows * cols)]
  40. self.data = data
  41. def set_val(self, x, y, val):
  42. self.data[y * self.cols + x] = val
  43. def get_val(self, x, y):
  44. return self.data[y * self.cols + x]
  45. def exist_val_four_around(self, x, y, ignore_set):
  46. move_arr = [(-1,0),(1,0),(0,-1),(0,1)]
  47. for dx,dy in move_arr:
  48. tx = x + dx
  49. ty = y + dy
  50. if (tx,ty) in ignore_set: continue
  51. if tx < 0 or tx >= self.cols or ty <0 or ty >= self.rows: continue
  52. if self.data[ty * self.cols + tx]: return True
  53. return False
  54. class IdiomLib():
  55. def __init__(self, block_num=12):
  56. self.word_dic={}
  57. self.word_arr=[]
  58. self.block_num=block_num
  59. self.matrix = Matrix(self.block_num, self.block_num)
  60. self.idiom_dic={}
  61. self.all_word_num=0
  62. self.hide_arr = []
  63. def load_idiom_from_file(self, filename='poetry.txt'):
  64. if sys.version_info < (3,0):
  65. f = open(filename)
  66. else:
  67. f = open(filename,encoding='UTF-8')
  68. all_idiom = f.readlines()
  69. f.close()
  70. for idiom in all_idiom:
  71. if sys.version_info < (3,0):
  72. idiom = idiom.strip().decode('utf-8')
  73. else:
  74. idiom = idiom.strip()
  75. for word in idiom:
  76. if word not in self.word_dic:
  77. self.word_dic[word] = [idiom]
  78. else:
  79. self.word_dic[word].append(idiom)
  80. self.word_arr = list(self.word_dic.keys())
  81. def check_new_idiom(self, new_idiom, new_dire, word_info):
  82. windex = new_idiom.index(word_info.word)
  83. cx,cy = word_info.i, word_info.j
  84. ignore_set = set([(cx,cy)])
  85. new_idiom_word_arr=[]
  86. for i in range(-windex,-windex+len(new_idiom)):
  87. if i==0:
  88. new_idiom_word_arr.append(word_info)
  89. else:
  90. tx = cx+i if new_dire == 0 else cx
  91. if tx < 0 or tx >= self.block_num: return None,None
  92. ty = cy if new_dire == 0 else cy+i
  93. if ty < 0 or ty >= self.block_num: return None,None
  94. if self.matrix.exist_val_four_around(tx, ty, ignore_set): return None,None
  95. old_word_info = self.matrix.get_val(tx, ty)
  96. if old_word_info:
  97. return None,None
  98. new_word_info = WordInfo(new_idiom[i+windex], tx, ty)
  99. new_idiom_word_arr.append(new_word_info)
  100. return new_idiom_word_arr,windex
  101. def add_idiom_to_matrix(self, idiom_num):
  102. if idiom_num == 0: return 0
  103. for idiom,idiom_info in self.idiom_dic.items():
  104. dire = idiom_info.dire
  105. new_dire = 1 - dire
  106. for word_info in idiom_info.word_arr:
  107. word = word_info.word
  108. idiom_list = self.word_dic[word]
  109. for new_idiom in idiom_list:
  110. if new_idiom in self.idiom_dic: continue
  111. new_idiom_word_arr,windex = self.check_new_idiom(new_idiom, new_dire, word_info)
  112. if new_idiom_word_arr:
  113. new_idiom_info = IdiomInfo(new_idiom)
  114. new_idiom_info.dire = new_dire
  115. for new_index in range(len(new_idiom_word_arr)):
  116. new_word_info = new_idiom_word_arr[new_index]
  117. if new_index == windex:
  118. new_idiom_info.word_arr.append(word_info)
  119. else:
  120. self.matrix.set_val(new_word_info.i, new_word_info.j , new_word_info)
  121. new_idiom_info.word_arr.append(new_word_info)
  122. self.idiom_dic[new_idiom] = new_idiom_info
  123. return len(new_idiom) -1 + self.add_idiom_to_matrix(idiom_num - 1)
  124. return 0
  125. def get_idiom_matrix(self, idiom_num):
  126. self.idiom_dic={}
  127. cx = int(self.block_num/2)-1
  128. cy = int(self.block_num/2)-1
  129. n = random.randint(0,len(self.word_arr)-1)
  130. word = self.word_arr[n]
  131. idiom = self.word_dic[word][0]
  132. wn = len(idiom)
  133. self.idiom_dic[idiom] = IdiomInfo(idiom)
  134. last_i = -100
  135. for i in range(len(idiom)):
  136. word_info = WordInfo(idiom[i],cx-int(wn/2)+1+i,cy)
  137. self.matrix.set_val(cx-int(wn/2)+1+i,cy,word_info)
  138. self.idiom_dic[idiom].word_arr.append(word_info)
  139. wn += self.add_idiom_to_matrix(idiom_num-1)
  140. return wn
  141. def get_hide_arr(self, percent):
  142. self.hide_arr=[]
  143. # for k,v in self.idiom_dic.items():
  144. # n = random.randint(0, len(v.word_arr)-1)
  145. # word_info = v.word_arr[n]
  146. # if word_info.hide_index != -1:continue
  147. # word = word_info.word
  148. # info = self.matrix.get_val(word_info.i,word_info.j)
  149. # info.word = ''
  150. # info.hide_index = len(self.hide_arr)
  151. # info.is_lock = False
  152. # self.hide_arr.append([word_info.i,word_info.j,word,None])
  153. # tmp_arr = []
  154. # for i in range(self.block_num):
  155. # for j in range(self.block_num):
  156. # info = self.matrix.get_val(i,j)
  157. # if info and info.word:
  158. # tmp_arr.append((i,j,info.word))
  159. # while len(self.hide_arr) < self.all_word_num*percent:
  160. # n = random.randint(0,len(tmp_arr)-1)
  161. # i,j,word = tmp_arr.pop(n)
  162. # info = self.matrix.get_val(i,j)
  163. # info.word = ''
  164. # info.hide_index = len(self.hide_arr)
  165. # info.is_lock = False
  166. # self.hide_arr.append([i,j,word,None])
  167. idiom_word_arr = []
  168. for k,v in self.idiom_dic.items():
  169. arr = []
  170. for word_info in v.word_arr:
  171. arr.append(word_info)
  172. idiom_word_arr.append([k, arr])
  173. #idiom_word_arr.sort(cmp=lambda x,y:cmp(len(y[-1]),len(x[-1])))
  174. idiom_word_arr.sort(key=lambda x:len(x[-1]))
  175. idiom_index = 0
  176. while len(self.hide_arr) < self.all_word_num*percent:
  177. tmp_arr = idiom_word_arr[idiom_index%len(idiom_word_arr)][1]
  178. n = random.randint(0,len(tmp_arr)-1)
  179. info = tmp_arr.pop(n)
  180. word=info.word
  181. info.word = ''
  182. info.hide_index = len(self.hide_arr)
  183. info.is_lock = False
  184. self.hide_arr.append([info.i,info.j,word,None])
  185. idiom_index+=1
  186. return self.hide_arr
  187. def get_next_select(self, x, y):
  188. arr = []
  189. for i in range(self.block_num):
  190. for j in range(self.block_num):
  191. info = self.matrix.get_val(i, j)
  192. if info is not None and len(info.word) == 0:
  193. dist = (i-x)*(i-x)+(j-y)*(j-y)
  194. if i<x: dist+=0.2
  195. if j<y: dist+=0.4
  196. arr.append((i,j,dist))
  197. if len(arr) == 0:
  198. return None
  199. #arr.sort(cmp=lambda x,y:cmp(x[-1],y[-1]))
  200. arr.sort(key=lambda x:x[-1])
  201. return (arr[0][0],arr[0][1])
  202. def check_idiom(self):
  203. for idiom, idiom_info in self.idiom_dic.items():
  204. tmp_idiom_str = ''
  205. word_arr = idiom_info.word_arr
  206. for word_info in word_arr:
  207. word = word_info.word
  208. if len(word) > 0:
  209. tmp_idiom_str+=word
  210. if len(tmp_idiom_str) == len(idiom):
  211. state = 1 if tmp_idiom_str == idiom else 2
  212. else:
  213. state = 0
  214. for word_info in word_arr:
  215. if word_info.state != 1: word_info.state = state
  216. for idiom, idiom_info in self.idiom_dic.items():
  217. word_arr = idiom_info.word_arr
  218. for word_info in word_arr:
  219. if word_info.state != 1:
  220. return False
  221. return True
  222. stage = 1
  223. def init(self, new_stage):
  224. idiom_num = int(new_stage/5)+3
  225. if new_stage>100:
  226. percent = 0.7
  227. else:
  228. percent = 0.2+(new_stage*1.0/100)*(0.7-0.2)
  229. self.matrix = Matrix(self.block_num, self.block_num)
  230. self.all_word_num = self.get_idiom_matrix(idiom_num)
  231. self.get_hide_arr(percent)
  232. self.select_rect = self.hide_arr[0][0],self.hide_arr[0][1]
  233. if __name__ == '__main__':
  234. pass
  235. # lib = IdiomLib(block_num=10)
  236. # lib.load_idiom_from_file()
  237. # arr = []
  238. # for i in range(1,101):
  239. # lib.init(i)
  240. # idiom_arr = []
  241. # for k,v in lib.idiom_dic.items():
  242. # idiom_arr.append(v.to_str())
  243. # hide_arr = []
  244. # for x,y,word,op in lib.hide_arr:
  245. # hide_arr.append('%s %s %s'%(x,y,word))
  246. # arr.append({'hide_num':len(hide_arr),'block_num':lib.block_num, 'word_num':lib.all_word_num,'idiom_arr':';'.join(idiom_arr),'hide_arr':';'.join(hide_arr)})
  247. # #arr.sort(cmp=lambda x,y:cmp(x['hide_num']*2+x['word_num'], y['hide_num']*2+y['word_num']))
  248. # arr.sort(key=lambda x:x['hide_num']*2+x['word_num'])
  249. # import json
  250. # f = open('idiom.json','w+')
  251. # f.write(json.dumps(arr))
  252. # f.close()

三、效果展示

1)第一关

​2)第五关

3)第十一关

关卡越高难度越高哦~填空词也会越来越高滴

总结

这款诗词填空小游戏还挺有趣的吧~不仅能学习诗词还能加深大家的记忆里,锻炼脑力!

如果是学生的话这个游戏挺适合的啦!学习诗词边学边玩儿啦声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】

推荐阅读
相关标签