Skip to content

Instantly share code, notes, and snippets.

@avh4
Last active August 29, 2015 14:23
Show Gist options
  • Save avh4/720ce0c2e7b1739c8a1f to your computer and use it in GitHub Desktop.
Save avh4/720ce0c2e7b1739c8a1f to your computer and use it in GitHub Desktop.
Cohesion / Coupling example 3
class Game {
int ballX = 0;
int ballY = 0;
int ballYSpeed = 3;
int paddleX = 30;
int paddleY = 500;
public void update() {
ballX = ballX + 5;
ballY = ballY + ballYSpeed;
if (ballX + 30 >= paddleX
&& ballX <= paddleX + 150
&& ballY + 30 >= paddleY
&& ballY <= paddleY + 12
) {
ballYSpeed = -3;
}
}
}
class Ball {
int x = 0;
int y = 0;
int ySpeed = 3;
public void move(Paddle paddle) {
x = x + 5;
y = y + ySpeed;
if (x + 30 >= paddle.x
&& x <= paddle.x + 150
&& y + 30 >= paddle.y
&& y <= paddle.y + 12
) {
ySpeed = -3;
}
}
}
class Paddle {
int x = 30;
int y = 500;
}
class Game {
Ball ball = new Ball();
Paddle paddle = new Paddle();
public void update() {
ball.move(paddle);
}
}
class Ball {
int x = 0;
int y = 0;
int ySpeed = 3;
Paddle paddle = new Paddle();
public void move() {
x = x + 5;
y = y + ySpeed;
if (x + 30 >= paddle.x
&& x <= paddle.x + 150
&& y + 30 >= paddle.y
&& y <= paddle.y + 12
) {
ySpeed = -3;
}
}
}
class Paddle {
int x = 30;
int y = 500;
}
class Game {
Ball ball = new Ball();
public void update() {
ball.move();
}
}
class Ball {
int x = 0;
int y = 0;
int ySpeed = 3;
public void move(Paddle paddle) {
x = x + 5;
y = y + ySpeed;
}
public void bounceUp() {
ySpeed = -3;
}
}
class Paddle {
int x = 30;
int y = 500;
}
class Game {
Ball ball = new Ball();
Paddle paddle = new Paddle();
public void update() {
ball.move(paddle);
if (ball.x + 30 >= paddle.x
&& ball.x <= paddle.x + 150
&& ball.y + 30 >= paddle.y
&& ball.y <= paddle.y + 12
) {
ball.bounceUp();
}
}
}
class Ball {
int x = 0;
int y = 0;
int ySpeed = 3;
public void move(Paddle paddle) {
x = x + 5;
y = y + ySpeed;
}
public void bounceUp() {
ySpeed = -3;
}
public Rectangle getBounds() {
return new Rectangle(x, y, 30, 30);
}
}
class Paddle {
int x = 30;
int y = 500;
public Rectangle getBounds() {
return new Rectangle(x, y, 150, 12);
}
}
class Game {
Ball ball = new Ball();
Paddle paddle = new Paddle();
public void update() {
ball.move(paddle);
Rectangle ballBounds = ball.getBounds();
Rectangle paddleBounds = paddle.getBounds();
if (ballBounds.intersects(paddleBounds)) {
ball.bounceUp();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment