Back to index

plone3  3.1.7
Public Member Functions | Static Public Attributes
kss.core.plugins.core.demo.snake.KSSSnake Class Reference
Inheritance diagram for kss.core.plugins.core.demo.snake.KSSSnake:
Inheritance graph
[legend]
Collaboration diagram for kss.core.plugins.core.demo.snake.KSSSnake:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def initialize
def setFoodDiv
def removeFoodDiv
def increaseScore
def loseGame
def moveSnake
def checkCollision
def changeDirection

Static Public Attributes

int MINTOP = 90
int MINLEFT = 90
int MAXTOP = 200
int MAXLEFT = 200
int SCORESTEP = 10
string FOODDIV = '<div class="food"></div>'
string LOSEDIV = '<div class="lose"><div class="gameover">GAME OVER</div>Click to play again.</div>'

Detailed Description

Definition at line 23 of file snake.py.


Member Function Documentation

Definition at line 124 of file snake.py.

00124 
00125     def changeDirection(self, keycode):
00126         keycode = int(keycode)
00127         if keycode not in [ord('w'), ord('a'), ord('d'), ord('s')] or self.request.SESSION.get('dir', '') == 'stop':
00128             return self.render()
00129         if keycode==ord('w'):
00130             self.request.SESSION['dir'] = 'up'
00131         if keycode==ord('a'):
00132             self.request.SESSION['dir'] = 'left'
00133         if keycode==ord('s'):
00134             self.request.SESSION['dir'] = 'down'
00135         if keycode==ord('d'):
00136             self.request.SESSION['dir'] = 'right'
00137         return self.render()
00138 

Definition at line 117 of file snake.py.

00117 
00118     def checkCollision(self):
00119         if self.request.SESSION.get('top', 0) == self.request.SESSION.get('food_top', 0)+10 and \
00120            self.request.SESSION.get('left', 0) == self.request.SESSION.get('food_left', 1):
00121             self.removeFoodDiv()
00122             self.increaseScore()
00123             self.setFoodDiv()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 69 of file snake.py.

00069 
00070     def increaseScore(self):
00071         self.request.SESSION['score'] += self.SCORESTEP
00072         self.getCommandSet('core').replaceInnerHTML('div.scoreField', '%s' % str(self.request.SESSION['score']))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 34 of file snake.py.

00034 
00035     def initialize(self):
00036         self.removeFoodDiv()
00037         self.getCommandSet('core').deleteNode('div.lose')
00038         self.getCommandSet('core').removeClass('div.board', 'boardRed')
00039         self.request.SESSION['top'] = self.MINTOP
00040         self.request.SESSION['left'] = self.MINLEFT
00041         self.request.SESSION['dir'] = 'right'
00042         self.request.SESSION['score'] = 0
00043         # initialize snake
00044         self.getCommandSet('core').setStyle('div.snake', 'top', '%spx' % str(self.request.SESSION.get('top', self.MINTOP)))
00045         self.getCommandSet('core').setStyle('div.snake', 'left', '%spx' % str(self.request.SESSION.get('left', self.MINLEFT)))
00046         # initialize playground
00047         self.getCommandSet('core').setStyle('div.board', 'width', '%spx' % str(self.MAXLEFT))
00048         self.getCommandSet('core').setStyle('div.board', 'height', '%spx' % str(self.MAXTOP))
00049         # initialize food
00050         self.setFoodDiv()
00051         # set score
00052         self.getCommandSet('core').replaceInnerHTML('div.scoreField', '%s' % str(self.request.SESSION['score']))
00053         return self.render()

Definition at line 73 of file snake.py.

00073 
00074     def loseGame(self):
00075         self.removeFoodDiv()
00076         self.getCommandSet('core').addClass('div.board', 'boardRed')
00077         self.getCommandSet('core').insertHTMLAsLastChild('div.board', self.LOSEDIV)
00078         self.request.SESSION['dir'] = 'stop'

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 79 of file snake.py.

00079 
00080     def moveSnake(self):
00081         dir = self.request.SESSION.get('dir', 'stop')
00082         currentTop = self.request.SESSION.get('top', self.MINTOP)
00083         currentLeft = self.request.SESSION.get('left', self.MINLEFT)
00084         if dir == 'stop':
00085             return self.render()
00086         if dir == 'right':
00087             newLeft = currentLeft + 10
00088             if newLeft >= self.MAXLEFT:
00089                 self.loseGame()
00090                 return self.render()
00091             self.getCommandSet('core').setStyle('div.snake', 'left', '%spx' % str(newLeft))
00092             self.request.SESSION['left'] = newLeft
00093         if dir == 'left':
00094             newLeft = currentLeft - 10
00095             if newLeft < 0:
00096                 self.loseGame()
00097                 return self.render()
00098             self.getCommandSet('core').setStyle('div.snake', 'left', '%spx' % str(newLeft))
00099             self.request.SESSION['left'] = newLeft
00100         if dir == 'up':
00101             newTop = currentTop - 10
00102             if newTop < 0:
00103                 self.loseGame()
00104                 return self.render()
00105             self.getCommandSet('core').setStyle('div.snake', 'top', '%spx' % str(newTop))
00106             self.request.SESSION['top'] = newTop
00107         if dir == 'down':
00108             newTop = currentTop + 10
00109             if newTop >= self.MAXTOP:
00110                 self.loseGame()
00111                 return self.render()
00112             self.getCommandSet('core').setStyle('div.snake', 'top', '%spx' % str(newTop))
00113             self.request.SESSION['top'] = newTop
00114         # check collision
00115         self.checkCollision()
00116         return self.render()

Definition at line 66 of file snake.py.

00066 
00067     def removeFoodDiv(self):
00068         self.getCommandSet('core').deleteNode('div.food')
    

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 54 of file snake.py.

00054 
00055     def setFoodDiv(self):
00056         top_rand = randint(0, self.MAXTOP-11) 
00057         left_rand = randint(0, self.MAXLEFT-10) 
00058         top_offset = top_rand / 10 * 10
00059         left_offset = left_rand / 10 * 10
00060         self.getCommandSet('core').insertHTMLAsLastChild('div.board', self.FOODDIV)
00061         self.getCommandSet('core').setStyle('div.food', 'top', '%spx' % str(top_offset))
00062         self.getCommandSet('core').setStyle('div.food', 'left', '%spx' % str(left_offset))
00063         self.getCommandSet('core').setStyle('div.food', 'display', 'block')
00064         self.request.SESSION['food_top'] = top_offset
00065         self.request.SESSION['food_left'] = left_offset

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

string kss.core.plugins.core.demo.snake.KSSSnake.FOODDIV = '<div class="food"></div>' [static]

Definition at line 31 of file snake.py.

string kss.core.plugins.core.demo.snake.KSSSnake.LOSEDIV = '<div class="lose"><div class="gameover">GAME OVER</div>Click to play again.</div>' [static]

Definition at line 32 of file snake.py.

Definition at line 28 of file snake.py.

Definition at line 27 of file snake.py.

Definition at line 26 of file snake.py.

Definition at line 25 of file snake.py.

Definition at line 29 of file snake.py.


The documentation for this class was generated from the following file: