diff options
author | Laurent Bernabé <laurent.bernabe@gmail.com> | 2013-09-20 17:52:16 (GMT) |
---|---|---|
committer | Laurent Bernabé <laurent.bernabe@gmail.com> | 2013-09-20 17:52:16 (GMT) |
commit | a664dd5a1ca7e878cc4ed0885dd3c8d6f8f7a441 (patch) | |
tree | aec43329a11655b56ff203cc616590c007270251 | |
parent | 89e8697333bd08a88d9bbb21dd7851b554b6549e (diff) |
Now calling move for a ball computes the next position of the ball for the next frame.
-rw-r--r-- | ball.py | 38 | ||||
-rw-r--r-- | elements_painter.py | 6 | ||||
-rw-r--r-- | main.py | 28 |
3 files changed, 40 insertions, 32 deletions
@@ -4,7 +4,7 @@ Created on Sat Sep 14 10:57:17 2013 @author: laurent-bernabe """ - +import pygame class Ball(object): @@ -12,19 +12,23 @@ class Ball(object): An abstractation of a ball. """ - def __init__(self, txt_font, txt_color, bg_color, operation): + def __init__(self, txt_font, txt_color, bg_color, operation, velocity): """ 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) """ + info = pygame.display.Info() + self._SCREEN_WIDTH, self._SCREEN_HEIGTH = info.current_w, 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 txt_size = txt_font.size(operation.get_text()) if txt_size[0] > txt_size[1]: max_txt_size = txt_size[0] @@ -72,7 +76,7 @@ class Ball(object): Accessor to the center position. => Tuple of 2 integers. """ - return self._center + return tuple([int(x) for x in self._center]) def move_to(self, new_center_position): """ @@ -81,10 +85,24 @@ class Ball(object): """ self._center = new_center_position - def move(self, move_value): - """ - Moves the ball by the values inside move_value. - move_value : by how much does it move ? => a tuple of 2 integers - """ - self._center = (self._center[0] + move_value[0], - self._center[1] + move_value[1]) + def move(self): + """ + Moves the ball by its current velocity. + Please notice that this velocity will change whenever the ball hits + a wall (a screen side). + """ + expected_new_center = (self._center[0] + self._velocity[0], + self._center[1] + self._velocity[1]) + 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): + 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_HEIGTH - radius): + self._velocity = (self._velocity[0], -self._velocity[1]) + self._center = (self._center[0] + self._velocity[0], + self._center[1] + self._velocity[1])
\ No newline at end of file diff --git a/elements_painter.py b/elements_painter.py index b1ab8a1..aab9aa1 100644 --- a/elements_painter.py +++ b/elements_painter.py @@ -17,12 +17,12 @@ def paint_ball(ball, surface): surface : the destination surface => PyGame.Surface """ pygame.draw.circle(surface, ball.get_bg_color(), ball.get_center(), - ball.get_diameter() / 2) + int(ball.get_diameter() / 2)) ball_center = ball.get_center() txt_width, txt_height = ball.get_txt_font().size(ball.get_operation(). get_text()) - txt_position = (ball_center[0] - txt_width / 2, - ball_center[1] - txt_height / 2) + txt_position = (int(ball_center[0] - txt_width / 2), + int(ball_center[1] - txt_height / 2)) txt_surface = ball.get_txt_font().render( ball.get_operation().get_text(), color=ball.get_txt_color()) surface.blit(txt_surface, txt_position) @@ -7,7 +7,7 @@ Created on Sat Sep 14 10:57:17 2013 import olpcgames import pygame -import sys +from sys import exit from olpcgames.pangofont import PangoFont from pygame.locals import QUIT from ball import * @@ -28,33 +28,23 @@ def main(): screen = pygame.display.set_mode(size) pygame.display.set_caption("Hit the balls") clock = pygame.time.Clock() - info = pygame.display.Info() - screen_size = info.current_w, info.current_h 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) - the_balls = [Ball(font, BLACK, BLUE, Operation(1000, 3000, OPER_MUL)), - Ball(font, BLACK, YELLOW, Operation(120, 45, OPER_SUB)), - Ball(font, BLACK, RED, Operation(9, 3, OPER_DIV)), - Ball(font, BLACK, GREEN, Operation(120, 240, OPER_ADD))] - the_balls[0].move((140, 170)) - the_balls[1].move((400, 300)) - the_balls[2].move((200, 80)) - the_balls[3].move((330, 70)) - + the_ball = Ball(font, BLACK, BLUE, Operation(1000, 3000, OPER_MUL), + (2, 1.2)) + the_ball.move_to((140, 170)) + while True: screen.fill(BACKGROUND) - for ball in the_balls: - paint_ball(ball, screen) - pygame.display.update() + paint_ball(the_ball, screen) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() - sys.exit() + exit() + pygame.display.update() clock.tick(FPS) + the_ball.move() if __name__ == "__main__": main() |