以下是代码:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import pygtk
pygtk.require(*2.0*)
import gtk
from random import randint
class Grid(gtk.Button):#最小格子类
def __init__(self,world):
super(Grid,self).__init__()
self.world = world
self.open = False
self.tag = False
self.number = 0
self.new = False
self.mines = False
self.Id = 0
self.connect(*button-press-event*,self.mouse_clicked)
def mouse_clicked(self,widget,event):
if event.type == gtk.gdk.BUTTON_PRESS:
if self.open:
self.open_around()
else:
if event.button == 3:
if self.tag:
self.set_label(**)
else:
self.set_label(*雷*)
self.tag = not self.tag
elif event.button == 1:
self.open_self()
self.world.open_can_grid()
def open_self(self):
if self.tag:
return
if not self.open:
self.open = not self.open
self.set_relief(gtk.RELIEF_NONE)
if self.mines:
self.set_label(***)
print(*GAME OVER*)
elif self.number:
self.set_label(str(self.number))
else:
self.new = True
if not self.world.tag:
self.world.tag = True
def open_around(self):
if self.number:
num = 0
x = self.Id % self.world.width
y = self.Id / self.world.width
if y > 0:
if x > 0:
if self.world.map[self.Id-self.world.width-1].tag:
num += 1
if self.world.map[self.Id-self.world.width].tag:
num += 1
if x < self.world.width - 1:
if self.world.map[self.Id-self.world.width+1].tag:
num += 1
if x > 0:
if self.world.map[self.Id-1].tag:
num += 1
if x < self.world.width - 1:
if self.world.map[self.Id+1].tag:
num += 1
if y < self.world.height - 1:
if x > 0:
if self.world.map[self.Id+self.world.width-1].tag:
num += 1
if self.world.map[self.Id+self.world.width].tag:
num += 1
if x < self.world.width - 1:
if self.world.map[self.Id+self.world.width+1].tag:
num += 1
if self.number == num:
if y > 0:
if x > 0:
self.world.map[self.Id-self.world.width-1].open_self()
self.world.map[self.Id-self.world.width].open_self()
if x < self.world.width - 1:
self.world.map[self.Id-self.world.width+1].open_self()
if x > 0:
self.world.map[self.Id-1].open_self()
if x < self.world.width - 1:
self.world.map[self.Id+1].open_self()
if y < self.world.height - 1:
if x > 0:
self.world.map[self.Id+self.world.width-1].open_self()
self.world.map[self.Id+self.world.width].open_self()
if x < self.world.width - 1:
self.world.map[self.Id+self.world.width+1].open_self()
self.world.open_can_grid()
class World(gtk.Table):#世界类
def __init__(self,width = 8,height = 8,mines = 10):
super(World,self).__init__()
self.width = width
self.height = height
self.mines = mines
self.tag = False
self.size = self.width * self.height
self.map = []
self.resize(self.width,self.height)
self.set_homogeneous(True)
self.add_widget()
self.add_mines()
def open_can_grid(self):
while self.tag:
self.tag = False
for i in range(self.size):
if self.map[i].new and not self.map[i].number:
self.map[i].new = False
x = i % self.width
y = i / self.width
if y > 0:
if x > 0:
self.map[i-self.width-1].open_self()
self.map[i-self.width].open_self()
if x < self.width - 1:
self.map[i-self.width+1].open_self()
if x > 0:
self.map[i-1].open_self()
if x < self.width - 1:
self.map[i+1].open_self()
if y < self.height - 1:
if x > 0:
self.map[i+self.width-1].open_self()
self.map[i+self.width].open_self()
if x < self.width - 1:
self.map[i+self.width+1].open_self()
def add_mines(self):
size = self.size - 1
for i in range(self.mines):
while True:
j = randint(0,size)
if not self.map[j].mines:
self.map[j].mines = True
x = j % self.width
y = j / self.height
if y > 0:
if x > 0:
self.map[j-self.width-1].number += 1
self.map[j-self.width].number += 1
if x < self.width - 1:
self.map[j-self.width+1].number += 1
if x > 0:
self.map[j-1].number += 1
if x < self.width -1:
self.map[j+1].number += 1
if y < self.height - 1:
if x > 0:
self.map[j+self.width-1].number += 1
self.map[j+self.width].number += 1
if x < self.width - 1:
self.map[j+self.width+1].number += 1
break
def add_widget(self):
i = 0
for y in range(self.height):
for x in range(self.width):
grid = Grid(self)
grid.Id = i
self.map.append(grid)
self.attach(grid,x,x+1,y,y+1)
i += 1
class GameApp(gtk.Window):
def __init__(self):
super(GameApp,self).__init__()
self.set_icon_from_file("gur-project-21.png")
self.connect(*destroy*,gtk.main_quit)
self.set_position(gtk.WIN_POS_CENTER)
#self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(2000, 2000, 2000))
toolbar = gtk.Toolbar()
toolbar.set_style(gtk.TOOLBAR_ICONS)
#newtb = gtk.ToolButton(gtk.STOCK_NEW)
#opentb = gtk.ToolButton(gtk.STOCK_OPEN)
#savetb = gtk.ToolButton(gtk.STOCK_SAVE)
#sep = gtk.SeparatorToolItem()
#quittb = gtk.ToolButton(gtk.STOCK_QUIT)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-12.png*)
btn.set_image(image)
btn.connect(*clicked*,self.modify_data)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-13.png*)
btn.set_image(image)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-14.png*)
btn.set_image(image)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-15.png*)
btn.set_image(image)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-16.png*)
btn.set_image(image)
toolbar.append_widget(btn,*我的工具*,*我的工具*)
self.vbox = gtk.VBox(False, 0)
self.world = World()
self.set_size_request(self.world.width*26,self.world.height*26+60)
self.add(self.vbox)
self.vbox.pack_start(toolbar, False, False, 0)
self.vbox.pack_start(self.world, True, True, 0)
self.show_all()
def run(self):
gtk.main()
def modify_data(self):#调用这个有问题
if self.world:
self.world.destroy()
self.hide()
self.world = World(16, 16, 40)
self.set_size_request(self.world.width*26,self.world.height*26+60)
self.vbox.pack_start(self.world, False, False, 0)
self.show_all()
if __name__ == *__main__*:
game = GameApp()
game.run()
#!/usr/bin/python
# -*- coding:utf-8 -*-
import pygtk
pygtk.require(*2.0*)
import gtk
from random import randint
class Grid(gtk.Button):#最小格子类
def __init__(self,world):
super(Grid,self).__init__()
self.world = world
self.open = False
self.tag = False
self.number = 0
self.new = False
self.mines = False
self.Id = 0
self.connect(*button-press-event*,self.mouse_clicked)
def mouse_clicked(self,widget,event):
if event.type == gtk.gdk.BUTTON_PRESS:
if self.open:
self.open_around()
else:
if event.button == 3:
if self.tag:
self.set_label(**)
else:
self.set_label(*雷*)
self.tag = not self.tag
elif event.button == 1:
self.open_self()
self.world.open_can_grid()
def open_self(self):
if self.tag:
return
if not self.open:
self.open = not self.open
self.set_relief(gtk.RELIEF_NONE)
if self.mines:
self.set_label(***)
print(*GAME OVER*)
elif self.number:
self.set_label(str(self.number))
else:
self.new = True
if not self.world.tag:
self.world.tag = True
def open_around(self):
if self.number:
num = 0
x = self.Id % self.world.width
y = self.Id / self.world.width
if y > 0:
if x > 0:
if self.world.map[self.Id-self.world.width-1].tag:
num += 1
if self.world.map[self.Id-self.world.width].tag:
num += 1
if x < self.world.width - 1:
if self.world.map[self.Id-self.world.width+1].tag:
num += 1
if x > 0:
if self.world.map[self.Id-1].tag:
num += 1
if x < self.world.width - 1:
if self.world.map[self.Id+1].tag:
num += 1
if y < self.world.height - 1:
if x > 0:
if self.world.map[self.Id+self.world.width-1].tag:
num += 1
if self.world.map[self.Id+self.world.width].tag:
num += 1
if x < self.world.width - 1:
if self.world.map[self.Id+self.world.width+1].tag:
num += 1
if self.number == num:
if y > 0:
if x > 0:
self.world.map[self.Id-self.world.width-1].open_self()
self.world.map[self.Id-self.world.width].open_self()
if x < self.world.width - 1:
self.world.map[self.Id-self.world.width+1].open_self()
if x > 0:
self.world.map[self.Id-1].open_self()
if x < self.world.width - 1:
self.world.map[self.Id+1].open_self()
if y < self.world.height - 1:
if x > 0:
self.world.map[self.Id+self.world.width-1].open_self()
self.world.map[self.Id+self.world.width].open_self()
if x < self.world.width - 1:
self.world.map[self.Id+self.world.width+1].open_self()
self.world.open_can_grid()
class World(gtk.Table):#世界类
def __init__(self,width = 8,height = 8,mines = 10):
super(World,self).__init__()
self.width = width
self.height = height
self.mines = mines
self.tag = False
self.size = self.width * self.height
self.map = []
self.resize(self.width,self.height)
self.set_homogeneous(True)
self.add_widget()
self.add_mines()
def open_can_grid(self):
while self.tag:
self.tag = False
for i in range(self.size):
if self.map[i].new and not self.map[i].number:
self.map[i].new = False
x = i % self.width
y = i / self.width
if y > 0:
if x > 0:
self.map[i-self.width-1].open_self()
self.map[i-self.width].open_self()
if x < self.width - 1:
self.map[i-self.width+1].open_self()
if x > 0:
self.map[i-1].open_self()
if x < self.width - 1:
self.map[i+1].open_self()
if y < self.height - 1:
if x > 0:
self.map[i+self.width-1].open_self()
self.map[i+self.width].open_self()
if x < self.width - 1:
self.map[i+self.width+1].open_self()
def add_mines(self):
size = self.size - 1
for i in range(self.mines):
while True:
j = randint(0,size)
if not self.map[j].mines:
self.map[j].mines = True
x = j % self.width
y = j / self.height
if y > 0:
if x > 0:
self.map[j-self.width-1].number += 1
self.map[j-self.width].number += 1
if x < self.width - 1:
self.map[j-self.width+1].number += 1
if x > 0:
self.map[j-1].number += 1
if x < self.width -1:
self.map[j+1].number += 1
if y < self.height - 1:
if x > 0:
self.map[j+self.width-1].number += 1
self.map[j+self.width].number += 1
if x < self.width - 1:
self.map[j+self.width+1].number += 1
break
def add_widget(self):
i = 0
for y in range(self.height):
for x in range(self.width):
grid = Grid(self)
grid.Id = i
self.map.append(grid)
self.attach(grid,x,x+1,y,y+1)
i += 1
class GameApp(gtk.Window):
def __init__(self):
super(GameApp,self).__init__()
self.set_icon_from_file("gur-project-21.png")
self.connect(*destroy*,gtk.main_quit)
self.set_position(gtk.WIN_POS_CENTER)
#self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(2000, 2000, 2000))
toolbar = gtk.Toolbar()
toolbar.set_style(gtk.TOOLBAR_ICONS)
#newtb = gtk.ToolButton(gtk.STOCK_NEW)
#opentb = gtk.ToolButton(gtk.STOCK_OPEN)
#savetb = gtk.ToolButton(gtk.STOCK_SAVE)
#sep = gtk.SeparatorToolItem()
#quittb = gtk.ToolButton(gtk.STOCK_QUIT)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-12.png*)
btn.set_image(image)
btn.connect(*clicked*,self.modify_data)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-13.png*)
btn.set_image(image)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-14.png*)
btn.set_image(image)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-15.png*)
btn.set_image(image)
toolbar.append_widget(btn,**,**)
btn = gtk.Button()
image = gtk.Image()
image.set_from_file(*png/gur-project-16.png*)
btn.set_image(image)
toolbar.append_widget(btn,*我的工具*,*我的工具*)
self.vbox = gtk.VBox(False, 0)
self.world = World()
self.set_size_request(self.world.width*26,self.world.height*26+60)
self.add(self.vbox)
self.vbox.pack_start(toolbar, False, False, 0)
self.vbox.pack_start(self.world, True, True, 0)
self.show_all()
def run(self):
gtk.main()
def modify_data(self):#调用这个有问题
if self.world:
self.world.destroy()
self.hide()
self.world = World(16, 16, 40)
self.set_size_request(self.world.width*26,self.world.height*26+60)
self.vbox.pack_start(self.world, False, False, 0)
self.show_all()
if __name__ == *__main__*:
game = GameApp()
game.run()