Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 95 lines (81 sloc) 2.661 kb
e62599b add lightout example
lkcl authored
1 # Copyright (C) 2009, Radoslav Kirov
2 #
3 # Lightout game, by Radoslav Kirov
4
5 import pyjd # this is dummy in pyjs.
6 from pyjamas.ui.FlowPanel import FlowPanel
7 from pyjamas.ui.RootPanel import RootPanel
8 from pyjamas.ui.SimplePanel import SimplePanel
9 from pyjamas.ui.Button import Button
10 from pyjamas.ui.CheckBox import CheckBox
11 from pyjamas.ui.HTML import HTML
12 from pyjamas.ui.Label import Label
13 from pyjamas.ui.Grid import Grid
14 from pyjamas import Window
15 from pyjamas import DOM
16 from pyjamas.ui.FocusWidget import FocusWidget
17
11a3680 @rkirov Fix for IE version of lightout game
rkirov authored
18
19 game = None
20
e62599b add lightout example
lkcl authored
21 class GridCell(FocusWidget):
22 def __init__(self,i,j):
23 self.i = i
24 self.j = j
25 self.light = True
26 element = DOM.createDiv()
27 #DOM.setInnerHTML(element,'<b>%i%i</b>' % (i,j))
28 FocusWidget.__init__(self, element)
29 self.redraw()
30 self.addClickListener(self)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
31
e62599b add lightout example
lkcl authored
32 def redraw(self):
33 if self.light:
34 self.setStyleName("on")
35 else:
36 self.setStyleName("off")
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
37
e62599b add lightout example
lkcl authored
38 def toggle(self):
39 if self.light:
40 self.light = False
41 else:
42 self.light = True
43 self.redraw()
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
44
11a3680 @rkirov Fix for IE version of lightout game
rkirov authored
45 def onClick(self,sender):
e62599b add lightout example
lkcl authored
46 if self.i>0:
47 self.parent.getWidget(self.i-1,self.j).toggle()
48 if self.i<self.parent.getRowCount()-1:
49 self.parent.getWidget(self.i+1,self.j).toggle()
50 if self.j>0:
51 self.parent.getWidget(self.i,self.j-1).toggle()
52 if self.j<self.parent.getColumnCount()-1:
53 self.parent.getWidget(self.i,self.j+1).toggle()
54 self.toggle()
55 self.check_win()
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
56
e62599b add lightout example
lkcl authored
57 def check_win(self):
58 for i in range(self.parent.getRowCount()):
59 for j in range(self.parent.getColumnCount()):
60 if self.parent.getWidget(i,j).light:
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
61 return
e62599b add lightout example
lkcl authored
62 Window.alert('You win!!! But can you beat the next level?')
63 global game
64 game.next_level()
65
66 class Game(SimplePanel):
67 def __init__(self,level):
68 self.level = level
69 SimplePanel.__init__(self)
11a3680 @rkirov Fix for IE version of lightout game
rkirov authored
70 self.start_game(self.level)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
71
11a3680 @rkirov Fix for IE version of lightout game
rkirov authored
72 def start_game(self, level=None):
73 if level is not None:
74 self.level = level
e62599b add lightout example
lkcl authored
75 dim = self.level
76 grid = Grid(dim,dim)
77 grid.setStyleName("grid")
78 for i in range(dim):
79 for j in range(dim):
80 gc = GridCell(i,j)
81 grid.setWidget(i,j,gc)
82 self.add(grid)
64eff55 @xtfxme find * -name '*.py' | xargs sed -i -r 's,\s+$,,'
xtfxme authored
83
e62599b add lightout example
lkcl authored
84 def next_level(self):
85 self.remove(self.getWidget())
86 self.level+=1
87 self.start_game()
11a3680 @rkirov Fix for IE version of lightout game
rkirov authored
88
89
e62599b add lightout example
lkcl authored
90 if __name__ == '__main__':
91 pyjd.setup("public/lightout.html")
92 game = Game(3)
bbe5891 RootPanel get function no longer exists
lkcl authored
93 RootPanel('game').add(game)
e62599b add lightout example
lkcl authored
94 pyjd.run()
Something went wrong with that request. Please try again.