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-20 22:40:15 (GMT)
committer Laurent Bernabé <laurent.bernabe@gmail.com>2013-09-20 22:40:15 (GMT)
commit24bccf02797828525c181d827654b2f0ebd67cff (patch)
treed672130a27af58aa6f04c29e53ebc8f3f2d6d4d4
parent6a9d0db65529cd94155b9ec7abbfeefcc4e2ebfd (diff)
Added a countdown time bar.
-rw-r--r--elements_painter.py20
-rw-r--r--main.py19
-rw-r--r--time_bar.py111
3 files changed, 146 insertions, 4 deletions
diff --git a/elements_painter.py b/elements_painter.py
index aab9aa1..6b1d7ff 100644
--- a/elements_painter.py
+++ b/elements_painter.py
@@ -26,3 +26,23 @@ def paint_ball(ball, surface):
txt_surface = ball.get_txt_font().render(
ball.get_operation().get_text(), color=ball.get_txt_color())
surface.blit(txt_surface, txt_position)
+
+def paint_time_bar(time_bar, surface):
+ """
+ Draws a time bar onto the given PyGame surface.
+ time_bar : the time bar => TimeBar
+ surface : the destination surface => PyGame.Surface
+ """
+ edge = time_bar.get_edge()
+ dead_rect = (edge[0], edge[1],
+ time_bar.get_width(), time_bar.get_height())
+ pygame.draw.rect(surface, time_bar.get_dead_color(), dead_rect)
+ try:
+ value = time_bar.get_value()
+ max_value = time_bar.get_max_value()
+ active_rect = (edge[0], edge[1],
+ int(time_bar.get_width() * value / max_value),
+ time_bar.get_height())
+ pygame.draw.rect(surface, time_bar.get_active_color(), active_rect)
+ except NameError:
+ pygame.draw.rect(surface, time_bar.get_active_color(), dead_rect)
diff --git a/main.py b/main.py
index 1761878..488f086 100644
--- a/main.py
+++ b/main.py
@@ -9,10 +9,11 @@ import olpcgames
import pygame
from sys import exit
from olpcgames.pangofont import PangoFont
-from pygame.locals import QUIT
-from ball import *
-from operation import *
-from elements_painter import *
+from pygame.locals import QUIT, USEREVENT
+from ball import Ball
+from operation import Operation, OPER_ADD, OPER_SUB, OPER_MUL, OPER_DIV
+from elements_painter import paint_ball, paint_time_bar
+from time_bar import TimeBar
import balls_collision
@@ -21,6 +22,7 @@ def main():
pygame.init()
FPS = 40
BACKGROUND = (255, 255, 255)
+ TIME_BAR_HEIGHT = 20
if olpcgames.ACTIVITY:
size = olpcgames.ACTIVITY.game_size
screen = pygame.display.set_mode(size)
@@ -35,6 +37,9 @@ def main():
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
+ DARK_GREEN = (0, 100, 0)
+ GRAY = (200, 200, 200)
+
the_balls = [Ball(font, BLACK, BLUE,
Operation(1000, 3000, OPER_MUL), (2, 1.2)),
Ball(font, BLACK, YELLOW,
@@ -50,14 +55,20 @@ def main():
the_balls[2].move_to((200, 80))
the_balls[3].move_to((330, 70))
+ time_bar = TimeBar(size[0], TIME_BAR_HEIGHT, DARK_GREEN, GRAY)
+ time_bar.start(1000, 10)
+
while True:
screen.fill(BACKGROUND)
+ paint_time_bar(time_bar, screen)
for ball in the_balls:
paint_ball(ball, screen)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
+ elif event.type == USEREVENT + 1:
+ time_bar.decrease()
pygame.display.update()
clock.tick(FPS)
for ball in the_balls:
diff --git a/time_bar.py b/time_bar.py
new file mode 100644
index 0000000..a228153
--- /dev/null
+++ b/time_bar.py
@@ -0,0 +1,111 @@
+# -*- coding: utf-8 -*-
+"""
+Manages time bar : a kind of reversed progress bar.
+Created on Fri Sep 20 23:19:47 2013
+
+@author: laurent-bernabe
+"""
+
+import pygame
+from pygame.locals import USEREVENT
+
+class TimeBar(object):
+ """
+ A kind of reversed progress bar.
+ """
+
+ def __init__(self, width, height, active_color, dead_color,
+ lftp_edge=(0,0)):
+ """
+ Constructor
+ width : width of the bar (when it has its maximum value) => integer
+ height : height of the bar => integer
+ active_color : color for active part => Tuple of 3 integers in [0,255]
+ dead_color : color for the dead part => Tuple of 3 integers in [0,255]
+ lftp_edge : coordinate of the left-top edge => tuple of two integers
+ """
+ self._width = width
+ self._height = height
+ self._lftp_edge = lftp_edge
+ self._active_color = active_color
+ self._dead_color = dead_color
+
+ def start(self, max_value, step):
+ """
+ Starts a new 'session'. Please, notice that time between two stages
+ (or ticks) is 100 milliseconds, and that you must call decrease()
+ method on USEREVENT+1 event of pygame.
+ max_value : the start value (don't forget that it is a reversed
+ progress bar) => integer
+ step : how many units will be removed at each stage => integer
+ """
+ self._value = max_value
+ self._max_value = max_value
+ self._step = step
+ pygame.time.set_timer(USEREVENT + 1, 100)
+
+ def decrease(self):
+ """
+ Decreases the value of the time bar, and stops its falling if there
+ is no more value unit.
+ """
+ self._value -= self._step
+ if self._value <= 0:
+ pygame.time.set_timer(USEREVENT + 1, 0)
+
+ def get_width(self):
+ """
+ Accessor to the width (width when it has its maximum value)
+ => integer
+ """
+ return self._width
+
+ def get_height(self):
+ """
+ Accessor to the height
+ => integer
+ """
+ return self._height
+
+ def get_edge(self):
+ """
+ Accessor to the left-top edge.
+ => tuple of two integers
+ """
+ return self._lftp_edge
+
+ def get_active_color(self):
+ """
+ Accessor to the active part (not elapsed) color.
+ => Tuple of 3 integers.
+ """
+ return self._active_color
+
+ def get_dead_color(self):
+ """
+ Accessor to the dead part (elapsed) color.
+ => Tuple of 3 integers
+ """
+ return self._dead_color
+
+ def get_value(self):
+ """
+ Accessor to the value.
+ Be careful ! May raise NameError, as value may not be defined yet.
+ => integer
+ """
+ try:
+ return self._value
+ except AttributeError:
+ raise NameError
+
+ def get_max_value(self):
+ """
+ Accessor to the max value.
+ Be careful ! May raise NameError, as max value may not be defined yet.
+ => integer
+ """
+ try:
+ return self._max_value
+ except AttributeError:
+ raise NameError \ No newline at end of file