//block game
var bar, block, blockGroup, ball, wallLeft, wallRight, wallTop;
var gameScreen = 0;
var wallBottom;
var num = 0;
var st;
var sound;
var img;
function preload(){
sound = loadSound('img/a.mp3');
img = loadImage('img/ball2.png');
}
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
gameInit();
}
function draw() {
if (gameScreen == 0) {
initScreen();
} else if (gameScreen == 1) {
gamePlayScreen();
} else if (gameScreen == 2) {
gameOverScreen();
} else if (gameScreen == 3) {
gameClearScreen();
}
drawSprites();
}
function initScreen() {
background(190);
textAlign(CENTER);
text("Click to start", width/2, height/1.4);
}
function gamePlayScreen() {
background(200);
gam();
}
function gameOverScreen() {
background(200, 220, 190);
textAlign(CENTER);
// fill(236, 240, 241);
fill(0);
textSize(33);
text("Game Over", width/2-4, height/2-40);
textSize(15);
text("Click to restart", width/2, height-30);
}
function gameClearScreen() {
background(200, 200, 190);
textAlign(CENTER);
// fill(236, 240, 241);
fill(0);
textSize(33);
text("Game clear", width/2-4, height/2-40);
textSize(15);
text("Click to start", width/2, height-30);
// gameOver();
}
function gameInit() {
var blockWidth = 40, //ブロックの幅
blockHeight = 16, //ブロックの高さ
blockMargin = 4, //ブロックの間隔
offset = 40; //ブロックのオフセット値
wallLeft = createSprite(-5, height/2, 10, height);
wallLeft.immovable = true;
wallRight=createSprite(width+5, height/2, 10, height);
wallRight.immovable = true;
wallTop = createSprite(width/2, -5, width, 10);
wallTop.immovable = true;
wallBottom=createSprite(width/2, height, width, 10);
wallBottom.immovable = true;
bar = createSprite(width/2, height-20, 80, 17);
bar.shapeColor = "#ffcc00";
bar.immovable = true;
ball = createSprite(width/2, height/2);
ball.addImage(img);
ball.scale = 0.5;
// ball.setSpeed(3, random(80, 100));
blockGroup = new Group;
for(var r=0;r<4;r++) { //縦のブロック数
for(var c=0;c<6;c++) { //横のブロック数
block = createSprite(offset+c*(blockWidth+blockMargin), offset+r*(blockHeight+blockMargin), blockWidth, blockHeight);
block.immovable = true;
blockGroup.add(block);
}
}
}
function touchMoved() {
bar.position.x = touchX;
//ブラウザ機能を無効化
return false;
}
function gam(){
if(ball.bounce(bar)) {
var swing=(ball.position.x-bar.position.x)/15;
ball.setSpeed(8, ball.getDirection()+swing);
}
ball.bounce(blockGroup,function(ball, block) {
block.remove();
sound.play();
num += 1;
});
ball.bounce(wallLeft);
ball.bounce(wallRight);
ball.bounce(wallTop);
ball.overlap(wallBottom,function(ball,wallBottom){
gameOver();
});
st = "score "+num;
fill(0);
noStroke();
te=textAlign(CENTER);
te.textSize(12);
te.text(st, width/2, 17);
if(num==24){
gameClear();
gameScreen=3;
}
}
function gclear(){
gameScreen = 0;
gameInit();
}
function startGame() {
ball.setSpeed(4, random(80, 100));
gameScreen=1;
}
function gameClear() {
gameScreen=3;
// block.remove();
bar.visible = false;
ball.visible = false;
for (i = 0; i< blockGroup.length; i++){
test = blockGroup[i];
test.visible = false;
}
num = 0;
}
function gameOver() {
gameScreen=2;
// block.remove();
bar.visible = false;
ball.visible = false;
for (i = 0; i< blockGroup.length; i++){
test = blockGroup[i];
test.visible = false;
}
num = 0;
}
function restart() {
gameScreen = 0;
gameInit();
}
function mousePressed() {
if (gameScreen==0) {
startGame();
}
if (gameScreen==2) {
restart();
}
if (gameScreen==3) {
gclear();
}
}
blog
Sound
//Sound on off
var song;
function preload() {
song = loadSound('asset/bgm.mp3');
}
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
background(255,0,0);
}
function draw() {
background(250,6);
textAlign(CENTER);
noStroke();
textSize(18);
fill(255,200,0);
text("Click Sound start and off",width/2, height/2);
}
function mousePressed() {
if ( song.isPlaying() ) { //isPlaying() returns a boolean
song.stop();
background(255,0,0);
} else {
song.play();
background(0,255,0);
}
}
bounce-collide-displace-overlap
//bounce1
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
sp = createSprite(30, 30, 30, 30);
sp.draw = function(){
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
}
function draw() {
background(205,255,255);
sp.bounce(wall);
drawSprites();
}
preview
//bounce2
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
sp = createSprite(30, 30, 30, 30);
sp.draw = function() {
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
}
function draw() {
background(205,255,255);
sp.bounce(wall, explosion);
// sp.bounce(wall);
drawSprites();
}
function explosion(sp, wall) {
wall.remove();
}
preview
//////////////////////////////////
//collide
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
sp = createSprite(30, 30, 30, 30);
sp.draw = function() {
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
}
function draw() {
background(205,255,255);
sp.collide(wall);
drawSprites();
}
preview
//collide2
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
sp = createSprite(30, 30, 30, 30);
sp.draw = function() {
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
}
function draw() {
background(205,255,255);
sp.collide(wall, explosion);
// sp.bounce(wall);
drawSprites();
}
function explosion(sp, wall) {
wall.shapeColor = color(0, 0, 255);
wall.position.x +=5;
}
preview
///////////////////////////////////
//displace
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
sp = createSprite(30, 30, 30, 30);
sp.draw = function() {
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
}
function draw() {
background(205,255,255);
sp.displace(wall);
drawSprites();
}
preview
//displace2
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
sp = createSprite(30, 30, 30, 30);
sp.draw = function() {
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
}
function draw() {
background(205,255,255);
sp.disp lace(wall, explosion);
// sp.bounce(wall);
drawSprites();
}
preview
///////////////////////////////
//overlap
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
sp = createSprite(30, 30, 30, 30);
sp.draw = function() {
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
}
function draw() {
background(205,255,255);
sp.overlap(wall, overlapped);
// sp.bounce(wall);
drawSprites();
}
preview
//overlap2
var wall, sp;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
sp = createSprite(30, 30, 30, 30);
sp.draw = function() {
fill(237,205,0);
ellipse(0,0, 30, 30);
}
sp.velocity.x = sp.velocity.y = 2;
wall = createSprite(60, 200, 340, 20);
wall.shapeColor = color(0,135,155);
wall.immovable = true;
}
function draw() {
background(205,255,255);
sp.overlap(wall, overlapped);
// sp.bounce(wall);
drawSprites();
}
/////////////////////////
AnalogClock
var MARGIN = 20;
var myFont;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
stroke(255);
smooth();
frameRate(30);
fill(255);
}
function draw() {
background(230);
var s2 = second();
var m2 = minute();
var h2 = hour();
// var t =year()+"年"+ month()+"月"+day()+"日" + h2 + ":" + m2 + ":" + s2;
var t =year()+"/"+month()+"/"+day()+"/" + h2 + ":" + m2 + ":" + s2;
noStroke();
textAlign(CENTER);
textSize(12);
fill(50);
text(t, width/2, 260);
noStroke();
textAlign(CENTER);
textSize(17);
fill(150);
text("KyouMinakami", width/2, 245);
var s = second();
var m = minute() + (s/60.0);
var h = hour()%12 + (m/60.0);
translate(width/2, height/2);
rotate(radians(180));
// 文字盤の表示
push();
fill(80);
noStroke();
for(var i=0; i<60; i++){
// rotate(radians(6));
rotate(radians(5.990));
rect(width/2-MARGIN,0,3,1);
}
for(var i=0; i<12; i++){
//rotate(radians(30));
rotate(radians(29.990));
rect(width/2-MARGIN,0,4,2);
}
pop();
noFill();
stroke(255,0,0);
// 秒針
push();
rotate(radians(s*(360/60)));
strokeWeight(1);
line(0,0,0,width/2-MARGIN);
pop();
noFill();
stroke(50)
// 分針
push();
rotate(radians(m*(360/60)));
strokeWeight(2);
line(0,0,0,width/2-MARGIN);
pop();
// 時針
push();
rotate(radians(h*(360/12)));
strokeWeight(4);
line(0,0,0,width/3-MARGIN);
pop();
}
//ハート
function heart(){
var R = 3;
fill(255,255,255)
noStroke();
strokeJoin(ROUND);
push();
translate(width/2, height/2);
beginShape();
for (var theta = 0; theta < 360; theta++) {
var x = R * (16 * sin(radians(theta)) * sin(radians(theta)) * sin(radians(theta)));
var y = (-1) * R * (13 * cos(radians(theta)) - 5 * cos(radians(2 * theta))
- 2 * cos(radians(3 * theta)) - cos(radians(4 * theta)));
vertex(x, y);
}
endShape(CLOSE);
pop();
}
time
//millis()を使う1
var resetAt;
var number;
function setup(){
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
noStroke();
smooth();
reset();
}
function reset(){
resetAt = millis() + 3000;
}
function draw(){
background(230);
fill(50);
textSize(12);
var timeLeft = resetAt - millis();
number = Math.round(timeLeft ) ;
text(number, 5, 15);
if(number <= 0){
textAlign(CENTER);
fill(0);
textSize(33);
text("Game over", width/2, height/2);
fill(230);
rect(1,2,60,20);
noLoop();
}
}
preview
/////////////////////////////
//millis()を使う2
var hd;
var m;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
}
function draw() {
background(240);
hello();
drawSprites();
}
function hello(){
textAlign(CENTER);
fill(200);
if(m > 4*1000 ){ //もし、counterが4秒を超えたら、
fill(240);
}
textSize(20);
text("4秒後に下の文字が消える", width/2, height/2);
m = millis();
fill(0);
noStroke();
textAlign(CENTER);
if(m > 4*1000 ){ //もし、counterが4秒を超えたら、
fill(240);
}
else{ //それまでは、
fill(0);
}
textSize(13);
hd = "When hitting a red mother ship within time,\n it's your win.";
text(hd, width/2, 225);
if(m > 4*1000 ){ //もし、counterが4秒を超えたら、
textAlign(CENTER);
fill(200);
textSize(16);
text("消えました", width/2, height/2);
}
}
preview
////////////////////////////
//millis()を使う3
var time1 = 2000;
var time2 = 5000;
var x = 35;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
}
function draw() {
var currentTime = millis();
background(230);
if (currentTime > time2) {
x -= 1;
if (x <= 35) { x = 35; }
} else if (currentTime > time1) {
x += 2;
}
var c = color('hsla(160, 100%, 50%, 0.8)');
fill(c);
stroke(0, 0, 255);
strokeWeight(10);
ellipse(x, height/2-15, 70, 70);
}
preview
////////////////////////////
//millis()を使う4
var m;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
noStroke();
smooth();
}
//プログラムが終了するまで、draw()の中は繰り返される
function draw() {
background(235);
m = millis();
m = m%(9*1000);
//mは9000で割った余りなので、常に0~8999の間の値を繰り返す。
if(m <= 2*1000){ fill(0, 255, 0); }
else if(m > 2*1000 && m <= 4*1000){
fill(255, 0, 0); //赤にする }
else if(m > 4*1000 && m <= 6*1000){
fill(255, 255, 0); }else if(m > 6*1000 && m <= 9*1000){
fill(0, 0,255);
}
ellipse(width/2, height/2, 200, 200);
}
preview
////////////////////////////
//millis()を使う5
var time;
var wait = 1500;
var tick = false;
function setup(){
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
time = millis();
smooth();
}
function draw(){
background(230);
if(millis() - time >= wait){
tick = !tick;
time = millis();
}
fill(255, 0, 0);
noStroke();
ellipse(150, tick?100:200, 90, 90);
}
preview
box2d
b2.jsとbox2d-html5.jsの読み込み
●物理空間の生成
b2newWorld(30, createVector(0, 9.8));
●物体の生成
b2Body(type, isDynamic, xy, wh, density密度, friction摩擦, restitutionはね返り係数, angle);
type ='box' 'circle' 'polygon' 'edge'
density = 10.0; // 材質の密度(重さ)
friction = .5; // 摩擦係数 0(摩擦なし)から1(摩擦MAX)
restitution = .8; // はね返り係数
//______________
function setup() {
createCanvas(300, 300);
b2newWorld(30, createVector(0, 9.8));
new b2Body('box',false,createVector(width/2,height-2), createVector(width,30));
new b2Body('circle',true,createVector(width/2, 20),createVector(40,40), 1, 0.5, 0.9);
}
function draw() {
background(227);
b2Update();
b2Draw(true);
}
preview
//////////////////////////
p5.js ellipseを指定する
var a, b;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
b2newWorld(30, createVector(0, 9.8));
a=new b2Body('box',false,createVector(width/2,height-5), createVector(width,10));
a.display(attr1);
b=new b2Body('circle',true,createVector(width/2,30), createVector(60,60), 1, 0.5, 0.9);
b.display(attr2);
}
function draw() {
background(227);
b2Update();
b2Draw(false); //trueではなくfalse
}
function attr1(body, fixture, position) {
fill(255,0,0);
noStroke();
b2Display(body, fixture, position);
}
function attr2(body, fixture, position) {
fill(0,0,255);
noStroke();
ellipse(position.x,position.y,body.wh(0).x,body.wh(0).y);
}
preview
////////////////////////////
//box2d test
var b, c, d;
function setup() {
var myCanvas = createCanvas(300, 300);
myCanvas.parent('myContainer');
b2newWorld(30, createVector(0, 9.8));
d=new b2Body('box',false,createVector(140,height-130), createVector(width-93, 20));
d.display(attr, 0);
new b2Body('box',false,createVector(width*0.9,height-220), createVector(width-50, 20),0,0,0,(Math.PI/180 *-9));
new b2Body('edge', false, v(width /2, height-2),
[v(-width/2, 0), v(width/2,0)]);
new b2Body('edge', false, v(0,0),
[v(width, 0), v(0,0)]);
new b2Body('edge', false, v(0,0),
[v(height, 0), v(height,width)]);
new b2Body('edge', false, v(0,0),
[v(0, height), v(0,0)]);
b = new b2Body('circle', true, v(width / 3,20), v(39,39),1, 0.1, 0.96);
b.color = color(255,150,0);
b.display(drawer);
c=new b2Body('circle',true, createVector(width/2.9,40), createVector(39,39), 1, 0.5, 0.9);
c.display(foo);
}
function draw() {
background(227);
b2Update();
b2Draw(false);
}
// stroke(0, 255, 0);
// strokeWeight(5);
var foo = function (body, fixture, pos) {
fill(255, 0, 0);
noStroke();
b2Display(body, fixture, pos);
}
function drawer(body, shape, position) {
fill(body.color);
noStroke();
ellipse(position.x, position.y, 40, 40);
}
function attr(body, fixture, position) {
fill(0,0,255);
noStroke();
rect(position.x,position.y,body.wh(0).x,body.wh(0).y, 10);
}
function v(x, y) {
return createVector(x, y);
}
preview
//////////////////////////////////////