-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlidePanel.py
More file actions
142 lines (107 loc) · 2.34 KB
/
SlidePanel.py
File metadata and controls
142 lines (107 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
import wx
from Tile import Tile
import Tools
class SlidePanel(wx.Panel):
def __init__(self,P,rows=4,cols=4):
super(SlidePanel, self).__init__(P)
if rows<2:
rows=4
if cols<3:
cols=4
self.rows=rows
self.cols=cols
self.SetMinSize( (cols*50, rows*40 ))
self.tiles=None
self.rex=None
self.won=True
self.moves=0
self.watch=None
self.startable=True
self.win=False
self.blank=None
self.initTiles()
self.setGame(self.rows,self.cols)
self.colorTiles()
self.Bind(wx.EVT_SIZE, self.resize)
print ("rows={0} cols={1}".format(self.rows,self.cols))
def setGame(self,rows,cols):
"""set game board size"""
if not self.startable:
if not Tools.confirm(self,'Game In Progress!! Quit Game?'):
return False
self.rows=rows
self.cols=cols
self.initTiles()
return True
def initTiles(self):
rr=[]
tt=[]
n=0
W,H=self.GetSize()
w=W/self.cols
h=H/self.rows
for r in range(self.rows):
y=r*h
for c in range(self.cols):
x=c*w
R=x,y,w,h
t=Tile(self,n,'%s'%(n+1), r, c )
n+=1
t.SetRect(R)
tt.append(t)
rr.append(r)
t.Bind(wx.EVT_LEFT_UP, self.click)
tt[-1].setBlank()
tt[-1].scale=.25
tt[-1].setText('Click\n\ to \n start')
self.blank=tt[-1]
self.startable=True
self.tiles=tt
self.rex=rr
self.startable=True
def colorTiles(self,bg=wx.CYAN,fg=wx.BLUE):
print ('Setting Colors bg=%s fg=%s'%(bg,fg ))
for t in self.tiles:
t.SetBackgroundColour(bg)
t.SetForegroundColour(fg)
self.Refresh()
def tAtRC(self,r,c):
"""return the tile at r,c"""
for t in self.tiles:
tr,tc=t.getRC()
if r==tr and c==tc:
return t
print ('Nothing at %s,%s'%(r,c))
return None
def rAtRC(self, r,c):
"""return the rectangle at r,c"""
i=r*self.cols+c
return self.rex[i]
def resize(self,e):
W,H=self.GetSize()
w=W/self.cols
h=H/self.rows
n=0
for r in range(self.rows):
y=r*h
for c in range(self.cols):
x=c*w
self.rex[n]=x,y,w,h
t=self.tAtRC(r,c)
t.SetRect(self.rex[n])
def click(self,e):
t=e.GetEventObject()
print('%s'%(t))
class App(wx.App):
def __init__(self):
super(App, self).__init__()
F=wx.Frame(None)
sp=SlidePanel(F,4,4)
box=wx.BoxSizer(wx.VERTICAL)
box.Add(sp, 1, wx.EXPAND, wx.ALL)
F.SetSizer(box)
F.Fit()
F.Show()
if __name__=='__main__':
App().MainLoop()