python大作业之记忆测试系统——卡牌

记忆测试系统通过回忆法测试记忆,分为初级、中级、高级三个级别,并通过记忆榜存储每个级别的成绩。单击“记忆测试”菜单可以选择初级/中级/高级,也可以查看初级记忆榜/中级记忆榜或高级记忆榜。选择级别后,将显示相应级别的测试区域。测试区域由若干个方块组成,每个方块都有一个图标,但该图标默认情况下不可见。用鼠标单击测试区中的任何一个方块,则计时器开始启动,同时该方块上的图标将显示出来。对于初级级别,用户必须用鼠标连续单击出 6 个图标相同的方块,也就是说,如果用户已经用鼠标连续单击出 n(n<6)个图标相同的方块,单击另一个房块时,如果该方块的图标和上一个相同,那么该方块的图标和前 n 次单击出的方块的图标仍然可见,否则,前 n 次单击出的方块的图标都变为不可见。连续单击出该级别所要求的若干个图彪相同的方块后,将显示保存成绩对话框,成绩按所用时间排序。用户可以通过该对话框选择是否将自己的成绩保存到成绩表中。

 

 

 

 

import random

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import pymysql

#全局变量,花费时间
global score


sty='''
QWidget {
  /* Set all widgets to be centered */
  qproperty-alignment: AlignCenter;
}

QLabel {
  /* Set the label font size and color */
  font-size: 36px;
  color: blue;
  /* Set the label text shadow */
  text-shadow: 0px 0px 5px white;
}

QPushButton {
  /* Set the button font size and color */
  font-size: 24px;
  color: white;
  /* Set the button background gradient */
  background: qradialgradient(cx:0.5, cy:0.5, radius:1,
    fx:0.5, fy:0.5,
    stop:0 #00ffff, stop:0.5 #008080, stop:1 #00ffff);
  /* Set the button border and border radius */
  border: 3px solid black;
  border-radius: 10px;
  /* Set the button box shadow */
  box-shadow: inset -5px -5px 10px rgba(0,0,0,0.5),
              inset 5px 5px 10px rgba(255,255,255,0.5);
}

QPushButton:hover {
  /* Set the button background gradient when hovered */
  background: qradialgradient(cx:0.5, cy:0.5, radius:1,
    fx:0.5, fy:0.5,
    stop:0 #ff00ff, stop:0.5 #800080, stop:1 #ff00ff);
}

QLineEdit {
  border: 2px solid blue;
  border-radius: 5px;
  background-color: lightblue;
}

QLineEdit:hover {
  border-color: green;
  background-color: lightgreen;
}

QLineEdit:read-only {
  border-color: gray;
  background-color: lightgray;
}

'''
#将成绩保存到数据库
def add_score(table):
    con=pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        port=3307,
        db='exam',
        charset='utf8'
    )
    cur=con.cursor()
    global score
    sql="insert into `%s` (`spendtime`) values('%d')"%(table,score)
    print(sql)
    cur.execute(sql)
    con.commit()
    cur.close()
    con.close()
#从数据库获取成绩
def get_score(table):
    con = pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        port=3307,
        db='exam',
        charset='utf8'
    )
    cur = con.cursor()
    sql="select `spendtime` from %s order by `spendtime`"%table
    cur.execute(sql)
    data=cur.fetchall()
    cur.close()
    con.close()
    return data
#获取图片排列
def get_random(x):
    f = set()
    res = []
    s=[]
    #获取10张图片的x张
    for i in range(x):
        k = random.randint(1, 10)
        while k in f:
            k = random.randint(1, 10)
        f.add(k)
        res.append(k)
    num=[]
    #以每个六张去排列
    for i in range(x):
        num.append(0)
    for i in range(6):
        a=[]
        for j in range(x):
            k=random.randint(0,x-1)
            while num[k] >=6 :
                k = random.randint(0, x - 1)
            num[k]=num[k]+1
            a.append(k)
        s.append(a)
    print(s)
    return s

