Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bernabé <laurent.bernabe@gmail.com>2013-09-26 15:31:36 (GMT)
committer Laurent Bernabé <laurent.bernabe@gmail.com>2013-09-26 15:31:36 (GMT)
commit204c9942bdd162883a9efb23f564f2866973ac99 (patch)
treede545ce1a007d9ad3d4c48e73680e2d8283d70d8
parentf04c2fd333c08919a1a1ae8309f9ebb16347474a (diff)
Adjusted font sizes, and added more control over operations configurations : see OperationConfig class of BallsGenerator module.
-rw-r--r--balls_generator.py36
-rw-r--r--elements_painter.py2
-rw-r--r--main.py25
-rw-r--r--main_game.py11
4 files changed, 51 insertions, 23 deletions
diff --git a/balls_generator.py b/balls_generator.py
index 9ae0119..ab18536 100644
--- a/balls_generator.py
+++ b/balls_generator.py
@@ -18,12 +18,15 @@ class OperationConfig(object):
what is the limit for its operands ?
"""
- def __init__(self, op_type, fst_op, snd_op):
+ def __init__(self, op_type, fst_op, snd_op, res_limit=-1):
"""
Constructor:
- op_type : operation type => either OPER_ADD, OPER_SUB, OPER_MUL or OPER_DIV
- fst_op : limit, in digit numbers, for first operand => integer
- snd_op : limit, in digit numbers, for second operand => integer
+ op_type : operation type => either OPER_ADD, OPER_SUB, OPER_MUL
+ or OPER_DIV
+ fst_op : limit for first operand => integer
+ snd_op : limit for second operand => integer
+ res_limit : limit on the result, unless we pass negative or zero value,
+ in which case there is no limit (useful for division) => integer
"""
if op_type == OPER_SUB or op_type == OPER_DIV:
# First operand can't have less digits than second operand in such
@@ -38,6 +41,7 @@ class OperationConfig(object):
self._op_type = op_type
self._fst_op = fst_op
self._snd_op = snd_op
+ self._res_limit = res_limit
def get_operation_type(self):
"""
@@ -48,18 +52,26 @@ class OperationConfig(object):
def get_first_operand_limit(self):
"""
- Accessor to the limit, in digits, of the first operand
+ Accessor to the limit of the first operand
=> integer
"""
return self._fst_op
def get_second_operand_limit(self):
"""
- Accessor to the limit, in digits, of the second operand
+ Accessor to the limit of the second operand
=> integer
"""
return self._snd_op
+ def get_result_limit(self):
+ """
+ Accessor to the result limit.
+ => negative or zero value if there is no restriction : integer
+ => positive value if there is a limit : integer
+ """
+ return self._res_limit
+
class BallsGenerator(object):
@@ -102,8 +114,16 @@ class BallsGenerator(object):
op_type = operation_config.get_operation_type()
lim_1 = operation_config.get_first_operand_limit()
lim_2 = operation_config.get_second_operand_limit()
- fst_operand = randint(2, 10 ** lim_1 - 1)
- snd_operand = randint(2, 10 ** lim_2 - 1)
+ lim_res = operation_config.get_result_limit()
+ fst_operand = randint(2, lim_1)
+ snd_operand = randint(2, lim_2)
+ result = Operation(fst_operand, snd_operand, op_type).\
+ get_result()
+ if lim_res > 0:
+ if result > lim_res:
+ continue
+ if result <= 1:
+ continue
if op_type == OPER_ADD or op_type == OPER_MUL:
break
elif op_type == OPER_SUB:
diff --git a/elements_painter.py b/elements_painter.py
index df1ef06..a21bfd2 100644
--- a/elements_painter.py
+++ b/elements_painter.py
@@ -78,7 +78,7 @@ def paint_results(game_area, balls_list, surface):
balls_list : list of balls => list of Ball
surface : the destination surface => PyGame.Surface
"""
- font = pygame.font.Font(None, 16)
+ font = pygame.font.Font(None, 40)
#font = PangoFont(family='Helvetica', size=16)
LINE_HEIGHT = font.size("0123456789")[1]
CIRCLES_RADIUS = LINE_HEIGHT / 2
diff --git a/main.py b/main.py
index 9c55ed1..24f1f45 100644
--- a/main.py
+++ b/main.py
@@ -17,12 +17,25 @@ class Game():
pass
def run(self):
- operations_config = [OperationConfig(OPER_ADD, 2, 2),
- OperationConfig(OPER_MUL, 2, 1),
- OperationConfig(OPER_SUB, 2, 2),
- OperationConfig(OPER_DIV, 2, 2)]
-
- play_game(30, operations_config)
+ levels = [
+ # level 1
+ [OperationConfig(OPER_ADD, 9, 9),
+ OperationConfig(OPER_MUL, 9, 9),
+ OperationConfig(OPER_SUB, 18, 9),
+ OperationConfig(OPER_DIV, 81, 9, 9)],
+ # level 2
+ [OperationConfig(OPER_ADD, 99, 99, 100),
+ OperationConfig(OPER_MUL, 99, 9),
+ OperationConfig(OPER_SUB, 100, 98),
+ OperationConfig(OPER_DIV, 891, 9, 99)],
+ # level 3
+ [OperationConfig(OPER_ADD, 999, 999, 1000),
+ OperationConfig(OPER_MUL, 99, 99, 1000),
+ OperationConfig(OPER_SUB, 1000, 998),
+ OperationConfig(OPER_DIV, 1000, 99, 99)]
+ ]
+
+ play_game(30, levels[2])
if __name__ == "__main__":
g = Game()
diff --git a/main_game.py b/main_game.py
index c899de5..c0a4752 100644
--- a/main_game.py
+++ b/main_game.py
@@ -78,12 +78,9 @@ def play_game(time_seconds, operations_config):
else:
size = screen.get_size()
-
clock = pygame.time.Clock()
- font = pygame.font.Font(None, 16)
- #font = PangoFont(family='Helvetica', size=16, bold=True)
- #end_font = PangoFont(family='Helvetica', size=30, bold=True)
- end_font = font = pygame.font.Font(None, 30)
+ font = pygame.font.Font(None, 40)
+ end_font = pygame.font.Font(None, 90)
END_TXT_POS = (int(size[0] / 4)), int(size[1] / 2.6)
game_state = GameState.NORMAL
@@ -155,8 +152,7 @@ def play_game(time_seconds, operations_config):
end_txt = "Success !"
else:
end_txt = "Failure !"
- end_txt_surface = end_font.render(end_txt,1,
- BLUE, RED)
+ end_txt_surface = end_font.render(end_txt, 1, BLUE, RED)
screen.blit(end_txt_surface, END_TXT_POS)
while Gtk.events_pending():
@@ -168,4 +164,3 @@ def play_game(time_seconds, operations_config):
exit()
elif event.type == USEREVENT + 2:
show_status = not show_status
-