level 1
n781014
楼主
# -*- coding:utf-8 -*-
from PyQt5.QtWidgets import QPushButton,QApplication,QGridLayout,QWidget
from PyQt5.QtCore import Qt
from sys import exit,argv
from random import randint
class Grid(QPushButton):#格子类,是小的格子
def __init__(self,world):
super(Grid,self).__init__()
self.mine = False
self.number = 0
self.open = False
self.new = False
self.tag = False
self.world = world
self.ID = 0
def mousePressEvent(self,event):
butt = event.button()
if butt == Qt.LeftButton:
self.OpenSelf()
self.world.MayOpen()
if butt == Qt.RightButton:
if self.open == False:
if self.tag == False:
self.tag = True
self.setText(u*雷*)
else:
self.tag = False
self.setText(**)
else:
self.OpenAround()
def OpenAround(self):
if self.open == True and self.number != 0:
world = self.world
x = self.ID % world.width
y = self.ID / world.width
num = 0
if y > 0 :
if x > 0:
if world.MineField[self.ID - world.width - 1].tag:
num += 1
if world.MineField[self.ID - world.width].tag:
num += 1
if x < (world.width - 1):
if world.MineField[self.ID - world.width + 1].tag:
num += 1
if x > 0:
if world.MineField[self.ID -1].tag:
num += 1
if x < (world.width - 1):
if world.MineField[self.ID + 1].tag:
num += 1
if y < (world.height - 1):
if x > 0:
if world.MineField[self.ID + world.width - 1].tag:
num += 1
if world.MineField[self.ID + world.width].tag:
num += 1
if x < (world.width - 1):
if world.MineField[self.ID + world.width + 1].tag:
num += 1
if num == self.number:
if y > 0 :
if x > 0:
world.MineField[self.ID - world.width - 1].OpenSelf()
world.MineField[self.ID - world.width].OpenSelf()
if x < (world.width - 1):
world.MineField[self.ID - world.width + 1].OpenSelf()
if x > 0:
world.MineField[self.ID -1].OpenSelf()
if x < (world.width - 1):
world.MineField[self.ID + 1].OpenSelf()
if y < (world.height - 1):
if x > 0:
world.MineField[self.ID + world.width - 1].OpenSelf()
world.MineField[self.ID + world.width].OpenSelf()
if x < (world.width - 1):
world.MineField[self.ID + world.width + 1].OpenSelf()
def OpenSelf(self):
if not self.open:
if not self.tag:
self.open = True
self.setFlat(True)
if self.mine:
self.setText(***)
#GAME OVER
elif self.number > 0:
self.setText(str(self.number))
else:
self.new = True
if not self.world.tag:
self.world.tag = True
class World(QGridLayout):#管埋格子
def __init__(self,width = 16,height = 16,mines = 40):
super(World,self).__init__()
self.MineField = []
self.width = width
self.height = height
self.mines = mines
self.tag = False
self.AddGrid()
self.setMines()
def AddGrid(self):
i = 0
for y in range(self.height):
for x in range(self.width):
#print(i)
grid = Grid(self)
grid.ID = i
self.MineField.append(grid)
self.addWidget(grid,y,x)
i += 1
def setMines(self):#ok
size = (self.width * self.height) - 1
i = 0
while True:
if i == self.mines:
break
j = randint(0, size)
if not self.MineField[j].mine:
self.MineField[j].mine = True
i += 1
x = j % self.width
y = j / self.width
if y > 0:
if x > 0:
self.MineField[j-self.width-1].number += 1
self.MineField[j-self.width].number += 1
if x < (self.width - 1):
self.MineField[j-self.width+1].number += 1
if x > 0:
self.MineField[j-1].number += 1
if x < (self.width - 1):
self.MineField[j+1].number += 1
if y < (self.height - 1):
if x > 0:
self.MineField[j+self.width-1].number += 1
self.MineField[j+self.width].number += 1
if x < (self.width - 1):
self.MineField[j+self.width+1].number += 1
def MayOpen(self):
while self.tag:
self.tag = False
for widget in self.MineField:
if widget.new == True:
widget.new = False
x = widget.ID % self.width
y = widget.ID / self.width
if y > 0:
if x > 0:
self.MineField[widget.ID - self.width - 1].OpenSelf()
self.MineField[widget.ID - self.width].OpenSelf()
if x < (self.width - 1):
self.MineField[widget.ID - self.width + 1].OpenSelf()
if x > 0:
self.MineField[widget.ID - 1].OpenSelf()
if x < (self.width - 1):
self.MineField[widget.ID + 1].OpenSelf()
if y < (self.height - 1):
if x > 0:
self.MineField[widget.ID + self.width - 1].OpenSelf()
self.MineField[widget.ID + self.width].OpenSelf()
if x < (self.width - 1):
self.MineField[widget.ID + self.width + 1].OpenSelf()
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.setWindowTitle(u*我的Qt窗口*)
layout = World()
self.setFixedSize(27*layout.width,27*layout.height)#设置固定大小
layout.setSpacing(0)
self.setBaseSize(0, 0)
self.setLayout(layout)
if __name__ == *__main__*:
app = QApplication(argv)
window = Window()
window.show()
exit(app.exec_())
2014年11月01日 03点11分
1
from PyQt5.QtWidgets import QPushButton,QApplication,QGridLayout,QWidget
from PyQt5.QtCore import Qt
from sys import exit,argv
from random import randint
class Grid(QPushButton):#格子类,是小的格子
def __init__(self,world):
super(Grid,self).__init__()
self.mine = False
self.number = 0
self.open = False
self.new = False
self.tag = False
self.world = world
self.ID = 0
def mousePressEvent(self,event):
butt = event.button()
if butt == Qt.LeftButton:
self.OpenSelf()
self.world.MayOpen()
if butt == Qt.RightButton:
if self.open == False:
if self.tag == False:
self.tag = True
self.setText(u*雷*)
else:
self.tag = False
self.setText(**)
else:
self.OpenAround()
def OpenAround(self):
if self.open == True and self.number != 0:
world = self.world
x = self.ID % world.width
y = self.ID / world.width
num = 0
if y > 0 :
if x > 0:
if world.MineField[self.ID - world.width - 1].tag:
num += 1
if world.MineField[self.ID - world.width].tag:
num += 1
if x < (world.width - 1):
if world.MineField[self.ID - world.width + 1].tag:
num += 1
if x > 0:
if world.MineField[self.ID -1].tag:
num += 1
if x < (world.width - 1):
if world.MineField[self.ID + 1].tag:
num += 1
if y < (world.height - 1):
if x > 0:
if world.MineField[self.ID + world.width - 1].tag:
num += 1
if world.MineField[self.ID + world.width].tag:
num += 1
if x < (world.width - 1):
if world.MineField[self.ID + world.width + 1].tag:
num += 1
if num == self.number:
if y > 0 :
if x > 0:
world.MineField[self.ID - world.width - 1].OpenSelf()
world.MineField[self.ID - world.width].OpenSelf()
if x < (world.width - 1):
world.MineField[self.ID - world.width + 1].OpenSelf()
if x > 0:
world.MineField[self.ID -1].OpenSelf()
if x < (world.width - 1):
world.MineField[self.ID + 1].OpenSelf()
if y < (world.height - 1):
if x > 0:
world.MineField[self.ID + world.width - 1].OpenSelf()
world.MineField[self.ID + world.width].OpenSelf()
if x < (world.width - 1):
world.MineField[self.ID + world.width + 1].OpenSelf()
def OpenSelf(self):
if not self.open:
if not self.tag:
self.open = True
self.setFlat(True)
if self.mine:
self.setText(***)
#GAME OVER
elif self.number > 0:
self.setText(str(self.number))
else:
self.new = True
if not self.world.tag:
self.world.tag = True
class World(QGridLayout):#管埋格子
def __init__(self,width = 16,height = 16,mines = 40):
super(World,self).__init__()
self.MineField = []
self.width = width
self.height = height
self.mines = mines
self.tag = False
self.AddGrid()
self.setMines()
def AddGrid(self):
i = 0
for y in range(self.height):
for x in range(self.width):
#print(i)
grid = Grid(self)
grid.ID = i
self.MineField.append(grid)
self.addWidget(grid,y,x)
i += 1
def setMines(self):#ok
size = (self.width * self.height) - 1
i = 0
while True:
if i == self.mines:
break
j = randint(0, size)
if not self.MineField[j].mine:
self.MineField[j].mine = True
i += 1
x = j % self.width
y = j / self.width
if y > 0:
if x > 0:
self.MineField[j-self.width-1].number += 1
self.MineField[j-self.width].number += 1
if x < (self.width - 1):
self.MineField[j-self.width+1].number += 1
if x > 0:
self.MineField[j-1].number += 1
if x < (self.width - 1):
self.MineField[j+1].number += 1
if y < (self.height - 1):
if x > 0:
self.MineField[j+self.width-1].number += 1
self.MineField[j+self.width].number += 1
if x < (self.width - 1):
self.MineField[j+self.width+1].number += 1
def MayOpen(self):
while self.tag:
self.tag = False
for widget in self.MineField:
if widget.new == True:
widget.new = False
x = widget.ID % self.width
y = widget.ID / self.width
if y > 0:
if x > 0:
self.MineField[widget.ID - self.width - 1].OpenSelf()
self.MineField[widget.ID - self.width].OpenSelf()
if x < (self.width - 1):
self.MineField[widget.ID - self.width + 1].OpenSelf()
if x > 0:
self.MineField[widget.ID - 1].OpenSelf()
if x < (self.width - 1):
self.MineField[widget.ID + 1].OpenSelf()
if y < (self.height - 1):
if x > 0:
self.MineField[widget.ID + self.width - 1].OpenSelf()
self.MineField[widget.ID + self.width].OpenSelf()
if x < (self.width - 1):
self.MineField[widget.ID + self.width + 1].OpenSelf()
class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.setWindowTitle(u*我的Qt窗口*)
layout = World()
self.setFixedSize(27*layout.width,27*layout.height)#设置固定大小
layout.setSpacing(0)
self.setBaseSize(0, 0)
self.setLayout(layout)
if __name__ == *__main__*:
app = QApplication(argv)
window = Window()
window.show()
exit(app.exec_())