Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ball.py
diff options
context:
space:
mode:
Diffstat (limited to 'ball.py')
-rw-r--r--ball.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/ball.py b/ball.py
index d4d1d46..6bbeea9 100644
--- a/ball.py
+++ b/ball.py
@@ -4,7 +4,6 @@ Created on Sat Sep 14 10:57:17 2013
@author: laurent-bernabe
"""
-import pygame
class Ball(object):
@@ -12,24 +11,26 @@ class Ball(object):
An abstractation of a ball.
"""
- def __init__(self, txt_font, txt_color, bg_color, operation, velocity):
+ def __init__(self, txt_font, txt_color, bg_color, operation,
+ move_area, velocity = (0,0)):
"""
Constructor
txt_font : text font => olpcgames.pangofont
txt_color : text color => Tuple of 3 integers in [0,255]
bg_color : background color => Tuple of 3 integers in [0,255]
operation : operation => An Operation value
- velocity : current direction of move => A tuple of 2 floats (x,y)
+ move_area : space where ball is allowed to be => Tuple of 4 integers (
+ left side x, top side y, right side x, bottom side y)
+ velocity : current direction of move (so a (0,0) velocity for a fixed
+ ball) => A tuple of 2 floats (x,y)
"""
- info = pygame.display.Info()
- self._SCREEN_WIDTH = info.current_w
- self._SCREEN_HEIGHT = info.current_h
self._txt_font = txt_font
self._txt_color = txt_color
self._bg_color = bg_color
self._operation = operation
self._center = (0, 0)
self._velocity = velocity
+ self._move_area = move_area
txt_size = txt_font.size(operation.get_text())
if txt_size[0] > txt_size[1]:
max_txt_size = txt_size[0]
@@ -84,7 +85,14 @@ class Ball(object):
Moves the ball to a particular place.
new_center_position : the new center position => a tuple of 2 integers
"""
- self._center = new_center_position
+ if (new_center_position[0] - self._diameter < self._move_area[0]
+ or new_center_position[0] + self._diameter > self._move_area[2]
+ or new_center_position[1] - self._diameter < self._move_area[1]
+ or new_center_position[1] + self._diameter > self._move_area[3]):
+ self._center = (self._move_area[0] + self._diameter,
+ self._move_area[1] + self._diameter)
+ else:
+ self._center = new_center_position
def move(self):
"""
@@ -97,13 +105,14 @@ class Ball(object):
self._center = expected_new_center
radius = self._diameter / 2
# Ball must not go across left or right wall.
- if (self._center[0] < radius
- or self._center[0] > self._SCREEN_WIDTH - radius):
+ if (self._center[0] < self._move_area[0] + radius
+ or self._center[0] > self._move_area[2] - radius):
self._velocity = (-self._velocity[0], self._velocity[1])
self._center = (self._center[0] + self._velocity[0],
self._center[1] + self._velocity[1])
- elif (self._center[1] < radius
- or self._center[1] > self._SCREEN_HEIGHT - radius):
+ # Ball must not go across top or bottom wall.
+ elif (self._center[1] < self._move_area[1] + radius
+ or self._center[1] > self._move_area[3] - radius):
self._velocity = (self._velocity[0], -self._velocity[1])
self._center = (self._center[0] + self._velocity[0],
self._center[1] + self._velocity[1])