var rec;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
rec = createSprite(width/2, height/2, 50, 50);
rec.shapeColor = color(255,255,255);
}
function draw() {
background(200,200,200);
drawSprites();
}
maps
var mapNum = 1;
var gBlocks;
function setup(){
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
gBlocks = new Group();
maps = [];
//maps.push([]);
maps.unshift([],[
[1,1,1,1,1,1,1,1,1,1],
[1,1,0,0,0,0,0,0,0,1],
[1,1,0,0,0,0,0,0,0,1],
[1,1,0,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1],
]);
for(i = 0; i < maps[mapNum].length; i++){
for(j = 0; j < maps[mapNum][i].length; j++){
if(maps[mapNum][i][j] === 1){
var block = createSprite(j * 30 +15, i * 30 +15, 30, 30)
gBlocks.add(block);
}
}
}
}
function draw() {
drawSprites();
}
Color match Game
//Color match Game
var idList = [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7];
var rows = 4;
var cols = 4;
var index = 0;
var blockSize;
var spacing = 0;
var blocks = [];
var clickCounter = 0;
var matchCounter = 0;
var firstBlock;
var secondBlock;
var scoreTimer;
var resetTime = 0;
var button;
var col;
function setup() {
var myCanvas = createCanvas(320, 320);
myCanvas.parent('myContainer');
colorMode(HSB);
blockSize = width / 4;
strokeWeight(4);
shuffle(idList, true);
for (var x = 0; x < cols; x++) {
for (var y = 0; y < rows; y++) {
var blockX = ((spacing + blockSize) * x) + spacing;
var blockY = ((spacing + blockSize) * y) + spacing;
blocks.push(new Block(blockX, blockY, blockSize, idList[index]));
index++;
}
}
col= color(220,190,230,100);
button = createButton('Click Restart');
button.position(120, 330);
button.size(200,30)
button.style("font-size", "17px");
button.style("color", "#fff");
button.style("background-color",col)
// button.html("Click Restart2");
button.mousePressed(changeBG);
button.hide();
}
function draw() {
background(80);
for (var i = 0; i < blocks.length; i++) {
hello();
blocks[i].display();
}
if (matchCounter == blocks.length / 2) {
gameOver();
button.show();
}
}
function hello(){
var m = millis();
noStroke();
textAlign(CENTER);
if(m > 3*1000 ){
fill(255);
text(" ", 500, 50);
}else{
textSize(28);
fill(20);
var hd = "Color Blending Game\nClick Start";
text(hd, width/2, 150);
}
}
function flip() {
for (var i = 0; i < blocks.length; i++) {
blocks[i].colorState = blocks[i].back;
}
}
function matchPair() {
for (var i = 0; i < blocks.length; i++) {
if (blocks[i].id == firstBlock) {
blocks[i].alive = false;
}
}
}
function gameOver() {
fill(235, 50);
noStroke();
// rect(0, 120, width, 240);
textSize(40);
fill(40);
textAlign(CENTER);;
text("Game Clear", width / 2, (height / 2) - 100);
textSize(32);
text("Time: " + scoreTimer + " sec", width / 2, (height / 2) - 50);
textSize(18);
text("Button Click to Restart", width / 2, (height / 2) + 80);
}
function changeBG() {
reset();
button.hide();
}
function reset() {
shuffle(idList, true);
blocks = [];
index = 0;
for (var x = 0; x < cols; x++) {
for (var y = 0; y < rows; y++) {
var blockX = ((spacing + blockSize) * x) + spacing;
var blockY = ((spacing + blockSize) * y) + spacing;
blocks.push(new Block(blockX, blockY, blockSize, idList[index]));
console.log(idList[index]);
index++;
}
}
for (var i = 0; i < blocks.length; i++) {
blocks[i].alive = true;
blocks[i].colorState = blocks[i].back;
}
matchCounter = 0;
resetTime = floor(millis() / 1000);
}
function checkPair() {
if (clickCounter == 2) {
if (firstBlock == secondBlock) {
matchCounter++;
setTimeout(matchPair, 200);
} else {
setTimeout(flip, 200);
}
clickCounter = 0;
}
}
function mouseClicked() {
for (var i = 0; i < blocks.length; i++) {
blocks[i].clicked();
}
checkPair();
scoreTimer = floor(millis() / 1000) - resetTime;
}
function touchEnded(){
for (var i = 0; i < blocks.length; i++) {
blocks[i].clicked();
}
checkPair();
scoreTimer = floor(millis() / 1000) - resetTime;
}
function Block(x, y, w, id) {
this.x = x;
this.y = y;
this.w = w;
this.id = id;
this.front = color(id * 22, 70, 70);
this.back = 255;
this.colorState = this.back;
this.alive = true;
this.display = function() {
if (this.alive == true) {
stroke(80);
fill(this.colorState);
rect(this.x, this.y, this.w, this.w);
}
}
this.clicked = function() {
if (this.colorState == this.back && this.alive) {
if (mouseX > this.x && mouseX < this.x + this.w && mouseY > this.y && mouseY < this.y + this.w) {
this.colorState = this.front;
if (clickCounter == 0) {
firstBlock = this.id;
} else if (clickCounter == 1) {
secondBlock = this.id;
}
clickCounter++;
}
}
}
}
video
//video play pause
var playing = false;
var fingers;
var button;
var col;
function setup() {
var myCanvas = createCanvas(320, 250);
myCanvas.parent('myContainer');
fingers = createVideo(['asset/m1.mp4','asset/m1.webm']);
background(0);
fingers.position(0, 0);
col= color(80,80,230,200);
button = createButton('play');
button.position(150, 252);
button.size(170,30)
button.style("font-size", "17px");
button.style("color", "#fff");
button.style("background-color",col)
button.mousePressed(toggleVid); // attach button listener
}
function toggleVid() {
if (playing) {
fingers.pause();
button.html('play');
} else {
fingers.loop();
button.html('pause');
}
playing = !playing;
}
//tablet
function setup() {
var myCanvas = createCanvas(320, 250);
myCanvas.parent('myContainer');
var video = createVideo(['asset/m1.mp4','asset/m1.webm']);
background(0);
video.position(0, 0);
video.play();
video.showControls();
}
function draw() {
}
sin-cos
//sin cos test
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
stroke(255, 0, 0);
function draw() {
background(235);
translate(width/2, height/2);
var x = 90*cos(frameCount*0.05);
var y = 90*sin(frameCount*0.05);
var c = color('hsla(0, 100%, 50%, 0.9)');
fill(c);
noStroke();
// xを表す点を描画してみる
ellipse(x, y, 60, 60);
}
preview
////////////////////////
//rasen move
var x = 0;
var y = 0;
var deg = 0;
var r = 0;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
}
function draw() {
background(230);
fill(255,0,0);
noStroke();
x = width / 2 + r*cos(radians(deg));
y = height / 2 + r*sin(radians(deg));
ellipse(x, y, 30, 30);
deg = deg + 2;
r = r + 0.1;
if(x<=0+15 || y<=0+15){
deg =0;
r = 0;
}
}
preview
/////////////////////////
//cos
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
stroke(255, 0, 0);
// strokeWeight(20);
}
function draw() {
background(235);
// 原点を画面中心に移動
translate(width/2, height/2);
// cos()が返した値をxに代入
var x = 100 * cos(frameCount*0.05);
var c = color('hsla(0, 100%, 50%, 0.9)');
fill(c);
noStroke();
// xを表す点を描画してみる
ellipse(x, 0,40,40);
}
preview
/////////////////////////////
//sin
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
stroke(0, 255, 0);
// strokeWeight(20);
}
function draw() {
background(235);
// 原点を画面中心に移動
translate(width/2, height/2);
// sin()が返した値をyに代入
var y = 100 * sin(frameCount*0.05);
var x = 100 * cos(frameCount*0.05);
var c = color('hsla(0, 100%, 50%, 0.9)');
fill(c);
noStroke();
// xを表す点を描画してみる
ellipse(0, y,50,50);
}
preview
///////////////////////////
//sin cos
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
noStroke();
fill(0);
}
function draw() {
background(235);
for(var x = 0; x <= width; x += 20) {
var y = height/2 + 100 * sin(frameCount*0.04 + x*0.02);
var c = color('hsla(0, 100%, 50%, 0.9)');
fill(c);
noStroke();
ellipse(x, y, 19, 19);
}
}
button
//button style
var button;
var col;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
background(0);
col= color(80,80,230,200);
button = createButton('Click Start');
button.position(100, 305);
button.size(200,30)
button.style("font-size", "17px");
button.style("color", "#fff");
button.style("background-color",col)
// button.html("Click Restart2");
button.mousePressed(changeBG);
// button.hide();
}
function changeBG() {
var val = color(random(255),random(255),random(255));
background(val);
// button.hide();
// button.show();
}
function draw() {
noStroke();
textAlign(CENTER);
textSize(18);
fill(255);
var hd = "BackGround color Change\nClick Start";
text(hd, width/2, 150);
}