Wednesday 2 April 2014

Object-oriented Design

I has been just quite recently that I have learned the principles of object-oriented design in so much details.

The idea is that you can break your program into objects and each object has its own perspective into the world. Suppose in the breaker game I talked about in the previous post, the game is divided into the following objects:

 Ball: has a width, height, dx (horizontal speed), and dy (vertical speed). It also has functions (methods) that detect whether or not the ball has had a collision with any given paddle. The ball does not know about paddle on its own but each time the main function passed a Paddle to this function and (literally) asks the ball if there has been any collision with that Paddle.

AIPaddle: Is a paddle that is controlled by AI (computer)and the user has no controls over its movements. just like ball, it has it's own width, height, and dx (no dy as the Paddles do not move vertically).

Paddle: I made this to be a child-class of AIPaddle. The reason for that was that regular paddles have all the features that AIPaddles have except that they can also (mainly) be controlled by the player.

No comments:

Post a Comment