lightout.py :  » Ajax » pyjamas » src » examples » lightout » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Ajax » pyjamas 
pyjamas » src » examples » lightout » lightout.py
# 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()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.