From 57419bb9cd31978a455dd9c5abf4842dc2c101dc Mon Sep 17 00:00:00 2001 From: Pablo Moleri Date: Fri, 18 Sep 2009 02:05:59 +0000 Subject: Sugar standard cursor added --- diff --git a/Quinteti.activity/MANIFEST.in b/Quinteti.activity/MANIFEST.in deleted file mode 100644 index 5b99791..0000000 --- a/Quinteti.activity/MANIFEST.in +++ /dev/null @@ -1,11 +0,0 @@ -include * -exclude *.pyc *~ dist -recursive-include activity * -recursive-exclude activity *.pyc *~ -recursive-include logic * -recursive-exclude logic *.pyc *~ -recursive-include gui * -recursive-exclude gui *.pyc -recursive-include olpcgames * -recursive-exclude olpcgames *.pyc *~ - diff --git a/Quinteti.activity/activity.py b/Quinteti.activity/activity.py index 48ddb06..63106a1 100644 --- a/Quinteti.activity/activity.py +++ b/Quinteti.activity/activity.py @@ -1,14 +1,35 @@ +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +# +# Copyright 2008, 2009 Pablo Moleri, ceibalJAM +# This file is part of Quinteti. +# +# Quinteti 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. +# +# Quinteti 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 Quinteti. If not, see . + +"""Quinteti Activity main module for Sugar import. + +This Activity is based on olpcgames.PyGameActivity. +The activity attributa game_name is the module name that +has the main() function.""" + from sugar.activity.activity import ActivityToolbox from olpcgames import activity from gettext import gettext as _ -#SERVICE = "org.laptop.HelloMesh" -#IFACE = SERVICE -#PATH = "/org/laptop/HelloMesh" - # PyGameActivity: http://www.vrplumber.com/sugar-docs/olpcgames.activity.html class Quinteti(activity.PyGameActivity): - """Set up Quin-te-ti activity.""" - game_name = 'run' - game_title = _('Quin-te-ti') + """Set up QuinTeTi activity.""" + game_name = 'main' # Module name with main() function. + game_title = _('QuinTeTi') game_size = None diff --git a/Quinteti.activity/gui/board.py b/Quinteti.activity/gui/board.py index 5cf5792..6cb67ab 100644 --- a/Quinteti.activity/gui/board.py +++ b/Quinteti.activity/gui/board.py @@ -1,10 +1,30 @@ +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +# +# Copyright 2008, 2009 Pablo Moleri, ceibalJAM +# This file is part of Quinteti. +# +# Quinteti 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. +# +# Quinteti 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 Quinteti. If not, see . + +"""Board represents the game board, and its capable of paint its elements in a given surface.""" + import pygame -from pygame.locals import * -from logic.GameState import GameState -from Button import Button -from Cell import Cell -from Player import Player +from logic.game import GameState + +from button import Button +from cell import Cell file_dir = "gui/" @@ -28,7 +48,8 @@ font_name = "DejaVu Serif" #"DejaVuLGCSerif.ttf" # None to load pygame defaul font_size = 24 user_font_color = (255, 255, 255) -class BoardUI: +"""Class Board keeps all the grafical elements as well as a reference to the logical game state.""" +class Board: # Center of initial number positions number_locations = [ @@ -78,11 +99,11 @@ class BoardUI: self.screen = screen self.game = game self.showing_instructions = False - self.initBoard() + self.init_board() - def initBoard (self): + def init_board (self): self.new_button = Button(new_image_coords, file_dir + new_image, self.new_game) - self.instructions_button = Button(instructions_coords, file_dir + instructions_button, self.show_instructions) + self.instructions_button = Button(instructions_coords, file_dir + instructions_button, self._show_instructions) self.cells = [] self.numbers = [] self.lastSelectedBoardCell = None @@ -90,11 +111,11 @@ class BoardUI: self.backgroundImage = pygame.image.load(image_fondo) - self.initCells() - self.initNumbers() + self._init_cells() + self._init_numbers() # Creates a sprite group, with all the board visible elements inside - self.paintBackground() + self._paint_background() self.items = pygame.sprite.Group() self.items.add(self.new_button) self.items.add(self.instructions_button) @@ -108,9 +129,9 @@ class BoardUI: def new_game(self): self.game = GameState("", "") - self.initBoard() + self.init_board() - def initCells(self): + def _init_cells(self): i = 1 for row in range(1,4): for col in range(1,4): @@ -119,30 +140,30 @@ class BoardUI: else: number = None location = self.locations[i-1] - self.cells.append( Cell(location, self.get_number_name(number), i, image_size) ) + self.cells.append( Cell(location, self._get_number_name(number), i, image_size) ) i += 1 - def initNumbers(self): + def _init_numbers(self): k = 0 for location in self.number_locations: k += 1 - self.numbers.append(Cell(location, self.get_number_name(k), k, image_size)) + self.numbers.append(Cell(location, self._get_number_name(k), k, image_size)) - def setPlayers (self, namePlayer1, namePlayer2): - self.game = GameState(namePlayer1, namePlayer2) + def set_players(self, name_player1, name_player2): + self.game = GameState(name_player1, name_player2) - def paintBackground(self): + def _paint_background(self): rect = self.backgroundImage.get_rect() rect.topleft = (0, 0) self.screen.blit(self.backgroundImage, rect) - def paint_winner(self, i): + def _paint_winner(self, i): image = pygame.image.load(file_dir + player_win_image) rect = image.get_rect() rect.topleft = self.players_score_box_location[i] self.screen.blit(image, rect) - def paintPlayersStatus(self): + def _paint_players_status(self): player1Name = "" player2Name = "" @@ -155,7 +176,7 @@ class BoardUI: self.font.set_bold(False) else: if self.game.get_player_score(i) >= self.game.get_player_score(3-i): - self.paint_winner(i-1) + self._paint_winner(i-1) player_name = self.game.get_player_name(i) #str_player = 'Jugador %s: %s' % (i, player_name) @@ -176,14 +197,14 @@ class BoardUI: # Using an sprite group all the items are painted: #self.items.clear(self.screen, self.backgroundImage) # If only sprites are cleared, players scores remain - self.paintBackground() # Instead, the whole background is repainted + self._paint_background() # Instead, the whole background is repainted self.items.draw(self.screen) - self.paintPlayersStatus() + self._paint_players_status() if self.showing_instructions: - self.paint_instructions() + self._paint_instructions() - def paint_instructions(self): + def _paint_instructions(self): image = pygame.image.load(file_dir + instructions_image) rect = image.get_rect() rect.center = self.screen.get_rect().center @@ -201,7 +222,7 @@ class BoardUI: # Checks if the selected coordinate is a board cell isCell = False for c in self.cells: - if c.coordsIn(x,y): + if c.coordsIn(x, y): isCell = True self.lastSelectedBoardCell = c if self.lastSelectedNumberCell != None: @@ -209,7 +230,7 @@ class BoardUI: player = self.game.get_enabled_player() ok = self.game.make_move(row, col, self.lastSelectedNumberCell.idxCell, player) if ok: - self.lastSelectedBoardCell.setImage( self.get_number_name(self.lastSelectedNumberCell.idxCell) ) + self.lastSelectedBoardCell.setImage( self._get_number_name(self.lastSelectedNumberCell.idxCell) ) self.items.add(self.lastSelectedBoardCell) self.items.remove(self.lastSelectedNumberCell) self.lastSelectedNumberCell.setImage(None) @@ -221,24 +242,26 @@ class BoardUI: for n in self.numbers: if n.coordsIn(x,y): if self.lastSelectedNumberCell: - self.lastSelectedNumberCell.setImage( self.get_number_name(self.lastSelectedNumberCell.idxCell) ) + self.lastSelectedNumberCell.setImage( self._get_number_name(self.lastSelectedNumberCell.idxCell) ) self.lastSelectedNumberCell = n - n.setImage( self.get_disabled_number_name(n.idxCell) ) + n.setImage( self._get_disabled_number_name(n.idxCell) ) if self.new_button.coordsIn(x, y): self.new_button.callback() + + return True - def show_instructions(self): + def _show_instructions(self): self.showing_instructions = True - def get_number_name(self, number): + def _get_number_name(self, number): if (number == None) or (number == 0): return None else: - return file_dir + image_number.replace("", str(number)) + return file_dir + image_number.replace("", str(number)) - def get_disabled_number_name(self, number): + def _get_disabled_number_name(self, number): if (number == None) or (number == 0): return None else: - return file_dir + image_disabled_number.replace("", str(number)) + return file_dir + image_disabled_number.replace("", str(number)) diff --git a/Quinteti.activity/gui/button.py b/Quinteti.activity/gui/button.py index fb5b53d..d2efbc2 100644 --- a/Quinteti.activity/gui/button.py +++ b/Quinteti.activity/gui/button.py @@ -1,7 +1,28 @@ +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +# +# Copyright 2008, 2009 Pablo Moleri, ceibalJAM +# This file is part of Quinteti. +# +# Quinteti 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. +# +# Quinteti 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 Quinteti. If not, see . + import pygame +"""Button is a PyGame Sprite with a callback function.""" + class Button(pygame.sprite.Sprite): - def __init__(self, initial_position, nomImage, callback): + def __init__(self, initial_position, nomImage, callback): pygame.sprite.Sprite.__init__(self) @@ -12,8 +33,7 @@ class Button(pygame.sprite.Sprite): self.callback = callback def coordsIn(self, x, y): - #print "Test x: %s < %s < %s Test y: %s < %s < %s" % (self.rect.left, x, self.rect.right, self.rect.top, y, self.rect.bottom) - if ( self.rect.collidepoint(x, y) ): + if self.rect.collidepoint(x, y): return True return False @@ -22,7 +42,3 @@ class Button(pygame.sprite.Sprite): self.image = pygame.image.load(nomImage) else: self.image = None - -# Codigo para debug de este modulo: -#if __name__ == "__main__": - diff --git a/Quinteti.activity/gui/cell.py b/Quinteti.activity/gui/cell.py index 12adf05..40a4578 100644 --- a/Quinteti.activity/gui/cell.py +++ b/Quinteti.activity/gui/cell.py @@ -1,13 +1,34 @@ +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +# +# Copyright 2008, 2009 Pablo Moleri, ceibalJAM +# This file is part of Quinteti. +# +# Quinteti 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. +# +# Quinteti 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 Quinteti. If not, see . + import pygame +"""Cell is a PyGame Sprite, capable of loading an image and retain a cell identifier.""" + class Cell(pygame.sprite.Sprite): - def __init__(self, initial_position, nomImage, idxCell, size): + def __init__(self, initial_position, nomImage, idxCell, size_rect): pygame.sprite.Sprite.__init__(self) self.idxCell = idxCell - self.rect = size.move(0, 0) # Attemping to move creates a copy + self.rect = size_rect.move(0, 0) # Attempting to move creates a copy self.rect.center = initial_position # Moves the recteangle to its predetermined center self.setImage(nomImage) @@ -31,8 +52,9 @@ class Cell(pygame.sprite.Sprite): col = (self.idxCell - 1) % 3 + 1 return row, col -# Codigo para debug de este modulo: if __name__ == "__main__": - cell = Cell([0, 0], "nulo.bmp", 6) + '''Debug Code.''' + image_size = pygame.Rect(0, 0, 97, 97) + cell = Cell([0, 0], "1.png", 6, image_size) row, col = cell.get_pos() print "%s %s" % (row, col) diff --git a/Quinteti.activity/gui/standardcursor.xbm b/Quinteti.activity/gui/standardcursor.xbm new file mode 100644 index 0000000..e01881a --- /dev/null +++ b/Quinteti.activity/gui/standardcursor.xbm @@ -0,0 +1,22 @@ +#define standardcursor_width 40 +#define standardcursor_height 40 +#define standardcursor_x_hot 0 +#define standardcursor_y_hot 0 +static unsigned char standardcursor_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, + 0xff, 0x3f, 0x00, 0xfc, 0xff, 0xff, 0x7f, 0x00, 0xfc, 0xff, 0xff, 0xff, + 0x00, 0xfc, 0xff, 0xff, 0xff, 0x00, 0xfc, 0xff, 0xff, 0xff, 0x00, 0xfc, + 0xff, 0xff, 0xff, 0x00, 0xfc, 0xff, 0xff, 0x7f, 0x00, 0xfc, 0xff, 0xff, + 0x3f, 0x00, 0xfc, 0xff, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x00, + 0xfc, 0xff, 0x03, 0x00, 0x00, 0xfc, 0xff, 0x07, 0x00, 0x00, 0xfc, 0xff, + 0x0f, 0x00, 0x00, 0xfc, 0xff, 0x1f, 0x00, 0x00, 0xfc, 0xfb, 0x3f, 0x00, + 0x00, 0xfc, 0xf3, 0x7f, 0x00, 0x00, 0xfc, 0xe3, 0xff, 0x00, 0x00, 0xfc, + 0xc3, 0xff, 0x01, 0x00, 0xfc, 0x83, 0xff, 0x03, 0x00, 0xfc, 0x03, 0xff, + 0x07, 0x00, 0xfc, 0x03, 0xfe, 0x0f, 0x00, 0xfc, 0x03, 0xfc, 0x1f, 0x00, + 0xfc, 0x03, 0xf8, 0x3f, 0x00, 0xfc, 0x03, 0xf0, 0x7f, 0x00, 0xfc, 0x03, + 0xe0, 0xff, 0x00, 0xfc, 0x03, 0xc0, 0xff, 0x01, 0xfc, 0x03, 0x80, 0xff, + 0x01, 0xfc, 0x03, 0x00, 0xff, 0x01, 0xf8, 0x01, 0x00, 0xfe, 0x03, 0xf0, + 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; diff --git a/Quinteti.activity/gui/standardcursor_mask.xbm b/Quinteti.activity/gui/standardcursor_mask.xbm new file mode 100644 index 0000000..a06d085 --- /dev/null +++ b/Quinteti.activity/gui/standardcursor_mask.xbm @@ -0,0 +1,17 @@ +#define standardcursor_width 40 +#define standardcursor_height 40 +static unsigned char standardcursor_bits[] = { + 0xff,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0x01,0xff,0xff,0xff,0xff,0x03, + 0xff,0xff,0xff,0xff,0x03,0xff,0xff,0xff,0xff,0x03,0xff,0xff,0xff,0xff,0x07, + 0xff,0xff,0xff,0xff,0x07,0xff,0xff,0xff,0xff,0x03,0xff,0xff,0xff,0xff,0x03, + 0xff,0xff,0xff,0xff,0x03,0xff,0xff,0xff,0xff,0x01,0xff,0xff,0xff,0xff,0x00, + 0xff,0xff,0x3f,0x00,0x00,0xff,0xff,0x7f,0x00,0x00,0xff,0xff,0xff,0x00,0x00, + 0xff,0xff,0xff,0x01,0x00,0xff,0xff,0xff,0x03,0x00,0xff,0xff,0xff,0x07,0x00, + 0xff,0xff,0xff,0x0f,0x00,0xff,0xff,0xff,0x1f,0x00,0xff,0xff,0xff,0x3f,0x00, + 0xff,0xff,0xff,0x7f,0x00,0xff,0xef,0xff,0xff,0x00,0xff,0xcf,0xff,0xff,0x01, + 0xff,0x8f,0xff,0xff,0x03,0xff,0x0f,0xff,0xff,0x07,0xff,0x0f,0xfe,0xff,0x07, + 0xff,0x0f,0xfc,0xff,0x0f,0xff,0x0f,0xf8,0xff,0x0f,0xff,0x0f,0xf0,0xff,0x0f, + 0xff,0x0f,0xe0,0xff,0x0f,0xff,0x0f,0xc0,0xff,0x0f,0xfe,0x07,0x80,0xff,0x07, + 0xfc,0x03,0x00,0xff,0x07,0x60,0x00,0x00,0xfe,0x03,0x00,0x00,0x00,0xf8,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00}; diff --git a/Quinteti.activity/logic/game.py b/Quinteti.activity/logic/game.py index 1dfb2a0..07df8ad 100644 --- a/Quinteti.activity/logic/game.py +++ b/Quinteti.activity/logic/game.py @@ -1,26 +1,48 @@ +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +# +# Copyright 2008, 2009 Pablo Moleri +# This file is part of Quinteti. +# +# Quinteti 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. +# +# Quinteti 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 Quinteti. If not, see . + +"""GamesState, keeps the state of a game, and encloses game logic.""" class GameState: - # Un constructor inicia la partida. def __init__(self, player_1, player_2, matrix_size=3, target_score=15): - self.player_1_name=player_1 - self.player_2_name=player_2 - self.player_1_score=0 - self.player_2_score=0 - self.turn=1 - self.target_score=target_score - self.matrix=[] - self.state=[] + ''' Creates a new game with the given players. ''' + + self.player_1_name = player_1 + self.player_2_name = player_2 + self.player_1_score = 0 + self.player_2_score = 0 + self.turn = 1 + self.target_score = target_score + self.matrix = [] + self.state = [] for i in range(0, matrix_size): self.matrix.append([]) self.state.append([]) for j in range(0, matrix_size): self.matrix[i].append(0) self.state[i].append(None) - self.numbers=range(1, len(self.matrix[0])*len(self.matrix)+1) + self.numbers = range(1, len(self.matrix[0])*len(self.matrix)+1) - # Un constructor que recupera el estado def fromString(string): + """A static method for loading a new game from a serialized game string.""" + dic = eval(string) state = dic['state'] matrix = dic['matrix'] @@ -39,40 +61,45 @@ class GameState: if number in game.numbers: game.numbers.remove(number) return game - fromString = staticmethod(fromString) # Crea un atributo estatico del tipo funcion - # Persiste el estado actual del juego + fromString = staticmethod(fromString) # Maps the function as an static class attribute + def serialization(self): + """Returns the game in a serialized string format.""" + return str(self) - # Obtiene el estado de una casilla, tupla numero y jugador o None si esta vacia. - # get_cell(row : int, col : int): (number: int, player: int) or None + def get_cell(self, row1, col1): - row, col = (row1-1, col1-1) + """Returns the cell state: (number, player) Or None.""" + row, col = row1-1, col1-1 return (self.matrix[row][col], self.state[row][col]) - # Obtiene los numeros que se pueden jugar. - # get_available_numbers(): [int] + def get_available_numbers(self): - return self.numbers + """Returns the list of available numbers (no played).""" - # Realiza una jugada en una celda y retorna si se pudo realizar. - # make_move(row: int, col : int, number : int, player: int): bool + return self.numbers + def make_move(self, row1, col1, number, player): + """Makes a move with the given number in the given cell. Returns a boolean if the move is valid.""" + row, col = (row1-1, col1-1) - if (self.state[row][col]==None): - if (self.turn==player): + if (self.state[row][col] == None): + if (self.turn == player): if (number in self.numbers): - #obtengo una copia de la columna + # shadow copy of the given column col_list = [fila[col] for fila in self.matrix] - #obtengo una copia de la fila + + # shadow copy of the given row row_list = self.matrix[row][:] + + # Test the move + score = 0 + score += self._check_action(col_list, row, number) + score += self._check_action(row_list, col, number) - score=0 - score+=self.check_action(col_list, row, number) - score+=self.check_action(row_list, col, number) - - self.state[row][col]=self.turn - self.matrix[row][col]=number + self.state[row][col] = self.turn + self.matrix[row][col] = number self.numbers.remove(number) if self.turn == 1: @@ -84,9 +111,9 @@ class GameState: return True return False - # Rutina privada para verificar si la jugada suma puntos, se pasa una lista - # que representa una columna o una fila y la jugada. - def check_action(self, list, pos, number): + def _check_action(self, list, pos, number): + """Tests if a move in a row (or column) scores.""" + list[pos] = number if 0 in list: return 0 @@ -95,33 +122,25 @@ class GameState: else: return 0 - - # Jugador habilitado para jugar, o None si la partida termina. - # get_enabled_player(): int def get_enabled_player(self): + """Returns the turn (enabled player) or None if the game is over.""" if len(self.numbers) == 0: return None else: return self.turn - # Puntaje de cada jugador - # get_player_score(player: int) def get_player_score(self, player): if player == 1 : return self.player_1_score else: return self.player_2_score - - # Obtiene el nombre de un jugador - # get_player_name(player: int): String + def get_player_name(self, player): if player == 1 : return self.player_1_name else: return self.player_2_name - - # Obtiene la cantidad de jugadores - # get_player_count(): int + def get_player_count(self): return 2 @@ -134,27 +153,20 @@ class GameState: 'player_1_score': self.player_1_score, 'player_2_score': self.player_2_score, 'target_score': self.target_score} - return str(dic) - + return str(dic) if __name__ == "__main__": - var=GameState("Juan", "Pablo") - print var.target_score + """Module test function.""" - var.make_move(0, 1, 2, 1) - var.make_move(1, 1, 7, 2) - var.make_move(2, 1, 6, 1) + game = GameState("Juan", "Pablo") + print game.target_score - print var + game.make_move(0, 1, 2, 1) + game.make_move(1, 1, 7, 2) + game.make_move(2, 1, 6, 1) - var2 = GameState.fromString( var.serialization() ) - print 'var2 %s' % (var2) - print var2.get_player_name(1) + print game -# print var.matrix -# print var.state -# print var.player_1_score -# print var.get_player_score(1) -# print var.player_2_score -# print var.get_player_score(2) -# print var.get_player_name(1) + game2 = GameState.fromString( game.serialization() ) + print 'game2 %s' % (game) + print game2.get_player_name(1) diff --git a/Quinteti.activity/logic/mesh.py b/Quinteti.activity/logic/mesh.py index 4744e75..04983aa 100644 --- a/Quinteti.activity/logic/mesh.py +++ b/Quinteti.activity/logic/mesh.py @@ -1,3 +1,22 @@ +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +# +# Copyright 2008, 2009 Pablo Moleri +# This file is part of Quinteti. +# +# Quinteti 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. +# +# Quinteti 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 Quinteti. If not, see . + from sugar.presence import presenceservice from sugar.presence.tubeconn import TubeConnection diff --git a/Quinteti.activity/main.py b/Quinteti.activity/main.py index a842923..7d8c717 100644 --- a/Quinteti.activity/main.py +++ b/Quinteti.activity/main.py @@ -1,37 +1,76 @@ -import pygame, olpcgames, logging -from olpcgames import pausescreen -from pygame.locals import * +#!/usr/bin/python +# -*- coding: iso-8859-1 -*- +# +# Copyright 2008, 2009 Pablo Moleri, ceibalJAM +# This file is part of Quinteti. +# +# Quinteti 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. +# +# Quinteti 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 Quinteti. If not, see . +# +# Contact information: +# Pablo Moleri pmoleri@gmail.com +# ceibalJAM http://ceibaljam.org -from BoardUI import BoardUI +"""Main module of the game. -from logic.GameState import GameState +This is the main module of the game, it can be executed as a standalone game +or imported as a sugar activity. +""" +import pygame +import olpcgames +import olpcgames.pausescreen +import logging + +from gui.board import Board +from logic.game import GameState #import logic.Mesh import os -log = logging.getLogger( 'quinteti run' ) -log.setLevel( logging.DEBUG ) +log = logging.getLogger('quinteti') +log.setLevel(logging.DEBUG) -MAX_FPS = 25 # Max frames per second -SLEEP_TIMEOUT = 25 # Seconds until the PauseScreen if no events show up +MAX_FPS = 25 # Max frames per second +SLEEP_TIMEOUT = 30 # Seconds until the PauseScreen if no events show up -# El modulo se llama desde run.py. def main(): + """Main function of the game. + + This function initializes the game and enters the PyGame main loop. + """ + + # Inits PyGame module pygame.init() - internal_size = (1200, 825) # The game is designed to work in this size (xo display size) - target_size = (900, 619) # The game will be sown in this size, useful for testing in regular PCs with less resolution than xo + # Loads Sugar standard cursor + a, b, c, d = pygame.cursors.load_xbm("gui/standardcursor.xbm", "gui/standardcursor_mask.xbm") + pygame.mouse.set_cursor(a, b, c, d) + + internal_size = (1200, 825) # The game is designed to work in this size (xo display size) + target_size = (900, 619) # The game will be sown in this size, useful for testing in regular PCs with less resolution than xo flags = 0 if olpcgames.ACTIVITY: # Running as Activity target_size = olpcgames.ACTIVITY.game_size - #logic.Mesh.init_mesh(log) # Mesh isn't ready in this version - #else: + #logic.Mesh.init_mesh(log) # Mesh isn't ready in this version + else: + pass # Uncomment this if want to execute fullscreen on regular PCs # flags = pygame.FULLSCREEN - real_screen = pygame.display.set_mode(target_size, flags) + + real_screen = pygame.display.set_mode(target_size, flags) # The scale factor beetween internal and target if internal_size == target_size: @@ -44,73 +83,76 @@ def main(): # Creates a new logic game, player names aren't used without mesh game = GameState("Jugador1", "Jugador2") - boardUI = BoardUI(internal_screen, game) - boardUI.paintBoardElements() + board = Board(internal_screen, game) + board.paintBoardElements() pygame.display.update() + # This clock is used to keep the game at the desired FPS. clock = pygame.time.Clock() - # Comienza el bucle principal - update = True # La primera vez tiene que pintar la pantalla + # Main loop + update = True # The first time the screen need to be updated running = True while running: # Waits for events, if none the game pauses: # http://wiki.laptop.org/go/Game_development_HOWTO#Reducing_CPU_Load milliseconds = clock.tick(MAX_FPS) # waits if the game is running faster than MAX_FPS - events = pausescreen.get_events(SLEEP_TIMEOUT) # Event-management loop with support for pausing after X seconds (20 here) + events = olpcgames.pausescreen.get_events(SLEEP_TIMEOUT) # Event-management loop with support for pausing after X seconds (20 here) if events: for event in events: if event.type == pygame.QUIT: running = False - elif event.type == pygame.KEYDOWN and (event.key == pygame.K_q or event.key == pygame.K_ESCAPE): + elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: running = False if event.type == pygame.MOUSEBUTTONDOWN: if scale: - [x, y] = [coord*s for (coord, s) in zip(event.pos, scale)] # Multiplica las coordenadas realeas por el factor - # para llevarlas a coordenadas internas + x = event.pos[0] * scale[0] # Multiplies the real coordinates by the scale factor + y = event.pos[1] * scale[1] # to get the internal coordinates else: (x, y) = event.pos - boardUI.processXY(x, y) - update = True + update = board.processXY(x, y) if event.type == pygame.USEREVENT: if event.code == olpcgames.FILE_READ_REQUEST: game = read_file(event.filename) log.debug("Loaded:" + game.serialization()) - boardUI = BoardUI(internal_screen, game) + board = Board(internal_screen, game) update = True if event.code == olpcgames.FILE_WRITE_REQUEST: save_file(event.filename, game) if update == True: - boardUI.paintBoardElements() + board.paintBoardElements() if scale: pygame.transform.scale(internal_screen, target_size, real_screen) update = False - pygame.display.update() + pygame.display.flip() # Una vez que sale del loop manda la senal de quit para que cierre la ventana pygame.quit() -def save_file(file, game): +def _save_file(file, game): + """Saves the game to the given file.""" string = game.serialization() fsock = open(file, 'w') fsock.write(string) fsock.close() -def read_file(file): +def _read_file(file): + """Loads the game from the given file.""" fsock = open(file, "r") string = fsock.read() fsock.close() return GameState.fromString(string) -# Codigo para debug de este modulo: if __name__ == "__main__": + """Standalone code.""" + logging.basicConfig() main() -- cgit v0.9.1