# 看了一段时间了Python,看了一下Qt,花了2个多小时根据terix写了这个漏洞挺多的snake<画图,时间,event>
# 写的过程中发现 对界面大小方面不懂,乱筹才分成这21x21的格子...
# 测试时发现对于那种处在边缘的格子 方法应该不对,先不管啦!!!
# 希望每一天都有收获!!!
import sys
from PyQt4 import QtGui,QtCore
import random,copy
oardMap = [[ [0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0],
[0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,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0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]
class Snake(QtGui.QWidget):
def __init__(self,parent=None):
super(Snake,self).__init__(parent)
self.board = Board()
score = QtGui.QLCDNumber()
score.setSegmentStyle(QtGui.QLCDNumber.Filled)
time = QtGui.QLCDNumber()
time.setSegmentStyle(QtGui.QLCDNumber.Filled)
start = QtGui.QPushButton('&Start')
start.setFocusPolicy(QtCore.Qt.NoFocus)
pause = QtGui.QPushButton("&Pause")
pause.setFocusPolicy(QtCore.Qt.NoFocus)
start.clicked.connect(self.board.start)
pause.clicked.connect(self.board.pause)
self.board.scoreChanged.connect(score.display)
self.board.timeChanged.connect(time.display)
# layout_ = QtGui.QVBoxLayout()
# layout_.addWidget(score)
# layout_.addWidget(time)
# layout_.addWidget(start)
# layout_.addWidget(pause)
#
#
# layout = QtGui.QHBoxLayout()
# layout.addWidget(self.board)
# layout.addLayout(layout_)
layout = QtGui.QGridLayout()
layout.addWidget(self.board,0,1,4,1)
layout.addWidget(time,0,2)
layout.addWidget(score,1,2)
layout.addWidget(start,2,2)
layout.addWidget(pause,3,2)
self.setLayout(layout)
self.setWindowTitle('snake')
self.resize(452,232)
class Map():
pass
class Board(QtGui.QFrame):
boardwidth = 20
boardheight = 20
scoreChanged = QtCore.pyqtSignal(int)
timeChanged = QtCore.pyqtSignal(int)
def __init__(self,parent=None):
super(Board,self).__init__(parent)
self.timer = QtCore.QBasicTimer()
self.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Sunken)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.box = [[0,0]]
self.board = boardMap[0]
self.isPaused = False
self.isStart = False
self.dir = 'r'
self.curx = 0
self.cury = 0
self.feat = False
#self.clearBoard()
self.resize(202,202)
self.rect = self.contentsRect()
#print self.rect.width(),self.rect.height()
#print self.width(),self.height()
def clearBoard(self):
self.board =[ [0 for i in xrange(Board.boardwidth+1)] for j in xrange(Board.boardheight+1) ]
def pause(self):
if not self.isStart:
return
self.isPaused = not self.isPaused
if self.isPaused:
self.timer.stop()
else:
self.timer.start(self.timeoutTime(),self)
self.update()
def timeoutTime(self):
return 1000 / 3
def timerEvent(self,event):
if event.timerId() == self.timer.timerId():
self.time += 1
self.direction(self.dir)
self.tryMove()
#print self.time
def direction(self,dir):
if dir == 'u':
self.cury -= self.height()
elif dir == 'd':
self.cury += self.height()
elif dir == 'l':
self.curx -= self.width()
elif dir == 'r':
self.curx += self.width()
def keyPressEvent(self,event):
if not self.isStart or self.isPaused:
super(Board,self).keyPressEvent(event)
return
key = event.key()
if key == QtCore.Qt.Key_Left:
if self.dir == 'r':
return
self.dir = 'l'
elif key == QtCore.Qt.Key_Right:
if self.dir == 'l':
return
self.dir = 'r'
elif key == QtCore.Qt.Key_Up:
if self.dir == 'd':
return
self.dir = 'u'
elif key == QtCore.Qt.Key_Down:
if self.dir == 'u':
return
self.dir = 'd'
else:
super(Board,self).keyPressEvent(event)
#self.tryMove()
def tryMove(self):
#print self.curx,self.cury
if self.curx<0 or self.curx>200 or self.cury<0 or self.cury>200:
self.feat = True
x = self.curx / self.width()
y = self.cury / self.height()
if x<=20 and y<=20:
#print x,y
if self.board[y][x]:
self.board[y][x] = 0
#self.box.append([y,x])
#self.curx = x*self.width()
#self.cury = y*self.height()
self.box.append([self.cury,self.curx])
if len(self.box) > 0:
for i in xrange(len(self.box)-1):
self.box[i] = self.box[i+1]
self.direction(self.dir)
if self.curx<0:
self.curx = 0
elif self.curx>200:
self.curx = 200
elif self.cury<0:
self.cury = 0
elif self.cury>200:
self.cury=200
self.box[len(self.box)-1] = [self.cury,self.curx]
self.score += 1
else:
if len(self.box) > 0:
for i in xrange(len(self.box)-1):
self.box[i] = self.box[i+1]
self.box[len(self.box)-1] = [y*self.height(),x*self.width()]
self.timeChanged.emit(self.time)
self.scoreChanged.emit(self.score)
self.update()
def start(self):
if self.isPaused:
return
self.isStart = True
self.score = 0
self.time = 0
# self.scoreChanged.emit(self.score)
# self.timeChanged.emit(self.time)
self.curx = 0
self.cury = 0
self.feat = False
self.box = [[0,0]]
self.clearBoard()
self.board = copy.deepcopy(boardMap[0])
#print boardMap[0]
#self.board[0][20] = 1
# self.board[0][8] = 1
# self.board[2][8] = 1
# self.board[19][8] = 1
self.scoreChanged.emit(self.score)
self.timeChanged.emit(self.time)
self.timer.start(self.timeoutTime(),self)
def width(self):
return self.rect.width() / Board.boardwidth
def height(self):
return self.rect.height() / Board.boardheight
def paintEvent(self,event):
super(Board,self).paintEvent(event)
painter = QtGui.QPainter(self)
rect = self.contentsRect()
if self.feat:
self.clearBoard()
self.curx = 0
self.cury = 0
self.dir = 'r'
self.pause()
self.isPaused = False
painter.drawText(rect,QtCore.Qt.AlignCenter,'Fail to game')
return
if self.isPaused:
painter.drawText(rect,QtCore.Qt.AlignCenter,"pause")
return
success = 0
for x in xrange(Board.boardheight+1):
for y in xrange(Board.boardwidth+1):
if self.board[x][y]:
success += 1
self.drawRect(painter,y*self.width(),x*self.height())
if not success:
self.curx = self.cury = 0
self.dir = 'r'
self.pause()
self.isPaused = False
painter.drawText(rect,QtCore.Qt.AlignCenter,'you win the game , try more !')
return
for i in self.box:
self.drawRect(painter,i[1],i[0])
#print 'x',self.curx,self.cury,'x',self.box[-1]
#self.drawRect(painter,self.curx,self.cury)
def drawRect(self,painter,x,y):
color = QtGui.QColor(0xCC6666)
painter.fillRect(x,y,self.width(),self.height(),color)
painter.setPen(color.light())
painter.drawLine(x,y+self.height(),x,y)
painter.drawLine(x,y,x+self.width(),y)
painter.setPen(color.dark())
painter.drawLine(x,y+self.height(),x+self.width(),y+self.height())
painter.drawLine(x+self.width(),y+self.height(),x+self.width(),y)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
snake = Snake()
snake.show()
sys.exit(app.exec_())