软工实验:黄金点游戏图形界面设计(双人合作)


前言

这篇博客主要以黄金点游戏为基础,关于黄金点游戏的规则和代码实现介绍已在上篇文章《软工实验:黄金点游戏(双人合作)》中有过提及,这不在赘述。这篇博客在上篇博客的基础上增加了图形UI界面设计,使玩家操作起来更加方便


游戏规则

黄金点游戏规则:
N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同学得到-2分,其他同学得0分。


收获与感悟

这次实验增加的UI图形界面,主要是调用了wxPython模块,里面提供了丰富的UI界面设计函数,操作运用十分方便,同时这次仍然采用小组配合式完成了这次的项目。

二、使用步骤

1.引入库

import numpy as np
import wx

2.黄金点函数(Gold_Point)

def Gold_Point(people):
    num = []
    for i in range(len(people)):
        print("请玩家"+people[i].name+"输入0-100的数字:")
        num.append(int(input()))
    Gpoint = np.mean(num)*0.618
    print("输入完成!结果如下:")
    num = [abs(x-Gpoint) for x in num]  # 减去Gpoint算距离
    num = [x-min(num) for x in num]  # 减去距离最小值,0为最近距离的数字
    farther_num = max(num)  # 得到最大的数字,数字对应的下标就是该人在people数组里的下标
    for i in range(len(num)):
        if num[i] == 0:
            people[i].change_score(len(people))
        elif num[i] == farther_num:
            people[i].change_score(-2)
        else:
            people[i].change_score(0)
    for person in people:
        person.show()

若存在多名距离相同的最高或最短距离玩家,那么所有最远距离玩家或最短距离玩家都将加分或减分

3.玩家类(person)

class person:
    def __init__(self, name):
        self.name = name
        self.score_list = []
        self.score = 10  # 初始分数为10

    def change_score(self, num):
        self.score += num
        self.score_list.append(self.score)

    def show(self):
        print(str(self.name)+":"+str(self.score))

    def show_scorelist(self):
        print(str(self.name)+":"+str(self.score_list))

4.构建框架

class my_frame(wx.Frame):
    """We simple derive a new class of Frame"""
    def __init__(self,parent, title):
        wx.Frame.__init__(self, parent, title=title,size=(1000,500))
        # self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)
        self.Show(True)
        self.CreateStatusBar()#创建窗口底部的状态栏
        filemenu = wx.Menu()
        menu_exit = filemenu.Append(wx.ID_EXIT, "Exit", "Termanate the game")
        filemenu.AppendSeparator()  # 画那根分割线
        menu_about = filemenu.Append(wx.ID_ABOUT, "About", "Game rules")#设置菜单的内容
        filemenu.AppendSeparator()  # 画那根分割线
        menu_score = filemenu.Append(wx.ID_ANY, "Score", "")#设置菜单的内容
        filemenu.AppendSeparator()
        menu_start = filemenu.Append(wx.ID_ANY, "Start New Game", "")
        filemenu.AppendSeparator()
        menu_continue = filemenu.Append(wx.ID_ANY, "Continue the Game", "")
        filemenu.AppendSeparator()
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, u"选项")
        self.SetMenuBar(menuBar)#创建菜单条
        self.Show(True)

        self.Bind(wx.EVT_MENU, self.on_about, menu_about)
        self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出现的事件,同需要处理的函数连接起来
        self.Bind(wx.EVT_MENU, self.on_score, menu_score)
        self.Bind(wx.EVT_MENU, self.on_start, menu_start)
        self.Bind(wx.EVT_MENU, self.on_continue, menu_continue)

5.菜单about

功能:查看游戏规则
    def on_about(self, e):#about按钮的处理函数
        dlg = wx.MessageDialog(self," N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁"
                                    "判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数)"
                                    ",得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同"
                                    "学得到-2分,其他同学得0分", "Game Rules",wx.OK)#创建一个对话框,有一个ok的按钮
        dlg.ShowModal()#显示对话框
        dlg.Destroy()#完成后,销毁它。

6.菜单exit

功能:退出游戏
       def on_exit(self,e):
        self.Close(True)

7.菜单score

功能:查看已有的分数记录
           def on_score(self,e):
        sc = ""
        for i in people:
            sc += i.show_scorelist()+"\n"
        dlg = wx.MessageDialog(self,sc, "Game Result",wx.OK)#创建一个对话框,有一个ok的按钮
        dlg.ShowModal()#显示对话框
        dlg.Destroy()#完成后,销毁它。

8.菜单start

功能:开始游戏
         def on_start(self,e):
        num = wx.GetNumberFromUser('参与人数','请输入人数(最多二十人,最少两人)','New Game', 2, 2, 20)
        people = initial(num)
        score = Gold_Point(people)
        sc = ""
        for i in score:
            sc = sc+i+"\n"
        dlg = wx.MessageDialog(self,sc, "Game Result",wx.OK)#创建一个对话框,有一个ok的按钮
        dlg.ShowModal()#显示对话框
        dlg.Destroy()#完成后,销毁它。

9.总代码

import numpy as np
import wx

people = []

class person:
    def __init__(self, name):
        self.name = name
        self.score_list = []
        self.score = 10  # 初始分数为10

    def change_score(self, num):
        self.score += num
        self.score_list.append(self.score)

    def show(self):
        return (str(self.name)+":"+str(self.score))

    def get_name(self):
        return self.name

    def show_scorelist(self):
        sc = ""
        for i in self.score_list:
            sc += str(i)+" "
        return (str(self.name)+":"+sc)


