[PV3D]ブロック崩し
ブロック崩しです。
SPACE : ゲーム開始
SPACEを押す前に↑ : yz軸カメラ移動
SPACEを押す前に↓ : xa軸カメラ移動
→ : バーを右に移動
← : バーを左に移動
ボール、ブロック、壁、バーの色はランダムです。
/**
* Copyright peso ( http://wonderfl.net/user/peso )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/mpIA
*/
package
{
import flash.events.*;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.*;
import org.papervision3d.cameras.*;
import org.papervision3d.materials.*;
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
import org.papervision3d.materials.special.*;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.*;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.typography.*;
import org.papervision3d.typography.fonts.*;
import org.papervision3d.view.*;
import org.papervision3d.view.layer.*;
public class block extends BasicView
{
private const M:uint = 5;
private const N:uint = 5;
private const blockW:uint = 100;
private const blockH:uint = 40;
private var a:Number = (-M*blockW/2)+blockW/2;
private var b:Number = 200;
private var block1:Cube;
private var bar:Cube;
private var blockarr:Array=[];
private var ball:Sphere;
private var vx:int = 0;
private var vy:int = 0;
private var wall_up:Cube;
private var wall_right:Cube;
private var wall_left:Cube;
private var barflg:Boolean = false;
private var gameflg:Boolean = true;
private var blockflg:Array=[];
private var rotx:Number = 0;
private var roty:Number = 0;
private var count:Number = 0;
public function block()
{
makeBlock();
makeBar();
makeBall();
makeWall();
makeExplane(barflg);
makeScore(count);
startRendering();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
// ブロックの作成
private function makeBlock():void{
for(var i:uint = 0; i<N; i++){
for(var j:uint = 0; j<M; j++){
var material_block:ColorMaterial = new ColorMaterial(0xFFFFFF * Math.random(), 1, true);
var materials_block:MaterialsList = new MaterialsList({all:material_block});
block1 = new Cube(materials_block, blockW, blockH, blockH, 1, 1, 1);
block1.x = a;
block1.y = b;
a += blockW;
scene.addChild(block1);
blockarr.push(block1);
}
a = (-M*blockW/2)+blockW/2;
b += blockH;
}
for(var k:uint = 0; k<M*N; k++){
blockflg[k] = 0;
}
}
// バーの作成
private function makeBar():void{
var material_bar:ColorMaterial = new ColorMaterial(0xFFFFFF * Math.random(), 1, true);
var materials_bar:MaterialsList = new MaterialsList({all:material_bar});
bar = new Cube(materials_bar, blockW, blockH, blockH, 1, 1, 1);
bar.x = 0;
bar.y = -b;
scene.addChild(bar);
}
// 球の作成
private function makeBall():void{
var material_ball:ColorMaterial = new ColorMaterial(0xFFFFFF*Math.random());
ball = new Sphere(material_ball, 20, 8, 8);
ball.y = -b+blockH;
vx = 19;
vy = 19;
scene.addChild(ball);
}
// 壁の作成
private function makeWall():void{
var material_wall:ColorMaterial = new ColorMaterial(0xFFFFFF*Math.random(), 1, true);
var materials_wall:MaterialsList = new MaterialsList({all:material_wall});
wall_up = new Cube(materials_wall, 700, blockH, blockH, 1, 1, 1);
wall_up.y = b+100;
scene.addChild(wall_up);
wall_right = new Cube(materials_wall, blockH, blockH, (b+100+blockH/2)*2, 1, 1, 1);
wall_right.x = 350 + blockH / 2;
scene.addChild(wall_right);
wall_left = new Cube(materials_wall, blockH, blockH, (b+100+blockH/2)*2, 1, 1, 1);
wall_left.x = -(350 + blockH / 2);
scene.addChild(wall_left);
}
private var preText1:Text3D = null;
// 説明の表示
private function makeExplane(barflg:Boolean):void{
if(barflg==false){
var material_exp:Letter3DMaterial = new Letter3DMaterial(0xFF0000);
material_exp.doubleSided = true;
var font:HelveticaBold = new HelveticaBold();
var word_exp:Text3D = new Text3D("PUSH SPACE!", font, material_exp);
word_exp.y = 100;
scene.addChild(word_exp);
preText1 = word_exp;
}
else{
scene.removeChild(preText1);
}
}
private var preText2:Text3D = null;
// スコア表示
private function makeScore(count:Number):void{
if(preText2 != null)scene.removeChild(preText2);
var material_score:Letter3DMaterial = new Letter3DMaterial(0x006699);
material_score.doubleSided = true;
var font:HelveticaBold = new HelveticaBold();
if(count<=9){
var word_score:Text3D = new Text3D("SCORE\n0"+count+"0", font, material_score);
word_score.y = -50;
}
else{
var word_score:Text3D = new Text3D("SCORE\n"+count+"0", font, material_score);
word_score.y = -50;
}
scene.addChild(word_score);
preText2 = word_score;
}
private function onKeyDown(e:KeyboardEvent):void{
if(barflg==false){
// 球の制御
if(ball.x<=-(350 - blockW / 2)){
ball.x = -(350 - blockW / 2);
}
else if(ball.x>=350 - blockW / 2){
ball.x = 350 - blockW / 2;
}
// 球を移動
if(e.keyCode==Keyboard.RIGHT){
ball.x += 35;
}
if(e.keyCode==Keyboard.LEFT){
ball.x -= 35;
}
// カメラの角度
if(e.keyCode==Keyboard.DOWN){
rotx += 5;
}
if(e.keyCode==Keyboard.UP){
roty += 5;
}
}
// ゲーム開始
if(e.keyCode==Keyboard.SPACE){
barflg = true;
makeExplane(barflg);
}
// バーの移動
if(e.keyCode==Keyboard.RIGHT){
bar.x += 35;
}
if(e.keyCode==Keyboard.LEFT){
bar.x -= 35;
}
}
private function onEnterFrame(e:Event):void{
// バーの制御
if(bar.x<=-(350 - blockW / 2)){
bar.x = -(350 - blockW / 2);
}
else if(bar.x>=350 - blockW / 2){
bar.x = 350 - blockW / 2;
}
if(barflg==true){
ball.x += vx;
ball.y += vy;
// 壁に当たったとき
if(ball.x<=-(350-20)){
vx *= -1;
}
if(ball.x>=350-20){
vx *= -1;
}
if(ball.y>=b+100-blockH/2-20){
vy *= -1;
}
// バーに当たったとき
if(ball.hitTestObject(bar)){
ball.y += 40;
vy *= -1;
}
// ブロックに当たったとき
for(var l:uint=0; l<M*N; l++){
if(blockflg[l] == 1) continue;
if(ball.hitTestObject(blockarr[l])){
blockflg[l] = 1;
blockarr[l].visible = false;
count += 1;
makeScore(count);
// 左上に当たったとき
if(ball.x+20 < blockarr[l].x-50+19 && ball.y-20 > blockarr[l].y+20-19){
vx *= -1;
vy *= -1;
}
// 右上に当たったとき
else if(ball.x-20 > blockarr[l].x+50-19 && ball.y-20 > blockarr[l].y+20-19){
vx *= -1;
vy *= -1;
}
// 左下に当たったとき
else if(ball.x+20 < blockarr[l].x-50+19 && ball.y+20 < blockarr[l].y-20+19){
vx *= -1;
vy *= -1;
}
// 右下に当たったとき
else if(ball.x-20 > blockarr[l].x+50-19 && ball.y+20 < blockarr[l].y-20+19){
vx *= -1;
vy *= -1;
}
// 上に当たったとき
else if(ball.y-20 > blockarr[l].y+20-19){
vy *= -1;
}
// 下に当たったとき
else if(ball.y+20 < blockarr[l].y-20+19){
vy *= -1;
}
// 左に当たったとき
else if(ball.x+20 < blockarr[l].x-50+19){
vx *= -1;
}
// 右に当たったとき
else if(ball.x-20 > blockarr[l].x+50-19){
vx *= -1;
}
}
}
}
camera.x = -1000 * Math.sin(rotx * Math.PI / 180);
camera.z = -1000 * Math.cos(rotx * Math.PI / 180);
camera.y = -1000 * Math.sin(roty * Math.PI / 180);
var material_result:Letter3DMaterial = new Letter3DMaterial(0xFF0000);
material_result.doubleSided = true;
var font:HelveticaBold = new HelveticaBold();
// 全部壊した時
if(count==M*N){
gameflg = false;
var word_clear:Text3D = new Text3D("CLEAR!!", font, material_result);
word_clear.y = 100
scene.addChild(word_clear);
}
// ゲームオーバー時
if(gameflg==true){
if(ball.y < -(b+100)){
ball.visible=false;
gameflg=false;
var word_gameover:Text3D = new Text3D("GAME OVER", font, material_result);
word_gameover.y = 100;
scene.addChild(word_gameover);
}
}
}
}
}