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

2022-07-28,,,,

文章目录

  • 前言
  • 收获与感悟
  • 二、使用步骤
    • 1.引入库
    • 2.构建框架
    • 3.菜单about
    • 4.菜单exit
    • 5.菜单score
    • 6.菜单start
    • 7.总代码
  • 结果展示
  • 总结

前言

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


收获与感悟

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

二、使用步骤

1.引入库

import numpy as np
import wx

2.构建框架

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)

3.菜单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()#完成后,销毁它。

4.菜单exit

功能:退出游戏

       def on_exit(self,e):
        self.Close(True)

5.菜单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()#完成后,销毁它。

6.菜单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()#完成后,销毁它。

7.总代码

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较为简陋,同时我们尚未实现不结束程序就可以重复刷新玩家的功能,可能会在后续进一步进行改进

本文地址:https://blog.csdn.net/weixin_45460575/article/details/109246044

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

下载本文的Word格式文档,以方便收藏与打印。