-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFallDownEngine.java
More file actions
152 lines (136 loc) · 3.23 KB
/
FallDownEngine.java
File metadata and controls
152 lines (136 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.awt.*;
import java.util.*;
import javax.swing.*;
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 BRICK_LAYER_DELAY = 100;
public static final int SPEED_UP_DELAY = 70;
private Vector<Brick> bricks = new Vector();
private Ball ball;
private int brickSpeed = 2;
private int brickDelay = 0;
private int speedDelay = 0;
private int points = -1;
public FallDownEngine()
{
ball = new Ball(WIDTH/2, HEIGHT/2);
createBrickLayer();
}
public void createBrickLayer()
{
int hole = (int)((WIDTH/Brick.WIDTH)*Math.random());
for(int i = 0; i < (WIDTH/Brick.WIDTH); i++)
{
if(i != hole)
{
bricks.add(new Brick(i*Brick.WIDTH+Brick.WIDTH/2, HEIGHT+Brick.HEIGHT));
}
}
points++;
}
public void removeOldBricks()
{
for(int i = 0; i < bricks.size(); i++)
{
if(bricks.get(i).getLocation().getY() < 0)
{
bricks.remove(i);
i--;
}
}
}
public void moveBricks()
{
for(int i = 0; i < bricks.size(); i++)
{
bricks.add(i, bricks.get(i).move(0,-brickSpeed));
bricks.remove(i+1);
}
}
public void affectBall()
{
for(int i = 0; i < bricks.size(); i++)
{
ball = bricks.get(i).affect(ball);
}
ball = ball.accelerate(0, GRAVITY);
if(ball.getLocation().getY() > HEIGHT)
ball = ball.setPosition((int)ball.getLocation().getX(), HEIGHT);
}
public void moveLeft()
{
ball = ball.moveLeft();
while(ball.getLocation().getX() < 0)
ball = ball.moveRight();
}
public void moveRight()
{
ball = ball.moveRight();
while(ball.getLocation().getX() > WIDTH)
ball = ball.moveLeft();
}
public void update()
{
if(ball.getLocation().getY() >= -Ball.RADIUS)
{
ball = ball.move();
moveBricks();
removeOldBricks();
brickDelay = brickDelay+brickSpeed;
if(brickDelay > BRICK_LAYER_DELAY)
{
brickDelay = 0;
speedDelay++;
if(speedDelay > SPEED_UP_DELAY)
{
speedDelay = 0;
brickSpeed++;
}
createBrickLayer();
}
affectBall();
}
}
public void draw(Graphics g)
{
if(ball.getLocation().getY() < -Ball.RADIUS)
{
g.setColor(Color.BLUE);
g.drawString("You Lose", WIDTH/2-27, HEIGHT/2);
}
else
ball.draw(g);
for(int i = 0; i < bricks.size(); i++)
bricks.get(i).draw(g);
g.setColor(Color.BLUE);
g.drawString("Points: "+points, 10, 20);
}
public void draw(Graphics g, FallDownComponent component)
{
if (ball.getLocation().getY() < -Ball.RADIUS) {
g.setColor(Color.BLUE);
g.drawString("You Lose", WIDTH / 2 - 27, HEIGHT / 2);
// Create and add buttons
JButton replayButton = new JButton("Replay");
JButton quitButton = new JButton("Quit");
replayButton.setBounds(WIDTH / 2 - 50, HEIGHT / 2 + 30, 100, 30);
quitButton.setBounds(WIDTH / 2 - 50, HEIGHT / 2 + 70, 100, 30);
replayButton.addActionListener(e -> component.resetGame());
quitButton.addActionListener(e -> System.exit(0));
component.add(replayButton);
component.add(quitButton);
component.revalidate();
component.repaint();
} else {
ball.draw(g);
for (int i = 0; i < bricks.size(); i++) {
bricks.get(i).draw(g);
}
g.setColor(Color.BLUE);
g.drawString("Points: " + points, 10, 20);
}
}
}