diff options
author | Laurent Bernabé <laurent.bernabe@gmail.com> | 2013-09-21 07:57:12 (GMT) |
---|---|---|
committer | Laurent Bernabé <laurent.bernabe@gmail.com> | 2013-09-21 07:57:12 (GMT) |
commit | 85ad85772d67a977cad43c68f3c2ad9b14f0bc54 (patch) | |
tree | 5f43efc7ede0e797b0c5216f7a7dee25ea1c43e3 | |
parent | 27ad681101acab42a52a3f817343c82bdcb88d28 (diff) |
Now the balls don't move across the whole screen, but inside the space that they are given.
-rw-r--r-- | ball.py | 31 | ||||
-rw-r--r-- | main.py | 36 |
2 files changed, 39 insertions, 28 deletions
@@ -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]) @@ -23,6 +23,13 @@ def main(): FPS = 40 BACKGROUND = (255, 255, 255) TIME_BAR_HEIGHT = 20 + BLACK = (0, 0, 0) + BLUE = (0, 0, 255) + YELLOW = (255, 255, 0) + RED = (255, 0, 0) + GREEN = (0, 255, 0) + DARK_GREEN = (0, 100, 0) + GRAY = (200, 200, 200) if olpcgames.ACTIVITY: size = olpcgames.ACTIVITY.game_size screen = pygame.display.set_mode(size) @@ -32,31 +39,26 @@ def main(): pygame.display.set_caption("Hit the balls") clock = pygame.time.Clock() font = PangoFont(family='Helvetica', size=16, bold=True) - BLACK = (0, 0, 0) - BLUE = (0, 0, 255) - YELLOW = (255, 255, 0) - RED = (255, 0, 0) - GREEN = (0, 255, 0) - DARK_GREEN = (0, 100, 0) - GRAY = (200, 200, 200) + + time_bar = TimeBar(size[0], TIME_BAR_HEIGHT, DARK_GREEN, GRAY) + time_bar.start(1000, 1) + + balls_area = (0, TIME_BAR_HEIGHT, size[0], size[1]) the_balls = [Ball(font, BLACK, BLUE, - Operation(1000, 3000, OPER_MUL), (2, 1.2)), + Operation(1000, 3000, OPER_MUL), balls_area, (2, 1.2)), Ball(font, BLACK, YELLOW, - Operation(120, 45, OPER_SUB), (1.6,-0.4)), + Operation(120, 45, OPER_SUB), balls_area, (1.6,-0.4)), Ball(font, BLACK, RED, - Operation(9, 3, OPER_DIV), (-0.8, 1.6)), + Operation(9, 3, OPER_DIV), balls_area, (-0.8, 1.6)), Ball(font, BLACK, GREEN, - Operation(120, 240, OPER_ADD), (1.7, -1.2))] + Operation(120, 240, OPER_ADD), balls_area, (1.7, -1.2))] - the_balls[0].move_to((140, 170)) - the_balls[1].move_to((400, 300)) + the_balls[0].move_to((430, 200)) + the_balls[1].move_to((400, 360)) 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, 1) + the_balls[3].move_to((330, 100)) while True: screen.fill(BACKGROUND) |