Openframeworksでブロック崩しもどきを作る②
さて、前回の続きです。今回は一気に完成版的なところまで行きます。
作るに関して参考にしている本はBeyond Interaction[改訂第2版] -クリエイティブ・コーディングのためのopenFrameworks実践ガイド
2.うごくものがある
を作っていきます。これは、もう単純に、
if (token){ ofSetColor(255, 255, 255); ofDrawCircle(location.x, location.y, 20); } ofDrawRectangle(mouseX-50, mouseY-25, 100, 50);
これで、書きました。tokenはキーボードの”b”を押すとTrueになるようになっています。なので、起動時はボールは動いてなくて、キーボードのbを打ったら動き始めるようになっています。同時に、ボールを当てるバー(Rectangle)もかいてます。
あーーー。
ここまできたんですけど、なんか、もうコードをばって載せたほうが早いと思ったので、一気に載せてしまいますね。笑笑
これが完成版
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ ofBackground(0, 0, 0); ofSetCircleResolution(32); //透明度の設定 ofEnableAlphaBlending(); //混色方法 //ofEnableBlendMode(OF_BLENDMODE_ADD); ofSetFrameRate(60); ofSetCircleResolution(30); //キーボード(b)を押した問に、ボールが出現 token = false; } //-------------------------------------------------------------- void ofApp::update(){ if (token){ location.x = location.x - velocity.x; location.y = location.y - velocity.y; for (int i=0; i<circles.size(); i++){ float j = circles.size(); circles[i].update(); ofPoint loc = circles[i].getPos(); float dist = ofDist(location.x, location.y, loc.x, loc.y); if(dist < 70 ){ if (circles[i].getRadius() != 0){ circles[i].setRadius(0); velocity.x = velocity.x * -1; velocity.y = velocity.y * -1; } else{ } } } } if (location.x < 0){ velocity.x = velocity.x * -1; } if (location.x > ofGetWidth()){ velocity.x = velocity.x * -1; } if (location.y < 0){ velocity.y = velocity.y * -1; } if (location.y-300 > ofGetHeight()){ std::exit(1); //velocity.y = velocity.y * -1; } if (location.x >= mouseX-50 && location.x <= mouseX+50 && location.y <= mouseY){ if (location.y + 45 >= mouseY){ //velocity.x = velocity.x * -1; velocity.y = velocity.y * -1; } } } //-------------------------------------------------------------- void ofApp::draw(){ for (int i=0; i<circles.size(); i++){ float value = i; if(circles[i].getRadius() != 0){ //ofDrawBitmapString("value: " + ofToString(value), 10, 10); circles[i].setWord(ofToString(value)); circles[i].draw(); } else { } } if (token){ ofSetColor(255, 255, 255); ofDrawCircle(location.x, location.y, 20); } ofDrawRectangle(mouseX-50, mouseY-25, 100, 50); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ switch (key) { case 'b': token = true; } } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ CustomCircle circle; circle.setPos(ofPoint(x,y)); circle.setRadius(ofRandom(50,50)); circles.push_back(circle); }
ではでは!!