//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();
}
}
コメントを残す