Breakout

프로세싱으로 만들어보았다.

교육용 샘플 정도로만 만들었기 때문에 더 이상 버전업은 하지 않을듯…

‘R’ 키를 누르면 재시작한다.

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
PVector pos;
PVector delta;
int paddle;
int nrows;
int ncols;
float bwidth;
float bheight;
boolean[][] bricks;
void setup() {
size(320, 240);
smooth();
colorMode(HSB);
pos = new PVector(width / 2, height / 2);
delta = new PVector(-2, -2);
paddle = 20;
nrows = 5;
ncols = 5;
bwidth = width / ncols;
bheight = 15;
bricks = new boolean[nrows][ncols];
reset();
}
void draw() {
// clear
background(#FFFFFF);
// hit wall ?
if (pos.x < 0 || pos.x > width) {
delta.x = -delta.x;
}
if (pos.y < 0) {
delta.y = -delta.y;
}
// hit paddle ?
if (pos.y > height) {
if (pos.x > mouseX - paddle && pos.x < mouseX + paddle) {
delta.y = -delta.y;
}
}
// hit brick ?
for (int r = 0; r < nrows; r++) {
for (int c = 0; c < ncols; c++) {
if (bricks[r][c]) {
if (pos.x > bwidth * c && pos.x < bwidth * (c + 1) &&
pos.y > bheight * r && pos.y < bheight * (r + 1)) {
// remove brick
bricks[r][c] = false;
// bound ball
delta.y = -delta.y;
}
}
}
}
// draw paddle
stroke(#000000);
strokeWeight(10);
line(mouseX - paddle, height, mouseX + paddle, height);
// draw ball
noStroke();
fill(#000000);
ellipse(pos.x, pos.y, 20, 20);
// draw bricks
stroke(#FFFFFF);
strokeWeight(1);
for (int r = 0; r < nrows; r++) {
fill(color(255.0 / nrows * r, 255, 255));
for (int c = 0; c < ncols; c++) {
if (bricks[r][c]) {
rect(bwidth * c, bheight * r, bwidth, bheight);
}
}
}
// move
pos.x += delta.x;
pos.y += delta.y;
}
void keyPressed() {
if(key == 'r' || key == 'R') {
reset();
}
}
void reset() {
// pos
pos.x = width / 2;
pos.y = height / 2;
// delta
delta.x = -2;
delta.y = -2;
// brick
for (int r = 0; r < nrows; r++) {
for (int c = 0; c < ncols; c++) {
bricks[r][c] = true;
}
}
}
Share Comments