Community--luchanghong.com

想做的话就没什么难的,昨晚把几个难题攻破了,可以玩了,今早又趁热打铁把关卡也设置了。 难题1: 显示数字的时候我用的QLabel,在绑定信号槽的时候,查了一下手册,有mousePressEvent()这个signal,但是用的时候对整个Window窗口都有效,这样没法判断,所以google了一下,发现可以给QLabel重新定义一个signal,也就是可以扩展QLabel为我们所用,方法如下:

1 class ExtendedQLabel(QtGui.QLabel):
2     def __init(self, parent):
3         QtGui.QLabel.__init__(self, parent)
4 
5     def mouseReleaseEvent(self, ev):
6         self.emit(QtCore.SIGNAL('clicked()'))

 

难题2: 在使用的过程中发现,要知道当前点击的是哪一个QLabel,所以要传递一个参数,最好是当前的QObject对象——QLabel,又去google查。毕竟刚学习,只好遇到什么就解决什么了,还好可以找到答案,原来一直认为参数是回调函数传递的,其实是在signal传递的,于是修改了上面的ExtendedQLabel,如下:

1 class ExtendedQLabel(QtGui.QLabel):
2     def __init(self, parent):
3         QtGui.QLabel.__init__(self, parent)
4 
5     def mouseReleaseEvent(self, ev):
6         self.emit(QtCore.SIGNAL('clicked(PyQt_PyObject)'), self)

 

难题3: 其实这个不算是什么难题,只不过是游戏进行过程中的逻辑判断,比如第一次点击要是1,然后依次累加,游戏出错就结束,把相关的参数初始化以便重新开始游戏,完成一关后进入下一关卡也要进行相关操作。 目前运行是可以玩的了,全部代码加起来也不过120行,说实话还是第一次做GUI程序,也没有任何经验,还好这个游戏简单,下面就分享一下代码,有冗余或者不对的地方请吐槽。

  1 # -*-encoding: utf-8 -*-
  2 __author__ = 'Administrator'
  3 import sys
  4 import random
  5 from PyQt4 import QtGui,QtCore
  6 
  7 class ExtendedQLabel(QtGui.QLabel):
  8     def __init(self, parent):
  9         QtGui.QLabel.__init__(self, parent)
 10 
 11     def mouseReleaseEvent(self, ev):
 12         self.emit(QtCore.SIGNAL('clicked(PyQt_PyObject)'), self)
 13 
 14 class MemoryWindow(QtGui.QMainWindow):
 15     def __init__(self, num = 3, parent = None):
 16         QtGui.QMainWindow.__init__(self, parent)
 17 
 18         self.setWindowTitle('Memory Game')
 19         self.setFixedSize(400, 400)
 20         self.init_num = num
 21         self.current_num = 0
 22         self.initGame()
 23 
 24     def setLabelText(self):
 25         num = 0
 26         for i in range(0, 5):
 27             for j in range(0, 5):
 28                 tmp = ExtendedQLabel()
 29                 tmp.setAlignment(QtCore.Qt.AlignCenter)
 30 
 31                 vars(self)['label_%d' % num] = tmp
 32                 self.layout.addWidget(tmp, i, j)
 33                 num += 1
 34         self.label_num = num
 35 
 36     def setRandomNum(self):
 37         label_list = [x for x in range(0, self.label_num)]
 38         self.res_label = []
 39         for x in range(0, self.init_num):
 40             number = random.randint(0, len(label_list)-1)
 41             label = vars(self)['label_%d' % label_list[number]]
 42             label.setText(str(x+1))
 43             label.setStyleSheet(
 44                 'background-color: rgb(255, 255, 127);'
 45                 'color: rgb(0, 170, 255);'
 46                 'font: 75 18pt "Axure Handwriting";'
 47             )
 48             self.connect(label, QtCore.SIGNAL("clicked(PyQt_PyObject)"), self.startGame)
 49             self.res_label.append(label)
 50             label_list.remove(label_list[number])
 51 
 52     def initGame(self):
 53         self.layout = QtGui.QGridLayout()
 54         self.widget = QtGui.QWidget(self)
 55         self.widget.setLayout(self.layout)
 56         self.setCentralWidget(self.widget)
 57         self.setLabelText()
 58         self.setRandomNum()
 59 
 60     def startGame(self, label):
 61         label_num = int(label.text())
 62         label.hide()
 63         for x in self.res_label:
 64             x.setStyleSheet(
 65                 'background-color: rgb(255, 255, 127);'
 66                 'color: rgb(255, 255, 127);'
 67                 'font: 75 18pt "Axure Handwriting";'
 68             )
 69         if not self.current_num:
 70             if label_num == 1:
 71                 self.current_num = label_num
 72             else:
 73                 self.gameOver()
 74         else:
 75             if self.current_num + 1 == label_num:
 76                 self.current_num += 1
 77                 if self.current_num == self.init_num:
 78                     self.gameVictory()
 79             else:
 80                 self.gameOver()
 81 
 82     def gameVictory(self):
 83         self.init_num += 1
 84         self.current_num = 0
 85         label = QtGui.QLabel('Game Victory!')
 86         label.setAlignment(QtCore.Qt.AlignCenter)
 87         button = QtGui.QPushButton('Next Step ?')
 88         button.setMaximumSize(80, 30)
 89         self.connect(button, QtCore.SIGNAL('clicked()'), self.restartGame)
 90         self.gameVictoryWidget = QtGui.QWidget()
 91         self.gameVictoryWidgetLayout = QtGui.QVBoxLayout()
 92         self.gameVictoryWidgetLayout.addWidget(label)
 93         self.gameVictoryWidgetLayout.addWidget(button)
 94         self.gameVictoryWidget.setLayout(self.gameVictoryWidgetLayout)
 95         self.setCentralWidget(self.gameVictoryWidget)
 96 
 97     def gameOver(self):
 98         self.current_num = 0
 99         label = QtGui.QLabel('Game Over!')
100         label.setAlignment(QtCore.Qt.AlignCenter)
101         button = QtGui.QPushButton('Retry ?')
102         button.setMaximumSize(80, 30)
103         self.connect(button, QtCore.SIGNAL('clicked()'), self.restartGame)
104         self.gameOverWidget = QtGui.QWidget()
105         self.gameOverWidgetLayout = QtGui.QVBoxLayout()
106         self.gameOverWidgetLayout.addWidget(label)
107         self.gameOverWidgetLayout.addWidget(button)
108         self.gameOverWidget.setLayout(self.gameOverWidgetLayout)
109         self.setCentralWidget(self.gameOverWidget)
110 
111     def restartGame(self):
112         self.initGame()
113 
114     def nextStep(self):
115         self.initGame()
116 
117 def startGame(num = 3):
118     app = QtGui.QApplication(sys.argv)
119     memoryWindow = MemoryWindow(num = num)
120     memoryWindow.show()
121     sys.exit(app.exec_())
122 
123 if __name__ == '__main__':
124     startGame()
posted on 2012-07-17 18:04  1990lch  阅读(641)  评论(1编辑  收藏  举报