胡锦涛 2009年08月14日 星期五 06:29 | 1769次浏览 | 4条评论
没事写的小程序 直接扔代码吧
学习python中
拼图游戏 还有很多Bug ORZ!见笑了
#!/usr/bin/env python
import random
import sys
import pygtk; pygtk.require("2.0")
import gtk
pixmap = None
press_x = 0
press_y = 0
split = 5
size = split * split
def combo_changed(widget):
global size,split,NumList
i = widget.get_active()
if i == 0:
split = 5
elif i == 1:
split = 6
else :
split = 7
size = split *split
init_list()
luan(NumList)
draw()
return True
def destroy(*arg):
window.hide()
gtk.main_quit()
def check(list):
for i in range(0,len(list)):
if i <> list[i]:
return False
return True
def luan_clicked(*arg):
luan(NumList)
draw()
return True
def button_press_event(widget, event):
global press_x,press_y
press_x = event.x
press_y = event.y
return True
def button_release_event(widget, event):
global pixbuf,press_x,press_y
win = drawing_area.window
w = pixbuf.get_width()
h = pixbuf.get_height()
w1 = int(w / split)
h1 = int(h / split)
if int(event.x / w1) != int(press_x / w1) or int(event.y / h1) != int(press_y / h1):
src_x = int(press_x / w1) * w1
src_y = int(press_y / h1) * h1
src_id = int(press_x / w1) + int(press_y / h1) * split
src_pos = NumList[src_id]
src_image_x = (src_pos % split) * w1
src_image_y = int(src_pos / split) * h1
desc_x = int(event.x / w1) * w1
desc_y = int(event.y / h1) * h1
desc_id = int(event.x / w1) + int(event.y / h1) * split
desc_pos = NumList[desc_id]
desc_image_x = (desc_pos % split) * w1
desc_image_y = int(desc_pos / split) * h1
temp = NumList[src_id]
NumList[src_id] = NumList[desc_id]
NumList[desc_id] = temp
pixmap.draw_pixbuf(drawing_area.get_style().white_gc,pixbuf,src_image_x,src_image_y,desc_x,desc_y,w1,h1,0,0,0)
pixmap.draw_pixbuf(drawing_area.get_style().white_gc,pixbuf,desc_image_x,desc_image_y,src_x,src_y,w1,h1,0,0,0)
r1 = gtk.gdk.Rectangle()
r1.x = 0
r1.y = 0
r1.width = w
r1.height = h
win.invalidate_rect(r1,True)
if check(NumList):
dlg = gtk.MessageDialog(window,gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_INFO,
gtk.BUTTONS_OK,
"Your are win!")
dlg.run()
dlg.destroy()
return True
def new(*arg):
global pixbuf
dlg = gtk.FileChooserDialog("Open File",
window,
buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL))
resp = dlg.run()
dlg.hide()
if resp == gtk.RESPONSE_OK:
fname = dlg.get_filename()
pixbuf = gtk.gdk.pixbuf_new_from_file(fname)
luan(NumList)
draw()
return True
def draw():
global pixmap,pixbuf,NumList
win = drawing_area.window
width, height = win.get_size()
pixmap = gtk.gdk.Pixmap(win, width, height)
#pixmap.draw_rectangle(drawing_area.get_style().white_gc, True, 0, 0, width, height)
for i in range(0,size):
w = pixbuf.get_width()
h = pixbuf.get_height()
w1 = int(w / split)
h1 = int(h / split)
x = (NumList[i] % split)*w1
y = int(NumList[i] / split)*h1
x1 = (i % split)*w1
y1 = int(i / split)*h1
pixmap.draw_pixbuf(drawing_area.get_style().white_gc,pixbuf,x,y,x1,y1,w1,h1,0,0,0)
r1 = gtk.gdk.Rectangle()
r1.x = 0
r1.y = 0
r1.width = width
r1.height = height
win.invalidate_rect(r1,True)
def expose_event(widget, event):
if pixmap is not None:
x, y, width, height = event.area
gc = widget.get_style().fg_gc[gtk.STATE_NORMAL]
widget.window.draw_drawable(gc, pixmap, x, y, x, y, width, height)
return False
def luan(list):
for i in range(0, len(list)):
rand_pos = random.randint(0, len(list)-1)
temp = list[rand_pos]
list[rand_pos] = list[i]
list[i] = temp
def init_list():
global NumList,size
NumList = []
for i in range(0,size):
NumList.append(i)
NumList = []
init_list()
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.maximize()
window.connect("destroy", destroy)
drawing_area = gtk.DrawingArea()
vbox = gtk.VBox()
button = gtk.Button("new")
button_luan = gtk.Button("luan")
hbox = gtk.HBox()
hbox.pack_start(button,False,False,10)
hbox.pack_start(button_luan,False,False,10)
hbox.pack_start(gtk.Label(" level:"),False,False,0)
combo = gtk.combo_box_new_text()
combo.append_text("Easy")
combo.append_text("Difficult")
combo.append_text("Hard")
combo.set_active(0)
combo.connect("changed",combo_changed)
hbox.pack_start(combo,False,False,10)
vbox.pack_start(hbox,False,False,0)
vbox.add(drawing_area)
button.connect("clicked", new)
button_luan.connect("clicked", luan_clicked)
#drawing_area.connect("configure_event", configure_event)
drawing_area.connect("expose_event", expose_event)
drawing_area.connect("button_press_event", button_press_event)
drawing_area.connect("button_release_event", button_release_event)
drawing_area.set_events(gtk.gdk.EXPOSURE_MASK |
gtk.gdk.LEAVE_NOTIFY_MASK |
gtk.gdk.BUTTON_PRESS_MASK |
gtk.gdk.BUTTON_RELEASE_MASK |
gtk.gdk.POINTER_MOTION_MASK |
gtk.gdk.POINTER_MOTION_HINT_MASK)
#xspixbuf = gtk.gdk.pixbuf_new_from_file("Screenshot.png")
window.add(vbox)
window.show_all()
gtk.main()
Zeuux © 2024
京ICP备05028076号
回复 李晓谦 2009年08月21日 星期五 14:36
回复 邓楠 2010年01月25日 星期一 05:04