Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add lightout example

git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@1248 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information...
commit e62599b1fde7b7780ed9bc18073e9905ccfe07df 1 parent 810ca61
lkcl authored
View
7 examples/lightout/build.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+# you will need to read the top level README, and run boostrap.py
+# and buildout in order to make pyjsbuild
+
+options="$*"
+#if [ -z $options ] ; then options="-O";fi
+../../bin/pyjsbuild --print-statements $options lightout
View
90 examples/lightout/lightout.py
@@ -0,0 +1,90 @@
+# Copyright (C) 2009, Radoslav Kirov
+#
+# Lightout game, by Radoslav Kirov
+
+import pyjd # this is dummy in pyjs.
+from pyjamas.ui.FlowPanel import FlowPanel
+from pyjamas.ui.RootPanel import RootPanel
+from pyjamas.ui.SimplePanel import SimplePanel
+from pyjamas.ui.Button import Button
+from pyjamas.ui.CheckBox import CheckBox
+from pyjamas.ui.HTML import HTML
+from pyjamas.ui.Label import Label
+from pyjamas.ui.Grid import Grid
+from pyjamas import Window
+from pyjamas import DOM
+from pyjamas.ui.FocusWidget import FocusWidget
+
+class GridCell(FocusWidget):
+ def __init__(self,i,j):
+ self.i = i
+ self.j = j
+ self.light = True
+ element = DOM.createDiv()
+ #DOM.setInnerHTML(element,'<b>%i%i</b>' % (i,j))
+ FocusWidget.__init__(self, element)
+ self.redraw()
+ self.addClickListener(self)
+
+ def redraw(self):
+ if self.light:
+ self.setStyleName("on")
+ else:
+ self.setStyleName("off")
+
+ def toggle(self):
+ if self.light:
+ self.light = False
+ else:
+ self.light = True
+ self.redraw()
+
+ def onClick(self, sender):
+ if self.i>0:
+ self.parent.getWidget(self.i-1,self.j).toggle()
+ if self.i<self.parent.getRowCount()-1:
+ print self.i+1, self.j
+ self.parent.getWidget(self.i+1,self.j).toggle()
+ if self.j>0:
+ self.parent.getWidget(self.i,self.j-1).toggle()
+ if self.j<self.parent.getColumnCount()-1:
+ self.parent.getWidget(self.i,self.j+1).toggle()
+ self.toggle()
+ self.check_win()
+
+ def check_win(self):
+ for i in range(self.parent.getRowCount()):
+ for j in range(self.parent.getColumnCount()):
+ if self.parent.getWidget(i,j).light:
+ return
+ Window.alert('You win!!! But can you beat the next level?')
+ global game
+ game.next_level()
+
+class Game(SimplePanel):
+ def __init__(self,level):
+ self.level = level
+ SimplePanel.__init__(self)
+ self.start_game()
+
+ def start_game(self):
+ dim = self.level
+ grid = Grid(dim,dim)
+ grid.setStyleName("grid")
+ for i in range(dim):
+ for j in range(dim):
+ gc = GridCell(i,j)
+ grid.setWidget(i,j,gc)
+ self.add(grid)
+
+ def next_level(self):
+ self.remove(self.getWidget())
+ self.level+=1
+ self.start_game()
+
+if __name__ == '__main__':
+ pyjd.setup("public/lightout.html")
+ game = Game(3)
+ RootPanel().get('game').add(game)
+ #RootPanel().add(game)
+ pyjd.run()
View
31 examples/lightout/public/lightout.css
@@ -0,0 +1,31 @@
+.on {
+ padding: 10px;
+ background-color: #C8C506;
+ -moz-user-select: none;
+ -webkit-user-select: none;
+ user-select: none;
+}
+
+.off {
+ padding: 10px;
+ background-color: #111;
+ -moz-user-select: none;
+ -webkit-user-select: none;
+ user-select: none;
+}
+
+.grid {
+ border-collapse:collapse;
+ /*border: 1px black;*/
+ padding: 0px;
+ margin: 0px;
+}
+
+#footer{
+ position:relative;
+ left:-100px;
+ text-align:right;
+ color:#555;
+ font:italic 10px Arial;
+ }
+
View
33 examples/lightout/public/lightout.html
@@ -0,0 +1,33 @@
+<html>
+ <!-- auto-generated html - you should consider editing and
+ adapting this to suit your requirements
+ -->
+ <head>
+ <meta name="pygwt:module" content="lightout">
+ <link rel='stylesheet' href='lightout.css'>
+ <title>Lights Out Game</title>
+ </head>
+ <body>
+ <script language="javascript" src="bootstrap.js"></script>
+ <h2>Lights Out</h2>
+ <div id="game"></div>
+ <p>Click on a cell to turn it off, but watch out the neighbors will follow. Your goal is to turn off all the cells.</p>
+ <div id="footer"><p><a href="lightout.zip">Get the Source Code</a><br>Created by <a href="http://www.math.uiuc.edu/~rkirov2/">Radoslav Kirov<a> <br>with <a href="http://pyjs.org">Pyjamas</a> (python port of Google's <a href="http://code.google.com/p/google-web-toolkit/">GWT</a>)<br> <a rel="license"
+href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/">
+<img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/80x15.png" />
+</a>
+</p>
+
+</div>
+<script type="text/javascript">
+var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
+document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
+</script>
+<script type="text/javascript">
+try {
+var pageTracker = _gat._getTracker("UA-360383-3");
+pageTracker._trackPageview();
+} catch(err) {}</script>
+
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.