第九周作业
第九周作业
一、理论学习
1、慕课学习




2、讲义学习
软件项目管理的对象是软件工程项目。它所涉及的范围覆盖了整个软件工程过程。 为使软件项目开发获得成功,关键问题是必须对软件项目的工作范围、可能风险、需要资源(人、硬件/软件)、要实现的任务、经历的里程碑、花费工作量(成本)、进度安排等做到心中有数。 这种管理在技术工作开始之前就应开始,在软件从概念到实现的过程中继续进行,当软件工程过程最后结束时才终止。绩效管理在软件项目管理中十分重要,直接决定软件项目是否朝正确的方向进行。绩效管理通常具有二维的评价体系,即完成任务的维度和团队贡献的维度。
二、实践学习
本周实践学习主要是解决以下4个问题:
1、完善数据库设置
2、将代码进行封装
3、对点字成诗游戏进行完善
4、对游戏中的bug进行修复
代码仓库连接:https://gitee.com/lixiaohua_03/joint_development_project。本周我主要负责结局问题2和问题3,并解决了部分问题4中出现的bug。针对问题2,我对代码进行了类的封装,方便以后游戏的发布和后期的维护。针对问题3,我在点字成诗游戏中增加了键入文字框、答案显示、答题状况显示和清除按钮设置,并且针对不同区域用户,设置了语言选项。针对问题4,我解决了时间显示结束和游戏结束后时间显示中出现的bug。
本周将基本实现了游戏的所有功能,其操作演示视频链接如下:https://www.bilibili.com/video/BV1QK411V7tZ?pop_share=1
全部代码如下:
1 # -*- coding: UTF-8 -*- 2 # Author: Lu Yao and Li Xiaohua 3 4 import json 5 import random 6 import time 7 import tkinter as tk 8 import tkinter.ttk as ttk 9 import tkinter.messagebox 10 from PIL import Image, ImageTk 11 from ChinesePoetry.langconv import * 12 13 14 class App_poemgame: 15 def __init__(self): 16 self.get_anwser = [] 17 self.right_count = 0 18 self.lan_count = 1 19 self.count = 0 20 self.ans_1 = 0 21 self.ans_2 = 0 22 self.ans_3 = 0 23 self.ans_4 = 0 24 self.ans_5 = 0 25 self.ans_6 = 0 26 self.ans_7 = 0 27 self.ans_8 = 0 28 self.ans_9 = 0 29 self.ans_10 = 0 30 self.ans_11 = 0 31 self.ans_12 = 0 32 self.title_1 = 0 33 self.author_1 = 0 34 self.title_2 = 0 35 self.author_2 = 0 36 self.title_3 = 0 37 self.author_3 = 0 38 self.title_4 = 0 39 self.author_4 = 0 40 self.title_5 = 0 41 self.author_5 = 0 42 self.title_6 = 0 43 self.author_6 = 0 44 self.title_7 = 0 45 self.author_7 = 0 46 self.title_8 = 0 47 self.author_8 = 0 48 self.title_9 = 0 49 self.author_9 = 0 50 self.title_10 = 0 51 self.author_10 = 0 52 self.title_11 = 0 53 self.author_11 = 0 54 self.title_12 = 0 55 self.author_12 = 0 56 self.str_anwser = '' 57 58 self.root = tk.Tk() 59 self.root.title(self.c_lan("诗词小游戏")) 60 self.root.geometry("550x300") 61 self.kwd_1 = tk.StringVar() 62 self.kwd_2 = tk.StringVar() 63 self.kwd_3 = tk.StringVar() 64 self.kwd_4 = tk.StringVar() 65 self.kwd_5 = tk.StringVar() 66 self.kwd_6 = tk.StringVar() 67 self.kwd_7 = tk.StringVar() 68 self.kwd_8 = tk.StringVar() 69 self.kwd_9 = tk.StringVar() 70 self.kwd_10 = tk.StringVar() 71 self.kwd_11 = tk.StringVar() 72 self.kwd_12 = tk.StringVar() 73 self.kwd_13 = tk.StringVar() 74 self.db = tk.StringVar() 75 self.mod = tk.StringVar() 76 self.lan = tk.StringVar() 77 self.point_show = False 78 self.answer_show = False 79 80 start_img = self.process_img("start.jpg") 81 self.start_bg = tk.Canvas(self.root, width=550, height=300) 82 self.start_bg.create_image(280, 160, image=start_img) 83 self.start_bg.pack() 84 self.start_bg.create_text(275, 100, font=("微软雅黑", 50, "bold"), fill="Snow", text=self.c_lan("诗 词 小 游 戏")) 85 set_img = self.process_img("start.jpg") 86 self.set_bg = tk.Canvas(self.root, width=550, height=300) 87 self.set_bg.create_image(280, 160, image=set_img) 88 play_img = self.process_img("play.jpg") 89 self.play_bg = tk.Canvas(self.root, width=550, height=300) 90 self.play_bg.create_image(275, 150, image=play_img) 91 92 self.choose_lan = ttk.Combobox(self.root, font=("微软雅黑", 14), textvariable=self.lan) 93 self.choose_lan["value"] = ("请选择字体/請選擇字體", "简体", self.c_lan("繁体")) 94 self.choose_lan.current(0) 95 self.choose_lan.place(x=155, y=165) 96 97 style_yellow = ttk.Style() 98 style_yellow.theme_use("clam") 99 style_yellow.configure("yellow.Horizontal.TProgressbar", background="yellow") 100 style_red = ttk.Style() 101 style_red.theme_use("clam") 102 style_red.configure("red.Horizontal.TProgressbar", background="red") 103 style_green = ttk.Style() 104 style_green.theme_use("clam") 105 style_green.configure("green.Horizontal.TProgressbar", background="green") 106 self.time_left = ttk.Progressbar(self.root, length=550, mode="determinate", maximum=100000, style="green.Horizontal.TProgressbar") 107 self.time_lab = tk.Label(self.root, bg="white", fg="black", font=("微软雅黑", 14, "bold"), width=3, height=1, relief="ridge", borderwidth=10) 108 109 self.sta_anwser = tk.Label(self.root, width=25, font=("微软雅黑", 10), anchor='w', justify='left') 110 self.key_anwser = tk.Label(self.root, width=14, font=("微软雅黑", 18), bg='white', anchor='w', justify='left') 111 self.clear_btn = tk.Button(self.root, text=self.c_lan("清除"), font=("微软雅黑", 12), command = self.clear_ans, 112 width=4) 113 114 self.enter_btn = tk.Button(self.root, text=self.c_lan("进入游戏/進入遊戲"), font=('微软雅黑', 14, "bold"), width=14, height=1, 115 command=self.set_game, relief="raised", borderwidth=10) 116 self.enter_btn.place(x=180, y=220) 117 self.start_btn = tk.Button(self.root, text=self.c_lan("开始游戏"), font=('微软雅黑', 14, "bold"), width=10, height=1, 118 command=self.choose_game, relief="raised", borderwidth=10) 119 self.quit_btn = tk.Button(self.root, text=self.c_lan("退出"), font=('微软雅黑', 14, "bold"), width=5, height=1, 120 command=self.quit_game, relief="raised", borderwidth=10) 121 self.change_btn = tk.Button(self.root, text=self.c_lan("换一组"), font=('微软雅黑', 14, "bold"), width=6, height=1, 122 command=self.choose_game, relief="raised", borderwidth=10) 123 self.back_btn = tk.Button(self.root, text=self.c_lan("返回设置"), font=('微软雅黑', 14, "bold"), width=7, height=1, 124 command=self.back_set, relief="raised", borderwidth=10) 125 self.btn_1 = tk.Button(self.root, textvariable=self.kwd_1, bg="red", fg="white", font=("微软雅黑", 15, "bold"), 126 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_1, self.ans_1, self.title_1, self.author_1)) 127 self.btn_2 = tk.Button(self.root, textvariable=self.kwd_2, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 128 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_2, self.ans_2, self.title_2, self.author_2)) 129 self.btn_3 = tk.Button(self.root, textvariable=self.kwd_3, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 130 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_3, self.ans_3, self.title_3, self.author_3)) 131 self.btn_4 = tk.Button(self.root, textvariable=self.kwd_4, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 132 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_4, self.ans_4, self.title_4, self.author_4)) 133 self.btn_5 = tk.Button(self.root, textvariable=self.kwd_5, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 134 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_5, self.ans_5, self.title_5, self.author_5)) 135 self.btn_6 = tk.Button(self.root, textvariable=self.kwd_6, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 136 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_6, self.ans_6, self.title_6, self.author_6)) 137 self.btn_7 = tk.Button(self.root, textvariable=self.kwd_7, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 138 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_7, self.ans_7, self.title_7, self.author_7)) 139 self.btn_8 = tk.Button(self.root, textvariable=self.kwd_8, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 140 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_8, self.ans_8, self.title_8, self.author_8)) 141 self.btn_9 = tk.Button(self.root, textvariable=self.kwd_9, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 142 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_9, self.ans_9, self.title_9, self.author_9)) 143 self.btn_10 = tk.Button(self.root, textvariable=self.kwd_10, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 144 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_10, self.ans_10, self.title_10, self.author_10)) 145 self.btn_11 = tk.Button(self.root, textvariable=self.kwd_11, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 146 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_11, self.ans_11, self.title_11, self.author_11)) 147 self.btn_12 = tk.Button(self.root, textvariable=self.kwd_12, bg="red", fg="white", font=("微软雅黑", 14, "bold"), 148 relief="raised", borderwidth=10, command=lambda: self.answer_qu(self.btn_12, self.ans_12, self.title_12, self.author_12)) 149 self.root.protocol("WM_DELETE_WINDOW", self.close_window) 150 self.root.mainloop() 151 152 def open_db(self, json_file): # 打开诗词库 153 text = open(json_file, "r", encoding="utf-8") 154 poetry_dict = json.load(text) 155 text.close() 156 return poetry_dict 157 158 def process_img(self, jpg_file): # 处理jpg格式的图片 159 source_img = Image.open(jpg_file) 160 real_img = ImageTk.PhotoImage(source_img) 161 return real_img 162 163 def countdown(self, seconds): # 倒计时模块 164 t = seconds * 100 + 100 165 i = 100000 // seconds 166 j = seconds 167 k = i // 100 168 self.time_left["value"] += i * (seconds // 10) * 0.3 169 self.time_lab.place(x=450, y=150) 170 for s in range(t, -1, -1): 171 if seconds <= 2 / 3 * j: 172 self.time_left["style"] = "yellow.Horizontal.TProgressbar" 173 if seconds <= 1 / 3 * j: 174 self.time_left["style"] = "red.Horizontal.TProgressbar" 175 try: 176 self.time_lab["text"] = s // 100 177 self.time_left["value"] -= k 178 self.root.update() 179 time.sleep(0.01) 180 seconds -= 0.01 181 except tk.TclError: 182 break 183 if s == 0: 184 self.change_btn["text"] = self.c_lan("重新开始") 185 if self.mod.get() == self.c_lan("出口成诗"): 186 tk.messagebox.showinfo(message=self.c_lan("时间到!共答对%s题") % self.right_count) 187 self.right_count = 0 188 elif self.mod.get() == self.c_lan("点字成诗"): 189 tk.messagebox.showinfo( 190 message=self.c_lan("游戏结束!共回答%s题,答对%s题!") % (self.count, self.right_count)) 191 self.sta_anwser.place_forget() 192 self.key_anwser.place_forget() 193 self.clear_btn.place_forget() 194 self.change_btn.place(x=155, y=220) 195 self.sta_anwser['text'] = '' 196 self.key_anwser['text'] = '' 197 self.str_anwser = '' 198 self.right_count = 0 199 self.count = 0 200 if self.count == 10: 201 self.count = 0 202 break 203 if self.right_count == 12: 204 self.right_count = 0 205 break 206 207 def show_btns(self): # 显示关键词按钮 208 self.btn_1.place(x=10, y=25, width=105, height=60) 209 self.btn_2.place(x=115, y=25, width=105, height=60) 210 self.btn_3.place(x=220, y=25, width=105, height=60) 211 self.btn_4.place(x=325, y=25, width=105, height=60) 212 self.btn_5.place(x=10, y=85, width=105, height=60) 213 self.btn_6.place(x=115, y=85, width=105, height=60) 214 self.btn_7.place(x=220, y=85, width=105, height=60) 215 self.btn_8.place(x=325, y=85, width=105, height=60) 216 self.btn_9.place(x=10, y=145, width=105, height=60) 217 self.btn_10.place(x=115, y=145, width=105, height=60) 218 self.btn_11.place(x=220, y=145, width=105, height=60) 219 self.btn_12.place(x=325, y=145, width=105, height=60) 220 221 def set_game(self): # 游戏设置界面 222 if self.lan.get() == "请选择字体/請選擇字體": 223 tk.messagebox.showinfo(message="请选择字体!\n請選擇字體!") 224 return 225 if self.lan.get() == "繁体": #字体默认为简体,把按钮初始化放在设置函数中才会发生字体变化 226 self.lan_count = 0 # 在次之后设置的字体均为繁体 227 self.set_bg.create_text(275, 38, font=("微软雅黑", 20, "bold"), fill="Snow", text=self.c_lan("游戏设置")) 228 self.choose_mod = ttk.Combobox(self.root, font=("微软雅黑", 14), textvariable=self.mod) 229 self.choose_mod["value"] = (self.c_lan("请选择游戏模式"), self.c_lan("出口成诗"), self.c_lan("点字成诗")) 230 self.choose_mod.current(0) 231 self.choose_db = ttk.Combobox(self.root, font=("微软雅黑", 14), textvariable=self.db) 232 self.choose_db["value"] = (self.c_lan("请选择诗词库"), self.c_lan("全段诗词库"), self.c_lan("小学诗词库")) 233 self.choose_db.current(0) 234 self.set_time = tk.Spinbox(self.root, font=("微软雅黑", 14), from_=100, to=1000, increment=10) 235 self.set_time["values"] = [self.c_lan("请设置时间(秒)"), 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 236 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 237 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500] 238 self.choose_lan.place_forget() 239 self.enter_btn.place_forget() 240 self.start_bg.pack_forget() 241 self.set_bg.pack() 242 self.set_time.place(x=155, y=175) 243 self.choose_db.place(x=155, y=120) 244 self.choose_mod.place(x=155, y=65) 245 self.start_btn.place(x=205, y=220) 246 self.start_btn["text"] = self.c_lan("开始游戏") 247 self.root.title(self.c_lan("诗词小游戏")) 248 self.change_btn["text"] = self.c_lan("换一组") 249 self.quit_btn["text"] = self.c_lan("退出") 250 self.back_btn["text"] = self.c_lan("返回设置") 251 self.clear_btn["text"] = self.c_lan("清除") 252 253 def identify_db(self): # 识别诗词库 254 global db_file 255 if self.db.get() == self.c_lan("全段诗词库"): 256 db_file = "全段诗词库.json" 257 if self.db.get() == self.c_lan("小学诗词库"): 258 db_file = "小学诗词库.json" 259 return db_file 260 261 def choose_game(self): # 选择游戏模式 262 try: 263 if self.mod.get() == self.c_lan("请选择游戏模式") or self.db.get() == self.c_lan("请选择诗词库") \ 264 or self.set_time.get() == self.c_lan("请设置时间(秒)"): 265 tk.messagebox.showinfo(message=self.c_lan("请在游戏开始前完成游戏设置!")) 266 elif self.mod.get() == self.c_lan("出口成诗"): 267 self.game_fun1() 268 elif self.mod.get() == self.c_lan("点字成诗"): 269 self.game_fun2() 270 except SyntaxError: 271 pass 272 273 def c_lan(self, line): 274 # 将简体转换成繁体 275 if self.lan_count == 0: 276 line = Converter("zh-hant").convert(line) 277 return line 278 if self.lan_count == 1: 279 return line 280 281 def game_fun1(self): # 出口成诗游戏逻辑函数 282 self.time_left["value"] = 100000 283 self.time_left["style"] = "green.Horizontal.TProgressbar" 284 record = [] 285 self.refresh_btns() 286 seconds = eval(self.set_time.get()) 287 json_file = self.identify_db() 288 poetry_dict = self.open_db(json_file) 289 self.set_time.place_forget() 290 self.choose_db.place_forget() 291 self.choose_mod.place_forget() 292 self.start_btn.place_forget() 293 self.set_bg.pack_forget() 294 self.change_btn.place(x=230, y=220) 295 self.back_btn.place(x=10, y=220) 296 self.quit_btn.place(x=455, y=220) 297 self.time_left.place(x=0, y=0) 298 self.play_bg.pack() 299 self.show_btns() 300 for i in range(100): 301 loc = random.randint(1, 100) 302 if loc not in record: 303 record.append(loc) 304 line_count = poetry_dict[loc - 1]["line"] 305 line_num = random.randint(1, line_count) 306 if len(record) == 1: 307 self.kwd_1.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 308 self.ans_1 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 309 self.title_1 = self.c_lan(poetry_dict[loc - 1]["title"]) 310 self.author_1 = self.c_lan(poetry_dict[loc - 1]["author"]) 311 elif len(record) == 2: 312 self.kwd_2.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 313 self.ans_2 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 314 self.title_2 = self.c_lan(poetry_dict[loc - 1]["title"]) 315 self.author_2 = self.c_lan(poetry_dict[loc - 1]["author"]) 316 elif len(record) == 3: 317 self.kwd_3.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 318 self.ans_3 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 319 self.title_3 = self.c_lan(poetry_dict[loc - 1]["title"]) 320 self.author_3 = self.c_lan(poetry_dict[loc - 1]["author"]) 321 elif len(record) == 4: 322 self.kwd_4.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 323 self.ans_4 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 324 self.title_4 = self.c_lan(poetry_dict[loc - 1]["title"]) 325 self.author_4 = self.c_lan(poetry_dict[loc - 1]["author"]) 326 elif len(record) == 5: 327 self.kwd_5.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 328 self.ans_5 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 329 self.title_5 = self.c_lan(poetry_dict[loc - 1]["title"]) 330 self.author_5 = self.c_lan(poetry_dict[loc - 1]["author"]) 331 elif len(record) == 6: 332 self.kwd_6.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 333 self.ans_6 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 334 self.title_6 = self.c_lan(poetry_dict[loc - 1]["title"]) 335 self.author_6 = self.c_lan(poetry_dict[loc - 1]["author"]) 336 elif len(record) == 7: 337 self.kwd_7.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 338 self.ans_7 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 339 self.title_7 = self.c_lan(poetry_dict[loc - 1]["title"]) 340 self.author_7 = self.c_lan(poetry_dict[loc - 1]["author"]) 341 elif len(record) == 8: 342 self.kwd_8.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 343 self.ans_8 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 344 self.title_8 = self.c_lan(poetry_dict[loc - 1]["title"]) 345 self.author_8 = self.c_lan(poetry_dict[loc - 1]["author"]) 346 elif len(record) == 9: 347 self.kwd_9.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 348 self.ans_9 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 349 self.title_9 = self.c_lan(poetry_dict[loc - 1]["title"]) 350 self.author_9 = self.c_lan(poetry_dict[loc - 1]["author"]) 351 elif len(record) == 10: 352 self.kwd_10.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 353 self.ans_10 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 354 self.title_10 = self.c_lan(poetry_dict[loc - 1]["title"]) 355 self.author_10 = self.c_lan(poetry_dict[loc - 1]["author"]) 356 elif len(record) == 11: 357 self.kwd_11.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 358 self.ans_11 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 359 self.title_11 = self.c_lan(poetry_dict[loc - 1]["title"]) 360 self.author_11 = self.c_lan(poetry_dict[loc - 1]["author"]) 361 elif len(record) == 12: 362 self.kwd_12.set(self.c_lan(poetry_dict[loc - 1]["keyword"][line_num - 1])) 363 self.ans_12 = self.c_lan(poetry_dict[loc - 1]["text"][line_num - 1]) 364 self.title_12 = self.c_lan(poetry_dict[loc - 1]["title"]) 365 self.author_12 = self.c_lan(poetry_dict[loc - 1]["author"]) 366 self.countdown(seconds) 367 break 368 if loc in record: 369 continue 370 371 def game_fun2(self): # 点字成诗游戏逻辑函数 372 global click_ans1 373 if self.count == 0: 374 self.time_left["value"] = 100000 375 self.time_left["style"] = "green.Horizontal.TProgressbar" 376 self.refresh_btns() 377 seconds = eval(self.set_time.get()) 378 json_file = self.identify_db() 379 poetry_dict = self.open_db(json_file) 380 self.set_time.place_forget() 381 self.choose_db.place_forget() 382 self.choose_mod.place_forget() 383 self.start_btn.place_forget() 384 self.set_bg.pack_forget() 385 self.change_btn.place_forget() 386 self.back_btn.place(x=10, y=220) 387 self.quit_btn.place(x=455, y=220) 388 self.time_left.place(x=0, y=0) 389 self.clear_btn.place(x=370, y=220) 390 self.key_anwser.place(x=140, y=220) 391 self.sta_anwser.place(x=140, y=260) 392 self.play_bg.pack() 393 # 随机生成诗句及答案 394 self.show_btns() 395 loc = random.randint(1, 100) 396 line_count = poetry_dict[loc - 1]["line"] 397 line_num = random.randint(1, line_count) 398 sentences = re.findall(r'[\u4E00-\u9FA5]+', poetry_dict[loc - 1]["text"][line_num - 1]) 399 sentence = sentences[random.randint(0, 1)] 400 click_ans1 = self.c_lan(sentence) 401 words = re.findall(r'[\u4E00-\u9FA5]', self.c_lan(sentence)) 402 # 随机加入干扰字 403 for i in range(0, 12 - len(words)): 404 loc2 = random.randint(1, 100) 405 line_count2 = poetry_dict[loc2 - 1]["line"] 406 line_num2 = random.randint(1, line_count2) 407 sentences2 = re.findall(r'[\u4E00-\u9FA5]+', poetry_dict[loc2 - 1]["text"][line_num2 - 1]) 408 sentence2 = sentences2[random.randint(0, 1)] 409 words2 = re.findall(r'[\u4E00-\u9FA5]', self.c_lan(sentence2)) 410 words.append(words2[random.randint(0, len(words2) - 1)]) 411 # 将生成好的选项加入按钮 412 random.shuffle(words) 413 self.kwd_1.set(words[0]) 414 self.kwd_2.set(words[1]) 415 self.kwd_3.set(words[2]) 416 self.kwd_4.set(words[3]) 417 self.kwd_5.set(words[4]) 418 self.kwd_6.set(words[5]) 419 self.kwd_7.set(words[6]) 420 self.kwd_8.set(words[7]) 421 self.kwd_9.set(words[8]) 422 self.kwd_10.set(words[9]) 423 self.kwd_11.set(words[10]) 424 self.kwd_12.set(words[11]) 425 if self.count == 0: 426 self.countdown(seconds) 427 428 def answer_qu(self, btn_x, ans_x, title_x, author_x): # 游戏回答界面及逻辑函数 429 if self.mod.get() == self.c_lan(self.c_lan("出口成诗")): 430 global ans_entry 431 global ans_window 432 global point_lab 433 global point_btn 434 btn_x["bg"] = "grey" 435 ans_window = tk.Toplevel() 436 ans_window.protocol("WM_DELETE_WINDOW", lambda: self.close_ans(btn_x)) 437 window_width = self.root.winfo_screenwidth() 438 window_height = self.root.winfo_screenheight() 439 w = int((window_width - 300) / 2) 440 h = int((window_height - 100) / 2) 441 ans_window.geometry("400x170+%s+%s" % (w, h)) 442 ans_window.title(self.c_lan("请输入答案")) 443 ans_entry = tk.Entry(ans_window, width=28, font=("微软雅黑", 14)) 444 ans_entry.pack(pady=10) 445 ans_lab = tk.Label(ans_window, text="“%s”" % btn_x["text"], font=("微软雅黑", 14, "bold")) 446 ans_lab.pack() 447 point_lab = tk.Label(ans_window, font=("微软雅黑", 14, "bold")) 448 point_lab.pack() 449 confirm_btn = tk.Button(ans_window, text=self.c_lan("确认"), command=lambda: self.judge_ans(btn_x, ans_x), 450 font=("微软雅黑", 14), width=5, height=1) 451 confirm_btn.pack(anchor=tk.S, side=tk.LEFT, padx=40, pady=10) 452 clear_btn = tk.Button(ans_window, text=self.c_lan("清除"), command=self.clear_ans, font=("微软雅黑", 14), 453 width=5, height=1) 454 clear_btn.pack(anchor=tk.S, side=tk.RIGHT, padx=40, pady=10) 455 point_btn = tk.Button(ans_window, text=self.c_lan("提示"), command=lambda: self.show_point(title_x, author_x, btn_x, ans_x), 456 font=("微软雅黑", 14), width=5, height=1) 457 point_btn.pack(anchor=tk.S, side=tk.BOTTOM, pady=10) 458 elif self.mod.get() == self.c_lan("点字成诗"): 459 btn_x["bg"] = "grey" 460 self.str_anwser += self.c_lan(btn_x["text"]) 461 self.key_anwser["text"] = self.c_lan(self.str_anwser) 462 if len(self.str_anwser) == len(click_ans1): 463 if self.str_anwser == click_ans1: 464 self.count += 1 465 self.right_count += 1 466 if self.count == 10: 467 self.sta_anwser["text"] = self.c_lan("回答正确!已回答%s题,答对%s题!") % (self.count, self.right_count) 468 tk.messagebox.showinfo( 469 message=self.c_lan("游戏结束!共回答%s题,答对%s题!") % (self.count, self.right_count)) 470 self.sta_anwser.place_forget() 471 self.key_anwser.place_forget() 472 self.clear_btn.place_forget() 473 self.change_btn["text"] = self.c_lan("重新开始") 474 self.change_btn.place(x=155, y=220) 475 self.sta_anwser['text'] = '' 476 self.key_anwser['text'] = '' 477 self.str_anwser = '' 478 self.right_count = 0 479 480 else: 481 self.sta_anwser["text"] = self.c_lan("回答正确!已回答%s题,答对%s题!") % (self.count, self.right_count) 482 self.str_anwser = '' 483 self.game_fun2() 484 else: 485 self.count += 1 486 self.key_anwser["text"] = click_ans1 487 if self.count == 10: 488 self.sta_anwser["text"] = self.c_lan("回答错误!已回答%s题,答对%s题!") % (self.count, self.right_count) 489 tk.messagebox.showinfo( 490 message=self.c_lan("游戏结束!共回答%s题,答对%s题!") % (self.count, self.right_count)) 491 self.sta_anwser.place_forget() 492 self.key_anwser.place_forget() 493 self.clear_btn.place_forget() 494 self.change_btn["text"] = self.c_lan("重新开始") 495 self.change_btn.place(x=155, y=220) 496 self.sta_anwser['text'] = '' 497 self.key_anwser['text'] = '' 498 self.str_anwser = '' 499 self.right_count = 0 500 501 else: 502 self.sta_anwser["text"] = self.c_lan("回答错误!已回答%s题,答对%s题!") % (self.count, self.right_count) 503 self.str_anwser = '' 504 self.game_fun2() 505 506 def refresh_btns(self): # 刷新显示关键字的按钮 507 self.btn_1["bg"] = "red" 508 self.btn_2["bg"] = "red" 509 self.btn_3["bg"] = "red" 510 self.btn_4["bg"] = "red" 511 self.btn_5["bg"] = "red" 512 self.btn_6["bg"] = "red" 513 self.btn_7["bg"] = "red" 514 self.btn_8["bg"] = "red" 515 self.btn_9["bg"] = "red" 516 self.btn_10["bg"] = "red" 517 self.btn_11["bg"] = "red" 518 self.btn_12["bg"] = "red" 519 520 def judge_ans(self, btn_y, ans_y): # 判断答案正误 521 if self.mod.get() == self.c_lan("出口成诗"): 522 if self.c_lan(ans_entry.get()) == ans_y: 523 self.right_count += 1 524 tk.messagebox.showinfo(message=self.c_lan("回答正确!")) 525 ans_window.destroy() 526 btn_y.place_forget() 527 if self.right_count == 12: 528 self.change_btn["text"] = self.c_lan("重新开始") 529 tk.messagebox.showinfo(message=self.c_lan("取得胜利!")) 530 else: 531 tk.messagebox.showerror(message=self.c_lan("回答错误!")) 532 533 def show_point(self, title_x, author_x, btn_x, ans_x): # 显示答案和提示 534 try: 535 if self.answer_show: 536 ans_window.destroy() 537 btn_x.place_forget() 538 btn_x["bg"] = "red" 539 self.point_show = False 540 self.answer_show = False 541 if self.point_show: 542 point_lab["text"] = "“%s”" % ans_x 543 point_btn["text"] = "跳过" 544 self.answer_show = True 545 if not self.point_show: 546 point_lab["text"] = "《%s》——%s" % (title_x, author_x) 547 point_btn["text"] = "答案" 548 self.point_show = True 549 except tk.TclError: 550 pass 551 552 def back_set(self): # 返回设置 553 self.btn_1.place_forget() 554 self.btn_2.place_forget() 555 self.btn_3.place_forget() 556 self.btn_4.place_forget() 557 self.btn_5.place_forget() 558 self.btn_6.place_forget() 559 self.btn_7.place_forget() 560 self.btn_8.place_forget() 561 self.btn_9.place_forget() 562 self.btn_10.place_forget() 563 self.btn_11.place_forget() 564 self.btn_12.place_forget() 565 self.time_left.place_forget() 566 self.time_lab.place_forget() 567 self.change_btn.place_forget() 568 self.quit_btn.place_forget() 569 self.play_bg.pack_forget() 570 self.back_btn.place_forget() 571 self.key_anwser .place_forget() 572 self.sta_anwser.place_forget() 573 self.clear_btn.place_forget() 574 self.str_anwser = '' 575 self.key_anwser['text'] = '' 576 self.sta_anwser['text'] = '' 577 self.right_count = 0 578 self.count = 0 579 self.set_game() 580 581 def close_ans(self, btn_z): # 关闭作答窗口所执行的操作 582 ans_window.destroy() 583 btn_z["bg"] = "red" 584 self.point_show = False 585 self.answer_show = False 586 587 def clear_ans(self): # 清空答案 588 if self.mod.get() == self.c_lan("出口成诗"): 589 ans_entry.delete(0, tk.END) 590 if self.mod.get() == self.c_lan("点字成诗"): 591 self.key_anwser['text'] = '' 592 self.str_anwser = '' 593 self.refresh_btns() 594 595 def close_window(self): # 关闭游戏窗口 596 if tk.messagebox.askokcancel(message=self.c_lan("您确定要退出吗?")): 597 self.root.destroy() 598 599 def quit_game(self): # 离开游戏 600 if tk.messagebox.askokcancel(message=self.c_lan("您确定要退出吗?")): 601 self.root.destroy() 602 603 604 if __name__ == '__main__': 605 game = App_poemgame() 606 607 print('游戏结束')
总结:经过九周的学习,对python编程语言逐渐熟练, 并且学会的了软件设计、项目管理以及团队协作。希望在以后的学习工作能能学以致用。
三、学习记录
| 学习阶段 | 学习内容 | 学习收获 |
| 2019.4.13 14:00-17:00 | 软件工程 |
了解软件系统测试和交付与维护 |
| 2019.4.15 14:00-17:00 | 讲义学习 | 了解软件项目管理 |
| 2019.4.17 08:00-11:00 | 课堂讨论 | 对软件项目管理 |
| 2019.4.18 09:00-17:00 | 实践学习 | 对函数进行封装,设计语言设置 |
| 2019.4.19 09:00-2100 | 实践学习 |
完善点字成诗游戏,修复时间显示bug |

浙公网安备 33010602011771号