#主窗口,初级,中级,高级窗口,结果窗口,成绩单窗口
#check函数,如果点击的和之前的一样就翻转,否则全部转为背面
class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.easy=None
        self.normal=None
        self.difficult=None
        self.easy_score=None
        self.normal_score=None
        self.difficult_score=None
        self.setup_ui()
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
    def setup_ui(self):
        self.resize(500,400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        vbox.setSpacing(40)

        head=QLabel(self)
        head.setText("记忆测试系统")
        vbox.addWidget(head)

        easy=QPushButton(self)
        easy.setText("初级")
        easy.clicked.connect(self.start_easy)
        vbox.addWidget(easy)

        normal=QPushButton(self)
        normal.setText("中级")
        normal.clicked.connect(self.start_normal)
        vbox.addWidget(normal)

        difficult=QPushButton(self)
        difficult.setText("高级")
        difficult.clicked.connect(self.start_difficult)
        vbox.addWidget(difficult)

        score1=QPushButton(self)
        score1.setText("初级记忆榜")
        score1.clicked.connect(self.browse_easy_score)
        vbox.addWidget(score1)

        score2=QPushButton(self)
        score2.setText("中级记忆榜")
        score2.clicked.connect(self.browse_normal_score)
        vbox.addWidget(score2)

        score3=QPushButton(self)
        score3.setText("高级记忆榜")
        score3.clicked.connect(self.browse_difficult_score)
        vbox.addWidget(score3)

    def start_easy(self):
        self.easy=EasyWindow()
        self.easy.show()
        self.close()
    def start_normal(self):
        self.normal=NormalWindow()
        self.normal.show()
        self.close()
    def start_difficult(self):
        self.difficult=DifficultWindow()
        self.difficult.show()
        self.close()
    def browse_easy_score(self):
        self.easy_score=EasyScore()
        self.easy_score.show()
        self.close()
    def browse_normal_score(self):
        self.normal_score=NormalScore()
        self.normal_score.show()
        self.close()
    def browse_difficult_score(self):
        self.difficult_score=DifficultScore()
        self.difficult_score.show()
        self.close()

class EasyWindow(QWidget):
    def __init__(self):
        global  score
        score=0
        super(EasyWindow, self).__init__()
        self.res=None
        self.pix=[]
        self.right=[]
        self.b=[]
        self.flag=True
        self.max_right=0
        self.spend_time=0
        self.timer=None
        self.grid=None
        self.vbox=None
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
        self.setup_ui()


    def setup_ui(self):
        self.resize(500, 400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        spend_time=QLabel(self)
        vbox.addWidget(spend_time)
        spend_time.setText("开始计时!")
        timer = QTimer(self)
        timer.timeout.connect(self.add_time)
        self.spend_time=spend_time
        self.timer=timer
        self.vbox=vbox
        grid=QGridLayout(self)
        self.grid=grid
        grid.setSpacing(20)
        self.pix=get_random(6)
        for i in range(6):
            self.right.append(0)

        for i in  range(6):
            a=[]
            for j in  range(6):
                t=QPushButton(self)
                t.setFixedSize(120,120)
                t.setStyleSheet("border-image: url(back.jfif)")
                t.clicked.connect(self.check)
                a.append(t)
                grid.addWidget(t,i,j,1,1)
            self.b.append(a)
        vbox.addLayout(grid)
    def check(self):
        if self.flag:
            self.timer.start(1000)
            self.flag=False
        sender=self.sender()
        x=None
        y=None
        for i in range(6):
            for j in  range(6):
                if self.b[i][j] == sender:
                    x=i
                    y=j
                    break
            if x is not None:
                break
        self.right[self.pix[x][y]]=self.right[self.pix[x][y]]+1
        if self.right[self.pix[x][y]] > self.max_right:
            self.max_right=self.right[self.pix[x][y]]
            sender.setStyleSheet("border-image: url(%d.png)"%(self.pix[x][y]+1))
        else:
            sender.setStyleSheet("border-image: url(%d.png)"%(self.pix[x][y]+1))
            self.max_right=0
            for i in range(6):
                self.right[i]=0
            for i in range(6):
                for j in range(6):
                    self.grid.itemAtPosition(i, j).widget().setStyleSheet("border-image: url(back.jfif)")
        if self.max_right >= 6:
            box=QMessageBox(self)
            box.setWindowTitle("保存确认")
            box.setText("是否保存本次成绩?")
            box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            Y = box.button(QMessageBox.Yes)
            Y.setText("确定")
            N = box.button(QMessageBox.No)
            N.setText("取消")
            box.exec_()
            if box.clickedButton() == Y:
                add_score(table="score_easy")
            else:
                pass
            self.res=Res()
            self.res.show()
            self.close()
    def add_time(self):
        global  score
        score=score+1
        h = score / 3600
        m = (score % 3600) / 60
        s = score % 60
        self.spend_time.setText("用时: %d时%d分%d秒"%(h,m,s))



class NormalWindow(QWidget):
    def __init__(self):
        global score
        score=0
        super(NormalWindow, self).__init__()
        self.res=None
        self.pix=[]
        self.right=[]
        self.b=[]
        self.flag=True
        self.max_right=0
        self.spend_time=0
        self.timer=None
        self.grid=None
        self.vbox=None
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
        self.setup_ui()


    def setup_ui(self):
        self.resize(500, 400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        spend_time=QLabel(self)
        vbox.addWidget(spend_time)
        spend_time.setText("开始计时!")
        timer = QTimer(self)
        timer.timeout.connect(self.add_time)
        self.spend_time=spend_time
        self.timer=timer
        self.vbox=vbox
        grid=QGridLayout(self)
        self.grid=grid
        grid.setSpacing(20)
        self.pix=get_random(8)
        for i in range(8):
            self.right.append(0)

        for i in  range(6):
            a=[]
            for j in  range(8):
                t=QPushButton(self)
                t.setFixedSize(120,120)
                t.setStyleSheet("border-image: url(back.jfif)")
                t.clicked.connect(self.check)
                a.append(t)
                grid.addWidget(t,i,j,1,1)
            self.b.append(a)
        vbox.addLayout(grid)
    def check(self):
        if self.flag:
            self.timer.start(1000)
            self.flag=False
        sender=self.sender()
        x=None
        y=None
        for i in range(6):
            for j in  range(8):
                if self.b[i][j] == sender:
                    x=i
                    y=j
                    break
            if x is not None:
                break
        self.right[self.pix[x][y]]=self.right[self.pix[x][y]]+1
        if self.right[self.pix[x][y]] > self.max_right:
            self.max_right=self.right[self.pix[x][y]]
            sender.setStyleSheet("border-image: url(%d.png)"%(self.pix[x][y]+1))
        else:
            self.max_right=0
            for i in range(8):
                self.right[i]=0
            for i in range(6):
                for j in range(8):
                    self.grid.itemAtPosition(i, j).widget().setStyleSheet("border-image: url(back.jfif)")
        if self.max_right >= 6:
            box=QMessageBox(self)
            box.setWindowTitle("保存确认")
            box.setText("是否保存本次成绩?")
            box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            Y = box.button(QMessageBox.Yes)
            Y.setText("确定")
            N = box.button(QMessageBox.No)
            N.setText("取消")
            box.exec_()
            if box.clickedButton() == Y:
                add_score(table="score_normal")
            else:
                pass
            self.res=Res()
            self.res.show()
            self.close()
    def add_time(self):
        global  score
        h = score / 3600
        m = (score % 3600) / 60
        s = score % 60
        self.spend_time.setText("用时: %d时%d分%d秒"%(h,m,s))

class DifficultWindow(QWidget):
    def __init__(self):
        super(DifficultWindow, self).__init__()
        global score
        score=0
        self.res=None
        self.pix=[]
        self.right=[]
        self.b=[]
        self.flag=True
        self.max_right=0
        self.spend_time=0
        self.timer=None
        self.grid=None
        self.vbox=None
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
        self.setup_ui()


    def setup_ui(self):
        self.resize(500, 400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        spend_time=QLabel(self)
        vbox.addWidget(spend_time)
        spend_time.setText("开始计时!")
        timer = QTimer(self)
        timer.timeout.connect(self.add_time)
        self.spend_time=spend_time
        self.timer=timer
        self.vbox=vbox
        grid=QGridLayout(self)
        self.grid=grid
        grid.setSpacing(20)
        self.pix=get_random(10)
        for i in range(10):
            self.right.append(0)

        for i in  range(6):
            a=[]
            for j in  range(10):
                t=QPushButton(self)
                t.setFixedSize(120,120)
                t.setStyleSheet("border-image: url(back.jfif)")
                t.clicked.connect(self.check)
                a.append(t)
                grid.addWidget(t,i,j,1,1)
            self.b.append(a)
        vbox.addLayout(grid)
    def check(self):
        if self.flag:
            self.timer.start(1000)
            self.flag=False
        sender=self.sender()
        x=None
        y=None
        for i in range(6):
            for j in  range(10):
                if self.b[i][j] == sender:
                    x=i
                    y=j
                    break
            if x is not None:
                break
        self.right[self.pix[x][y]]=self.right[self.pix[x][y]]+1
        if self.right[self.pix[x][y]] > self.max_right:
            self.max_right=self.right[self.pix[x][y]]
            sender.setStyleSheet("border-image: url(%d.png)"%(self.pix[x][y]+1))
        else:
            self.max_right=0
            for i in range(10):
                self.right[i]=0
            for i in range(6):
                for j in range(10):
                    self.grid.itemAtPosition(i, j).widget().setStyleSheet("border-image: url(back.jfif)")
        if self.max_right >= 6:
            box=QMessageBox(self)
            box.setWindowTitle("保存确认")
            box.setText("是否保存本次成绩?")
            box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            Y = box.button(QMessageBox.Yes)
            Y.setText("确定")
            N = box.button(QMessageBox.No)
            N.setText("取消")
            box.exec_()
            if box.clickedButton() == Y:
                add_score(table="score_difficult")
            else:
                pass
            self.res=Res()
            self.res.show()
            self.close()
    def add_time(self):
        global  score
        score=score+1
        h=score/3600
        m=(score%3600)/60
        s=score%60
        self.spend_time.setText("用时: %d时%d分%d秒"%(h,m,s))

class Res(QWidget):
    def __init__(self):
        super(Res,self).__init__()
        self.main_window=None
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
        self.setup_ui()
    def setup_ui(self):
        self.resize(500,400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        label=QLabel(self)
        global score
        h = score / 3600
        m = (score % 3600) / 60
        s = score % 60
        label.setText("本次成绩为:  %d时%d分%d秒"%(h,m,s))
        vbox.addWidget(label)
        back_home=QPushButton(self)
        back_home.setText("回到主菜单")
        back_home.clicked.connect(self.back)
        vbox.addWidget(back_home)

    def back(self):
        self.main_window=MainWindow()
        self.main_window.show()
        self.close()
class EasyScore(QWidget):
    def __init__(self):
        super(EasyScore,self).__init__()
        self.main_window=None
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
        self.setup_ui()
    def setup_ui(self):
        self.resize(500,400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        vbox.setSpacing(40)
        data=get_score("score_easy")
        id=0
        for i in data:
            t=QLabel(self)
            id=id+1
            h=i[0]/3600
            m=(i[0]%3600)/60
            s=i[0]%60
            t.setText("第%d名: 用时 %d时%d分%d秒"%(id,h,m,s))
            vbox.addWidget(t)
        back=QPushButton(self)
        back.setText("回到主界面")
        back.clicked.connect(self.back)
        vbox.addWidget(back)
    def back(self):
        self.main_window=MainWindow()
        self.main_window.show()
        self.close()
class NormalScore(QWidget):
    def __init__(self):
        super(NormalScore,self).__init__()
        self.main_window=None
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
        self.setup_ui()
    def setup_ui(self):
        self.resize(500,400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        vbox.setSpacing(40)
        data=get_score("score_normal")
        id=0
        for i in data:
            t=QLabel(self)
            id=id+1
            h = i[0] / 3600
            m = (i[0] % 3600) / 60
            s = i[0] % 60
            t.setText("第%d名: 用时 %d时%d分%d秒"%(id,h,m,s))
            vbox.addWidget(t)
        back=QPushButton(self)
        back.setText("回到主界面")
        back.clicked.connect(self.back)
        vbox.addWidget(back)
    def back(self):
        self.main_window=MainWindow()
        self.main_window.show()
        self.close()

class DifficultScore(QWidget):
    def __init__(self):
        super(DifficultScore,self).__init__()
        self.main_window=None
        self.setWindowIcon(QIcon('logo.jfif'))
        self.setStyleSheet(sty)
        self.setup_ui()
    def setup_ui(self):
        self.resize(500,400)
        self.setWindowTitle("记忆测试系统")

        vbox=QVBoxLayout(self)
        vbox.setSpacing(40)
        data=get_score("score_difficult")
        id=0
        for i in data:
            t=QLabel(self)
            id=id+1
            h = i[0] / 3600
            m = (i[0] % 3600) / 60
            s = i[0] % 60
            t.setText("第%d名: 用时 %d时%d分%d秒"%(id,h,m,s))
            vbox.addWidget(t)
        back=QPushButton(self)
        back.setText("回到主界面")
        back.clicked.connect(self.back)
        vbox.addWidget(back)
    def back(self):
        self.main_window=MainWindow()
        self.main_window.show()
        self.close()


if __name__ == '__main__':
    app=QApplication(sys.argv)
    t=MainWindow()
    t.show()
    sys.exit(app.exec_())

 

 

posted @ 2023-05-27 00:10  突破铁皮  阅读(131)  评论(0编辑  收藏  举报