def Gold_Point(people):
    num = []
    fscore = []
    for i in range(len(people)):
        num.append(wx.GetNumberFromUser('参赛数字','请'+people[i].get_name()+'输入数字(最小为0,最大为99)','Num'+str(i), 0, 0, 99))
    Gpoint = np.mean(num)*0.618
    num = [abs(x-Gpoint) for x in num]  # 减去Gpoint
    num = [x-min(num) for x in num]  # 减去最小值
    farther_num = max(num)
    for i in range(len(num)):
        if num[i] == 0:
            people[i].change_score(len(people))
        elif num[i] == farther_num:
            people[i].change_score(-2)
        else:
            pass
    for person in people:
        fscore.append(person.show())
    return fscore


def initial(people_num):
    global people
    people = []
    # 初始化
    for i in range(people_num):
        name = wx.GetTextFromUser("Name",  caption="Player"+str(i+1)+"puts your name", default_value="",  parent=None)
        people.append(person(name))
    return people


class my_frame(wx.Frame):
    """We simple derive a new class of Frame"""
    def __init__(self,parent, title):
        wx.Frame.__init__(self, parent, title=title,size=(1000,500))
        # self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)
        self.Show(True)
        self.CreateStatusBar()#创建窗口底部的状态栏
        filemenu = wx.Menu()
        menu_exit = filemenu.Append(wx.ID_EXIT, "Exit", "Termanate the game")
        filemenu.AppendSeparator()  # 画那根分割线
        menu_about = filemenu.Append(wx.ID_ABOUT, "About", "Game rules")#设置菜单的内容
        filemenu.AppendSeparator()  # 画那根分割线
        menu_score = filemenu.Append(wx.ID_ANY, "Score", "")#设置菜单的内容
        filemenu.AppendSeparator()
        menu_start = filemenu.Append(wx.ID_ANY, "Start New Game", "")
        filemenu.AppendSeparator()
        menu_continue = filemenu.Append(wx.ID_ANY, "Continue the Game", "")
        filemenu.AppendSeparator()
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, u"选项")
        self.SetMenuBar(menuBar)#创建菜单条
        self.Show(True)

        self.Bind(wx.EVT_MENU, self.on_about, menu_about)
        self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出现的事件,同需要处理的函数连接起来
        self.Bind(wx.EVT_MENU, self.on_score, menu_score)
        self.Bind(wx.EVT_MENU, self.on_start, menu_start)
        self.Bind(wx.EVT_MENU, self.on_continue, menu_continue)

    def on_about(self, e):#about按钮的处理函数
        dlg = wx.MessageDialog(self," N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁"
                                    "判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数)"
                                    ",得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同"
                                    "学得到-2分,其他同学得0分", "Game Rules",wx.OK)#创建一个对话框,有一个ok的按钮
        dlg.ShowModal()#显示对话框
        dlg.Destroy()#完成后,销毁它。
    def on_exit(self,e):
        self.Close(True)
    def on_score(self,e):
        sc = ""
        for i in people:
            sc += i.show_scorelist()+"\n"
        dlg = wx.MessageDialog(self,sc, "Game Result",wx.OK)#创建一个对话框,有一个ok的按钮
        dlg.ShowModal()#显示对话框
        dlg.Destroy()#完成后,销毁它。
    def on_start(self,e):
        num = wx.GetNumberFromUser('参与人数','请输入人数(最多二十人,最少两人)','New Game', 2, 2, 20)
        people = initial(num)
        score = Gold_Point(people)
        sc = ""
        for i in score:
            sc = sc+i+"\n"
        dlg = wx.MessageDialog(self,sc, "Game Result",wx.OK)#创建一个对话框,有一个ok的按钮
        dlg.ShowModal()#显示对话框
        dlg.Destroy()#完成后,销毁它。
    def on_continue(self,e):
        if len(people)==0:
            dlg = wx.MessageDialog(self,"there is no player! try start new game!", "error!",wx.OK)#创建一个对话框,有一个ok的按钮
            dlg.ShowModal()#显示对话框
            dlg.Destroy()#完成后,销毁它
        else:
            score = Gold_Point(people)
            sc = ""
            for i in score:
                sc = sc+i+"\n"
            dlg = wx.MessageDialog(self,sc, "Game Result",wx.OK)#创建一个对话框,有一个ok的按钮
            dlg.ShowModal()#显示对话框
            dlg.Destroy()#完成后,销毁它

app = wx.App(False)
frame = my_frame(None, 'Gold Point Game')
app.MainLoop()


结果展示

运行出现自动弹窗
运行出现自动弹窗
在这里插入图片描述
点击后start开始游戏,按照游戏规则提示依次输入结果,首先输入参与人数
在这里插入图片描述
紧接着继续输入玩家名字
在这里插入图片描述
当完成姓名输入,自动弹窗请求输入数字

在这里插入图片描述
当完成数字输入后分数自动弹出,点击确认后弹窗销毁
在这里插入图片描述点击continue继续游戏,此次直接请求数字
在这里插入图片描述
当为每名玩家完成输入后得到此轮结果
在这里插入图片描述
点击score展示几轮游戏的结果

在这里插入图片描述
点击about查看规则,最终Exit退出

总结

这篇博客所展示的内容主要是在上一篇博客的基础之上进行了推进,设计了一个图形UI界面,通过双人合作,使得任务圆满顺利完成,虽然完成了UI设计,但是UI较为简陋,同时我们尚未实现不结束程序就可以重复刷新玩家的功能,可能会在后续进一步进行改进

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值