diff options
author | Alan Aguiar <alanjas@hotmail.com> | 2013-04-09 20:37:32 (GMT) |
---|---|---|
committer | Alan Aguiar <alanjas@hotmail.com> | 2013-04-09 20:37:32 (GMT) |
commit | c664c16ead11d17ece6b6db5b5acfe519b991cb1 (patch) | |
tree | aae411b8b2cf468e5b1ac128547dba0f2c7566ad | |
parent | 947b3c50941192cc7d04e1d0a453d72d3fe87d2b (diff) |
changes for sugargame and headers
-rwxr-xr-x | activity.py | 82 | ||||
-rwxr-xr-x | cells.py | 307 | ||||
-rwxr-xr-x | colors.py | 33 | ||||
-rwxr-xr-x | pieces.py | 251 | ||||
-rwxr-xr-x | sprites.py | 285 |
5 files changed, 514 insertions, 444 deletions
diff --git a/activity.py b/activity.py index 709f063..bb5ded1 100755 --- a/activity.py +++ b/activity.py @@ -1,27 +1,69 @@ -# This file is part of Cell Management. +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# +# This file is part of Cell Management. +# +# Cell Management is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Cell Management is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Cell Management. If not, see <http://www.gnu.org/licenses/>. -# Cell Management is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. +import gtk +from sugar.activity import activity +from sugar.graphics.toolbarbox import ToolbarBox +from sugar.graphics.toolbutton import ToolButton +from sugar.activity.widgets import ActivityToolbarButton +from sugar.activity.widgets import StopButton +from sugar.graphics.toolbarbox import ToolbarButton +import sugargame.canvas -# Cell Management is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +import cells -# You should have received a copy of the GNU General Public License -# along with Cell Management. If not, see <http://www.gnu.org/licenses/>. +class Activity(activity.Activity): + def __init__(self, handle): + activity.Activity.__init__(self, handle) -import olpcgames -from olpcgames import activity -from gettext import gettext as _ + self.max_participants = 1 + self.actividad = cells.Game() + self.build_toolbar() + self._pygamecanvas = sugargame.canvas.PygameCanvas(self) + self.set_canvas(self._pygamecanvas) + self._pygamecanvas.grab_focus() + self._pygamecanvas.run_pygame(self.actividad.mainloop) -class Activity(activity.PyGameActivity): - - game_name = 'cells' - game_title = _('Cells') - game_size = None + def build_toolbar(self): + + toolbox = ToolbarBox() + activity_button = ActivityToolbarButton(self) + toolbox.toolbar.insert(activity_button, -1) + activity_button.show() + + separator = gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_expand(True) + toolbox.toolbar.insert(separator, -1) + + stop_button = StopButton(self) + stop_button.props.accelerator = '<Ctrl>q' + toolbox.toolbar.insert(stop_button, -1) + stop_button.show() + + self.set_toolbar_box(toolbox) + + toolbox.show_all() + + def read_file(self, file_path): + pass + + def write_file(self, file_path): + pass - @@ -1,58 +1,66 @@ #! /usr/bin/env python -
-# 'Cells' writen by Nolan Baker - September 18, 2008
-# based on the logic puzzle 'Cell Management' by Dr. Mark Goadrich
- -# Cells is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# Cells is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with Cells. If not, see <http://www.gnu.org/licenses/>.
- -import math, pygame, olpcgames
+# -*- coding: utf-8 -*- +# +# 'Cells' writen by Nolan Baker - September 18, 2008 +# based on the logic puzzle 'Cell Management' by Dr. Mark Goadrich +# +# Cells is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Cells is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Cells. If not, see <http://www.gnu.org/licenses/>. + +import gtk +import math +import pygame from pygame.locals import * from pieces import * -from sprites import *
+from sprites import * from colors import * -from random import *
-
-################################################################################
-# Game
-################################################################################
-
-class Game():
- def __init__(self, fps = 30):
- pygame.init() +from random import * - self.screen = pygame.display.set_mode((1200,900)) +################################################################################ +# Game +################################################################################ - # time stuff
- self.clock = pygame.time.Clock()
+class Game(): + + def __init__(self, fps = 30): self.fps = fps - + + def load_all(self): + pygame.init() + + self.screen = pygame.display.get_surface() + if not(self.screen): + self.screen = pygame.display.set_mode((800, 800)) + + # time stuff + self.clock = pygame.time.Clock() + # how many cells - self.cell_count = 2
+ self.cell_count = 2 # let's keep score self.move_count = 0 - def dist(self, x1, y1, x2, y2):
- return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
-
- def polToCart(self, r, angle):
- # here the center is (512, 384)
- angle *= math.pi / 180.0
- x = int(r * math.cos(angle)) + 600
- y = int(r * math.sin(angle)) + 450
- return x, y
-
+ def dist(self, x1, y1, x2, y2): + return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) + + def polToCart(self, r, angle): + # here the center is (512, 384) + angle *= math.pi / 180.0 + x = int(r * math.cos(angle)) + 600 + y = int(r * math.sin(angle)) + 450 + return x, y + def drawBoard(self): # this is the giant colorful thing you see when you start the game pos = (600, 450) @@ -64,47 +72,47 @@ class Game(): pygame.draw.circle(self.background, yellow, pos, 85) def setupBoard(self): - # make board
+ # make board self.background = pygame.Surface(self.screen.get_size()).convert() - r, g, b = randint(50, 255), 0, randint(50, 150)
+ r, g, b = randint(50, 255), 0, randint(50, 150) self.background.fill((r,g,b)) self.drawBoard() -
- # make pieces
- self.escArea = EscapeArea(self)
+ + # make pieces + self.escArea = EscapeArea(self) - # a list that corresponds to a hiding space's allignment - # weather a hiding space is hostile or friendly
+ # a list that corresponds to a hiding space's allignment + # weather a hiding space is hostile or friendly hf = ["h", "f"] * (self.cell_count / 2) # <- this gives us an int if self.cell_count % 2 == 1: - hf += ["f"]
- shuffle(hf)
+ hf += ["f"] + shuffle(hf) - # a list that keeps track of cells
- self.cells = []
+ # a list that keeps track of cells + self.cells = [] - # put the cells in the list
- for i in range(0, self.cell_count):
- x = Cell(self, i)
- self.cells.append(x)
+ # put the cells in the list + for i in range(0, self.cell_count): + x = Cell(self, i) + self.cells.append(x) - # create a list of numbers [1 : the number of cells) - # this is closed on the left
- nums = range(1, self.cell_count)
- shuffle(nums)
-
- n = 0
- while len(nums) != 0:
- x = nums.pop(0)
- y = Hideout(self, x, self.cells[x], hf.pop())
- self.cells[n].setAdjHS(y)
- self.cells[x].setMyHS(y)
- n = x
-
- y = Hideout(self, 0, self.cells[0], hf.pop())
- self.cells[n].setAdjHS(y)
- self.cells[0].setMyHS(y)
-
+ # create a list of numbers [1 : the number of cells) + # this is closed on the left + nums = range(1, self.cell_count) + shuffle(nums) + + n = 0 + while len(nums) != 0: + x = nums.pop(0) + y = Hideout(self, x, self.cells[x], hf.pop()) + self.cells[n].setAdjHS(y) + self.cells[x].setMyHS(y) + n = x + + y = Hideout(self, 0, self.cells[0], hf.pop()) + self.cells[n].setAdjHS(y) + self.cells[0].setMyHS(y) + # now shuffle the cells (and effectively the hiding spaces) # this game is not solvable if the number of cells mod 4 # is 0 or 1 and the corresponding hiding space @@ -115,10 +123,10 @@ class Game(): if self.cell_count % 4 == (2 or 3): solvable == True break - count = 0
+ count = 0 shuffle(self.cells) for i in range(0, self.cell_count): - a = self.cells[i].species
+ a = self.cells[i].species b = self.cells[(i + 1) % self.cell_count].adj_hs.species if a == b: count += 1 @@ -127,23 +135,23 @@ class Game(): break if count != self.cell_count: solvable = True -
- # and let the cells and hiding spaces know where they've been put
- for j in range(0, self.cell_count):
- a = self.cells[j]
- a.seti(j)
- a.getAdjHS().seti(j)
- angle = (((360.0 / self.cell_count) * j) - 90) % 360
- x1, y1 = self.polToCart(245, angle)
- x2, y2 = self.polToCart(140, angle)
- a.setPos(x1, y1)
- a.getAdjHS().setPos(x2, y2)
-
- # lastly... sprites
- self.guard = Guard(self)
- self.guards = Group((self.guard))
- for i in self.cells:
- i.makePrisoners()
+ + # and let the cells and hiding spaces know where they've been put + for j in range(0, self.cell_count): + a = self.cells[j] + a.seti(j) + a.getAdjHS().seti(j) + angle = (((360.0 / self.cell_count) * j) - 90) % 360 + x1, y1 = self.polToCart(245, angle) + x2, y2 = self.polToCart(140, angle) + a.setPos(x1, y1) + a.getAdjHS().setPos(x2, y2) + + # lastly... sprites + self.guard = Guard(self) + self.guards = Group((self.guard)) + for i in self.cells: + i.makePrisoners() def resetGame(self): already_reset = True @@ -158,7 +166,7 @@ class Game(): def gameloop(self): self.setupBoard() playing = True - while playing:
+ while playing: self.clock.tick(self.fps) text1 = Text(str(self.move_count), @@ -168,18 +176,21 @@ class Game(): text2 = Text("(h)elp", size = 50) text2.rect.topleft = (10, 10) self.text = Group((text1, text2)) -
+ if (len(self.escArea.prisoners.sprites()) == self.cell_count and self.guard.moving == False): pygame.time.wait(3000) playing = False -
- # Handle Input Events
- for event in pygame.event.get():
- # this one is for the box in the top right marked X
- if event.type == QUIT:
+ + while gtk.events_pending(): + gtk.main_iteration() + + # Handle Input Events + for event in pygame.event.get(): + # this one is for the box in the top right marked X + if event.type == QUIT: playing, self.running = False, False - # and this one is for the "ESC" key
+ # and this one is for the "ESC" key if event.type == KEYDOWN: if event.key == K_ESCAPE: self.move_count = 0 @@ -190,39 +201,39 @@ class Game(): self.resetGame() elif event.key == K_h: self.help() -
- # update sprites
- self.guards.update()
- self.escArea.prisoners.update()
- for i in range(0,self.cell_count):
- self.cells[i].prisoners.update()
- self.cells[i].getAdjHS().prisoners.update()
- self.cells[i].text.update()
-
- # draw everything
- self.screen.blit(self.background, (0, 0))
- self.escArea.prisoners.draw(self.screen)
- for i in range(0,self.cell_count):
- a = self.cells[i]
- a.text.draw(self.screen)
- a.getAdjHS().text.draw(self.screen)
- a.prisoners.draw(self.screen)
- a.getAdjHS().prisoners.draw(self.screen)
+ + # update sprites + self.guards.update() + self.escArea.prisoners.update() + for i in range(0,self.cell_count): + self.cells[i].prisoners.update() + self.cells[i].getAdjHS().prisoners.update() + self.cells[i].text.update() + + # draw everything + self.screen.blit(self.background, (0, 0)) + self.escArea.prisoners.draw(self.screen) + for i in range(0,self.cell_count): + a = self.cells[i] + a.text.draw(self.screen) + a.getAdjHS().text.draw(self.screen) + a.prisoners.draw(self.screen) + a.getAdjHS().prisoners.draw(self.screen) self.guards.draw(self.screen) - self.text.draw(self.screen)
+ self.text.draw(self.screen) # finally, refresh the screen - pygame.display.flip()
+ pygame.display.flip() def makeMenu(self): self.new_game = False - self.background = pygame.Surface(self.screen.get_size()).convert()
+ self.background = pygame.Surface(self.screen.get_size()).convert() self.background.fill(yellow) cell_text = Text("Cells", size = int(160)) - cell_text.rect.center = ((600,450))
+ cell_text.rect.center = ((600,450)) self.text = Group((cell_text)) - prompt_text = Text("press any key to begin", size = int(35))
+ prompt_text = Text("press any key to begin", size = int(35)) prompt_text.rect.center = (600,530) self.flashing_text = Group((prompt_text)) @@ -251,17 +262,21 @@ class Game(): self.screen.fill(white) text.draw(self.screen) - for event in pygame.event.get():
- # this one is for the box in the top right marked X
- if event.type == QUIT:
+ while gtk.events_pending(): + gtk.main_iteration() + + for event in pygame.event.get(): + # this one is for the box in the top right marked X + if event.type == QUIT: self.running = False - # and this one is for the "ESC" key
- if event.type == KEYDOWN and event.key == K_ESCAPE:
- helping = False
+ # and this one is for the "ESC" key + if event.type == KEYDOWN and event.key == K_ESCAPE: + helping = False pygame.display.flip() def mainloop(self): + self.load_all() self.makeMenu() self.running = True count = 0 @@ -270,14 +285,17 @@ class Game(): self.screen.blit(self.background, (0, 0)) - for event in pygame.event.get():
- # this one is for the box in the top right marked X
- if event.type == QUIT:
+ while gtk.events_pending(): + gtk.main_iteration() + + for event in pygame.event.get(): + # this one is for the box in the top right marked X + if event.type == QUIT: self.running = False - # and this one is for the "ESC" key
+ # and this one is for the "ESC" key if event.type == KEYDOWN: - if event.key == K_ESCAPE:
- self.running = False
+ if event.key == K_ESCAPE: + self.running = False elif self.cell_count == 2 and not self.new_game: self.new_game = True @@ -292,7 +310,7 @@ class Game(): text1 = Text("Congratulations", color = white, size = 120) text2 = Text("You finished in " + str(self.move_count) + " moves.", color = white, size = 60) - text2.rect.top = text1.rect.bottom + 10
+ text2.rect.top = text1.rect.bottom + 10 self.text = Group((text1, text2)) self.text.draw(self.background) @@ -302,13 +320,14 @@ class Game(): count += 1 if (count / (self.fps / 2)) % 2 == 1: self.flashing_text.draw(self.screen) -
+ pygame.display.flip() -
+ def main(): cells = Game() cells.mainloop() -
-if __name__ == "__main__":
- main()
-
pygame.quit ()
+ +if __name__ == "__main__": + main() + +pygame.quit () @@ -1,23 +1,26 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# # colors.py is part of Cells. Cells is free software: you can # redistribute it and/or modify it under the terms of the GNU # General Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version.
-
-# Cells is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
+# either version 3 of the License, or (at your option) any later version. +# +# Cells is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License # along with Cells. If not, see <http://www.gnu.org/licenses/>. -################################################################################
-# Colors
-################################################################################
-red = (255,0,0)
-green = (0,255,0)
+################################################################################ +# Colors +################################################################################ +red = (255,0,0) +green = (0,255,0) blue = (0,0,255) -yellow = (255,255,0)
+yellow = (255,255,0) black = (0,0,0) -grey = (150,150,150)
+grey = (150,150,150) white = (255,255,255) @@ -1,152 +1,155 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# # pieces.py is part of Cells. Cells is free software: you can # redistribute it and/or modify it under the terms of the GNU # General Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version.
-
-# Cells is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
+# either version 3 of the License, or (at your option) any later version. +# +# Cells is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License # along with Cells. If not, see <http://www.gnu.org/licenses/>. -import math, pygame
+import math, pygame from pygame.locals import * -from sprites import *
+from sprites import * from colors import * from random import * -################################################################################
-# The Cells
-################################################################################
-
-class Cell():
- def __init__(self, game, species):
- self.game = game
- self.species = species
+################################################################################ +# The Cells +################################################################################ + +class Cell(): + def __init__(self, game, species): + self.game = game + self.species = species def isSelected(self): - m = pygame.mouse.get_pos()
+ m = pygame.mouse.get_pos() d = self.game.dist(m[0], m[1], self.x, self.y) if d < 54: return True -
- def seti(self, i):
- self.i = i
-
- def setPos(self, x, y):
- self.x = x
+ + def seti(self, i): + self.i = i + + def setPos(self, x, y): + self.x = x self.y = y pygame.draw.circle(self.game.background, black, (x, y), 54) pygame.draw.circle(self.game.background, white, (x, y), 50) -
- text = Text(str(self.species + 1), size = 20)
- text.rect.center = (self.x, self.y - 20)
- self.text = Group((text))
-
- def makePrisoners(self):
- self.prisoners = Group()
- self.prisoners.set_pos(self.x, self.y)
- self.prisoner1 = Prisoner(self.game, self.species, self.x, self.y, 'left')
- self.prisoner2 = Prisoner(self.game, self.species, self.x, self.y, 'right')
- self.prisoners.add((self.prisoner1, self.prisoner2))
+ + text = Text(str(self.species + 1), size = 20) + text.rect.center = (self.x, self.y - 20) + self.text = Group((text)) + + def makePrisoners(self): + self.prisoners = Group() + self.prisoners.set_pos(self.x, self.y) + self.prisoner1 = Prisoner(self.game, self.species, self.x, self.y, 'left') + self.prisoner2 = Prisoner(self.game, self.species, self.x, self.y, 'right') + self.prisoners.add((self.prisoner1, self.prisoner2)) def reset(self): self.prisoner1.kill() self.prisoner2.kill() self.prisoners.add((self.prisoner1, self.prisoner2)) -
- def setMyHS(self, hs):
- self.my_hs = hs
-
- def getMyHS(self):
- return self.my_hs
-
- def setAdjHS(self, hs):
- self.adj_hs = hs
-
- def getAdjHS(self):
- return self.adj_hs
-
- def addPrisoner(self, prisoner):
- self.prisoners.add(prisoner)
-
- def update(self):
- if (len(self.prisoners) != 0 and
- self.getAdjHS().canHelp()):
- prisoner = self.prisoners.sprites()[0]
- self.prisoners.remove(prisoner)
- self.getMyHS().addPrisoner(prisoner)
- -################################################################################
-# The Escape Area
-################################################################################
-
-class EscapeArea():
- def __init__(self, game):
- self.game = game
- self.prisoners = Group()
- self.prisoners.set_pos(600,450)
-
- def addPrisoner(self, prisoner):
+ + def setMyHS(self, hs): + self.my_hs = hs + + def getMyHS(self): + return self.my_hs + + def setAdjHS(self, hs): + self.adj_hs = hs + + def getAdjHS(self): + return self.adj_hs + + def addPrisoner(self, prisoner): self.prisoners.add(prisoner) -
-################################################################################
-# The Hideout
-################################################################################
-
-class Hideout():
- def __init__(self, game, species, cell, HorF):
- self.game = game
- self.species = species
- self.my_cell = cell
- self.HorF = HorF
- self.prisoners = Group()
- self.escArea = game.escArea
-
- def seti(self, int):
- self.i = int
-
- def setPos(self, x, y):
- self.x = x
- self.y = y
+ + def update(self): + if (len(self.prisoners) != 0 and + self.getAdjHS().canHelp()): + prisoner = self.prisoners.sprites()[0] + self.prisoners.remove(prisoner) + self.getMyHS().addPrisoner(prisoner) + +################################################################################ +# The Escape Area +################################################################################ + +class EscapeArea(): + def __init__(self, game): + self.game = game + self.prisoners = Group() + self.prisoners.set_pos(600,450) + + def addPrisoner(self, prisoner): + self.prisoners.add(prisoner) + +################################################################################ +# The Hideout +################################################################################ + +class Hideout(): + def __init__(self, game, species, cell, HorF): + self.game = game + self.species = species + self.my_cell = cell + self.HorF = HorF + self.prisoners = Group() + self.escArea = game.escArea + + def seti(self, int): + self.i = int + + def setPos(self, x, y): + self.x = x + self.y = y self.prisoners.set_pos(self.x, self.y) - # since we're at it, let's use our crayon to color on the board
- # this is so we can tell if a space is 'hostile' or 'friendly'
- color = red
- if self.HorF == "f":
+ # since we're at it, let's use our crayon to color on the board + # this is so we can tell if a space is 'hostile' or 'friendly' + color = red + if self.HorF == "f": color = green - pygame.draw.circle(self.game.background, black, (x,y), 54)
- pygame.draw.circle(self.game.background, color, (x,y), 50)
-
- text = Text(str(self.species + 1), size = 20, color = white)
- text.rect.center = (self.x, self.y - 20)
- self.text = Group((text))
-
- def isOccupied(self):
- if len(self.prisoners.sprites()) == 0:
- return False
- return True
-
- def vacate(self):
- if self.isOccupied():
- x = self.prisoners.sprites()[0]
- self.prisoners.remove(x)
- self.my_cell.prisoners.add(x)
-
- def canHelp(self):
- return ((self.isOccupied() and self.HorF == "f") or
- (not self.isOccupied() and self.HorF == "h"))
-
- def escape(self, prisoner):
- self.escArea.addPrisoner(prisoner)
-
- def addPrisoner(self, prisoner):
- if not self.isOccupied():
- self.prisoners.add((prisoner))
- else:
- self.escape(prisoner)
+ pygame.draw.circle(self.game.background, black, (x,y), 54) + pygame.draw.circle(self.game.background, color, (x,y), 50) + + text = Text(str(self.species + 1), size = 20, color = white) + text.rect.center = (self.x, self.y - 20) + self.text = Group((text)) + + def isOccupied(self): + if len(self.prisoners.sprites()) == 0: + return False + return True + + def vacate(self): + if self.isOccupied(): + x = self.prisoners.sprites()[0] + self.prisoners.remove(x) + self.my_cell.prisoners.add(x) + + def canHelp(self): + return ((self.isOccupied() and self.HorF == "f") or + (not self.isOccupied() and self.HorF == "h")) + + def escape(self, prisoner): + self.escArea.addPrisoner(prisoner) + + def addPrisoner(self, prisoner): + if not self.isOccupied(): + self.prisoners.add((prisoner)) + else: + self.escape(prisoner) @@ -1,28 +1,31 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# # sprites.py is part of Cells. Cells is free software: you can # redistribute it and/or modify it under the terms of the GNU # General Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version.
-
-# Cells is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
+# either version 3 of the License, or (at your option) any later version. +# +# Cells is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License # along with Cells. If not, see <http://www.gnu.org/licenses/>. -import math, pygame
+import math, pygame from pygame.locals import * -from colors import *
+from colors import * from random import * -################################################################################
-# the Super Group... because regular groups aren't good enough
-################################################################################
-
+################################################################################ +# the Super Group... because regular groups aren't good enough +################################################################################ + class Group(pygame.sprite.Group): - # We'll call it a quick fix; I meant to add more.
- def set_pos(self, x, y): self.x, self.y = x, y
+ # We'll call it a quick fix; I meant to add more. + def set_pos(self, x, y): self.x, self.y = x, y ################################################################################ # Text Sprite @@ -37,46 +40,46 @@ class Text(pygame.sprite.Sprite): self.image = self.original.copy() self.rect = self.image.get_rect() -################################################################################
-# Moving Sprites are the way to go
-################################################################################
-
-class MovingSprite(pygame.sprite.Sprite):
- def __init__(self, game):
- pygame.sprite.Sprite.__init__(self)
+################################################################################ +# Moving Sprites are the way to go +################################################################################ + +class MovingSprite(pygame.sprite.Sprite): + def __init__(self, game): + pygame.sprite.Sprite.__init__(self) self.game = game -
- def angle(self, x, y):
- distance = self.game.dist(self.rect.centerx, self.rect.centery, x, y)
- angle = math.acos(float(x - self.rect.centerx) / float(distance))
- angle *= 180.0 / math.pi # because pygame likes degrees
- if y > self.rect.centery:
- angle = 360 - angle
- return angle
-
- def move(self, angle):
- dx = math.cos(angle * math.pi / 180) * self.speed
- dy = -math.sin(angle * math.pi / 180) * self.speed
- self.rect.move_ip(dx, dy)
- - -################################################################################
-# The Prisoner
-################################################################################
-
-class Prisoner(MovingSprite):
- def __init__(self, game, species, x, y, LorR):
- MovingSprite.__init__(self, game)
- self.species = species
+ + def angle(self, x, y): + distance = self.game.dist(self.rect.centerx, self.rect.centery, x, y) + angle = math.acos(float(x - self.rect.centerx) / float(distance)) + angle *= 180.0 / math.pi # because pygame likes degrees + if y > self.rect.centery: + angle = 360 - angle + return angle + + def move(self, angle): + dx = math.cos(angle * math.pi / 180) * self.speed + dy = -math.sin(angle * math.pi / 180) * self.speed + self.rect.move_ip(dx, dy) + + +################################################################################ +# The Prisoner +################################################################################ + +class Prisoner(MovingSprite): + def __init__(self, game, species, x, y, LorR): + MovingSprite.__init__(self, game) + self.species = species self.speed = randint(16, 22) - self.LorR = LorR
+ self.LorR = LorR - # this is the image you see most of the time + # this is the image you see most of the time self.original = pygame.Surface((40,40)).convert() self.original.fill(yellow) pygame.draw.circle(self.original, black, (20,20), 20) - self.original.set_colorkey(yellow)
- self.rect = self.original.get_rect()
+ self.original.set_colorkey(yellow) + self.rect = self.original.get_rect() # this is the one you see when the mouse is hovering # over it and it's in the escape area @@ -91,92 +94,92 @@ class Prisoner(MovingSprite): text.rect.center = self.rect.center self.text = Group((text)) self.text.draw(self.image) -
- if self.LorR == 'left':
- self.rect.center = (x - 21, y)
- else:
- self.rect.center = (x + 21, y)
-
- def update(self):
- m = pygame.mouse.get_pos()
- d = self.game.dist(m[0], m[1], self.rect.centerx, self.rect.centery)
- button1, button2, button3 = pygame.mouse.get_pressed()
- a = self.game.escArea.prisoners
- if (d <= 20 and
+ + if self.LorR == 'left': + self.rect.center = (x - 21, y) + else: + self.rect.center = (x + 21, y) + + def update(self): + m = pygame.mouse.get_pos() + d = self.game.dist(m[0], m[1], self.rect.centerx, self.rect.centery) + button1, button2, button3 = pygame.mouse.get_pressed() + a = self.game.escArea.prisoners + if (d <= 20 and self in a.sprites() and not self.game.guard.moving): self.image = self.selected_img.copy() self.text.draw(self.image) if (button1 or button2 or button3): - self.game.move_count += 1
- a.remove(self)
- for i in range(0, self.game.cell_count):
- if self.species == self.game.cells[i].getMyHS().species:
- self.game.cells[i].getMyHS().addPrisoner(self)
+ self.game.move_count += 1 + a.remove(self) + for i in range(0, self.game.cell_count): + if self.species == self.game.cells[i].getMyHS().species: + self.game.cells[i].getMyHS().addPrisoner(self) else: self.image = self.original.copy() self.text.draw(self.image) -
- x = self.groups()[0].x
- y = self.groups()[0].y
-
- if self.LorR == 'left': x -= 21
- else: x += 21
-
- d = self.game.dist(x, y, 600, 450)
+ + x = self.groups()[0].x + y = self.groups()[0].y + + if self.LorR == 'left': x -= 21 + else: x += 21 + + d = self.game.dist(x, y, 600, 450) if d < 85: - angle = (((360.0 / self.game.cell_count) * self.species) - 90) % 360
- x, y = self.game.polToCart(int(60), angle)
-
- d = self.game.dist(x, y, self.rect.centerx, self.rect.centery)
- if d > self.speed: self.move(self.angle(x, y))
- else: self.rect.center = (x, y)
+ angle = (((360.0 / self.game.cell_count) * self.species) - 90) % 360 + x, y = self.game.polToCart(int(60), angle) + + d = self.game.dist(x, y, self.rect.centerx, self.rect.centery) + if d > self.speed: self.move(self.angle(x, y)) + else: self.rect.center = (x, y) -################################################################################
-# The Guard
-################################################################################
-
-class Guard(MovingSprite):
- def __init__(self, game):
- MovingSprite.__init__(self, game)
-
+################################################################################ +# The Guard +################################################################################ + +class Guard(MovingSprite): + def __init__(self, game): + MovingSprite.__init__(self, game) + self.speed = 15 - self.image = pygame.Surface((40,40))
+ self.image = pygame.Surface((40,40)) self.image.fill(white) pygame.draw.circle(self.image, black, (20,20), 20) pygame.draw.circle(self.image, red, (20,20), 17) - self.image.set_colorkey(white)
- self.rect = self.image.get_rect()
- self.moving = False
- self.rect.center = (-100, -100)
- self.destx, self.desty = 50, 50
- self.i ,self.stop = 0, 0
-
- def setDest(self, dest):
- self.destx, self.desty = dest[0], dest[1]
-
- def circ(self, x, y):
- distance = self.game.dist(600, 450, x, y)
- angle = math.acos(float(x - 600) / float(distance))
- angle *= 180.0 / math.pi # because pygame likes degrees
- if y > 450:
- angle = 360 - angle
- angle = (480 - angle) % 360
- i = int(angle / (360.0 / self.game.cell_count))
- return i
-
- def path(self, i):
- d = int(350)
- angle = []
- for j in range(-90, 400, (360 / self.game.cell_count)):
- angle.append(j)
- x = (int(d * math.cos(angle[i] * math.pi / 180.0)) + 600)
- y = (int(d * math.sin(angle[i] * math.pi / 180.0)) + 450)
- return (x,y)
-
- def update(self):
- button1, button2, button3 = pygame.mouse.get_pressed()
+ self.image.set_colorkey(white) + self.rect = self.image.get_rect() + self.moving = False + self.rect.center = (-100, -100) + self.destx, self.desty = 50, 50 + self.i ,self.stop = 0, 0 + + def setDest(self, dest): + self.destx, self.desty = dest[0], dest[1] + + def circ(self, x, y): + distance = self.game.dist(600, 450, x, y) + angle = math.acos(float(x - 600) / float(distance)) + angle *= 180.0 / math.pi # because pygame likes degrees + if y > 450: + angle = 360 - angle + angle = (480 - angle) % 360 + i = int(angle / (360.0 / self.game.cell_count)) + return i + + def path(self, i): + d = int(350) + angle = [] + for j in range(-90, 400, (360 / self.game.cell_count)): + angle.append(j) + x = (int(d * math.cos(angle[i] * math.pi / 180.0)) + 600) + y = (int(d * math.sin(angle[i] * math.pi / 180.0)) + 450) + return (x,y) + + def update(self): + button1, button2, button3 = pygame.mouse.get_pressed() if not self.moving: for i in range(0, len(self.game.cells)): x, y = self.game.cells[i].x, self.game.cells[i].y @@ -184,28 +187,28 @@ class Guard(MovingSprite): pygame.draw.circle(self.game.background, grey, (x, y), 50) if (button1 or button2 or button3): - self.game.move_count += 1
- self.moving = True
- self.i = i
+ self.game.move_count += 1 + self.moving = True + self.i = i self.stop = self.i self.setDest(self.path(self.i)) else: pygame.draw.circle(self.game.background, white, (x, y), 50) -
-
- d = self.game.dist(self.rect.centerx, self.rect.centery,
- self.destx, self.desty)
-
- if d > self.speed and self.moving:
- self.move(self.angle(self.destx, self.desty))
- - elif self.moving:
- self.game.cells[self.i].update()
- self.game.cells[self.i].getAdjHS().vacate()
- self.i = (self.i + 1) % self.game.cell_count
- if self.i != self.stop:
- self.setDest(self.path(self.i))
- else: self.moving = False
-
-
+ + + d = self.game.dist(self.rect.centerx, self.rect.centery, + self.destx, self.desty) + + if d > self.speed and self.moving: + self.move(self.angle(self.destx, self.desty)) + + elif self.moving: + self.game.cells[self.i].update() + self.game.cells[self.i].getAdjHS().vacate() + self.i = (self.i + 1) % self.game.cell_count + if self.i != self.stop: + self.setDest(self.path(self.i)) + else: self.moving = False + + |