Created
August 16, 2020 10:35
-
-
Save ryuchan00/02010e83bfa9b14fdbd166240540828c to your computer and use it in GitHub Desktop.
ブロック崩し
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final int numBlocksCols = 5, numBlocksRows = 10; | |
final int numBlocks = numBlocksCols * numBlocksRows; | |
final int blockWidth = 30, blockHeight = 10, space = 2; | |
final int padWidth = 40, padHeight = 10; | |
final color std_colors[] = {color(255, 0, 0) // red | |
, color(255, 0, 255) // magenta | |
, color(255, 255, 0) // yellow | |
, color(0, 255, 255) // cyan | |
, color(0, 255, 0) // green | |
}; | |
final color padColor = color(200, 255, 200); // paleGreen | |
Block[][] blocks; | |
Pad pad; | |
int w = 40; | |
int h = 10; | |
int d = 20, y = 0, x, vx = 0, vy = 0, r = d / 2; | |
void setup() { | |
size(322, 400); | |
noStroke(); | |
blocks = new Block[numBlocksCols][numBlocksRows]; | |
for(int j = 0; j < numBlocksCols; j++){ | |
for(int i = 0; i < numBlocksRows; i++){ | |
int x = space * (i + 1) + blockWidth * i; | |
int y = space * (j + 1) + blockHeight * j; | |
blocks[j][i] = new Block(std_colors[j], x, y, blockWidth, blockHeight); | |
} | |
} | |
pad = new Pad(padColor, width / 2, height - padHeight, padWidth, padHeight); | |
ellipseMode(CORNER); | |
x = width / 2 - r; | |
ellipse(x, d / 2, d, d); | |
frameRate(240); | |
} | |
void draw() { | |
background(255); | |
for(int i = 0; i < numBlocks; i++) | |
blocks[int(i / numBlocksRows)][i % numBlocksRows].draw(); | |
pad.move(width); | |
ellipse(x, y, d, d); | |
x += vx; | |
y += vy; | |
if (x < 0 || x + d > width) { | |
vx *= -1; | |
if (x < 0) { | |
x *= -1; | |
} else if (x + d > width) { | |
x = 2 * (width - d) - x; | |
} | |
} | |
if (y < 0 || (y + d == height - h && (x + r <= mouseX + (w / 2) && x + r >= mouseX - (w / 2)))) { | |
vy *= -1; | |
if (y < 0) { | |
y *= -1; | |
} else if (y + d == height - h && (x + r <= mouseX + w / 2 && x + r >= mouseX - w / 2 )) { | |
y = 2 * (height - h - d) - y; | |
} | |
} | |
} | |
void mouseClicked() { | |
x = width / 2 - r; | |
y = 0; | |
fill(255, 0, 0); | |
ellipse(x, y, d, d); | |
vx = 1; | |
vy = 1; | |
} | |
class Block { | |
private color c; // block color | |
protected int x, y, w, h; // position:(x, y), size:(w, h) | |
Block() {}; // constructor | |
Block(color c, int x, int y, int w, int h){ // constructor | |
setColor(c); | |
setPosition(x, y); | |
setSize(w, h); | |
} | |
public void setColor(color c){ this.c = c; } | |
public void setPosition(int x, int y){ this.x = x; this.y = y; } | |
public void setSize(int w, int h){ this.w = w; this.h = h; } | |
public void draw(){ | |
fill(c); | |
rect(x, y, w, h); | |
} | |
} | |
class Pad extends Block { // c, x, y, w and h are inheritted from Block Class | |
Pad(){}; // constructor | |
Pad(color c, int x, int y, int w, int h){ // constructor | |
setColor(c); | |
setPosition(x, y); | |
setSize(w, h); | |
} | |
// setColor, setSize, setPosition and draw are also inheritted from Block Class | |
boolean collideLeftWall(){ return x < 0; } | |
boolean collideRightWall(int w){ return x + this.w > w; } | |
public void move(int w){ | |
x = mouseX - this.w / 2; | |
if(collideLeftWall()) x = 0; | |
else if(collideRightWall(w)) x = w - this.w; | |
draw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment