QT PySide 连连看小游戏

来源:互联网 发布:淘宝网怎么登录不了 编辑:程序博客网 时间:2024/05/16 09:18

学了PySide,花了一天时间做出了以前很想写的一个游戏,然后写完主体的逻辑之后就不想再接着往下写了。

在windows下写的

6*6的方格,总共6种图像,网上找的6张gif图片,然后把大小用windows的画图变成50*50像素的图像,这是点击之前的图像。然后把gif图中白色的像素涂黑变成点击之后的图像(这个也是用windows画图解决的)。刚接触python,感觉格式会很好,但是self这个有点不爽的。

myvis[i][j] 表示(i,j)这个点是未点击的,点击过的,还是空白

mymap[i][j] 这个点应该显示何种图像

InitBoard 初始化连连看的地图

图像的绘制交给paintEvent

在鼠标点击的时候,检查两个点之后是否可以连接首先检查两个点的图像是不是相同的,然后检查是不是经过至多三条线段就能到达(这个我是用DFS(深度优先搜索)的,深搜的代价有点大,可以用BFS(宽度优先搜索)来优化,但是由于我不想接着写下去,就没有优化了)。

写的很粗糙,只是练习用。

# -*- coding:utf-8 -*-import sys,randomfrom PySide import QtGui, QtCoreclass Link(QtGui.QWidget):    def __init__(self):        super(Link,self).__init__()        self.initUI()    def initUI(self):        hbox = QtGui.QHBoxLayout()        board = Board(self)        hbox.addWidget(board)        #side = Side(self)        #hbox.addWidget(side)        self.setLayout(hbox)        #self.setGeometry(300,300,350,300)        #print self.size()        #center()        self.setWindowTitle("Link")        self.show()class Board(QtGui.QWidget):    BW = 6    BH = 6    def __init__(self,parent):        super(Board,self).__init__()        self.is_press = False        self.setMinimumSize(400,400)        self.setGeometry(0,0,400,400)        self.InitBoard()        self.ex = 0        self.ey = 0        self.prex = 0        self.prey = 0    def InitBoard(self):        self.mypix = ["nouse"]        self.mypixc = ["nouse"]        self.mymap = [([0]*(self.BW+1)) for i in range(self.BH+1)]        self.myvis = [([0]*(self.BW+2)) for i in range(self.BH+2)]        for i in range(1,7):            self.mypix.append(QtGui.QPixmap("link0"+str(i)+".gif"))            self.mypixc.append(QtGui.QPixmap("link0"+str(i)+"_c.gif"))        USE = ["nouse"]        for i in range(1,self.BW+1):            for j in range(1,self.BH+1):                USE.append(i)        LIM = self.BW * self.BH        for i in range(1,self.BW+1):            for j in range(1,self.BH+1):                rint = random.randint(1,LIM)                self.mymap[i][j] = USE[rint]                del USE[rint]                LIM = LIM - 1        for i in range(0,self.BW+2):            self.myvis[i][0] = 2            self.myvis[i][self.BH+1] = 2        for i in range(0,self.BH+2):            self.myvis[0][i] = 2            self.myvis[self.BW+1][i] = 2        #print "size : "        #print self.size()        self.rectW = self.size().width() / (self.BW + 2)        #print "rectW : "+str(self.rectW)        self.rectH = self.size().height() / (self.BH + 2)        #print "rectH : "+str(self.rectH)    def paintEvent(self,event):        qp = QtGui.QPainter(self)        qp.begin(self)        self.drawBoard(qp)        qp.end()    def drawBoard(self,qp):        # 给6*6的棋盘画上图画        for i in range(1,self.BW+1):            for j in range(1,self.BH+1):                if self.myvis[i][j] == 0:                    #print "("+str(i)+","+str(j)+")"                    qp.drawPixmap(i*self.rectW,j*self.rectH,self.mypix[self.mymap[i][j]],0,0,self.rectW,self.rectH)                elif self.myvis[i][j] == 1:                    qp.drawPixmap(i*self.rectW,j*self.rectH,self.mypixc[self.mymap[i][j]],0,0,self.rectW,self.rectH)    def mousePressEvent(self,e):        px = e.pos().x()        py = e.pos().y()        if px < self.rectW or py < self.rectH :            return        for i in range(1,7):            if px >= i * self.rectW :                self.ex = i            if py >= i * self.rectH :                self.ey = i        if self.myvis[self.ex][self.ey] != 0:            return        self.myvis[self.ex][self.ey] = 1        if not self.is_press:            self.prex = self.ex            self.prey = self.ey            self.is_press = True        else:            self.is_press = False            if self.mymap[self.prex][self.prey] == self.mymap[self.ex][self.ey] and self.checkRoute(self.prex,self.prey,self.ex,self.ey):                self.myvis[self.ex][self.ey] = 2                self.myvis[self.prex][self.prey] = 2            else:                self.myvis[self.ex][self.ey] = 0                self.myvis[self.prex][self.prey] = 0        self.update()    def checkRoute(self,prex,prey,ex,ey):        self.is_find = False        self.FRvis = [([0]*(self.BW+2)) for i in range(self.BH+2)]        self.fx = [1,-1,0,0]        self.fy = [0,0,1,-1]        self.Find_Route(prex,prey,ex,ey,0,-1)        print self.fx        print self.fy        #print self.is_find        return self.is_find    def Find_Route(self,prex,prey,ex,ey,bend,d):        if bend > 3:            return         print "(%d,%d)->(%d,%d) d:%d bend :%d"%(prex,prey,ex,ey,d,bend)        if self.is_find:            return        for i in range(4):            tmpx = self.fx[i] + prex            tmpy = self.fy[i] + prey            if tmpx >= 0 and tmpx <= self.BW+1 and tmpy >= 0 and tmpy <= self.BH+1 and not self.FRvis[tmpx][tmpy]:                self.FRvis[tmpx][tmpy] = 1                if i != d:                    bend = bend + 1                if tmpx == ex and tmpy == ey and bend <= 3:                    print "(%d,%d)->(%d,%d) d:%d bend :%d"%(prex,prey,ex,ey,d,bend)                    self.is_find = True                    return                if self.myvis[tmpx][tmpy] != 2:                    self.FRvis[tmpx][tmpy] = 0                    if i != d:                        bend = bend - 1                    continue                self.Find_Route(tmpx,tmpy,ex,ey,bend,i)                self.FRvis[tmpx][tmpy] = 0                if i != d:                    bend = bend - 1class Side(QtGui.QWidget):    def __init__(self,parent):        super(Side,self).__init__()        self.setMinimumSize(100,400)    def paintEvent(self,event):        qp = QtGui.QPainter(self)        qp.begin(self)        self.drawRect(qp)        qp.end()    def drawRect(self,qp):        col = QtGui.QColor(0,255,0)        qp.setPen(col)        brush = QtGui.QBrush(QtCore.Qt.SolidPattern);        brush.setColor(col)        qp.setBrush(brush)        #print "\nSide:"        #print self.geometry()        #print self.size()        qp.drawRect(0,0,self.size().width(),self.size().height())def main():    app = QtGui.QApplication(sys.argv)    ex = Link()    sys.exit(app.exec_())if __name__ == '__main__':    main()
0 0
原创粉丝点击