| 这个作业属于哪个课程 | 构建之法-2021秋-福州大学软件工程 |
|---|---|
| 这个作业的要求在哪里 | 2021秋软工实践第二次结对编程作业 |
| 这个作业的目标 | 基于第一次结对编程作业设计的原型,开发出一款博饼软件 |
| 学号 | 031902432 |
| 结队成员学号 | 031902434 |
| 结对小伙伴的博客作业地址 | https://bbs.csdn.net/topics/601404887 |
| Github仓库地址 | https://github.com/huuu-code/dice.git |
| 视频演示链接地址 | https://www.bilibili.com/video/bv13f4y1c7eB |
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 10 | 15 |
| · Estimate | · 估计这个任务需要多少时间 | 10 | 15 |
| Development | 开发 | 960 | 930 |
| · Analysis | · 需求分析 (包括学习新技术) | 300 | 400 |
| · Design Spec | · 生成设计文档 | ||
| · Design Review | · 设计复审 (和同事审核设计文档) | ||
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 30 | 20 |
| · Design | · 具体设计 | 180 | 120 |
| · Coding | · 具体编码 | 300 | 210 |
| · Code Review | · 代码复审 | 30 | 20 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 120 | 160 |
| Reporting | 报告 | 70 | 50 |
| · Test Report | · 测试报告 | ||
| · Size Measurement | · 计算工作量 | 60 | 40 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 10 | 10 |
| 合计 | 1040 | 995 |
本人Github主页 队友Github主页
代码规范:请点击这
| 第N周 | 新增代码(行) | 累计代码(行) | 本周学习耗时(小时) 累计学习耗时(小时) | 重要成长 |
|---|---|---|---|---|
| 1 | 200 | 200 | 10 | 熟悉了pygame中的一些基本语言和用法 |
| ·2 | · 300 | 500 | 10 | 对作业的要求实现了大部分的功能 |
| … |
首先我写了一个button模块,下面是其中的画按钮的代码
# 定义一个Button类 class Button: # 传入按钮文字,坐标,大小,背景颜色,文字颜色,默认文字大小32 def __init__ ( self, title, pos, size, bg_color, text_color, font_size=32 ): self.title = title self.pos = pos self.size = size self.bg_color = bg_color self.text_color = text_color self.font_size = font_size self.old_color = bg_color # 画按钮 def draw1 ( self, window ): self.rect = pygame.Rect ( self.pos [ 0 ], self.pos [ 1 ], self.size [ 0 ], self.size [ 1 ] ) pygame.draw.rect ( window, self.bg_color, self.rect ) self.font = pygame.font.Font ( '素材/字体1.ttf', self.font_size ) text = self.font.render ( self.title, True, self.text_color ) t_w, t_h = text.get_size ( ) window.blit ( text,(self.pos [ 0 ] + self.size [ 0 ] / 2 - t_w / 2, self.pos [ 1 ] + self.size [ 1 ] / 2 - t_h / 2) )
其次是实现判定具体博饼等级的代码,我把它放在了“博饼”模块中
# 创建虚拟骰子的类 class Die(): def __init__(self, die_sides=6): self.die_sides = die_sides def roll(self): return randint(1, self.die_sides) # 创建6个虚拟的骰子 die_1 = Die() die_2 = Die() die_3 = Die() die_4 = Die() die_5 = Die() die_6 = Die() # 创建函数,检查每次掷骰子结果所对应的博饼奖项 def check_result(): if bb().count(1) == 2 and bb().count(4) == 4: bb_result.append(A) elif bb().count(4) == 6: bb_result.append(B) elif bb().count(1) == 6: bb_result.append(C) elif bb().count(2) == 6 or bb().count(3) == 6 or bb().count(5) == 6 or bb().count(6) == 6: bb_result.append(D) elif bb().count(4) == 5: bb_result.append(E) elif bb().count(1) == 5 or bb().count(2) == 5 or bb().count(3) == 5 or bb().count(5) == 5 or bb().count(6) == 5: bb_result.append(F) elif bb().count(4) == 4: bb_result.append(G) elif bb().count(1) == 1 and bb().count(2) == 1 and bb().count(3) == 1 and bb().count(4) == 1 and bb().count(5) == 1 and bb().count(6) == 1: bb_result.append(H) elif bb().count(4) == 3: bb_result.append(I) elif bb().count(1) == 4 or bb().count(2) == 4 or bb().count(3) == 4 or bb().count(5) == 4 or bb().count(6) == 4: bb_result.append(J) elif bb().count(4) == 2: bb_result.append(K) elif bb().count(4) == 1: bb_result.append(L) else: bb_result.append(M) return bb_result
最后是实现页面切换的代码,就是基本的判断鼠标位置是否是在按钮的范围内,点击按钮并松开,会跳转到相应的界面
for event in pygame.event.get ( ): # 主页跳转游戏页面 if event.type == pygame.MOUSEBUTTONDOWN: if button1.is_down ( event.pos, window ): pass if event.type == pygame.MOUSEBUTTONUP: if button1.is_up ( event.pos, window ): bg_game ( ) button5.draw1 ( window ) #show_number() pygame.display.update ( ) # 游戏页面跳转结束页面 if event.type == pygame.MOUSEBUTTONDOWN: if button5.is_down ( event.pos, window ): pass if event.type == pygame.MOUSEBUTTONUP: if button5.is_up ( event.pos, window ): bg_game ( ) button5.draw1( window ) button6.draw1 ( window) show_result() pygame.display.update ( ) # 结束页面跳转回主页 if event.type == pygame.MOUSEBUTTONDOWN: if button6.is_down ( event.pos, window ): pass if event.type == pygame.MOUSEBUTTONUP: if button6.is_up ( event.pos, window ): bg_game ( ) button1.draw1 ( window ) button2.draw1 ( window ) button3.draw1 ( window ) button4.draw1 ( window ) pygame.display.update ( ) # 跳转规则奖励页面 if event.type == pygame.MOUSEBUTTONDOWN: if button2.is_down ( event.pos, window ) or button4.is_down ( event.pos, window ) : pass if event.type == pygame.MOUSEBUTTONUP: if button2.is_up ( event.pos, window ) or button4.is_up ( event.pos, window ): rule_bg ( ) button7.draw1 ( window ) pygame.display.update ( ) # 规则页面跳转主页 if event.type == pygame.MOUSEBUTTONDOWN: if button7.is_down ( event.pos, window ): pass if event.type == pygame.MOUSEBUTTONUP: if button7.is_up ( event.pos, window ): bg_game ( ) button1.draw1 ( window ) button2.draw1 ( window ) button3.draw1 ( window ) button4.draw1 ( window ) running = False pygame.display.update ( ) # 退出游戏 if event.type == pygame.MOUSEBUTTONDOWN: if button3.is_down ( event.pos, window ): pass if event.type == pygame.MOUSEBUTTONUP: if button3.is_up ( event.pos, window ): pygame.quit() if event.type == pygame.QUIT: pygame.quit() exit()六、重点功能、编程思考:
1.代码模块的安排与整体框架:
一开始茫然,只能先确定必须完成的功能:核心的摇骰子功能以及页面的图片与按钮的实现。然后再结合原型模型开始入手设想,而后结合 pygame 的学习,再参考一些其他游戏的框架逻辑,再随着编码的开始,对整体做出以下安排:
- 将游戏分为三个模块:游戏界面、产生摇骰子的结果、按钮
- 在游戏界面的模块之中导入剩余两个部分的模块
- 只在游戏界面的模块之中进行窗口的渲染与更新
2.骰子的动画效果
由于能力有限,初学pygame,暂时还做不到动画效果,所以我们索性直接没用骰子效果,直接写了一个模块来判定结果,这样的情况下就会有一些不稳定,相同博饼奖励等级下的具体点数我们不清楚。
3. 要该如何做到页面切换:
粗略的思路大约是点击按钮就切换,但此处具体的实现我们两人产生一定的分歧。一个是判断的过程虽然比较简单,但是代码量还是有点多,稍不注意就会出现bug,后面我们决定写一个按钮事件和鼠标位置判定的模块,在主代码中调用它们就能实现减少主代码冗长易出错的问题。还有一个分歧大概是直接在一个函数里渲染刷新界面或者是跳转到另外一个函数中渲染刷新界面。但是考虑到函数之间的嵌套可能会有点奇怪的错误,于是采用了在一个函数之中用不同的函数 draw 去渲染再刷新的操作方法。至于如何选择不同的函数 draw ,则需要配合监听鼠标点击事件的位置来判断按钮的点击,选择不同函数。
1.感觉和自己一个人编程大不一样,自己一个人写代码有时候因为一个bug大半天查不出问题,还可能debug时出现更多的bug。结对的话队友会从另一个方面考虑,可以给你很多启发和想法,这样的学习效率提高了不少。
2.在这次编程中,看到了自己很多的不足之处,掌握的编程知识太少,需要从零开始学习。pygame是实现编写小游戏的基础,我特地花费了时间去B站看其他up主的pygame视频教程学习基本语句及其用法,在那里有许多的前辈给了我很多的方法。
3.这次的作业还有许多需要改进的地方,比如骰子模型的动画效果,奖品的输入设置效果,代码简洁等问题,这更说明了我需要学习的还有很多,还要更加努力,深入学习python。



