Skip to content

clase BallSpeedAttributes #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions FallDown/src/FallDownEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
public class FallDownEngine
{
public static final double GRAVITY = .5;
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
public static final int WIDTH_SCREEN = 300;
public static final int HEIGHT_SCREEN = 300;
public static final int BRICK_LAYER_DELAY = 100;
public static final int SPEED_UP_DELAY = 20;

Expand All @@ -18,18 +18,18 @@ public class FallDownEngine

public FallDownEngine()
{
ball = new Ball(WIDTH/2, HEIGHT/2);
ball = new Ball(WIDTH_SCREEN/2, HEIGHT_SCREEN/2);
createBrickLayer();
}

public void createBrickLayer()
{
int hole = (int)((WIDTH/Brick.WIDTH)*Math.random());
for(int i = 0; i < (WIDTH/Brick.WIDTH); i++)
int hole = (int)((WIDTH_SCREEN/Brick.WIDTH_SCREEN)*Math.random());
for(int i = 0; i < (WIDTH_SCREEN/Brick.WIDTH_SCREEN); i++)
{
if(i != hole)
{
bricks.add(new Brick(i*Brick.WIDTH+Brick.WIDTH/2, HEIGHT+Brick.HEIGHT));
bricks.add(new Brick(i*Brick.WIDTH_SCREEN+Brick.WIDTH_SCREEN/2, HEIGHT_SCREEN+Brick.HEIGHT_SCREEN));
}
}
points++;
Expand Down Expand Up @@ -63,8 +63,8 @@ public void affectBall()
ball = bricks.get(i).affect(ball);
}
ball = ball.accelerate(0, GRAVITY);
if(ball.getLocation().getY() > HEIGHT)
ball = ball.setPosition((int)ball.getLocation().getX(), HEIGHT);
if(ball.getLocation().getY() > HEIGHT_SCREEN)
ball = ball.setPosition((int)ball.getLocation().getX(), HEIGHT_SCREEN);
}

public void moveLeft()
Expand All @@ -77,7 +77,7 @@ public void moveLeft()
public void moveRight()
{
ball = ball.moveRight();
while(ball.getLocation().getX() > WIDTH)
while(ball.getLocation().getX() > WIDTH_SCREEN)
ball = ball.moveLeft();
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public void draw(Graphics g)
if(ball.getLocation().getY() < -Ball.RADIUS)
{
g.setColor(Color.BLUE);
g.drawString("You Lose", WIDTH/2-27, HEIGHT/2);
g.drawString("You Lose", WIDTH_SCREEN/2-27, HEIGHT_SCREEN/2);
}
else
ball.draw(g);
Expand Down
112 changes: 58 additions & 54 deletions Pong/src/Ball.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,70 @@

public class Ball
{
public static final int RADIUS = 10;
public static final int RADIUS = 10;

private int x, y;
private int dx, dy;
private Color color;
private int speed;
private int speedUpDelay = 25;
private int delay = 0;
private int x, y;
private int dx, dy;
private Color color;

public Ball(Color c, int x, int y)
{
color = c;
this.x = x;
this.y = y;
dx = (Math.random() < .5)?1:-1;
dy = (Math.random() < .5)?1:-1;
speed = 4;
}
private BallSpeedAttributes attributesSpeed;

public void move()
{
delay = (delay+1)%speedUpDelay;
if(delay == 0)
speed++;
x+=(int)(getUnitDX()*speed);
y+=(int)(getUnitDY()*speed);
}
public Ball(Color c, int x, int y)
{
color = c;
this.x = x;
this.y = y;
dx = (Math.random() < .5) ? 1 : -1;
dy = (Math.random() < .5) ? 1 : -1;
attributesSpeed = new BallSpeedAttributes();
}

public Point getLocation()
{
return new Point(x,y);
}
public void move()
{
attributesSpeed.incrementDelay();
if (attributesSpeed.isTimeToSpeedUp())
attributesSpeed.incrementSpeed();
x += (int) (getUnitDX() * attributesSpeed.getSpeed());
y += (int) (getUnitDY() * attributesSpeed.getSpeed());
}

private double getUnitDX()
{
return ((double)dx/(double)(Math.sqrt(dx*dx + dy*dy)));
}
public int getX()
{
return x;
}

private double getUnitDY()
{
return ((double)dy/(double)(Math.sqrt(dx*dx + dy*dy)));
}
public int getY()
{
return y;
}

public void bounceSide()
{
dx = -dx;
dy = (int)(Math.random()*8)-4;
move();
move();
}
private double getUnitDX()
{
return ((double) dx / (double) (Math.sqrt(dx * dx + dy * dy)));
}

public void bounceTop()
{
dy = -dy;
move();
}
private double getUnitDY()
{
return ((double) dy / (double) (Math.sqrt(dx * dx + dy * dy)));
}

public void draw(Graphics g)
{
g.setColor(color);
g.fillOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2);
}
}
public void bounceSide()
{
dx = -dx;
dy = (int) (Math.random() * 8) - 4;
move();
move();
}

public void bounceTop()
{
dy = -dy;
move();
}

public void draw(Graphics g)
{
g.setColor(color);
g.fillOval(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2);
}
}
48 changes: 48 additions & 0 deletions Pong/src/BallSpeedAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
public class BallSpeedAttributes {
private int speed;
private int speedUpDelay;
private int delay;

public BallSpeedAttributes() {
speed = 0;
speedUpDelay = 25;
delay = 0;
}

public BallSpeedAttributes(int speed, int speedUpDelay, int delay) {
this.speed = speed;
this.speedUpDelay = speedUpDelay;
this.delay = delay;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
if (speed >= 0) {
this.speed = speed;
}
}

public int getSpeedUpDelay() {
return speedUpDelay;
}

public void setSpeedUpDelay(int speedUpDelay) {
if (speedUpDelay >= 0) {
this.speedUpDelay = speedUpDelay;
}
}

public int getDelay() {
return delay;
}

public void setDelay(int delay) {
if (delay >= 0) {
this.delay = delay;
}
}

}
4 changes: 2 additions & 2 deletions Pong/src/PongEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ else if(ball.getLocation().getX() > WIDTH)
leftScore();

if(leftComputer)
left.moveTo((int)ball.getLocation().getY());
left.moveTo((int)ball.getY());
if(rightComputer)
right.moveTo((int)ball.getLocation().getY());
right.moveTo((int)ball.getY());

}

Expand Down