Back to index

plone3  3.1.7
snake.py
Go to the documentation of this file.
00001 # Copyright (c) 2005-2007
00002 # Authors: KSS Project Contributors (see docs/CREDITS.txt)
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License version 2 as published
00006 # by the Free Software Foundation.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00016 # 02111-1307, USA.
00017 
00018 from kss.core import KSSView, force_unicode, KSSExplicitError, kssaction
00019 from time import time
00020 from random import randint
00021 from persistent.list import PersistentList
00022 
00023 class KSSSnake(KSSView):
00024 
00025     MINTOP = 90
00026     MINLEFT = 90
00027     MAXTOP = 200
00028     MAXLEFT = 200
00029     SCORESTEP = 10
00030 
00031     FOODDIV = '<div class="food"></div>'
00032     LOSEDIV = '<div class="lose"><div class="gameover">GAME OVER</div>Click to play again.</div>'
00033 
00034     def initialize(self):
00035         self.removeFoodDiv()
00036         self.getCommandSet('core').deleteNode('div.lose')
00037         self.getCommandSet('core').removeClass('div.board', 'boardRed')
00038         self.request.SESSION['top'] = self.MINTOP
00039         self.request.SESSION['left'] = self.MINLEFT
00040         self.request.SESSION['dir'] = 'right'
00041         self.request.SESSION['score'] = 0
00042         # initialize snake
00043         self.getCommandSet('core').setStyle('div.snake', 'top', '%spx' % str(self.request.SESSION.get('top', self.MINTOP)))
00044         self.getCommandSet('core').setStyle('div.snake', 'left', '%spx' % str(self.request.SESSION.get('left', self.MINLEFT)))
00045         # initialize playground
00046         self.getCommandSet('core').setStyle('div.board', 'width', '%spx' % str(self.MAXLEFT))
00047         self.getCommandSet('core').setStyle('div.board', 'height', '%spx' % str(self.MAXTOP))
00048         # initialize food
00049         self.setFoodDiv()
00050         # set score
00051         self.getCommandSet('core').replaceInnerHTML('div.scoreField', '%s' % str(self.request.SESSION['score']))
00052         return self.render()
00053 
00054     def setFoodDiv(self):
00055         top_rand = randint(0, self.MAXTOP-11) 
00056         left_rand = randint(0, self.MAXLEFT-10) 
00057         top_offset = top_rand / 10 * 10
00058         left_offset = left_rand / 10 * 10
00059         self.getCommandSet('core').insertHTMLAsLastChild('div.board', self.FOODDIV)
00060         self.getCommandSet('core').setStyle('div.food', 'top', '%spx' % str(top_offset))
00061         self.getCommandSet('core').setStyle('div.food', 'left', '%spx' % str(left_offset))
00062         self.getCommandSet('core').setStyle('div.food', 'display', 'block')
00063         self.request.SESSION['food_top'] = top_offset
00064         self.request.SESSION['food_left'] = left_offset
00065 
00066     def removeFoodDiv(self):
00067         self.getCommandSet('core').deleteNode('div.food')
00068     
00069     def increaseScore(self):
00070         self.request.SESSION['score'] += self.SCORESTEP
00071         self.getCommandSet('core').replaceInnerHTML('div.scoreField', '%s' % str(self.request.SESSION['score']))
00072 
00073     def loseGame(self):
00074         self.removeFoodDiv()
00075         self.getCommandSet('core').addClass('div.board', 'boardRed')
00076         self.getCommandSet('core').insertHTMLAsLastChild('div.board', self.LOSEDIV)
00077         self.request.SESSION['dir'] = 'stop'
00078 
00079     def moveSnake(self):
00080         dir = self.request.SESSION.get('dir', 'stop')
00081         currentTop = self.request.SESSION.get('top', self.MINTOP)
00082         currentLeft = self.request.SESSION.get('left', self.MINLEFT)
00083         if dir == 'stop':
00084             return self.render()
00085         if dir == 'right':
00086             newLeft = currentLeft + 10
00087             if newLeft >= self.MAXLEFT:
00088                 self.loseGame()
00089                 return self.render()
00090             self.getCommandSet('core').setStyle('div.snake', 'left', '%spx' % str(newLeft))
00091             self.request.SESSION['left'] = newLeft
00092         if dir == 'left':
00093             newLeft = currentLeft - 10
00094             if newLeft < 0:
00095                 self.loseGame()
00096                 return self.render()
00097             self.getCommandSet('core').setStyle('div.snake', 'left', '%spx' % str(newLeft))
00098             self.request.SESSION['left'] = newLeft
00099         if dir == 'up':
00100             newTop = currentTop - 10
00101             if newTop < 0:
00102                 self.loseGame()
00103                 return self.render()
00104             self.getCommandSet('core').setStyle('div.snake', 'top', '%spx' % str(newTop))
00105             self.request.SESSION['top'] = newTop
00106         if dir == 'down':
00107             newTop = currentTop + 10
00108             if newTop >= self.MAXTOP:
00109                 self.loseGame()
00110                 return self.render()
00111             self.getCommandSet('core').setStyle('div.snake', 'top', '%spx' % str(newTop))
00112             self.request.SESSION['top'] = newTop
00113         # check collision
00114         self.checkCollision()
00115         return self.render()
00116 
00117     def checkCollision(self):
00118         if self.request.SESSION.get('top', 0) == self.request.SESSION.get('food_top', 0)+10 and \
00119            self.request.SESSION.get('left', 0) == self.request.SESSION.get('food_left', 1):
00120             self.removeFoodDiv()
00121             self.increaseScore()
00122             self.setFoodDiv()
00123 
00124     def changeDirection(self, keycode):
00125         keycode = int(keycode)
00126         if keycode not in [ord('w'), ord('a'), ord('d'), ord('s')] or self.request.SESSION.get('dir', '') == 'stop':
00127             return self.render()
00128         if keycode==ord('w'):
00129             self.request.SESSION['dir'] = 'up'
00130         if keycode==ord('a'):
00131             self.request.SESSION['dir'] = 'left'
00132         if keycode==ord('s'):
00133             self.request.SESSION['dir'] = 'down'
00134         if keycode==ord('d'):
00135             self.request.SESSION['dir'] = 'right'
00136         return self.render()
00137