Field field; Ball ball; Racket racket; Block[] blocks; boolean isGameOver; int cmx, cmy; void setup(){ background( 0 ); size( 300, 400 ); framerate( 30 ); smooth(); initParam(); } void initParam(){ field = new Field( 50, 75, 200, 250 ); ball = new Ball( field.x+field.w/2, field.y+field.h-20, random(2)+2, random(-2)-2 ); racket = new Racket( field.x, field.y+field.h - 10, 30, 5 ); blocks = new Block[48]; for( int y = 0; y < 7; y++ ){ for( int x = 0; x < 6; x++ ){ int pos = y*6+x; blocks[pos] = new Block( field.x+(1*(x+1)+1)+32*x, field.y+(1*(y+1)+1)+15*y, 32, 15, pos ); } } cmx = ball.x; cmy = ball.y; isGameOver = false; } void loop(){ background( 0 ); if( isGameOver ){ initParam(); } racket.relax( field ); ball.relax( field, racket, blocks ); cmx += ( racket.x + racket.w/2 - cmx ) * 0.2; cmy += ( ball.y - cmy ) * 0.2; float xr = ( (float)cmx - (float)width / 2.0f ) / ( (float)width / 2.0f ); float yr = ( (float)cmy - (float)height / 2.0f ) / ( (float)height / 2.0f ); xr *= PI / 2.3f; yr *= PI / 2.3f; translate( width / 2, height / 2 ); rotateX( yr ); rotateY( -xr ); translate( -width / 2, -height / 2 ); field.paintSelf(); ball.paintSelf(); racket.paintSelf(); for( int i = 0; i < blocks.length; i++ ){ if( blocks[i] == null ) continue; blocks[i].paintSelf(); } } class Field{ int x, y; int w, h; Field( int x, int y, int w, int h ){ this.x = x; this.y = y; this.w = w; this.h = h; } void paintSelf(){ noFill(); stroke( 255 ); rect( x, y, w, h ); } } class Ball{ int x, y; float dx, dy; int s = 8; Ball( int x, int y, float dx, float dy ){ this.x = x; this.y = y; this.dx = dx; this.dy = dy; } void relax( Field field, Racket racket, Block[] blocks ){ x += dx; y += dy; //壁&ラケットとの衝突判定 int hs = s / 2; if( x < field.x +hs ){ x = field.x +hs; dx *= -1; } else if( x > field.x + field.w -hs ){ x = field.x + field.w -hs; dx *= -1; } if( y < field.y +hs ){ y = field.y +hs; dy *= -1; } else if( y > racket.y - hs ){ if( x > racket.x && x < racket.x + racket.w ){ y = racket.y - hs; dy *= -1; dx += ( racket.x - racket.lastx ) * 0.2; } else{ isGameOver = true; } } //ブロックとの衝突判定 for( int i = 0; i < blocks.length; i++ ){ Block b = blocks[i]; if( b == null ) continue; int lastx = x - (int)dx; int lasty = y - (int)dy; if( x > b.x && x < b.x + b.w ){ if( b.y + b.h > y - hs && b.y + b.h < lasty + hs ){ y = b.y + b.h + hs; dy *= -1; b.hitEvent( blocks, ball ); } else if( b.y < y + hs && b.y > lasty - hs ){ y = b.y - hs; dy *= -1; b.hitEvent( blocks, ball ); } } else if( y > b.y && y < b.y + b.h ){ if( b.x + b.w > x - hs && b.x + b.w < lastx + hs ){ x = b.x + b.w + hs; dx *= -1; b.hitEvent( blocks, ball ); } else if( b.x < x + hs && b.x > lastx - hs ){ x = b.x - hs; dx *= -1; b.hitEvent( blocks, ball ); } } } } void paintSelf(){ fill( 255 ); noStroke(); int hs = s / 2; ellipse( x -hs, y -hs, s, s ); } } class Racket{ int x, y; int lastx; int w, h; Racket( int x, int y, int w, int h ){ this.x = this.lastx = x; this.y = y; this.w = w; this.h = h; } void relax( Field field ){ lastx = x; x = mouseX; if( x < field.x ){ x = field.x; } else if( x + w > field.x + field.w ){ x = field.x + field.w - w; } } void paintSelf(){ fill( 255 ); noStroke(); rect( x, y, w, h ); } } class Block{ int x, y; int w, h; int p; int id; Block( int x, int y, int w, int h, int id ){ this.x = x; this.y = y; this.w = w; this.h = h; this.p = 3; this.id = id; } void paintSelf(){ noStroke(); fill( 255 ); rect( x, y, w, h ); } void hitEvent( Block[] blocks, Ball ball ){ p--; w -= 8; h -= 4; x += 4; y += 2; if( p == 0 ){ ball.dx *= 1.01; ball.dy *= 1.01; blocks[id] = null; } } }