From 68ea580445e91018e817193419da90b895c3808b Mon Sep 17 00:00:00 2001 From: mamomeri Date: Fri, 18 Aug 2023 15:10:13 -0500 Subject: [PATCH 1/3] clase BallSpeedAttributes --- Pong/src/Ball.java | 7 +++-- Pong/src/BallSpeedAttributes.java | 48 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 Pong/src/BallSpeedAttributes.java diff --git a/Pong/src/Ball.java b/Pong/src/Ball.java index beaaf6d..39c4e9b 100644 --- a/Pong/src/Ball.java +++ b/Pong/src/Ball.java @@ -7,9 +7,9 @@ public class Ball private int x, y; private int dx, dy; private Color color; - private int speed; - private int speedUpDelay = 25; - private int delay = 0; + + private BallSpeedAttributes attributesSpeed; + public Ball(Color c, int x, int y) { @@ -19,6 +19,7 @@ public Ball(Color c, int x, int y) dx = (Math.random() < .5)?1:-1; dy = (Math.random() < .5)?1:-1; speed = 4; + attributesSpeed = new BallSpeedAttributes(); } public void move() diff --git a/Pong/src/BallSpeedAttributes.java b/Pong/src/BallSpeedAttributes.java new file mode 100644 index 0000000..dfd5d81 --- /dev/null +++ b/Pong/src/BallSpeedAttributes.java @@ -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; + } + } + +} From 627b5bffbaa43d790907baa7b6f0acabff5c680d Mon Sep 17 00:00:00 2001 From: mamomeri Date: Fri, 18 Aug 2023 18:13:45 -0500 Subject: [PATCH 2/3] chain messages --- Pong/src/Ball.java | 113 +++++++++++++++++----------------- Pong/src/PongEnvironment.java | 4 +- 2 files changed, 60 insertions(+), 57 deletions(-) diff --git a/Pong/src/Ball.java b/Pong/src/Ball.java index 39c4e9b..844de37 100644 --- a/Pong/src/Ball.java +++ b/Pong/src/Ball.java @@ -2,67 +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 BallSpeedAttributes attributesSpeed; - + 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; - attributesSpeed = new BallSpeedAttributes(); - } + 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); - } -} \ No newline at end of file + 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); + } +} diff --git a/Pong/src/PongEnvironment.java b/Pong/src/PongEnvironment.java index 39f26d7..2218066 100644 --- a/Pong/src/PongEnvironment.java +++ b/Pong/src/PongEnvironment.java @@ -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()); } From aa7f1557b47af0676d7f9e3f8d21a005995b1b56 Mon Sep 17 00:00:00 2001 From: mamomeri Date: Fri, 18 Aug 2023 20:47:04 -0500 Subject: [PATCH 3/3] magic number corregido --- FallDown/src/FallDownEngine.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/FallDown/src/FallDownEngine.java b/FallDown/src/FallDownEngine.java index df3fa1f..37ab1c0 100644 --- a/FallDown/src/FallDownEngine.java +++ b/FallDown/src/FallDownEngine.java @@ -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; @@ -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++; @@ -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() @@ -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(); } @@ -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);