Skip to content

Instantly share code, notes, and snippets.

@cleemy-desu-wayo
Created October 26, 2025 05:22
study20251026 (study in Processing 4)

study20251026 (study in Processing 4)

screen shot of three_games

this is just a study in Processing 4
ただの習作です

  • study20251026_hitsuji
    This is a sheep herding game. You only use the mouse pointer to move. Control the cat to herd the sheep. Clear the game by gathering all the sheep in one spot anywhere.
    羊飼いゲーム(羊追いゲーム)です。使用するのはマウスポインタの移動のみです。猫を操作して羊を追い立ててください。どこでもいいので、羊を一箇所に集めることができたらゲームクリアです。
  • study20251026_tetris
    This is a falling block puzzle game similar to Tetris, using only the up, down, left and right keys.
    テトリスに似た落ちものパズルです。上下左右のキーのみを使用します。
  • study20251026_shooting
    This is a 2D shooting game. Use the up, down, left, right and space keys.
    2Dシューティングゲームです。上下左右とスペースキーを使用します。

images

Images can be downloaded from the following link: 画像は以下よりダウンロード可能:

/*
* study20251026_hitsuji
* written by cleemy desu wayo / Licensed under CC0 1.0
* 2025-10-26
* ----
* this is just a study in Processing
*
* the following preparations are required:
* 1. save daniel-j-schwarz-8RRVfAU0dZ8-unsplash.jpg from https://unsplash.com/photos/8RRVfAU0dZ8
* 2. save publicdomainq-0060985ceffwm.png from https://publicdomainq.net/cat-animal-0060985/
* 3. change the line that initializes fontName (if necessary)
*
* ----
* how to play
* You only use the mouse pointer to move. Control the cat to herd the sheep.
* Clear the game by gathering all the sheep in one spot anywhere.
*
*/
PGraphics pgBg;
PGraphics pgCat;
String fontName = "Noto Sans Mono"; // change this line appropriately
String timeStr;
int drawCnt = 0;
int afterClearCnt = 0;
int fieldWidth = 600;
int fieldHeight = 600;
int fieldMarginX = 40;
int fieldMarginY = 40;
int catWidth = 76;
int catHeight = 48;
int gameMode = 0; // 0:normal 1:clear
int sheepNum = 80;
int sheepSize = 14;
int avoidDistance = 50;
int clearDistance = 25;
float firstFrameTime = -1;
Sheep[] sheeps = new Sheep[sheepNum];
class Sheep {
float x;
float y;
float moveX;
float moveY;
int avoidCnt;
Sheep() {
this.x = fieldMarginX + int(random(fieldWidth - fieldMarginX * 2));
this.y = fieldMarginY + int(random(fieldHeight - fieldMarginY * 2));
this.setMove();
}
void setMove() {
this.moveX = random(1) / 6;
if (int(random(2)) == 0) {
this.moveX = 0 - this.moveX;
}
this.moveY = random(1) / 6;
if (int(random(2)) == 0) {
this.moveY = 0 - this.moveY;
}
}
void move() {
if (avoidCnt > 0) {
avoidCnt--;
if (avoidCnt == 0) {
this.setMove();
}
}
this.x += this.moveX;
if (this.x > fieldWidth - fieldMarginX) {
this.moveX = 0 - random(1) / 6;
} else if (this.x < fieldMarginX) {
this.moveX = random(1) / 6;
}
this.y += this.moveY;
if (this.y > fieldHeight - fieldMarginY) {
this.moveY = 0 - random(1) / 6;
} else if (this.y < fieldMarginY) {
this.moveY = random(1) / 6;
}
if (this.x < fieldMarginX) {
this.x = fieldMarginX;
} else if (this.x > fieldWidth - fieldMarginX) {
this.x = fieldWidth - fieldMarginX;
}
if (this.y < fieldMarginY) {
this.y = fieldMarginY;
} else if (this.y > fieldHeight - fieldMarginY) {
this.y = fieldHeight - fieldMarginY;
}
}
void avoidCat() {
if (abs(this.y - mouseY) < avoidDistance) {
if (this.x >= mouseX && (this.x - mouseX) < avoidDistance) {
this.moveX = 0.2 + random(1) / 6;
avoidCnt = 80;
} else if (this.x < mouseX && (mouseX - this.x) < avoidDistance) {
this.moveX = -0.2 - random(1) / 6;
avoidCnt = 80;
}
}
if (abs(this.x - mouseX) < avoidDistance) {
if (this.y >= mouseY && (this.y - mouseY) < avoidDistance) {
this.moveY = 0.2 + random(1) / 6;
avoidCnt = 80;
} else if (this.y < mouseY && (mouseY - this.y) < avoidDistance) {
this.moveY = -0.2 - random(1) / 6;
avoidCnt = 80;
}
}
}
}
void setup() {
size(600, 600);
PFont pfont = createFont(fontName, 18, true);
textFont(pfont);
PImage bg = loadImage("daniel-j-schwarz-8RRVfAU0dZ8-unsplash.jpg");
pgBg = createGraphics(600, 900);
pgBg.beginDraw();
pgBg.image(bg, 0, -220, 600, 900);
pgBg.endDraw();
PImage imgCat = loadImage("publicdomainq-0060985ceffwm.png");
pgCat = createGraphics(catWidth, catHeight);
pgCat.beginDraw();
pgCat.image(imgCat, 0, 0, catWidth, catHeight);
pgCat.endDraw();
noStroke();
stroke(0, 90);
noCursor();
frameRate(60);
for (int i = 0; i < sheepNum; i++) {
sheeps[i] = new Sheep();
}
}
void draw() {
float minX = fieldWidth;
float maxX = 0;
float minY = fieldHeight;
float maxY = 0;
drawCnt++;
if (firstFrameTime < 0) { firstFrameTime = millis(); println("init time: " + firstFrameTime); }
if (gameMode == 0 ||
(gameMode == 1 && afterClearCnt > 3 && afterClearCnt < 6) ||
(gameMode == 1 && afterClearCnt > 9 && afterClearCnt < 12) ||
(gameMode == 1 && afterClearCnt > 15 && afterClearCnt < 18) ||
(gameMode == 1 && afterClearCnt > 21)) {
image(pgBg, 0, 0);
image(pgCat, mouseX - int(catWidth / 2), mouseY - int(catHeight / 2));
} else {
background(0);
}
if (gameMode == 0) {
timeStr = "Time: " + nf((millis() - firstFrameTime) / 1000.0, 1, 3) + " sec";
}
fill(0);
text(timeStr, fieldWidth - 239, 21);
fill(255);
text(timeStr, fieldWidth - 240, 20);
for (int i = 0; i < sheepNum; i++) {
sheeps[i].avoidCat();
sheeps[i].move();
// for judgeing whether the game is cleared
if (minX > sheeps[i].x) { minX = sheeps[i].x; }
if (maxX < sheeps[i].x) { maxX = sheeps[i].x; }
if (minY > sheeps[i].y) { minY = sheeps[i].y; }
if (maxY < sheeps[i].y) { maxY = sheeps[i].y; }
}
// game clear
if (maxX - minX < clearDistance && maxY - minY < clearDistance) {
gameMode = 1;
}
// draw all sheep
fill(255);
for (int i = 0; i < sheepNum; i++) {
circle(sheeps[i].x, sheeps[i].y, sheepSize);
}
if (gameMode == 1) {
fill(0);
text("GAME CLEAR!!!", fieldWidth - 239, 51);
fill(255);
text("GAME CLEAR!!!", fieldWidth - 240, 50);
afterClearCnt++;
}
}
/*
* study20251026_shooting
* written by cleemy desu wayo / Licensed under CC0 1.0
* 2025-10-26
* ----
* this is just a study in Processing
*
* the following preparations are required:
* 1. save nasa-Q1p7bh3SHj8-unsplash.jpg from https://unsplash.com/photos/Q1p7bh3SHj8
* 2. save publicdomainq-0060985ceffwm.png from https://publicdomainq.net/cat-animal-0060985/
* 3. change the line that initializes fontName (if necessary)
*
* ----
* possibly the updated version is here:
* https://gist.github.com/cleemy-desu-wayo
*
* ----
* how to play
* LEFT key: move left
* RIGHT key: move right
* UP key: move up
* DOWN key: move down
* SPACE key: fire
*
*/
PGraphics pgBg;
PGraphics pgCat;
String fontName = "Noto Sans Mono"; // change this line appropriately
String timeStr;
int drawCnt = 0;
int afterClearCnt = 0;
int fieldWidth = 800;
int fieldHeight = 600;
int fieldMarginX = 0;
int fieldMarginY = 40;
int gameMode = 0; // 0: normal 1: clear 2: game over
Cat cat = new Cat();
int sheepNum = 40;
int sheepSize = 28;
int avoidDistance = 220;
int clearDistance = 25;
Sheep[] sheeps = new Sheep[sheepNum];
float firstFrameTime = -1;
class Cat {
float x;
float y;
int width = 76;
int height = 48;
int moveSpeed = 4;
Boolean moveLeft = false;
Boolean moveRight = false;
Boolean moveUp = false;
Boolean moveDown = false;
int maxPower = 10;
int power = maxPower;
float fireX;
float fireY;
float fireLength = 40;
float fireSpeed = 1.4;
Boolean fireAlive = false;
Cat() {
this.x = 800 / 2 - this.width / 2;
this.y = 500;
}
void move() {
if (this.moveLeft) {
this.x -= this.moveSpeed;
}
if (this.moveRight) {
this.x += this.moveSpeed;
}
if (this.moveUp) {
this.y -= this.moveSpeed;
}
if (this.moveDown) {
this.y += this.moveSpeed;
}
if (this.x < 2) {
this.x = 2;
}
if (this.x > fieldWidth - cat.width - fireSpeed) {
this.x = fieldWidth - cat.width - fireSpeed;
}
if (this.y < 2) {
this.y = 2;
}
if (this.y > fieldHeight - cat.height - fireSpeed) {
this.y = fieldHeight - cat.height - fireSpeed;
}
this.moveFire();
this.fireCollisionCheck();
}
void fire() {
if (this.fireAlive) {
return;
}
this.fireAlive = true;
this.fireX = this.x + this.width / 2;
this.fireY = this.y - fireLength;
}
void moveFire() {
// move fire
if (fireAlive) {
this.fireY = this.fireY - 2;
if (this.fireX < 0) {
this.fireAlive = false;
}
if (this.fireY < 0) {
this.fireAlive = false;
}
}
}
void fireCollisionCheck() {
if (fireAlive) {
for (int i = 0; i < sheepNum; i++) {
if (abs(this.fireX - sheeps[i].x) < sheepSize / 2 &&
(this.fireY <= sheeps[i].y + sheepSize / 2 &&
this.fireY >= sheeps[i].y - this.fireLength)) {
sheeps[i].kill();
this.fireAlive = false;
break;
}
}
}
}
}
class Sheep {
Boolean alive = true;
float x;
float y;
float moveX;
float moveY;
int avoidCnt;
Bullet bullet = new Bullet();
Sheep() {
this.x = fieldMarginX + int(random(fieldWidth - fieldMarginX * 2));
this.y = fieldMarginY + int(random(fieldHeight - fieldMarginY * 6));
this.setMove();
}
void setMove() {
this.moveX = random(1);
if (int(random(2)) == 0) {
this.moveX = 0 - this.moveX;
}
this.moveY = 0.5 + random(1);
if (int(random(2)) == 0) {
this.moveY = 0 - this.moveY;
}
}
void move() {
if (avoidCnt > 0) {
avoidCnt--;
if (avoidCnt == 0) {
this.setMove();
}
}
this.x += this.moveX;
if (this.x > fieldWidth - fieldMarginX) {
this.x = 0;
} else if (this.x < fieldMarginX) {
this.x = fieldWidth - fieldMarginX;
}
this.y += this.moveY;
if (this.y > fieldHeight - fieldMarginY * 5) {
this.moveY = 0 - random(1);
} else if (this.y < fieldMarginY) {
this.moveY = random(1);
}
if (this.x < fieldMarginX) {
this.x = fieldMarginX;
} else if (this.x > fieldWidth - fieldMarginX) {
this.x = fieldWidth - fieldMarginX;
}
if (this.y < fieldMarginY) {
this.y = fieldMarginY;
} else if (this.y > fieldHeight - fieldMarginY * 5) {
this.y = fieldHeight - fieldMarginY * 5;
}
}
void avoidCat() {
if (abs(this.y - cat.y) < avoidDistance) {
if (this.x >= cat.x && (this.x - cat.x) < avoidDistance) {
this.moveX = 1.4 + random(1) * 2;
avoidCnt = 20;
} else if (this.x < cat.x && (cat.x - this.x) < avoidDistance) {
this.moveX = -1.4 - random(1) * 2;
avoidCnt = 20;
}
}
if (abs(this.x - cat.x) < avoidDistance) {
if (this.y >= cat.y && (this.y - cat.y) < avoidDistance) {
this.moveY = 0.5 + random(1) / 3;
avoidCnt = 20;
} else if (this.y < cat.y && (cat.y - this.y) < avoidDistance) {
this.moveY = -0.5 - random(1) / 3;
avoidCnt = 20;
}
}
}
void fire(float targetX, float targetY) {
if (this.bullet.alive) {
return;
}
this.bullet.x = this.x;
this.bullet.y = this.y;
this.bullet.speedX = (targetX - x) / 80;
this.bullet.speedY = (targetY - y) / 80;
this.bullet.alive = true;
}
void moveBullet() {
this.bullet.move();
}
void bulletCollisionCheck() {
}
void kill() {
this.x = -100;
this.y = -100;
this.moveX = 0;
this.moveY = 0;
this.alive = false;
}
}
// bullets of enemy(sheep)
class Bullet {
Boolean alive = false;
float x;
float y;
float speedX = 12;
float speedY = 0;
float size = 10;
void move() {
this.x += this.speedX;
this.y += this.speedY;
if (this.x < 0 || this.x > fieldWidth || this.y < 0 || this.y > fieldHeight) {
this.alive = false;
}
}
}
void setup() {
size(800, 600);
PFont pfont = createFont(fontName, 18, true);
textFont(pfont);
PImage bg = loadImage("nasa-Q1p7bh3SHj8-unsplash.jpg");
pgBg = createGraphics(902, 600);
pgBg.beginDraw();
pgBg.image(bg, -50, 0, 902, 600);
pgBg.endDraw();
PImage imgCat = loadImage("publicdomainq-0060985ceffwm.png");
pgCat = createGraphics(cat.width, cat.height);
pgCat.beginDraw();
pgCat.image(imgCat, 0, 0, cat.width, cat.height);
pgCat.endDraw();
noStroke();
stroke(0, 90);
frameRate(60);
for (int i = 0; i < sheepNum; i++) {
sheeps[i] = new Sheep();
}
}
void draw() {
Boolean isGameCleared;
int sheepAliveCnt = 0;
drawCnt++;
if (firstFrameTime < 0) {
firstFrameTime = millis();
println("init time: " + firstFrameTime);
}
image(pgBg, 0, 0);
if (gameMode == 0 || gameMode == 1) {
cat.move();
image(pgCat, cat.x, cat.y);
if (cat.fireAlive) {
fill(255, 127, 127);
rect(cat.fireX, cat.fireY, 4, cat.fireLength);
}
}
if (gameMode == 0) {
timeStr = "Time: " + nf((millis() - firstFrameTime) / 1000.0, 1, 3) + " sec";
}
fill(0);
text(timeStr, fieldWidth - 239, 21);
fill(255);
text(timeStr, fieldWidth - 240, 20);
if (gameMode == 0) {
isGameCleared = true;
// move sheep
for (int i = 0; i < sheepNum; i++) {
if (sheeps[i].alive) {
sheeps[i].avoidCat();
sheeps[i].move();
sheepAliveCnt++;
isGameCleared = false;
}
}
// game clear
if (isGameCleared) {
gameMode = 1;
}
}
if (gameMode == 0 && drawCnt > 600) {
// fire by sheep
for (int i = 0; i < sheepNum; i++) {
if (sheeps[i].alive) {
if (int(random((sheepAliveCnt * sheepAliveCnt))) == 0) {
sheeps[i].fire(cat.x + cat.width / 2, cat.y + cat.height / 2);
}
}
}
}
// move bullets
for (int i = 0; i < sheepNum; i++) {
sheeps[i].moveBullet();
}
// check bullets collision
if (gameMode == 0) {
for (int i = 0; i < sheepNum; i++) {
if (sheeps[i].bullet.alive) {
if (sheeps[i].bullet.x > cat.x &&
sheeps[i].bullet.x < cat.x + cat.width &&
sheeps[i].bullet.y > cat.y &&
sheeps[i].bullet.y < cat.y + cat.height) {
println("i: " + i + " bullet.x: " + sheeps[i].bullet.x + " bullet.y: " + sheeps[i].bullet.y);
sheeps[i].bullet.alive = false;
cat.power -= 1;
if (cat.power < 1) { gameMode = 2; break; }
}
}
}
}
// draw all sheep
fill(255);
for (int i = 0; i < sheepNum; i++) {
if (sheeps[i].alive) {
circle(sheeps[i].x, sheeps[i].y, sheepSize);
}
}
// draw all bullets
if (gameMode == 0) {
fill(222, 222, 255);
for (int i = 0; i < sheepNum; i++) {
if (sheeps[i].bullet.alive) {
circle(sheeps[i].bullet.x, sheeps[i].bullet.y, sheeps[i].bullet.size);
}
}
}
drawPowerGage();
if (gameMode == 1) {
fill(0);
text("GAME CLEAR!!!", fieldWidth - 239, 51);
fill(255);
text("GAME CLEAR!!!", fieldWidth - 240, 50);
afterClearCnt++;
} else if (gameMode == 2) {
fill(0);
text("GAME OVER!!!", fieldWidth - 239, 51);
fill(255);
text("GAME OVER!!!", fieldWidth - 240, 50);
}
}
void drawPowerGage() {
int areaMarginX = 45;
int areaMarginY = 10;
int gageWidth = 18;
int gageHeight = 48;
int margin = 6;
for (int i = 1; i <= cat.power; i++) {
if (i <= 3) {
fill(255, 0, 0);
} else if (i <= 5) {
fill(255, 255, 0);
} else {
fill(0, 255, 0);
}
rect(areaMarginX + (gageWidth + margin) * (i - 1),
areaMarginY,
gageWidth,
gageHeight,
5);
}
}
void keyPressed() {
if (key == ' ') {
cat.fire();
} else if (keyCode == LEFT) {
cat.moveLeft = true;
} else if (keyCode == RIGHT) {
cat.moveRight = true;
} else if (keyCode == UP) {
cat.moveUp = true;
} else if (keyCode == DOWN) {
cat.moveDown = true;
}
}
void keyReleased() {
if (keyCode == LEFT) {
cat.moveLeft = false;
} else if (keyCode == RIGHT) {
cat.moveRight = false;
} else if (keyCode == UP) {
cat.moveUp = false;
} else if (keyCode == DOWN) {
cat.moveDown = false;
}
}
/*
* study20251026_tetris
* written by cleemy desu wayo / Licensed under CC0 1.0
* 2025-10-26
* ----
* this is just a study in Processing
*
* the following preparations are required:
* save cassidy-james-blaede-UBcXr0DcTAY-unsplash.jpg from https://unsplash.com/photos/UBcXr0DcTAY
*
* ----
* possibly the updated version is here:
* https://gist.github.com/cleemy-desu-wayo
*
* ----
* how to play
* LEFT key: move left
* RIGHT key: move right
* UP key: ratate
* DOWN key: move down
*
*/
PGraphics pg;
int drawCnt = 0;
int lastRedraw = -1;
int leftMargin = 362;
int topMargin = 115;
int blockSize = 20;
int blockMargin = 3;
int minoRotate;
int minoX; // X position
int minoY; // Y position
int stepInterval = 12;
int lastMinoRotate = -1;
int lastMinoX = -1;
int lastMinoY = -1;
int matrixSizeX = 14;
int matrixSizeY = 22;
int[][] blockMatrix = new int[matrixSizeX][matrixSizeY];
int mino;
int nextMino;
int[][][][] minoBlock = {
// 0: T
{{{0, 0}, {-1, 0}, {0, -1}, {1, 0}},
{{0, 0}, {0, -1}, {1, 0}, {0, 1}},
{{0, 0}, {-1, 0}, {0, 1}, {1, 0}},
{{0, 0}, {-1, 0}, {0, -1}, {0, 1}}},
// 1: I
{{{0, 0}, {-1, 0}, {1, 0}, {2, 0}},
{{0, 0}, {0, -1}, {0, 1}, {0, 2}},
/*{{0, 0}, {-2, 0}, {-1, 0}, {1, 0}},
{{0, 0}, {0, 1}, {0, -1}, {0, -2}}},*/
{{0, 0}, {-1, 0}, {1, 0}, {2, 0}},
{{0, 0}, {0, -1}, {0, 1}, {0, 2}}},
// 2: O
{{{0, 0}, {1, 0}, {0, -1}, {1, -1}},
{{0, 0}, {1, 0}, {0, -1}, {1, -1}},
{{0, 0}, {1, 0}, {0, -1}, {1, -1}},
{{0, 0}, {1, 0}, {0, -1}, {1, -1}}},
// 3: S
{{{0, 0}, {-1, 0}, {0, -1}, {1, -1}},
{{0, 0}, {0, -1}, {1, 0}, {1, 1}},
{{0, 0}, {-1, 0}, {0, -1}, {1, -1}},
{{0, 0}, {0, -1}, {1, 0}, {1, 1}}},
// 4: Z
{{{0, 0}, {-1, -1}, {0, -1}, {1, 0}},
{{0, 0}, {1, -1}, {1, 0}, {0, 1}},
{{0, 0}, {-1, -1}, {0, -1}, {1, 0}},
{{0, 0}, {1, -1}, {1, 0}, {0, 1}}},
// 5: J
{{{0, 0}, {-1, -1}, {-1, 0}, {1, 0}},
{{0, 0}, {1, -1}, {0, -1}, {0, 1}},
{{0, 0}, {-1, 0}, {1, 0}, {1, 1}},
{{0, 0}, {0, -1}, {0, 1}, {-1, 1}}},
// 6: L
{{{0, 0}, {-1, 0}, {1, 0}, {1, -1}},
{{0, 0}, {0, -1}, {0, 1}, {1, 1}},
{{0, 0}, {-1, 1}, {-1, 0}, {1, 0}},
{{0, 0}, {-1, -1}, {0, -1}, {0, 1}}}
};
void setup() {
size(1280, 720);
PImage bg = loadImage("cassidy-james-blaede-UBcXr0DcTAY-unsplash.jpg");
pg = createGraphics(1280, 720);
pg.beginDraw();
pg.image(bg, 0, 0, 1280, 720);
pg.endDraw();
noStroke();
noSmooth();
frameRate(60);
//randomSeed(0);
mino = int(random(7));
nextMino = int(random(7));
minoX = 6;
minoY = 1;
minoRotate = 0;
resetBlock();
}
void draw() {
drawCnt++;
if (drawCnt >= lastRedraw + stepInterval) {
clearCurrentMino();
if (checkBlock(mino, minoX, minoY + 1, minoRotate) == 1) {
minoY++;
putCurrentMino();
} else {
putCurrentMino();
// check aligned lines
checkAlignedLines();
// next mino
mino = nextMino;
nextMino = int(random(7));
minoX = 6;
minoY = 1;
minoRotate = 0;
if (checkBlock(mino, minoX, minoY, minoRotate) == 0) {
resetBlock();
putCurrentMino();
}
}
}
if (lastMinoRotate != minoRotate || lastMinoX != minoX || lastMinoY != minoY) {
// draw bg
image(pg, 0, 0);
// draw all blocks of main area
fill(0, 0, 0, 127);
rect(320, 70, 400, 600);
fill(255, 255, 255, 255);
for (int i = 0; i < matrixSizeX; i++) {
for (int j = 0; j < matrixSizeY; j++) {
if (blockMatrix[i][j] == 1) {
square(leftMargin + (blockSize + blockMargin) * i,
topMargin + (blockSize + blockMargin) * j,
blockSize);
}
}
}
// draw next mino
int nextMinoMarginX;
fill(0, 0, 0, 127);
rect(805, 90, 160, 160);
fill(255, 255, 255, 255);
for (int i = 0; i < 4; i++) {
if (nextMino == 1) {
nextMinoMarginX = 875 - 10;
} else if (nextMino == 2) {
nextMinoMarginX = 875 - 12;
} else {
nextMinoMarginX = 875;
}
square(nextMinoMarginX + (blockSize + blockMargin) * minoBlock[nextMino][0][i][0],
168 + (blockSize + blockMargin) * minoBlock[nextMino][0][i][1],
blockSize);
}
lastMinoRotate = minoRotate;
lastMinoY = minoY;
lastMinoX = minoX;
lastRedraw = drawCnt;
}
}
void resetBlock() {
for (int i = 0; i < matrixSizeX; i++) {
for (int j = 0; j < matrixSizeY; j++) {
if (j == matrixSizeY - 1) {
if (i >= 1 && i < matrixSizeX - 1) {
blockMatrix[i][j] = 1;
} else {
blockMatrix[i][j] = 0;
}
} else {
if (i == 1 || i == matrixSizeX - 2) {
blockMatrix[i][j] = 1;
} else {
blockMatrix[i][j] = 0;
}
}
}
}
}
void clearCurrentMino() {
int x;
int y;
for (int i = 0; i < 4; i++) {
x = minoX + minoBlock[mino][minoRotate][i][0];
y = minoY + minoBlock[mino][minoRotate][i][1];
blockMatrix[x][y] = 0;
}
}
void putCurrentMino() {
//println("_mino:" + mino);
int x;
int y;
for (int i = 0; i < 4; i++) {
x = minoX + minoBlock[mino][minoRotate][i][0];
y = minoY + minoBlock[mino][minoRotate][i][1];
blockMatrix[x][y] = 1;
}
}
int checkBlock(int mino, int pos, int step, int rotate) {
int result = 1; // ok
int x;
int y;
for (int i = 0; i < 4; i++) {
x = pos + minoBlock[mino][rotate][i][0];
y = step + minoBlock[mino][rotate][i][1];
if (blockMatrix[x][y] == 1) {
result = 0;
}
}
return result;
}
void checkAlignedLines() {
Boolean isAlignedLine;
for (int i = 0; i < matrixSizeY - 1; i++) {
isAlignedLine = true;
for (int j = 2; j <= matrixSizeX - 2; j++) {
if (blockMatrix[j][i] == 0) {
isAlignedLine = false;
}
}
if (isAlignedLine) {
println("align y:" + i);
deleteLine(i);
}
}
}
void deleteLine(int line) {
for (int i = line; i >= 1; i--) {
for (int j = 2; j <= matrixSizeX - 2; j++) {
blockMatrix[j][i] = blockMatrix[j][i - 1];
}
}
}
void keyPressed() {
if (keyCode == LEFT) {
clearCurrentMino();
if (checkBlock(mino, minoX - 1, minoY, minoRotate) == 1) {
minoX--;
}
putCurrentMino();
} else if (keyCode == RIGHT) {
clearCurrentMino();
if (checkBlock(mino, minoX + 1, minoY, minoRotate) == 1) {
minoX++;
}
putCurrentMino();
} else if (keyCode == DOWN) {
clearCurrentMino();
if (checkBlock(mino, minoX, minoY + 1, minoRotate) == 1) {
minoY++;
}
putCurrentMino();
} else if (keyCode == UP) {
clearCurrentMino();
int rotateCandidate = minoRotate + 1;
int minoXCandidate = minoX;
if (rotateCandidate > 3) { rotateCandidate = 0; }
if (mino == 1 && minoX == 10) {
minoXCandidate = minoX - 1;
}
if (checkBlock(mino, minoXCandidate, minoY, rotateCandidate) == 1) {
minoRotate = rotateCandidate;
minoX = minoXCandidate;
}
putCurrentMino();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment